1

I have a working AJAX function within WordPress, There I'm looping through HTML to get my posts which then are accessible in my AJAX Success CB where they are appended to their div.

My question:

How would I format my PHP query func values (a while loop, an array and several separate pagination values) for proper use within JavaScript?

Here's what my function looks like as is;

<?php 
function _custom_paginate(){ 

    $paged = $_POST['count'];
    $args = array(
      'posts_per_page'  => 12,
      'category'        => 3,
      'paged'           => $paged, 
      'post_status'     => 'publish'
    ); 

    $query = new WP_Query($args);
    if ( $query->have_posts() ) :
    while ($query->have_posts()) : $query->the_post(); ?>

      <div class="large-3 three columns box">
        <div class="thumb">
          <?php the_content();?>
          <div class="thumb-foot">
            <div class="thumbinfo">
                <p>Posted on <?php the_time('m.d.Y') ?></p>
            </div>
            <div class="thumb-social">             
              <div class="thumb-twitter"></div>
              <div class="thumb-facebook"></div> 
            </div>
          </div>
        </div>
      </div>
    <?php 
    endwhile;
    endif;

   //Several pagination values
   $total_pages = $query->max_num_pages;
   if ($total_pages > 1){
   $current_page = max(1, get_query_var('paged'));
    ?>
    <div class="pagi-contain">
      <div id="pagi" class="large-12">

        <!-- Append pagination here #pagi-->

        <ul class="pagi">
          <li id="ajaxprev" style="float:left;">prev</li>
          <li id="pagenum"><?php echo $current_page; echo ' ... '.$total_pages; ?></li>
          <li id="total_pages"></li>
          <li id="ajaxnext" style="float:right;">next</li>
        </ul>

      </div>
    </div>
    <?php
}
exit;

}

You don't have to rewrite this but a lesson in formatting each value into an accessible array(?) for JS to work with each value separately. I found this person seems to have done what I am trying but the subject here is not PHP side...

Here is an example that is hopefully more clear on what I'm attempting to make work. http://jsfiddle.net/BenRacicot/LRmc3/

2
  • us1.php.net/json_encode Commented Jan 10, 2014 at 21:25
  • Yes I know the function in which to json_encode arrays. The question is 'how do I build up my query content html, and other values into one array?' Commented Jan 10, 2014 at 21:26

1 Answer 1

2

I'm not 100% clear what data you want to format, but just do this:

json_encode($variable_you_want_as_json);

To format the entire page's output, use this:

ob_start(); // Start output buffering (echos after this go into the buffer, not to the client) 

// all the code and output you want formatted
// example: echo "Some HTML I want to store in a JSON-encoded variable";

$html_data = ob_get_contents(); // Gets the contents of the buffer
ob_end_clean(); // empties the buffer
echo json_encode($html_data); // encodes and echoes whatever you did between ob_start() and ob_get_contents()

It sounds like you want multiple values, so you can just replace the variable in the last line with an array, like this:

$my_array = array('html_data'=>$html_data,
    'max_num_pages' => $query->max_num_pages,
    'foo'=>$foo,
    'bar'=>$bar,
    'some_other_var' => $some_other_var);
echo json_encode($my_array);
Sign up to request clarification or add additional context in comments.

6 Comments

That isn't my question
@BenRacicot Your question isn't clear. How do you want to store the data? "How do I build up my query content html, and other values into one array?" is not a clear question. What values?
@BenRacicot Your question is very unclear, IE the 'I'm not 100% clear what data you want to format' line. However, you can feed any values into json_encode, like an array. ie json_encode(array( 'html' => $html, 'result' => $result)); and so forth
The values that come back from my loop and also the pagination values such as max_num_pages and current_page.
Awesome Ed, much closer. ONly how to I format that HTML? Aren't there going to like 30 loops through that, and should each line be put into $html_data ?
|

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.