How to number the rows being displayed?

Discussion in 'PHP' started by alhen, Aug 4, 2008.

  1. #1
    Hi there,
    I've tried googling this, but I'm not sure what to search for...

    What I have is a table that holds gig dates for my band. I have it to where it only shows for the year I want to look at.

    What I want is when I display a certain year (2008 for example) I want to have the entries numbered so I can just see how many there are total. I have the code to just give me a row count, but can't seem to get each entry numbered like this:

    1. Lucky Lounge - 8/9/2008
    2. Bob's Place - 8/15/2008
    3. And so on...

    Can you help me with this?

    Thanks!
     
    alhen, Aug 4, 2008 IP
  2. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if you are doign a while() loop through the entries, do this.
    $i = 1;
    while(getrow) {
    //print your data, including $i
    $i++;
    }
     
    matthewrobertbell, Aug 4, 2008 IP
  3. yleiko

    yleiko Peon

    Messages:
    74
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    alternatively you can use ordered list tag

    
    <ol>
    <li>First data</li>
    <li>Second data</li>
    <li>Third data</li>
    </ol>
    
    HTML:
     
    yleiko, Aug 4, 2008 IP
  4. lovelycesar

    lovelycesar Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That's right, it seems that you extract from 1 database.
     
    lovelycesar, Aug 4, 2008 IP
  5. alhen

    alhen Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you for the help.

    Can you tell me how that would fit into what I already have?

    
            <?  $hostname = "localhost";
    		$username = "****";
    		$password = "****";
    		$usertable = "****";
    		$database = "****";
    		$todate = date("Y-m-d");
    		$year = date("Y");
    	
    	MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
    	@mysql_select_db( "$database") or die( "Unable to select database"); 
    	
    	$query="SELECT * FROM $usertable WHERE date < '$todate' AND year='$year' ORDER BY date DESC";
    	$result=mysql_query($query) or die(mysql_error());
    	if (mysql_num_rows($result) == 0) {  echo "<div class=\"text\" align=\"center\"><b>That year was not found</b></div>";
    	} else {
    	while ($row = mysql_fetch_assoc($result)) {
    	echo "<table width=\"345\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
    	  <tr>
    		<td><table width=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
    		  <tr>
    			<td width=\"100%\"><span class=\"bigtext\"\"><i><b>" . $row['day'] . "</b> - <b>" . $row['disp_date'] . " @ " . $row['timestart'] . " " . $row['ampm'] . "</b></i></span></td>
    		  </tr>
    		</table></td>
    	  </tr>
    	  <tr>
    		<td class=\"text\">" . $row['location'] . "<br>
    		<div class=\"text\">Paid: $" . $row['paid'] . "</div></td>
    	  </tr>
    	</table><center><hr size=\"1\" color=\"white\"></center>";
    	 }
    	}
    	
    	?>
    
    PHP:
     
    alhen, Aug 5, 2008 IP
  6. spyka

    spyka Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #6
    
      $i = 0;
     while ($row = mysql_fetch_assoc($result)) {
        echo "<table width=\"345\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
          <tr>
            <td><table width=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
              <tr>
                <td width=\"100%\"><span class=\"bigtext\"\"><i><b>{$i} " . $row['day'] . "</b> - <b>" . $row['disp_date'] . " @ " . $row['timestart'] . " " . $row['ampm'] . "</b></i></span></td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td class=\"text\">" . $row['location'] . "<br>
            <div class=\"text\">Paid: $" . $row['paid'] . "</div></td>
          </tr>
        </table><center><hr size=\"1\" color=\"white\"></center>";
         $i++;
         }
        }
    
    PHP:
     
    spyka, Aug 5, 2008 IP
  7. lovelycesar

    lovelycesar Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    For further design, you can change the background of TD tags while its number ($i) is even or odd.
    
    function is_even($i){
    // calculate $i here
    // if $i = even, return TRUE
    }
    if(is_even($i)=TRUE){ 
    // print background-color = red
    else {
    //print background-color = blue, for example
    }
    
    Code (markup):
     
    lovelycesar, Aug 20, 2008 IP