I want to pause a while loop until user submit a form, so how can I do that?

1.5k views Asked by At

I have a 25 advertiserids from CJ and now I want to create 2 post in different category of wordpress from each advertiserid given. So I have created following script but it is not pausing for user input, so how can I do that ? If its not possible with while then is there any other method to do this ? in script $ad is array with advertiser's id value and $adcat is also a array with advertiser's catagory

function cjlinks($n)
{
    global $ad, $adcat;


        $URI = 'https://product-search.api.cj.com/v2/product-search?website-id=12345678'.
            '&advertiser-ids='.$ad[$n].
            '&records-per-page=2';

    $context = stream_context_create(
    array(
    'http' => array(
        'method' => 'GET',
        'header' => 'Authorization: ' .
            'my api id'

        )
    ));

    $res = new SimpleXMLElement(file_get_contents($URI, false, $context));
    return $res;
}   
$a = 0;
while ($a < 25)
{

    echo 'advertiser id is:  '.$ad[$a].'<br/>advertiser - catagory is:  '.$adcat[$a]->child.
         '<br/>';

    if (isset($_SESSION['sumit'])){


    $data = cjlinks($a);
    $attributes = $data->products->attributes();

        if ($attributes->{'total-matched'} == 0){
            echo 'No products found ...try again with new keyword.';
            }else{
                foreach ($data->products[0] as $product) 
                {
                // Sanitize data.
                $price = number_format((float)$product->price, 2, '.', ' ');
                $image = '<a href="'.$product->{'buy-url'}.'"><img src="'.$product->{'image-url'}                            .'" style="float: right"/></a>';
                $pd =  $image.$product->description .'<a href="'.$product->{'buy-url'}.
                        '">...For more details and to buy it click here</a>';
                $p = array('post_title'    => $product->name,
                    'post_content'  => $pd,
                    'post_status'   => 'publish',
                    'post_author'   => 1,
                    'post_category'  =>array($_GET['cat']));
                $pr = wp_insert_post( $p, $wp_error );
                echo '<br/>posted...post ID is:'.$pr;
                wp_reset_query();  // Restore global post data stomped by the_post().

                }
            }   

        }else{
        echo 'please complete form';
        $a = $a+1;
    }
 }




?>
<html>
<body>

<form action="catag.php" method="get">
<table>




<tr>

         <td><label> Select a catagory from list:</label></td></tr>
         <tr>
            <?php

            foreach($cat as $key=>$val){



                        echo '<tr><td><input type="radio" value="'.$val->cat_ID.'" name="cat" id="'.$val->cat_ID.'">'.$val->cat_name.'</td></tr>';
                  }     

                ?>

              </tr>


</table>
<input type="submit" name="submit" value="submit">
</form><br>


</body>
</html>
1

There are 1 answers

7
adamdehaven On BEST ANSWER

You can not literally "pause" a php script, as php is executed on the server before the page even loads.

To execute any kind of a "pause" you would need to write your function in Javascript or other Client Side (Browser Executed) code, or send something like an Ajax request to a php page that would then update the current page on response.