arcanine Posted March 12, 2009 Share Posted March 12, 2009 Hello there I've got two questions that I hope you can help me with firstly as you can see here I've got a loop which makes a list of links out of the id field and name fields of my database <?php $result = mysql_query("SELECT `id`,`name` FROM linkvideos_category"); while($row = mysql_fetch_assoc($result)){ echo' <a class="categorylink" href="'.$web_path.'video_listing.php?category='.$row['id'].'">'.$row['name'].'</a> <br /> <img src="'.$pro_path.'/images/box_middle_line.jpg" /> '; } ?> Now I want the loop on the last iteration not to output that image and I'm not sure how & lastly I input a new name and id into my database but its not being reflected in my code, is there any reason why it wouldn't be updating and appending my new item to this list when I run the website? Thanks for your time, arcanine. ps what does that $result string do exactly? I've modified the code from a tutorial I found Quote Link to comment https://forums.phpfreaks.com/topic/149030-last-itteration-of-a-loop/ Share on other sites More sharing options...
lonewolf217 Posted March 12, 2009 Share Posted March 12, 2009 hmm you can use mysql_num_rows to get the number of results, then have a counter that decreases as it goes through the loop. if the counter is zero, dont output the image. dont know about the second part, can you manually query it and see the result ? Quote Link to comment https://forums.phpfreaks.com/topic/149030-last-itteration-of-a-loop/#findComment-782572 Share on other sites More sharing options...
arcanine Posted March 12, 2009 Author Share Posted March 12, 2009 I was modifying the wrong table haha thanks I wouldn't have looked otherwise, okay I've taken a shot at what you said but please keep in mind I'm not a php coder by a long stretch <?php $result = mysql_query("SELECT `id`,`name` FROM linkvideos_category"); $num_rows = mysql_num_rows($result); while($row = mysql_fetch_assoc($result)){ if ($num_rows != 0 ){ $num_rows--; echo' <a class="categorylink" href="'.$web_path.'video_listing.php?category='.$row['id'].'">'.$row['name'].'</a> <br /> <img src="'.$pro_path.'/images/box_middle_line.jpg" />} '; else { echo' <a class="categorylink" href="'.$web_path.'video_listing.php?category='.$row['id'].'">'.$row['name'].'</a>} '; } ?> I think it says if $num_rows isn't 0 (so isn't on the last iteration) then output as normal else output without the image Quote Link to comment https://forums.phpfreaks.com/topic/149030-last-itteration-of-a-loop/#findComment-782589 Share on other sites More sharing options...
samshel Posted March 12, 2009 Share Posted March 12, 2009 if ($num_rows != 1 ){ ... } else { } $num_rows--; try putting the decreament after the if else condition. u need to decrement after u check it. and i think u need to check for 1, rather than 0 for last record. Quote Link to comment https://forums.phpfreaks.com/topic/149030-last-itteration-of-a-loop/#findComment-782591 Share on other sites More sharing options...
lonewolf217 Posted March 12, 2009 Share Posted March 12, 2009 my logic was slightly off, num_rows should be == 1 when its the last row. you can just echo your row counter each time to make sure its correct Quote Link to comment https://forums.phpfreaks.com/topic/149030-last-itteration-of-a-loop/#findComment-782592 Share on other sites More sharing options...
arcanine Posted March 12, 2009 Author Share Posted March 12, 2009 it was zero in the end had a bit of trouble with it but got there in the end for those interested finished code was: <?php $result = mysql_query("SELECT `id`,`name` FROM linkvideos_category"); $num_rows = mysql_num_rows($result); while($row = mysql_fetch_assoc($result)){ $num_rows--; if ($num_rows != 0 ){ echo' <a class="categorylink" href="'.$web_path.'video_listing.php?category='.$row['id'].'">'.$row['name'].'</a> <br /> <img src="'.$pro_path.'/images/box_middle_line.jpg" />'; } else { echo' <a class="categorylink" href="'.$web_path.'video_listing.php?category='.$row['id'].'">'.$row['name'].'</a>'; }} ?> Thanks a lot for your help guys Quote Link to comment https://forums.phpfreaks.com/topic/149030-last-itteration-of-a-loop/#findComment-782601 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.