BandonRandon Posted October 13, 2009 Share Posted October 13, 2009 Ok so this is a beginner stupid question but I'm new to functions. This is my first time to trying to get data from a database into a function. For some reason it's only getting the first result out of the database instead of looping though until the limit is reached. Any Ideas? function get_news_list($limit, $type, $author){ //get the limit of news if(empty($limit)){ $count_rows = mysql_query("SELECT * FROM `news_data`") or die(mysql_error()); $limit = mysql_num_rows($count_rows); } //see if we should show news from all authors or just one if(!empty($author)){ $get_news_query = mysql_query("SELECT * FROM `news_data` WHERE `valid` = '1' AND author_id = '$author' ORDER BY `id` DESC LIMIT $limit") or die(mysql_error()); } else{ $get_news_query = mysql_query("SELECT * FROM `news_data` WHERE `valid` = '1' ORDER BY `id` DESC LIMIT $limit") or die(mysql_error()); } //loop though the news and get the news until the limit is reached while($news_row = mysql_fetch_array($get_news_query)) { $news = ($news_row['text']); return $news; } } Quote Link to comment https://forums.phpfreaks.com/topic/177504-solved-getting-data-from-a-database-inside-a-function-using-a-while-loop/ Share on other sites More sharing options...
trq Posted October 13, 2009 Share Posted October 13, 2009 return exits the function, you call return the first time through your while loop. You will likely wont something like.... $news = array(); while($news_row = mysql_fetch_array($get_news_query)) { $news[] = $news_row['text']; } return $news; Quote Link to comment https://forums.phpfreaks.com/topic/177504-solved-getting-data-from-a-database-inside-a-function-using-a-while-loop/#findComment-935896 Share on other sites More sharing options...
BandonRandon Posted October 13, 2009 Author Share Posted October 13, 2009 I could easily get it to work using "echo" but word on the street is this is bad form... is this true because while($news_row = mysql_fetch_array($get_news_query)) { $news = $news_row['text']; echo $news; } works well Quote Link to comment https://forums.phpfreaks.com/topic/177504-solved-getting-data-from-a-database-inside-a-function-using-a-while-loop/#findComment-935904 Share on other sites More sharing options...
trq Posted October 13, 2009 Share Posted October 13, 2009 Using echo within the function makes your function less re-usable, because now your locked in to whatever formatting the function spits out. Quote Link to comment https://forums.phpfreaks.com/topic/177504-solved-getting-data-from-a-database-inside-a-function-using-a-while-loop/#findComment-935905 Share on other sites More sharing options...
BandonRandon Posted October 13, 2009 Author Share Posted October 13, 2009 That makes sense, I'm actually trying to write a function that will format my news from the database and spit it out onto the page. What's the "proper" way to format let's say a news div using a function. I thought return was the equivalent but i just learned it's not. Quote Link to comment https://forums.phpfreaks.com/topic/177504-solved-getting-data-from-a-database-inside-a-function-using-a-while-loop/#findComment-935906 Share on other sites More sharing options...
trq Posted October 13, 2009 Share Posted October 13, 2009 You would simply have the function return an array of news records then loop through that. eg; $newsitems = getNews(); foreach ($newsitems as $news) { echo "<div class='newsitem'>"; echo " <div class='title'>{$news['title']}</div>"; echo " <div class='content'>{$news['content']}</div>"; echo "</div>"; } Quote Link to comment https://forums.phpfreaks.com/topic/177504-solved-getting-data-from-a-database-inside-a-function-using-a-while-loop/#findComment-935907 Share on other sites More sharing options...
BandonRandon Posted October 13, 2009 Author Share Posted October 13, 2009 Thanks, sorry I'm a slow learner. So i still want to use a while loop inside the function to add all the data to an array? Functions and arrays are both things I've stayed away from until now (three years down the road) when I'm finding out how useful they are. Quote Link to comment https://forums.phpfreaks.com/topic/177504-solved-getting-data-from-a-database-inside-a-function-using-a-while-loop/#findComment-935912 Share on other sites More sharing options...
trq Posted October 13, 2009 Share Posted October 13, 2009 So i still want to use a while loop inside the function to add all the data to an array? Yes you will still need the other loop within the function to actually create the array. This is why I wouldn't really use functions as specific as the one you have there, it doesn't really do anything but add extra overhead. Not that loops are huge, but its something to think about. Quote Link to comment https://forums.phpfreaks.com/topic/177504-solved-getting-data-from-a-database-inside-a-function-using-a-while-loop/#findComment-935914 Share on other sites More sharing options...
BandonRandon Posted October 13, 2009 Author Share Posted October 13, 2009 Thanks, My code was more of just an example, I really want to be able to get all the news data (not just the text) into an array. I also will need to do stuff like grab the author id from another table then use that as a display name. In the past I've just echoed out the information onto the page using echo right into the file i wanted the news. I just thought it might look better and even work better to use a function. Quote Link to comment https://forums.phpfreaks.com/topic/177504-solved-getting-data-from-a-database-inside-a-function-using-a-while-loop/#findComment-935915 Share on other sites More sharing options...
Zane Posted October 13, 2009 Share Posted October 13, 2009 I really want to be able to get all the news data (not just the text) into an array. while($news_row = mysql_fetch_array($get_news_query)) $news[] = $news_row; return $news; then do as thorpe told you Quote Link to comment https://forums.phpfreaks.com/topic/177504-solved-getting-data-from-a-database-inside-a-function-using-a-while-loop/#findComment-935958 Share on other sites More sharing options...
BandonRandon Posted October 14, 2009 Author Share Posted October 14, 2009 Just to clarify, it's the current standard to do it with the two loops instead of just writing a function that will echo out the data into a function? Quote Link to comment https://forums.phpfreaks.com/topic/177504-solved-getting-data-from-a-database-inside-a-function-using-a-while-loop/#findComment-936497 Share on other sites More sharing options...
trq Posted October 14, 2009 Share Posted October 14, 2009 Just to clarify, it's the current standard to do it with the two loops instead of just writing a function that will echo out the data into a function? There is no standard, it up to you. Functions however (IMO) are meant to be reusable. Quote Link to comment https://forums.phpfreaks.com/topic/177504-solved-getting-data-from-a-database-inside-a-function-using-a-while-loop/#findComment-936513 Share on other sites More sharing options...
BandonRandon Posted October 14, 2009 Author Share Posted October 14, 2009 thanks for all the input and time helping me learn. I think even if I use echo i'll still be able to reuse the function with the same formatting. I guess what I'm trying to do is write a function that will echo out the news wherever the function is called. So I guess I don't see the point of using the second loop. Thank you very much though. I've learned a lot from this thread and will mark it as resolved so hopefully others will find it useful. Quote Link to comment https://forums.phpfreaks.com/topic/177504-solved-getting-data-from-a-database-inside-a-function-using-a-while-loop/#findComment-936516 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.