PDA

View Full Version : Limiting Columns And Rows In Php-please Help!



shdt
September 10th, 2007, 03:13 PM
I am trying to limit rows and columns in a table through while statement, Now when I use :

<?php

$pl_id = $row['place'];
$result1 = mysql_query ("SELECT * FROM places") or die (mysql_error());

$tdcount = 1;
$numtd = 5; // number of cells per row
echo "<table border=1>";
while ($row1 = mysql_fetch_array ($result1))
{ if ($tdcount == 1)
echo "<tr>";
echo "<td><a href=hHome_display.php?x=".$row1['id'] .">".$row1['place_name']."</a></td>"; // display as you like
if ($tdcount == $numtd)
{
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
} // time to close up our table
if ($tdcount!= 1)
{
while ($tdcount <= $numtd) {
echo "<td>&nbsp;</td>";
$tdcount++; } echo "</tr>";
}
echo "</table>";
?>

It works fine, but if try to bring it into another while statement like :

<?php
$result = mysql_query("SELECT distinct place FROM hh_details") or die (mysql_error());
while ($row = mysql_fetch_array ($result))
{
$pl_id = $row['place'];
$result1 = mysql_query ("SELECT * FROM places WHERE id = '$pl_id'") or die (mysql_error());

$tdcount = 1;
$numtd = 5; // number of cells per row
echo "<table border=1>";
while ($row1 = mysql_fetch_array ($result1))
{ if ($tdcount == 1)
echo "<tr>";
echo "<td><a href=hHome_display.php?x=".$row1['id'] .">".$row1['place_name']."</a></td>"; // display as you like
if ($tdcount == $numtd)
{
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
} // time to close up our table
if ($tdcount!= 1)
{
while ($tdcount <= $numtd) {
echo "<td>&nbsp;</td>";
$tdcount++; } echo "</tr>";
}
echo "</table>";
}
?>

It returns a tr with five td's, the first td displays data, while the others are blank and then another tr, the first td displays data and the others blank. Where am I going wrong? Please help!

shdt
September 11th, 2007, 11:05 AM
Thanks for viewing, I solved it myself.

The code should be

<table border="1" cellpadding="2" cellspacing="2" bordercolor="#000000" align="center">
<?php while ($row = mysql_fetch_array ($result))
{
$pl_id = $row['place'];
$result1 = mysql_query ("SELECT * FROM places WHERE id = '$pl_id'") or die (mysql_error());


while ($row1 = mysql_fetch_array ($result1))
{ if ($tdcount == 1)
echo "<tr>";
?>
<td align="center" style="padding-left:3px; padding-right:3px; padding-top:1px; padding-bottom:1px" width="90px">
<?php
echo "<a href=hHome_display.php?x=".$row1['id'] .">".$row1['place_name']."</a>"; // display as you like
?>
</td>
<?php
if ($tdcount == $numtd)
{
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
}
}// time to close up our table
if ($tdcount!= 1)
{
while ($tdcount <= $numtd) {
echo "<td>&nbsp;</td>";
$tdcount++; } echo "</tr>";
}
?>
</table>