Jump to content

How Do I Show Numbering To Table?


lovephp

Recommended Posts

how do i do it guys?

 

here is the code

while($res = mysql_fetch_array($result))
           {
						    $id = $res['id'];
               $date = $res['time'];
  						 $newdate = date("D, j M, Y", $date);    
						    if($res['approval'] == "Yes"){
               echo "<tr align='center' bgcolor='#CDE861'>";
						    }else if($res['approval'] == "No"){
						    echo "<tr align='center' bgcolor='#F28598'>";
						    }else{
						    echo "<tr align='center' bgcolor='#EDA553'>";
						    }
               echo"<td><font color='black'>" .$res['randid']."</font></td>";
						    echo"<td><font color='black'>" .strtoupper($res['center'])."</font></td>";
               echo"<td><font color='black'>" .$res['customer_name']."</font></td>";
               echo"<td><font color='black'>". $res['home_phone']. "</font></td>";
               echo"<td><font color='black'>". $newdate. "</font></td>";
						    if($res['approval'] == "Yes"){
						    echo"<td>Approved</td>";
						    }else if($res['approval'] == "No"){
						    echo"<td>Rejected</td>";
						    }else{
						    echo"<td>Pending</td>";
						    }
echo"<td> <a href ='view.php?id=$id'><center>Click to View </center></a></td>";
               if($name=='off'){
						    echo '';

 

i want to the rows to display results by numbering like

 

1. id name etc etc

2. id name etc etc

Link to comment
https://forums.phpfreaks.com/topic/270468-how-do-i-show-numbering-to-table/
Share on other sites

Some notes first:

 

1. The FONT tags have been deprecated for many, many years!

2. Simplify your logic by determining the values to display at the beginning of the loop and then do the output after.

 

To add the row numbers, just add a variable and increment and output it on each iteration.

 

$rowNo = 0;
while($res = mysql_fetch_array($result))
{
   $rowNo++; //Increment Row Number
   $date = date("D, j M, Y", $res['time']);
   $center = strtoupper($res['center']);

   if($res['approval'] == "Yes") {
    $approvalText = "Approved":
    $approvalColor = '#CDE861';
   } else if($res['approval'] == "No") {
    $approvalText = "Rejected":
    $approvalColor = '#F28598';
   } else {
    $approvalText = "Pending":
    $approvalColor = '#EDA553';
   }

   echo "<tr align='center' bgcolor='{$approvalColor}'>\n";
   echo "  <td style='color:black;'>{$rowNo}</td>";
   echo "  <td style='color:black;'>{$res['randid']}</td>";
   echo "  <td style='color:black;'>{$center}</td>";
   echo "  <td style='color:black;'>{$res['customer_name']}</td>";
   echo "  <td style='color:black;'>{$res['home_phone']}</td>";
   echo "  <td style='color:black;'>{$date}</td>";
   echo "  <td>{$approvalText}</td>";
   echo "  <td><a href ='view.php?id={$res['id']}'><center>Click to View </center></a></td>\n";

   //And continue on for other rows

   echo "</tr>\n";
}

  On 11/8/2012 at 7:50 PM, Psycho said:

Some notes first:

 

1. The FONT tags have been deprecated for many, many years!

2. Simplify your logic by determining the values to display at the beginning of the loop and then do the output after.

 

To add the row numbers, just add a variable and increment and output it on each iteration.

 

$rowNo = 0;
while($res = mysql_fetch_array($result))
{
$rowNo++; //Increment Row Number
$date = date("D, j M, Y", $res['time']);
$center = strtoupper($res['center']);

if($res['approval'] == "Yes") {
 $approvalText = "Approved":
 $approvalColor = '#CDE861';
} else if($res['approval'] == "No") {
 $approvalText = "Rejected":
 $approvalColor = '#F28598';
} else {
 $approvalText = "Pending":
 $approvalColor = '#EDA553';
}

echo "<tr align='center' bgcolor='{$approvalColor}'>\n";
echo " <td style='color:black;'>{$rowNo}</td>";
echo " <td style='color:black;'>{$res['randid']}</td>";
echo " <td style='color:black;'>{$center}</td>";
echo " <td style='color:black;'>{$res['customer_name']}</td>";
echo " <td style='color:black;'>{$res['home_phone']}</td>";
echo " <td style='color:black;'>{$date}</td>";
echo " <td>{$approvalText}</td>";
echo " <td><a href ='view.php?id={$res['id']}'><center>Click to View </center></a></td>\n";

//And continue on for other rows

echo "</tr>\n";
}

 

thanks for this mate really helpful. but im still not sure how to go on about doing the numbering?

Perhaps you missed it?

  On 11/8/2012 at 7:50 PM, Psycho said:

To add the row numbers, just add a variable and increment and output it on each iteration.

 

$rowNo = 0;
while($res = mysql_fetch_array($result))
{
   $rowNo++; //Increment Row Number
   $date = date("D, j M, Y", $res['time']);
   $center = strtoupper($res['center']);

   if($res['approval'] == "Yes") {
       $approvalText = "Approved":
       $approvalColor = '#CDE861';
   } else if($res['approval'] == "No") {
       $approvalText = "Rejected":
       $approvalColor = '#F28598';
   } else {
       $approvalText = "Pending":
       $approvalColor = '#EDA553';
   }

   echo "<tr align='center' bgcolor='{$approvalColor}'>\n";
   echo "  <td style='color:black;'>{$rowNo}</td>";
   echo "  <td style='color:black;'>{$res['randid']}</td>";
   echo "  <td style='color:black;'>{$center}</td>";
   echo "  <td style='color:black;'>{$res['customer_name']}</td>";
   echo "  <td style='color:black;'>{$res['home_phone']}</td>";
   echo "  <td style='color:black;'>{$date}</td>";
   echo "  <td>{$approvalText}</td>";
   echo "  <td><a href ='view.php?id={$res['id']}'><center>Click to View </center></a></td>\n";

   //And continue on for other rows

   echo "</tr>\n";
}

$rowNo = 0;
while($res = mysql_fetch_array($result))
{
$rowNo++; //Increment Row Number
$date = date("D, j M, Y", $res['time']);
$center = strtoupper($res['center']);


if($res['approval'] == "Yes") {
$approvalText = "Approved":
$approvalColor = '#CDE861';
} else if($res['approval'] == "No") {
$approvalText = "Rejected":
$approvalColor = '#F28598';
} else {
$approvalText = "Pending":
$approvalColor = '#EDA553';
}?>


<tr align='center' bgcolor='<?php {$approvalColor} \n ?>'>
<td style='color:black;'><?php {$rowNo}; ?></td>
<td style='color:black;'><?php {$res['randid']}; ?></td>
<td style='color:black;'><?php {$center}; ?></td>
<td style='color:black;'><?php {$res['customer_name']}; ?></td>
<td style='color:black;'><?php {$res['home_phone']}; ?></td>
<td style='color:black;'><?php {$date}; ?></td>
<td><?php {$approvalText}; ?></td>
<td><a href ="view.php?id=<?php {$res['id']}; ?>"><center>Click to View </center></a></td><br />


//And continue on for other rows


</tr><br /><?php
}

 

sorry but thats the better way to right it instead of using all the echo codes

  On 11/9/2012 at 6:24 AM, JamesThePanda said:
sorry but thats the better way to right it instead of using all the echo codes

 

Note that code like the following will give a parse error about an unexpected '}'.

 

<td style='color:black;'><?php {$rowNo}; ?></td>

 

You still need an echo statement and the curly quotes aren't needed. Also, the inline styles could be replaced with a style sheet.

 

<tr><td><?php echo $rowNo; ?></td></tr>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.