Japhet Smith Japhet Smith - 2 months ago 5
MySQL Question

Ordered list for table

My database data is echoed in a table list but I will like for the list to be ordered for reference purposes. I have tried several approaches but it doesn't seem to work. Any way possible? here is my code

<table width="auto" border="0" align="center" cellpadding="2" cellspacing="1" class="text">
<tr align="center" id="listTableHeader">
<td>S/N</td>
<td>First Name</td>
<td>Last Name</td>
</tr>
<?php
while($row = dbFetchAssoc($result)) {
extract($row);

if ($i%2) {
$class = 'row1';
} else {
$class = 'row2';
}

$i += 1;
?>
<tr class="<?php echo $class; ?>">

<td width="70" align="center"><a href="javascript:deleteUser(<?php echo $id; ?>);">Delete</a></td>
<td></td>
<td><?php echo $Firstame; ?></td>
<td><?php echo $LastName; ?></td>
</tr>
<?php
} // end while

?>
<tr>
<td colspan="5">&nbsp;</td>
</tr>
<tr>
<td colspan="5" align="right"></td>
</tr>
</table>

Answer

I can see some mistakes in your code like disorder of some statements.I don't know what is your actual code.How ever here iI am share with my knowledge through the small example.I think it may be helps to solve your issue.You just look the following code and change it with your tables and database connection credential.If any problem in this,you can tell me.I will give the further solution as I can.

    <table width="auto" border="0" align="center" cellpadding="2" cellspacing="1" class="text">
  <tr align="center" id="listTableHeader">
      <td></td>   
  <td>S/N</td>
  <td>First Name</td>
   <td>Last Name</td>
   </tr>
   <?php
   $conn=  mysqli_connect("localhost", "root", "", "test");
if(!$conn)
{
    echo 'not';die;
}
   $result=mysqli_query($conn,"select * from animals");
   $i=0;
   $j=1;
while($row = mysqli_fetch_array($result)) {
    $Firstname=$row['name'];
    $LastName=$row['animal'];
   $id=$row['animal_ID'];

    if ($i%2) {
        $class = 'row1';
    } else {
        $class = 'row2';
    }

    $i += 1;

?>
<tr class="<?php echo $class; ?>"> 

<td width="70" align="center"><a href="javascript:deleteUser(<?php echo $id; ?>);">Delete</a></td>
<td><?php echo $j;?></td>
   <td><?php echo $Firstname; ?></td>
   <td><?php echo $LastName; ?></td>
    </tr>
<?php
 $j++;
} // end while

?>
  <tr> 
   <td colspan="5">&nbsp;</td>
  </tr>
  <tr> 
   <td colspan="5" align="right"></td>
  </tr>
 </table>
Comments