delphi123 Posted January 6, 2008 Share Posted January 6, 2008 Can someone explain while loops in numpty language to me? I'm trying to cycle through news items fetched from a table in the code below, but I don't really understand what the while part does: while ($getNews = mysql_fetch_array($result)) I always see mysql_fetch_array in while loops - but what does the loop actually do? Does it automatically increment or something? If so, how does it know what to increment (I'm trying to get it to loop down the rows, but why not the columns or every piece of data?) $query = ("SELECT news_id, category, title, text, img_url, linkto, ext_url, DATE_FORMAT(date,'%d %M, %Y') as sd FROM news_posts ORDER BY date DESC LIMIT 0, 3") or DIE ("Can't retrieve"); $result=@mysql_query($query) or die("Error in News"); <?php while ($getNews = mysql_fetch_array($result)) { echo "<div id='hiddenNews1' style='display:none;'> <a href='#'><img src='".$site.$getNews['img_url']."' width=292 height=176 /></a> </div>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84763-dont-understand-while-loops/ Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 The while loop runs as long as the object in its parentheses is not false or 0. mysql_fetch_array returns an array if there are results or false if there are none. Once while($getNews = false) the while loop stops because it hits false or 0. Quote Link to comment https://forums.phpfreaks.com/topic/84763-dont-understand-while-loops/#findComment-431962 Share on other sites More sharing options...
kratsg Posted January 6, 2008 Share Posted January 6, 2008 The while loop runs as long as the object in its parentheses is not false or 0. mysql_fetch_array returns an array if there are results or false if there are none. Once while($getNews = false) the while loop stops because it hits false or 0. Not quite the right explanation. Look at mysql_fetch_array($result). This is an array. So we have an array of 'n' items, and we will go through each index in the array, until we can't go to the next one (n+1 item). At this point, it returns a false error as that array value doesn't exist anymore. Quote Link to comment https://forums.phpfreaks.com/topic/84763-dont-understand-while-loops/#findComment-431969 Share on other sites More sharing options...
delphi123 Posted January 6, 2008 Author Share Posted January 6, 2008 well that's sort of what I assumed, but the above code is just returning the first news image, and not 3 like I expected? Have I done something stupid? Quote Link to comment https://forums.phpfreaks.com/topic/84763-dont-understand-while-loops/#findComment-431980 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 Try changing: $query = ("SELECT news_id, category, title, text, img_url, linkto, ext_url, DATE_FORMAT(date,'%d %M, %Y') as sd FROM news_posts ORDER BY date DESC LIMIT 0, 3") or DIE ("Can't retrieve"); To: $query = "SELECT news_id, category, title, text, img_url, linkto, ext_url, DATE_FORMAT(date,'%d %M, %Y') as sd FROM news_posts ORDER BY date DESC LIMIT 0, 3"; And if you're only using the img_url column, you don't have to select all that. $query = "SELECT img_url FROM news_posts ORDER BY date DESC LIMIT 0, 3"; Quote Link to comment https://forums.phpfreaks.com/topic/84763-dont-understand-while-loops/#findComment-431987 Share on other sites More sharing options...
trq Posted January 6, 2008 Share Posted January 6, 2008 The while loop runs as long as the object in its parentheses is not false or 0. mysql_fetch_array returns an array if there are results or false if there are none. Once while($getNews = false) the while loop stops because it hits false or 0. Not quite the right explanation. Look at mysql_fetch_array($result). This is an array. So we have an array of 'n' items, and we will go through each index in the array, until we can't go to the next one (n+1 item). At this point, it returns a false error as that array value doesn't exist anymore. And neither is that. Each time mysql_fetch_array() is called with a result resource as its argument it returns an array representing a database row, it then moves the internal pointer forward one position. It will keep doing this until there are no more rows within the result set, where it will then return false. Placing a call to mysql_fetch_array within a while loop means it will fetch each row of the given result resource and then exit the loop once the rows are exhausted. Quote Link to comment https://forums.phpfreaks.com/topic/84763-dont-understand-while-loops/#findComment-432065 Share on other sites More sharing options...
Barand Posted January 6, 2008 Share Posted January 6, 2008 I thought that was what Ken2k said. Quote Link to comment https://forums.phpfreaks.com/topic/84763-dont-understand-while-loops/#findComment-432213 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 I thought that was what Ken2k said. Eh, people love to be technical and confuse newbies. Quote Link to comment https://forums.phpfreaks.com/topic/84763-dont-understand-while-loops/#findComment-432214 Share on other sites More sharing options...
redarrow Posted January 6, 2008 Share Posted January 6, 2008 but ken this is a hard word to read sorry m8 object in its parentheses Quote Link to comment https://forums.phpfreaks.com/topic/84763-dont-understand-while-loops/#findComment-432216 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 but ken this is a hard word to read sorry mate object in its parentheses Ya got me. But I'm not sure what else to use. Feel free to come up with something simpler. Quote Link to comment https://forums.phpfreaks.com/topic/84763-dont-understand-while-loops/#findComment-432220 Share on other sites More sharing options...
redarrow Posted January 6, 2008 Share Posted January 6, 2008 i did i read thorpe sorry Quote Link to comment https://forums.phpfreaks.com/topic/84763-dont-understand-while-loops/#findComment-432222 Share on other sites More sharing options...
trq Posted January 6, 2008 Share Posted January 6, 2008 I thought that was what Ken2k said. . I was replying to kratsg not Ken2k7 Quote Link to comment https://forums.phpfreaks.com/topic/84763-dont-understand-while-loops/#findComment-432224 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.