I’m trying incorporate a “News Feed” into my site, pulling information from two different tables (Using two seperate queries). One feed pulls current stories “$newsfeed” and the other pulls history stories “$historyfeed”. Both tables need to remain seperate. I’m attempting to place these queries in a random order for a set number or rows. For example, I want 5 stories listed, with history and news choosen randomly.
The order might be: History, News, History, History, History
the next visit might produce: News, News, New, History, News
This part seems to work fine…
However, I’ve been unable to move to the next row in the query. So five of the exact same news stories are produced, instead of moving to the next row of the query. See the example code below:
//DB connection established earlier
$newsfeed = mysql_query("SELECT * FROM newsfeed WHERE story_type='business'"); //News Story Query
$row_newsfeed = mysql_fetch_assoc($newsfeed);
$historyfeed = mysql_query("SELECT * FROM newsfeed WHERE story_type='business'"); //History Story Query
$row_historyfeed = mysql_fetch_assoc($historyfeed);
$storycount = 0;
while ($storycount < 5) {
$storytype = rand(1,2); //randomly select next story type
switch ($storytype){
case: "1" //if next story is a new story
storybubble($row_newsfeed); //function to echo newsfeed content
$storycount++;
break;
case: "2" //if next story is a history story
storybubble($row_historyfeed); //function to echo historyfeed content
$storycount++;
break;
default:
echo "Error";
} //switch statement
} //story count while statement
You create the variables at the top, and load stories into
$historyfeedand$newsfeed; but you keep using the same variables in your loop. This should work a little better:It’s populating the variables when they’re needed, instead of at the start of the code.