dongnai3960 2013-05-18 13:59
浏览 78
已采纳

为什么这不会退出while循环? [关闭]

I'm trying to exit outside this while look, so my script can post to Wordpress.com blogs. However, even when I try to use break; inside the if statement, the loop continues.

This function starts up the script and basically handles the posting:

function getFlogArticle($url, $mail) {  
list($id, $title, $content, $tags) = getNewArticle();
while ($id != 0)
{
    $start = getTime(); 
    doesArticleExist($url, $id);
        if ($exist = 0)
            {
                wordpress($title, $content, $mail, $tags, $url, $id);
                break;  
                $end = getTime(); 
                echo  '<strong>Exist While</strong>: '.round($end - $start,4).' seconds<br />'; 
            }
    list($id, $title, $content, $tags) = getNewArticle();   
    echo 'I cant stop'; 
}
}

This function grabs the article from the database every time doesARticleExist() returns 1:

function getNewArticle() {
$start = getTime(); 
global $db;
$count = $db->query("SELECT * FROM flog_articles");
$count = $count->num_rows;
$offset = mt_rand(0, $count - 1);
$stmt = "SELECT * FROM flog_articles LIMIT 1 OFFSET $offset";
$result = $db->query($stmt);
$post = $result->fetch_array(MYSQLI_ASSOC);
return array($post['article_id'], $post['article_title'], $post['article_content'], $post['article_keyword']);
$end = getTime(); 
echo  '<strong>getNewArticle()</strong>: '.round($end - $start,4).' seconds<br />';
}

And this script checks to see if the article exists in the database. If it doesn't, it returns a 0. If it does, it returns a 1.

function doesArticleExist($url, $id) {
$start = getTime(); 
global $db; 
$count = $db->query("SELECT * FROM flog_posted WHERE http = $url AND article_id = $id");
$count = $count->num_rows;

if ($count > 0) {
    $exist = 1;
    return $exist;
} else{
    $exist = 0;
    return $exist;
}
$end = getTime();
echo  '<strong>doesArticleExist()</strong>: '.round($end - $start,4).' seconds<br />';
}

Basically, the script gets an article from the database. After it gets the article, it checks to see if that article/url combination exists in another table of the same database. If it does not exist, i want it to post to the wordpress blog, and then break out of the loop, so it won't post again.

The only problem is that it does not even exit the loop. Is it because the exist values are not being passed?

  • 写回答

3条回答 默认 最新

  • doxp30826 2013-05-18 14:04
    关注

    Don't use break. Use this

    $willStop=false;
    while (($id != 0)&&(!$willStop))
    {
        $start = getTime(); 
        doesArticleExist($url, $id);
        if ($exist == 0)
            {
                wordpress($title, $content, $mail, $tags, $url, $id);
                $willStop=true;  
                $end = getTime(); 
                echo  '<strong>Exist While</strong>: '.round($end - $start,4).' seconds<br />'; 
            }
        list($id, $title, $content, $tags) = getNewArticle();   
        echo 'I cant stop'; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog