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

为什么这不会退出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'; 
    }
    
    点赞 评论
  • dongwen3410 2013-05-18 14:02

    Use == for comparisons. What you are doing is assigning 0 to $exist, which will always fail the if statement.

    点赞 评论
  • dongzipu7517 2013-05-18 15:22

    So there's a number of things in your code that could be improved, see the code below as a suggestion:

    function getFlogArticles($url, $mail) {
        $list = getNewArticles();
        foreach($list as $article_id => $article) {
            wordpress($article['title'],$article['content'],$mail,$article['tags'],$url,$article_id);
        }
    }
    
    function getNewArticles($url) {
        global $db;
        $stmt = "SELECT article_id AS id,article_title AS title,article_content AS content,article_keyword AS tags FROM flog_articles";
        $result = $db->query($stmt);
        $articles = array();
        while($row = $result->fetch_array(MYSQLI_ASSOC)) {
            $articles[$row['id']] = $row;
        }
        if(empty($articles)) return array();
        $idlist = implode(',',array_keys($articles));
        //$url should be escaped as per your database type (eg, mysql_real_escape_string)
        $exists = array();
        $result = $db->query("SELECT article_id AS id FROM flog_posted WHERE http = '$url' AND article_id IN ($idlist)");
        while($row = $result->fetch_array(MYSQLI_ASSOC)) {
            $exists[$row['id']] = 1;
        }
        $articles = array_intersect_key($articles,$exists);
        return $articles;
    }
    

    There's a number of improvements, specifically though, you'll only be making 2 calls to your database to return all the rows you need, instead of 3 queries for each new valid article you wish to process.

    If your database is returning thousands of rows per run, then you might be better off doing it the way you were originally doing it to some extent, but the overhead of running hundreds of very small queries is, in my experience, more CPU-expensive than running one or two larger queries.

    (I can't test the code at the moment, so apologies if it doesn't work or do exactly what you need it to right out of the box)

    Hope it helps :)

    点赞 评论

相关推荐 更多相似问题