Jump to content

While Loop


wiggst3r

Recommended Posts

Hi

 

I'm trying to write a news archive system, which, on the news page, displays one news article and links to the others in the db.

 

When I click on the link to one of them, I want it to display in another page, and just display that particular article.

 

I'm trying to write a while loop, which loop through the articles, and displays the individual article, based on it's id, but i'm a little stuck.

 

I've written a fucntion to display the news article

 

	function get_one_article($id)
	{
    $id = mysql_real_escape_string($id);
	    $query = "SELECT * FROM news WHERE id = '$id' LIMIT 1";
	    $result = @mysql_query ($query); // Run the query.
	    $num = mysql_num_rows($result);
	    $one_row = mysql_fetch_array($result, MYSQL_ASSOC);
	    return $one_row;
	}

 

I want to put the following code into a while loop and get the news story for it's id

 

echo $one_row = get_one_article($_GET['id']);

 

When i var_dump that code, I can see the articles details, just can't seem to loop through and print it to the screen

 

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/112614-while-loop/
Share on other sites

But if you're getting one news article, why would you need to loop? For that matter, what could possibly be looped through? If you're only grabbing one row, you do the mysql_fetch_array and then you're done. Right? What more is there to do?

 

 

fake edit: oh I think I see what you're doing. You're going about it the wrong way though. your while loop needs to be like this

$query = "SELECT * FROM News WHERE ***insert range of id's desired, such as id > '1' AND id < '10' or something *** ";
$result = mysql_query($query);
while($row = mysql_fetch_array))
{
  echo "$row[article_name]<br>\n
          $row[article_description]<br>\n
          ";
// and so on... variables made up bc I dont know what yours are called

}

 

oh, and if you're only trying to get one id(instead of a range of id's) just do the query like you have it, but then use the code I provided. The way I have it setup, it will automatically step through all rows selected, whether it's just one or 10 or however many

Link to comment
https://forums.phpfreaks.com/topic/112614-while-loop/#findComment-578928
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.