php通过ajax加载增加数量

I have a php loop with some php increasing numbers:

<loop start>
    <?php $num=0; $onum=0; ?>
    <div class="<?php echo ++num; ?>"></div>
    <div class="<?php echo ++onum; ?>"></div>
</loop end>

Now this loop is running to get posts on the page, but the posts on the other page is loaded to that same page with ajax and for that the number values are reset.

So instead of getting this:

<loop start>
    <div class="1"></div>
    <div class="2"></div>
</loop end>
<loop start>
    <div class="3"></div>
    <div class="4"></div>
</loop end>
<!-- ajax load -->
<loop start>
    <div class="5"></div>
    <div class="6"></div>
</loop end>

I'm getting this:

<loop start>
    <div class="1"></div>
    <div class="2"></div>
</loop end>
<loop start>
    <div class="3"></div>
    <div class="4"></div>
</loop end>
<!-- ajax load -->
<loop start>
    <div class="1"></div>
    <div class="2"></div>
</loop end>

Any ideas?

When you make your ajax call your PHP script has no idea what the current value is. So you need to send it with each (ajax) request.

http://example.com/your-ajax-script.php?num=3&onum=4

Then on the server side you'd get the values like so :

$num = isset($_GET['num']) && !empty($_GET['num']) ? (int)$_GET['num'] : 0;
$onum = isset($_GET['onum']) && !empty($_GET['onum']) ? (int)$_GET['onum'] : 0;

// the reste of your code.

I suggest you to send last loop's last div's class in the ajax parameter, so it will require a change at the php end too like:

<loop start>
    <?php 
       $num=isset($_REQUEST['num']) ? $_REQUEST['num'] : 0; 
       $onum=isset($_REQUEST['num']) ? $_REQUEST['num'] : 0;
    ?>
    <div class="<?php echo ++num; ?>"></div>
    <div class="<?php echo ++onum; ?>"></div>
</loop end>

and you can use ajax like this:

$.post('url', {
      num:parseInt($('loop:last').find('div:last').attr('class'), 10)
   }, function (data){
      // success callback
});

You have to declare var outside of loop

<?php $num=0; $onum=0; ?>

<loop start>    
    <div class="<?php echo ++num; ?>"></div>
    <div class="<?php echo ++onum; ?>"></div>
</loop end>