0

I want to show a message displaying the result of each iteration of a for loop in php.

I have tried several methods using jquery, ajax etc but they doesn't seem to fit my needs or either i am not getting to work around with them.

What my script does?

It scans a website, grab some links, then visits each link one by one and grab an image from each page and finally send it to my wordpress including its title and image.

What's the problem?

The problem is that as there can be several links (more than 200), so even though my scripts displays message regarding the success but it shows the messages after the whole loop is finished executing.

What I want to do?

Now, what i want to do that during each iteration the message should be shown on the browser rather than waiting for the loop to complete i.e. during 1st iteration (Posted Successfully) then during 2nd (Not Posted) and so on...

Here is the code:

<?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


?>
  • I'm afraid there is no easy answer to this question. You probably would need WebSockets or run this process inside an AJAX call instead. – jsalonen Jun 01 '14 at 13:39
  • Yeah, this is what i've been seeking since many days. The problem is that i don't have firm grip on Ajax methods. – Faizan Saleem Jun 01 '14 at 13:50
  • I would probably call this script via AJAX by using jQuery and then display some kind of progress information to user while it is running. Some ideas: http://stackoverflow.com/questions/19126994/what-is-the-cleanest-way-to-get-the-progress-of-jquery-ajax-request - if you need more specific information on progress then you could try something like Oboe.js to write the progress as JSON: http://oboejs.com/ - Haven't tried that myself though. – jsalonen Jun 01 '14 at 14:01

0 Answers0