Tabela de agendamento

Enviada por Rodrigo 
Rodrigo
Tabela de agendamento
21 de November de 2017 às 12:17AM
Pessoal, me ajudem.
Estou com um problema no código abaixo, gostaria de exibir os dados que pego do MYSQL em uma tabela em HTML. Dê uma olhada no código:

<html lang="pt-br">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>lab</title>
</head>
<body>
<?php
include('connect_DB.php');
$query_comentario = "SELECT * FROM lab";
$comentario = mysql_query($query_comentario) or die (mysql_error());
$row_comentario = mysql_fetch_assoc($comentario);
$totalRows_comentario = mysql_num_rows($comentario);
$result = 0;
$soma=0;
?>
<form method="post" action="inserir_horario.php" enctype="multipart/form-data">
<table width="600" border="1" bordercolor="#000000">
<tr>
<td bgcolor="#A9A9A9"></td>
<td bgcolor='#A9A9A9'><center> 13/11/2017 </center></td><!--<font color="#ffffff"> alguma coisa</font> //<strong> negrito</strong> -->
<td bgcolor='#A9A9A9'><center> 14/11/2017 </center></td>
<td bgcolor='#A9A9A9'><center> 15/11/2017 </center></td>
<td bgcolor='#A9A9A9'><center> 16/11/2017 </center></td>
<td bgcolor='#A9A9A9'><center> 17/11/2017 </center></td>
</tr>
<?php do { ?>
<tr>
<?php $campo = $row_comentario['campo'];
$instrutor = $row_comentario['instrutor'];
$num++;
echo "<td bgcolor='#A9A9A9'>$num.ยบ</td>";?>
<td>
<?php if ($campo <=5){ ?>
<center><a href="/rodrigo/favorito.php"> <?php echo $instrutor; ?> </a>
<input type="checkbox" name="tipo" value="<?php echo $instrutor; ?>"></center>
<?php } ?>
</td>
<td>
<?php if (($campo >=6) && ($campo <=10)){ ?>
<center><a href="/rodrigo/favorito.php"> <?php echo $instrutor; ?> </a>
<input type="checkbox" name="tipo" value="<?php echo $instrutor; ?>"></center>
<?php } ?>
</td>
<td>
<?php if (($campo >=11) && ($campo <=15)){ ?>
<center><a href="/rodrigo/favorito.php"> <?php echo $instrutor; ?> </a>
<input type="checkbox" name="tipo" value="<?php echo $campo; ?>"></center>
<?php } ?>
</td>
<td>
<?php if (($campo >=16) && ($campo <=20)){ ?>
<center><a href="/rodrigo/favorito.php"> <?php echo $instrutor; ?> </a>
<input type="checkbox" name="tipo" value="<?php echo $campo; ?>"></center>
<?php } ?>
</td>
<td>
<?php if (($campo >=21) && ($campo <=25)){ ?>
<center><a href="/rodrigo/favorito.php"> <?php echo $instrutor; ?> </a>
<input type="checkbox" name="tipo" value="<?php echo $campo; ?>"></center>
<?php } ?>
</td>
</tr>
<?php } while ($row_comentario = mysql_fetch_assoc($comentario));?>
</table></br></br>
<input type="submit" value="Enviar" />
</form>
</body>
</html>

Gostaria que esses dados vindos do MYSQL sejam exibidos igual essa tabela do código abaixo, mas não estou conseguindo fazer um loop dentro desse código para exibir os dados que estão no banco de dados. Segue o código.

<html lang="pt-br">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>lab</title>
</head>
<body>
<?php
include('connect_DB.php');
$query_comentario = "SELECT * FROM lab";
$comentario = mysql_query($query_comentario) or die (mysql_error());
$row_comentario = mysql_fetch_assoc($comentario);
$totalRows_comentario = mysql_num_rows($comentario);
?>
<form method="post" action="inserir_horario.php" enctype="multipart/form-data">
<table width="600" border="1" bordercolor="#000000">
<tr>
<td bgcolor="#A9A9A9"></td>
<td bgcolor='#A9A9A9'><center> 13/11/2017 </center></td><!--<font color="#ffffff"> alguma coisa</font> //<strong> negrito</strong> -->
<td bgcolor='#A9A9A9'><center> 14/11/2017 </center></td>
<td bgcolor='#A9A9A9'><center> 15/11/2017 </center></td>
<td bgcolor='#A9A9A9'><center> 16/11/2017 </center></td>
<td bgcolor='#A9A9A9'><center> 17/11/2017 </center></td>
</tr>
<?php
for ($j = 1; $j <= 5; $j++) {
$num++;
echo "<tr>";
echo "<td bgcolor='#A9A9A9'>$num.ยบ</td>";
for ($i = 1; $i <= 5; $i++) {
$result = $result+1;?><td> <?php
if ($result == $campo){
echo $instrutor;

}
?> <center><a href="/rodrigo/favorito.php"> <?php echo $result; ?> </a>
<input type="checkbox" name="tipo" value="<?php $result ?>"></center></td>
<?php
}
echo "</tr>";
}
?>
</table></br></br>
<input type="submit" value="Enviar" />
</form>
</body>
</html>

Como eu faço esse loop pegar os dados do BD e ao mesmo tempo a tabela fique alinhada?
Trambulhao
Re: Tabela de agendamento
22 de November de 2017 às 10:06AM
Cara, as funções mysql_* para o MySQL estão desatualizadas na versão 7 do PHP. Não será isso? Agora só PDO ou Mysqli.
Trambulhao
Re: Tabela de agendamento
22 de November de 2017 às 10:33AM
Testa aí.. lembrando que as funções mysql_* estão desatualizadas... +cedo ou + tarde vais ter problemas.

<?php
include('connect_DB.php');
$comentario = mysql_query("SELECT * FROM lab") or die(mysql_error());
$totalRows_comentario = mysql_num_rows($comentario);
$result = 0;
$soma = 0;
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>lab</title>
<style>
.tb-horatio {
width: 600px;
border: 1px solid #000;
margin-bottom: 40px;
}
.tb-horatio > tr > td {
padding: 5px;
text-align: center;
}
.td-num {
background-color: #A9A9A9;
}
.td-date {
background-color: #A9A9A9;
}
</style>
</head>
<body>
<?php
if (empty($totalRows_comentario)):
echo 'Nenhum registo encontrado!';
else:
?>
<form method="post" action="inserir_horario.php" enctype="multipart/form-data">
<table class="tb-horario">
<tr>
<td class="td-date"></td>
<td class="td-date">13/11/2017</td>
<td class="td-date">14/11/2017</td>
<td class="td-date">15/11/2017</td>
<td class="td-date">16/11/2017</td>
<td class="td-date">17/11/2017</td>
</tr>
<?php
while ($row_comentario = mysql_fetch_assoc($comentario)):
$campo = $row_comentario['campo'];
$instrutor = $row_comentario['instrutor'];
$num++;
?>
<tr>
<td class='td-num'><?php echo $num; ?></td>
<td>
<?php if ($campo <= 5): ?>
<a href="/rodrigo/favorito.php"><?php echo $instrutor; ?></a>
<input type="checkbox" name="tipo" value="<?php echo $instrutor; ?>">
<?php
endif; ?>
</td>
<td>
<?php if ( ($campo >= 6) && ($campo <= 10)): ?>
<a href="/rodrigo/favorito.php"><?php echo $instrutor; ?></a>
<input type="checkbox" name="tipo" value="<?php echo $instrutor; ?>">
<?php
endif; ?>
</td>
<td>
<?php if ( ($campo >= 11) && ($campo <= 15)): ?>
<a href="/rodrigo/favorito.php"><?php echo $instrutor; ?></a>
<input type="checkbox" name="tipo" value="<?php echo $campo; ?>">
<?php
endif; ?>
</td>
<td>
<?php if ( ($campo >= 16) && ($campo <= 20)): ?>
<a href="/rodrigo/favorito.php"><?php echo $instrutor; ?></a>
<input type="checkbox" name="tipo" value="<?php echo $campo; ?>">
<?php
endif; ?>
</td>
<td>
<?php if ( ($campo >= 21) && ($campo <= 25)): ?>
<a href="/rodrigo/favorito.php"><?php echo $instrutor; ?></a>
<input type="checkbox" name="tipo" value="<?php echo $campo; ?>">
<?php
endif; ?>
</td>
</tr>
<?php
endwhile; ?>
</table>

<input type="submit" value="Enviar" />
</form>
<?php endif; ?>
</body>
</html>
Trambulhao
Re: Tabela de agendamento
22 de November de 2017 às 11:00AM
OPS! Havia um erro no anterior..

<?php
include('connect_DB.php');
$comentario = mysql_query("SELECT * FROM lab") or die(mysql_error());
$totalRows_comentario = mysql_num_rows($comentario);
$num = 0;
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>lab</title>
<style>
.tb-horatio {
width: 600px;
border-collapse: collapse;
margin-bottom: 20px;
}
.tb-horatio td {
padding: 5px;
border: 1px solid #000;
text-align: center;
}
.td-num {
background-color: #A9A9A9;
}
.td-date {
background-color: #A9A9A9;
}
</style>
</head>
<body>
<?php
if (empty($totalRows_comentario)):
echo 'Nenhum registo encontrado!';
else:
?>
<form method="post" action="inserir_horario.php" enctype="multipart/form-data">
<table class="tb-horatio">
<tr>
<td class="td-date"></td>
<td class="td-date">13/11/2017</td>
<td class="td-date">14/11/2017</td>
<td class="td-date">15/11/2017</td>
<td class="td-date">16/11/2017</td>
<td class="td-date">17/11/2017</td>
</tr>
<?php
while ($row_comentario = mysql_fetch_assoc($comentario)):
$campo = $row_comentario['campo'];
$instrutor = $row_comentario['instrutor'];
$num++;
?>
<tr>
<td class='td-num'><?php echo $num; ?></td>
<td>
<?php if ($campo <= 5): ?>
<a href="/rodrigo/favorito.php"><?php echo $instrutor; ?></a>
<input type="checkbox" name="tipo" value="<?php echo $instrutor; ?>">
<?php endif; ?>
</td>
<td>
<?php if ( ($campo >= 6) && ($campo <= 10)): ?>
<a href="/rodrigo/favorito.php"><?php echo $instrutor; ?></a>
<input type="checkbox" name="tipo" value="<?php echo $instrutor; ?>">
<?php endif; ?>
</td>
<td>
<?php if ( ($campo >= 11) && ($campo <= 15)): ?>
<a href="/rodrigo/favorito.php"><?php echo $instrutor; ?></a>
<input type="checkbox" name="tipo" value="<?php echo $campo; ?>">
<?php endif; ?>
</td>
<td>
<?php if ( ($campo >= 16) && ($campo <= 20)): ?>
<a href="/rodrigo/favorito.php"><?php echo $instrutor; ?></a>
<input type="checkbox" name="tipo" value="<?php echo $campo; ?>">
<?php endif; ?>
</td>
<td>
<?php if ( ($campo >= 21) && ($campo <= 25)): ?>
<a href="/rodrigo/favorito.php"><?php echo $instrutor; ?></a>
<input type="checkbox" name="tipo" value="<?php echo $campo; ?>">
<?php endif; ?>
</td>
</tr>
<?php endwhile; ?>
</table>
<input type="submit" value="Enviar" />
</form>
<?php endif; ?>
</body>
</html>
Rodrigo
Re: Tabela de agendamento
22 de November de 2017 às 11:42AM
Muito Obrigado Trambulhao.
Realmente preciso estudar essa parte de PDO ou Mysqli.

Mas acredito que eu tenha explicado errado. O que preciso é uma tabela igual a essa (código abaixo) mas que puxe os dados do banco de dados. Uma tabela 6x6 (6 linhas e 6 colunas). Não estou entendendo onde estou errando. Me ajude ai por favor.

<table width="600" border="1" bordercolor="#000000">
<tr>
<td bgcolor="#A9A9A9"></td>
<td bgcolor='#A9A9A9'><center> 13/11/2017 </center></td><!--<font color="#ffffff"> alguma coisa</font>
<td bgcolor='#A9A9A9'><center> 14/11/2017 </center></td>
<td bgcolor='#A9A9A9'><center> 15/11/2017 </center></td>
<td bgcolor='#A9A9A9'><center> 16/11/2017 </center></td>
<td bgcolor='#A9A9A9'><center> 17/11/2017 </center></td>
</tr>
<?php
for ($j = 1; $j <= 5; $j++) {
$num++;
echo "<tr>";
echo "<td bgcolor='#A9A9A9'>$num.ยบ</td>";
for ($i = 1; $i <= 5; $i++) {
$result = $result+1;?><td> <?php
if ($result == $campo){
echo $instrutor;
}
?> <center><a href="/rodrigo/favorito.php"> <?php echo $result; ?> </a>
<input type="checkbox" name="tipo" value="<?php $result ?>"></center></td>
<?php
}
echo "</tr>";
?>
</table>
Trambulhao
Re: Tabela de agendamento
22 de November de 2017 às 08:48PM
Você precisa imprimir a cada ciclo do FOR a linha da tabela com o teu conteúdo

No teu código o </tr> na está dentro do bloco FOR como deveria.. a linha começa no <tr> e termina no </tr>

ASSIM:

<tabela> <!-- início da tabela -->

[ FOR ]

<tr> <!-- início da linha -->
<td>..</td>
<td>..</td>
</tr> <!-- final da linha -->

[ /FOR ]

</table> <!-- final da tabela -->


EXEMPLO:

<?php
$num = 0;
?>
<table width="600" border="1" bordercolor="#000000">
<tr>
<td bgcolor="#A9A9A9"></td>
<td bgcolor='#A9A9A9'><center> 13/11/2017 </center></td>
<td bgcolor='#A9A9A9'><center> 14/11/2017 </center></td>
<td bgcolor='#A9A9A9'><center> 15/11/2017 </center></td>
<td bgcolor='#A9A9A9'><center> 16/11/2017 </center></td>
<td bgcolor='#A9A9A9'><center> 17/11/2017 </center></td>
</tr>
<?php
for ($j = 1; $j <= 6; $j++) {
$num++;
echo "<tr>";
echo "<td>$j</td>";
echo "<td>$j</td>";
echo "<td>$j</td>";
echo "<td>$j</td>";
echo "<td>$j</td>";
echo "<td>$j</td>";
echo "</tr>";
}
?>
</table>
Rodrigo
Re: Tabela de agendamento
23 de November de 2017 às 12:55AM
Se eu fizer como você me disse, todos os dados da tabela aparecem somente em uma coluna, e não distribuido. exemplo:

<?php
include('connect_DB.php');
$comentario = mysql_query("SELECT * FROM lab") or die(mysql_error());
$totalRows_comentario = mysql_num_rows($comentario);
$num = 0;
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>lab</title>
<style>
.tb-horatio {
width: 600px;
border-collapse: collapse;
margin-bottom: 20px;
}
.tb-horatio td {
padding: 5px;
border: 1px solid #000;
text-align: center;
}
.td-num {
background-color: #A9A9A9;
}
.td-date {
background-color: #A9A9A9;
}
</style>
</head>
<body>
<?php
if (empty($totalRows_comentario)):
echo 'Nenhum registo encontrado!';
else:
?>
<form method="post" action="inserir_horario.php" enctype="multipart/form-data">
<table class="tb-horatio">
<tr>
<td class="td-date"></td>
<td class="td-date">13/11/2017</td>
<td class="td-date">14/11/2017</td>
<td class="td-date">15/11/2017</td>
<td class="td-date">16/11/2017</td>
<td class="td-date">17/11/2017</td>
</tr>
<?php
while ($row_comentario = mysql_fetch_assoc($comentario)):
$campo = $row_comentario['campo'];
$instrutor = $row_comentario['instrutor'];
$num++;
?>
<tr>
<td class='td-num'><?php echo $num; ?></td>
<td>
<a href="/rodrigo/favorito.php"><?php echo $instrutor; ?></a>
<input type="checkbox" name="tipo" value="<?php echo $instrutor; ?>">
</td>
<?php endwhile; ?>
</table>
<input type="submit" value="Enviar" />
</form>
<?php endif; ?>
</body>
</html>
Trambulhao
Re: Tabela de agendamento
23 de November de 2017 às 08:08PM
Cria um arquivo e coloca o script abaixo.. vê se isso que você quer?

<?php
$db = [
1 => [
'instrutor' => 'João',
'comentario' => '2'
],
2 => [
'instrutor' => 'Pedro',
'comentario' => '10'
],
3 => [
'instrutor' => 'Maria',
'comentario' => '6'
],
4 => [
'instrutor' => 'Adriana',
'comentario' => '15'
],
5 => [
'instrutor' => 'Fabiana',
'comentario' => '7'
],
6 => [
'instrutor' => 'Rodrigo',
'comentario' => '4'
],
7 => [
'instrutor' => 'Ana',
'comentario' => '24'
],
8 => [
'instrutor' => 'Ronaldo',
'comentario' => '18'
],
9 => [
'instrutor' => 'Marcelo',
'comentario' => '2'
]
];
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>lab</title>
<style>
.tb-horatio {
width: 600px;
border-collapse: collapse;
margin-bottom: 20px;
}
.tb-horatio td {
padding: 5px;
border: 1px solid #000;
text-align: center;
}
.td-num {
background-color: #A9A9A9;
}
.td-date {
background-color: #A9A9A9;
}
</style>
</head>
<body>
<?php
if (empty($db)):
echo 'Nenhum registo encontrado!';
else:
?>
<form method="post" action="inserir_horario.php" enctype="multipart/form-data">
<table class="tb-horatio">
<tr>
<td class="td-date"></td>
<td class="td-date">13/11/2017</td>
<td class="td-date">14/11/2017</td>
<td class="td-date">15/11/2017</td>
<td class="td-date">16/11/2017</td>
<td class="td-date">17/11/2017</td>
</tr>
<?php
$num = 0;
for ($i = 1; $i <= count($db); $i++):
$campo = $db[$i]['comentario'];
$instrutor = $db[$i]['instrutor'];
$num++;
?>
<tr>
<td class='td-num'><?php echo $num; ?></td>
<td>
<?php if ($campo <= 5): ?>
<a href="/rodrigo/favorito.php"><?php echo $instrutor; ?></a>
<input type="checkbox" name="tipo" value="<?php echo $instrutor; ?>">
<?php endif; ?>
</td>
<td>
<?php if ( ($campo >= 6) && ($campo <= 10)): ?>
<a href="/rodrigo/favorito.php"><?php echo $instrutor; ?></a>
<input type="checkbox" name="tipo" value="<?php echo $instrutor; ?>">
<?php endif; ?>
</td>
<td>
<?php if ( ($campo >= 11) && ($campo <= 15)): ?>
<a href="/rodrigo/favorito.php"><?php echo $instrutor; ?></a>
<input type="checkbox" name="tipo" value="<?php echo $campo; ?>">
<?php endif; ?>
</td>
<td>
<?php if ( ($campo >= 16) && ($campo <= 20)): ?>
<a href="/rodrigo/favorito.php"><?php echo $instrutor; ?></a>
<input type="checkbox" name="tipo" value="<?php echo $campo; ?>">
<?php endif; ?>
</td>
<td>
<?php if ( ($campo >= 21) && ($campo <= 25)): ?>
<a href="/rodrigo/favorito.php"><?php echo $instrutor; ?></a>
<input type="checkbox" name="tipo" value="<?php echo $campo; ?>">
<?php endif; ?>
</td>
</tr>
<?php endfor; ?>
</table>
<input type="submit" value="Enviar" />
</form>
<?php endif; ?>
</body>
</html>
Rodrigo
Re: Tabela de agendamento
23 de November de 2017 às 09:58PM
Exatamente isso!
Mas agora é só substituir essa parte do código por uma SQL pegando os dados do MYSQL?
$db = [
1 => [
'instrutor' => 'João',
'comentario' => '2'
],
2 => [
'instrutor' => 'Pedro',
'comentario' => '10'
],
3 => [
'instrutor' => 'Maria',
'comentario' => '6'
],
4 => [
'instrutor' => 'Adriana',
'comentario' => '15'
],
5 => [
'instrutor' => 'Fabiana',
'comentario' => '7'
]
];

Muito Obrigado mesmo! Você entendeu exatamente o que eu queria!
Trambulhao
Re: Tabela de agendamento
23 de November de 2017 às 11:00PM
Exatamente! só substituir.. ou copia e cola o código da minha resposta do dia de 22 de November de 2017 às 05:00AM que está completo. A publicação que começa com "OPS! Havia um erro..."
Você precisa estar logado no PHPBrasil.com para poder enviar mensagens para os nossos fóruns.

Faça o login aqui.