Jump to content

[SOLVED] Getting data from a database inside a function using a while loop


Recommended Posts

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;
}

}

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;

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

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.

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>";
}

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.

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.

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.

 

 

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

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.