Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML and PHP loop

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
How do I get my HTML table to be created and data to be entered in slotts AAAAA, BBBBBB etc. I cannot seem to get any table output moving it within the PHP loop.

<?
session_start();

mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("pagefeeds") or die(mysql_error());

$query="SELECT * FROM betasp";
$result=mysql_query($query);

// this tells me how many rows in table
$num=mysql_numrows($result);
// echo $num;

mysql_close();

// loop through the records in database
$i=0;
while ($i < $num) {

$one=mysql_result($result,$i,"Code");
$two=mysql_result($result,$i,"Description");

echo "<b>$one $two</b><br><hr><br>";
$i++;
}


?>

<html>

<head>
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Code</title>
</head>

<body>

<form method="POST" action="" >

<p>&nbsp;</p>
<table border="1" width="100%" id="table1">
<tr>
<td colspan="5">&nbsp;</td>
</tr>
<tr>
<td width="18%" align="center"><font face="Arial"><b>Code</b></font></td>
<td width="34%" align="center"><font face="Arial"><b>Description</b></font></td>
<td width="5%" align="center"><font face="Arial"><b>Qty</b></font></td>
<td width="18%"><font face="Arial"><b>Unit Price £</b></font></td>
<td width="22%" rowspan="6">&nbsp;</td>
</tr>


<tr>
<td width="18%">AAAAAAAA</td>
<td width="34%">BBBBBBBB</td>
<td width="5%" align="center">&nbsp;</td>
<td width="18%">CCCCCCCC</td>
</tr>

</table>

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>

</body>

</html>


I only need the table headings bit shown once.

Any help much appreciated. The form event posting info is ommited at the moment. Regards
 
How about doing it like this;

Code:
<table border="0" cellpadding="0" cellpadding="0" width="80%"> 
<tr> 
  <td><strong>Field</strong></td>
  <td><strong>Type</strong></td>
</tr>
<tr> 
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<?
include ("admin/dbconf.php"); // include your db connection
			  
// Get Items Out //
$query = "show columns in users;";
$result = mysql_query($query);
 
while ($row = mysql_fetch_array($result)) {
?>
<tr> 
  <td><? echo $row['Field']; ?></td>
  <td><? echo $row['Type']; ?></td>
</tr>
<?
}
?>
</table>

That will print your table heading before the loop, the content in the middle (once for each row returned) and then end the table with </table> once finished.

I wish someone would just call me Sir, without adding 'Your making a scene'.

Rob
 
Many thanks, however is there a way to sandwich just showing my results where they should go, as I can then have that part clearer for me to confiugure the table further, and wanting to keep columns set at widths etc.

I have tried removing showing the two fields at the start of the program, and carrying them down as echo statements later on with <? ?> tags but either get errors or nothings there. Many thanks
 
Nearly there, but now the end of my table is crunched up. Anyone spot mistake, thanks

<form method="POST" action="" >

<p>&nbsp;</p>
<table border="1" width="100%" id="table1">
<tr>
<td colspan="5">&nbsp;</td>
</tr>
<tr>
<td width="18%" align="center"><font face="Arial"><b>Code</b></font></td>
<td width="34%" align="center"><font face="Arial"><b>Description</b></font></td>
<td width="5%" align="center"><font face="Arial"><b>Qty</b></font></td>
<td width="18%"><font face="Arial"><b>Unit Price £</b></font></td>
<td width="22%" rowspan="6">&nbsp;</td>
</tr>

<?
$i=0;
while ($i < $num) {
$one=mysql_result($result,$i,"Code");
$two=mysql_result($result,$i,"Description");
?>

<tr>
<td width="18%" align="center"><font face="Arial"><b><? echo $one; ?></b></font></td>
<td width="34%" align="center"><font face="Arial"><b><? echo $two; ?></b></font></td>
<td width="5%" align="center"><font face="Arial"><b>---</b></font></td>
<td width="18%"><font face="Arial"><b>nothing</b></font></td>
<td width="22%" rowspan="6">&nbsp;</td>
</tr>

<?
// echo $one;
// echo "<td>" < echo $one >; >echo "</td>";

$i++;
}
?>

</table>

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
 
Can someone please tell me why when I insert the remmed out PHP the table (right hand end) loses its merge to the above cell, and looks unhappy'

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table border="1" width="80%" id="table3">
<tr>
<td colspan="4">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>code</td>
<td>descip</td>
<td>qty</td>
<td>unit price</td>
<td rowspan="2">&nbsp;</td>
</tr>

<?
// $i=0;
// while ($i < $num) {
// $one=mysql_result($result,$i,"Code");
// $two=mysql_result($result,$i,"Description");
?>




<tr>
<td>aaa</td>
<td>bbb</td>
<td>cccc</td>
<td>dddd</td>
</tr>
<?
// echo $one;
// echo "<td>" < echo $one >; >echo "</td>";

// $i++;
// }
?>


</table>

Would be so good to get rid of this so I can carry on. Thanks
 
Here's my trusty dynamic php/mysql table, you'll hate the colours but it works like a charm.

Code:
	echo "<table align=\"center\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\">\n";

	$row=0;

// display field names

		print "<th>row</th>";

	for ($i = 0; $i < mysql_num_fields($result); $i++) {

		print "<th>".mysql_field_name($result,$i)."</th>\n";

	}

	while ($myrow = @mysql_fetch_array($result)){

// start the results display table

		$row++;

		if($row%2){

			$color="#CCCCCC";

		}else{

			$color="#9FC6C4";

		}

		echo "<tr><td>$row</td>";

		for ($i = 0; $i < ($columns); $i++) {

			echo "<td bgcolor=\"$color\"> $myrow[$i] </td>\n";

		}

		echo "</tr>\n";

	}

	mysql_free_result($result);



	echo "</table>\n";

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top