for循环的每次迭代期间显示输出

时间:2014-06-01 13:34:39

标签: javascript php jquery wordpress

我想在php中显示一条消息,显示for循环的每次迭代的结果。

我已经尝试了几种使用jquery,ajax等的方法,但它们似乎不符合我的需要,或者我不能解决它们。

我的脚本做了什么?

它扫描网站,抓取一些链接,然后逐个访问每个链接并从每个页面抓取图像,最后将其发送到我的wordpress包括其标题和图像。

有什么问题?

问题在于,因为可能有多个链接(超过200个),所以即使我的脚本显示有关成功的消息,但它会在整个循环执行完毕后显示消息。

我想做什么?

现在,我想在每次迭代期间做到这一点,消息应该显示在浏览器上,而不是等待循环完成,即在第一次迭代(发布成功),然后在第二次(未发布)等等。

以下是代码:

<?php

$category = array();

for ($i = 0; $i < count($result); $i++)
{
  set_time_limit(30);

  // Check if post already exists
  global $wpdb;
  $query = $wpdb->prepare('SELECT ID FROM ' . $wpdb->posts .
     ' WHERE post_name = %s',sanitize_title_with_dashes($result[$i]->title));
  $cID = $wpdb->get_var($query);

  if (!empty($cID))
  {
     echo $result[$i]->title .
         " <font style='color:#FF0000; font-weight:bold;'>Already Exists.</font><br />";
  } else
  {
     $inner_html = file_get_html($result[$i]->href);

     $inner_result_image = $inner_html->find('meta[property=og:image]',1);
     $inner_result_cats = $inner_html->find('a[rel=category tag]');

     $title = $result[$i]->title;
     $body = "<a href='$inner_result_image->content'><img src='$inner_result_image->content' /></a>";

     // Automatically Create new Category if it doesn't exist.

     foreach ($inner_result_cats as $cats)
     {
         if (!($cats->innertext == "Jobs"))
         {

             array_push($category,$cats->innertext);

             $count = 0;

             // For Cities
             if (search_city($cats->innertext))
             {
                 wp_insert_term($cats->innertext,'category',array(
                     'description' => '',
                     'slug' => '',
                     'parent' => '14')); // Jobs by City
             } else
             {
                 $count += 1;
             }


             // For Qualification
             if (search_qualification($cats->innertext))
             {
                 wp_insert_term($cats->innertext,'category',array(
                     'description' => '',
                     'slug' => '',
                     'parent' => '51')); // Jobs by Qualification
             } else
             {
                 $count += 1;
             }


             // International Jobs
             if (check_international(parse_url($cats->href,PHP_URL_PATH)))
             {
                 wp_insert_term($cats->innertext,'category',array(
                     'description' => '',
                     'slug' => '',
                     'parent' => '52')); // International Jobs
             } else
             {
                 $count += 1;
             }

             if ($count == 3)
             {
                 wp_insert_term($cats->innertext,'category',array('description' => '','slug' =>
                         ''));

             }


         } // End if NewsPaper Check


     } // End if Auto Category

     echo "<br />";


     $postdate = new IXR_Date(strtotime($date));


     $title = htmlentities($title,ENT_NOQUOTES,"UTF-8");

     $content = array(
         'title' => $title,
         'description' => $body,
         'mt_allow_comments' => 1, // 1 to allow comments
         'mt_allow_pings' => 1, // 1 to allow trackbacks
         'post_type' => 'post',
         'date_created_gmt' => $postdate,
         'categories' => $category,
         );

     // Create the client object
     $client = new IXR_Client('http://localhost/FDJ/xmlrpc.php');

     $username = "Admin";
     $password = "Password";
     $params = array(0,
         $username,
         $password,
         $content,
         true); // Last parameter is 'true' which means post immediately, to save as draft set it as 'false'

     // Run a query for PHP
     if (!$client->query('metaWeblog.newPost',$params))
     {
         die('Something went wrong - ' . $client->getErrorCode() . ' : ' . $client->
             getErrorMessage());
         echo $title . " <font style='color:#FF0000; font-weight:bold;'>Not Posted.</font><br />";
     } else
         echo $title . " <font style='color:#00FF00; font-weight:bold;'>Posted Successfully.</font><br />";
  }

} // End if Main


?>

0 个答案:

没有答案