Php 在两个单独的表查询之间切换

Php 在两个单独的表查询之间切换,php,mysql,switch-statement,Php,Mysql,Switch Statement,我正在尝试将一个“新闻提要”合并到我的站点中,从两个不同的表中提取信息(使用两个独立的查询)。一个提要提取当前故事“$newsfeed”,另一个提要提取历史故事“$historyfeed”两个表需要保持分开。我正试图将这些查询以随机顺序放置在一组数字或行中。例如,我想列出5个故事,随机选择历史和新闻 顺序可能是:历史,新闻,历史,历史,历史 下一次访问可能会产生:新闻,新闻,新,历史,新闻 这部分似乎工作得很好 但是,我无法移动到查询中的下一行。因此,将生成五个完全相同的新闻故事,而不是移动到查

我正在尝试将一个“新闻提要”合并到我的站点中,从两个不同的表中提取信息(使用两个独立的查询)。一个提要提取当前故事“$newsfeed”,另一个提要提取历史故事“$historyfeed”两个表需要保持分开。我正试图将这些查询以随机顺序放置在一组数字或行中。例如,我想列出5个故事,随机选择历史和新闻

顺序可能是:历史,新闻,历史,历史,历史

下一次访问可能会产生:新闻,新闻,新,历史,新闻

这部分似乎工作得很好

但是,我无法移动到查询中的下一行。因此,将生成五个完全相同的新闻故事,而不是移动到查询的下一行。请参见下面的示例代码:

//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
//先前建立的数据库连接
$newsfeed=mysql\u query(“从新闻提要中选择*,其中的故事\u type='business'”)//新闻故事查询
$row\u newsfeed=mysql\u fetch\u assoc($newsfeed);
$historyfeed=mysql\u query(“从新闻提要中选择*,其中的故事\u type='business'”)//历史故事查询
$row\u historyfeed=mysql\u fetch\u assoc($historyfeed);
$storycount=0;
而($storycount<5){
$storytype=rand(1,2);//随机选择下一个故事类型
开关($storytype){
案例:“1”//如果下一个故事是新故事
storybubble($row_newsfeed);//用于回显新闻源内容的函数
$storycount++;
打破
案例:“2”//如果下一个故事是历史故事
storybubble($row_historyfeed);//用于回显historyfeed内容的函数
$storycount++;
打破
违约:
回声“错误”;
}//开关语句
}//语句时的故事计数

您在顶部创建变量,并将故事加载到
$historyfeed
$newsfeed
;但是您在循环中一直使用相同的变量。这应该会更好一些:

case: "1" //if next story is a new story
        $row_newsfeed = mysql_fetch_assoc($newsfeed);
        storybubble($row_newsfeed); //function to echo newsfeed content
        $storycount++;
break;
case: "2" //if next story is a history story
        $row_historyfeed = mysql_fetch_assoc($historyfeed);
        storybubble($row_historyfeed); //function to echo historyfeed content
        $storycount++;
它是在需要时填充变量,而不是在代码的开头。

mysql\u fetch\u assoc()返回一行结果

如果要返回下一行,需要再次调用mysql\u fetch\u assoc()

因此,您可以将对storybubble的调用更改为:

storybubble(mysql_fetch_assoc($row_newsfeed));
然而,您最好构建一个行数组,然后从中提取

$newsfeed = array();
$historyfeed = array();
while($row = mysql_fetch_assoc($newsfeed)) {
 $newsfeed[] = $row;
}

while($row = mysql_fetch_assoc($historyfeed)) {
 $historyfeed[] = $row;
}

...

switch($storytype) {
 case 1:
storybubble(array_shift($newsfeed));
break;
}

...

@RichStreets——我建议你看看芙蓉的答案;这是同样的逻辑,但我认为它比我的解决方案更优雅