monkeytooth Posted February 26, 2010 Share Posted February 26, 2010 Ok, I have a loop running. Works fine. But now I want to add a hr tag between results. Which that I can do, what I can't do though is figure out how to find if while the loop is running if the result being echoed out is the last one or not. Because I don't want an hr under the last result. So is there any way to determine the last result in a loop and remove an element from it in such a way? Link to comment https://forums.phpfreaks.com/topic/193481-while-and-for-loops/ Share on other sites More sharing options...
alpine Posted February 26, 2010 Share Posted February 26, 2010 You need a total results to do this easily, example <?php $i = 1; while ($i <= 10) { $i++; if($i <> 10) echo "<hr>"; } ?> Link to comment https://forums.phpfreaks.com/topic/193481-while-and-for-loops/#findComment-1018578 Share on other sites More sharing options...
monkeytooth Posted February 26, 2010 Author Share Posted February 26, 2010 while($results = mysql_fetch_array($display_result)) { /*code to repeat omitted*/ } I should have added this with my original post. I am taking info out of a DB, to which a static count does not exist. I could show 3 results I could show 50 pending on the query. So I wouldnt be able to set an if statement on it with a set number. So is there a way to find the last entry in a loop built like above and remove a piece from it as previously asked? Or am I just shooting in the dark for hopes.. Link to comment https://forums.phpfreaks.com/topic/193481-while-and-for-loops/#findComment-1018745 Share on other sites More sharing options...
jl5501 Posted February 26, 2010 Share Posted February 26, 2010 $rows = mysql_num_rows($display_result); Link to comment https://forums.phpfreaks.com/topic/193481-while-and-for-loops/#findComment-1018748 Share on other sites More sharing options...
Psycho Posted February 26, 2010 Share Posted February 26, 2010 Just use a counter and compare with the total records returned from the query using mysql_num_rows() as jl5501 suggests above. Another option is to put the results into an array and implode() the array with whatever divider you want. Exmple #1: $totalRows = mysql_num_rows($result); $rowCount = 0; while($row = mysql_fetch_assoc($result)) { $rowCount++; //Echo the results echo "{$row['somedata']}<br />\n"; if ($rowCount < $totalRows) { echo "<hr>\n"; } } Exmple #2: $outputArray = array(); while($row = mysql_fetch_assoc($result)) { //Add data to result array $outputArray[] = "{$row['somedata']}<br />\n"; } //Echo output echo implode("<hr>\n", $outputArray); Link to comment https://forums.phpfreaks.com/topic/193481-while-and-for-loops/#findComment-1018755 Share on other sites More sharing options...
monkeytooth Posted February 27, 2010 Author Share Posted February 27, 2010 Thank you all for your help.. Link to comment https://forums.phpfreaks.com/topic/193481-while-and-for-loops/#findComment-1018766 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.