0

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?

5
  • PHP doesn't keep state and you should rather handle this with JavaScript if it's a presentation thing Commented Feb 18, 2016 at 9:26
  • You would need to send the start post index as a parameter with your ajax script. Commented Feb 18, 2016 at 9:26
  • why don't you send the last loop's last div class in the request parameter. Commented Feb 18, 2016 at 9:30
  • There are already some good answers here, so I just want to recommend you use data- attributes for the numbers instead of classes. Use classes only for stylistic or script-related issues. You might also count the items using JS instead of tagging them. Commented Feb 18, 2016 at 9:43
  • In the end i decide changing the core of my code so, thanks anyway, hope it help's some people... Commented Feb 18, 2016 at 14:31

3 Answers 3

1

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.
Sign up to request clarification or add additional context in comments.

Comments

1

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
});

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.