unemployment Posted February 8, 2011 Share Posted February 8, 2011 How do I tell php to add first and last class at the beginning and end of a while loop. <?php while (($e_row = mysql_fetch_assoc($e_query)) !== false) { ?> <li class="companyname first last"> blah </li> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/227089-first-and-last/ Share on other sites More sharing options...
Roman Fedorov Posted February 8, 2011 Share Posted February 8, 2011 Use http://php.net/manual/en/function.mysql-num-rows.php to know how much rows you are going to write, then keep a counter variable, when it is 1 then write the "first" when it is the rows number write "last" Link to comment https://forums.phpfreaks.com/topic/227089-first-and-last/#findComment-1171550 Share on other sites More sharing options...
lastkarrde Posted February 8, 2011 Share Posted February 8, 2011 Get the number of results returned using mysql_num_rows, then keep a count of a variable (eg $i) and increment it in the loop. Then in the loop if($i == 0) { //first } if($i == result from numrows) { // last } Link to comment https://forums.phpfreaks.com/topic/227089-first-and-last/#findComment-1171555 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.