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
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
Share on other sites

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.