简体   繁体   English

我想暂停一会儿循环,直到用户提交表单,那么我该怎么做?

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

I have a 25 advertiserids from CJ and now I want to create 2 post in different category of wordpress from each advertiserid given. 我有25位来自CJ的广告客户ID,现在我想根据给定的每个广告客户ID在不同的wordpress类别中创建2个帖子。 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 ? 如果使用while不可能,那么还有其他方法可以做到吗? in script $ad is array with advertiser's id value and $adcat is also a array with advertiser's catagory 脚本中的$ ad是具有广告客户ID值的数组,而$ adcat也是具有广告客户的类别的数组

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>

You can not literally "pause" a php script, as php is executed on the server before the page even loads. 您不能从字面上“暂停”一个php脚本,因为php甚至在页面加载之前就已经在服务器上执行了。

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. 要执行任何类型的“暂停”,您需要使用Javascript或其他客户端(浏览器执行)代码编写函数,或将类似Ajax请求的内容发送至php页面,然后在响应时更新当前页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM