So Kim So Kim - 1 month ago 6
PHP Question

PHP pagination table how to change the display to top to bottom

I am trying to create the PHP pagination table.. but in below format..

ID: data

Comments: data

Date: data


but now my table is like this:


ID Comments Date

data data data

By any chance anyone could let me know how to change it? .......my code is like this now:

<?
$connection=Mysql_connect('XX','XX','XX');
if(!$connection)
{
echo 'connection is invalid';
}
else
{
Mysql_select_db('XX',$connection);

}
//check if the starting row variable was passed in the URL or not
if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
//we give the value of the starting row to 0 because nothing was found in URL
$startrow = 0;
//otherwise we take the value from the URL
} else {
$startrow = (int)$_GET['startrow'];
}
//this part goes after the checking of the $_GET var
$fetch = mysql_query("SELECT * FROM products LIMIT $startrow, 10")or
die(mysql_error());
$num=Mysql_num_rows($fetch);
if($num>0)
{
echo "<table border=0 >";
echo "<tr><td>ID</td><td>Drug</td><td>quantity</td></tr>";
for($i=0;$i<$num;$i++)
{
$row=mysql_fetch_row($fetch);


echo "<tr>";
echo"<td>$row[1]</td>";

echo"<td>$row[2]</td>";
echo"<td>$row[3]</td>";
echo"<td >$row[5]</td>";
echo"</tr>";
}//for
echo"</table>";
}
//now this is the link..
echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+10).'">Next</a>';

$prev = $startrow - 10;

//only print a "Previous" link if a "Next" was clicked
if ($prev >= 0)
echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.$prev.'">Previous</a>';
?></td>
<td width="331">&nbsp;</td>
</tr>
</table></td>
</tr>
</table>
</td>
</tr>
<?php
include("footer.php");
?>


sorry just figure out now..

$row=mysql_fetch_row($fetch);
echo "<tr>";
echo"<td>ID:$row[1]</td>";
echo "</tr>";
echo "<tr>";
echo"<td>Drug:$row[2]</td>"; echo "</tr>"; echo "<tr>";
echo"<td>QTY:$row[3]</td>"; echo "</tr>"; echo "<tr>";
echo"<td >Date:$row[5]</td>"
;echo "</tr>";
echo"</tr>"; echo"<tr height ='20'>";echo"</tr>";
echo"<tr >";echo"</tr>";echo"<tr >";echo"</tr>";
}//for
echo"</table>";
}

Answer Source

Oh, I got it now !

That you can achieve below way:

<table cellpadding="0" cellspacing="0">
 <tr>
  <th>ID</th>
  <th>Comments</th>
  <th>Date</th>
 </tr>
 <?php 
  if(mysql_num_rows($recordsets) > 0){
   foreach ($recordsets as $recordset): 
 ?>
  <tr>
    <td><?php echo $recordset['id']); ?></td>
    <td><?php echo $recordset['comments']); ?></td>
    <td><?php echo $recordset['date']); ?></td>
   </tr>
  <?php 
      endforeach; 
    } else {
  ?>
  <tr>
   <td colspan="3">No records found !</td>
  </tr>
  <?php 
    }
  ?>
</table>

Here, first row will be your table records header information and then loop through your records result sets.

Hope it helps you !