[SOLVED] Getting various entries and display the randomly


#1

Hi everyone,
I have a kind of news bit on my website, the news is kept in the sql database with various rows… headline, story, url etc. I want to show 4 stories from the database randomly. I can do this if i do a simple picture with text next to it and set it to count 4. But what im trying to do is have a main story, with Pic on the left story on the right, then under that the other 3 stories, pic on top with headline under it.

Where ive got to is that everything works except all 4 places are showig the same pic/story and not the 4 different ones that I would like it to.

For an example of what im trying to do see http://www.wanadoo.co.uk/ the section “on wanadoo today”

Any help would be great, and please be gentle with me :o)

I have included the code below.

Thanks

Lee

[code]

<?php //connect to your database ** EDIT REQUIRED HERE ** mysql_connect("XXX","XXX","XXX"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db(“XXX”) or die(“Unable to select database”); //select which database we’re using

// Build SQL Query
$query = “SELECT * FROM gznews ORDER by rand() LIMIT 0, 4;”;

$numresults=mysql_query($query);
$result = mysql_query($query) or die(“X Couldn’t execute query”);

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$headline = $row[“headline”];
$story = $row[“story”];
$url = $row[“url”];
$pic = $row[“pic”];
}?>

On the scene
" width="118" height="100" alt="">

<?php echo"$headline" ?>
<?php echo"$story" ?>

" target="_blank">

" width="100" height="84" alt="">
<?php echo"$headline" ?>

" target="_blank">

" width="100" height="84" alt="">

<?php echo"$headline" ?>

" target="_blank">

" width="100" height="84" alt="">

<?php echo"$headline" ?>

" target="_blank">

[/code]

#2

I think the problem is in that when you get the data from the query your assignment of local variables is not correct.

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$headline = $row["headline"];
$story = $row["story"];
$url = $row["url"];
$pic = $row["pic"];
}

You ultimately only get whatever the last story was (from the query). If you consider that you get 4 stories (From the query LIMIT 0, 4) Then you loop through the data set with the WHILE loop (above)… You take the first set of data and put it into local variables $headlin, $story, $url, and $pic… then you loop again to get the second set of data but have done NOTHING with the first set. So you effectively OVERWRITE the first set with the second, then the third and finally with the last.

You could reconsider using another array to store your data more effectively.

// now you can display the results returned
$counter = 0;  // Initialize a counter for an array
while ($row= mysql_fetch_array($result)) {
  // Assign data appropriately into an array for later use
  $headline[$counter] = $row["headline"]; 
  $story[$counter] = $row["story"];
  $url[$counter] = $row["url"];
  $pic[$counter] = $row["pic"];

  $counter++;  //Increment counter for next loop
}

Then when you need to call the data, use $headline[0] for story 1, $headline[1] for story 2, etc…


#3

Hiya, the second option has worked… many thanks for helping.

Cheers
Lee