0

I am performing the below code within wordpress. To give a brief outline of what I am trying to achieve: I query the database to return a list of exercises, values (reps and sets for exercises) are assigned to them and stored in $_SESSION in the following array structure.

Array
(
[0] => Array
    (
        [ExerciseID] => 75
        [Sets] => 
        [Reps] => 
    )

[1] => Array
    (
        [ExerciseID] => 690
        [Sets] => 3
        [Reps] => 3
    )

)

The code: I return the exercise ID's into a array so I can then run a WP_Query using them. When the results are returned I need them to display the reps and sets (the variables that are in the array) with the exercise.

<?php
$ids = array();
if(empty($_SESSION['collection']))
{echo"<p>There's nothing in your collection, an add some by searching above</p>";}
else
{
foreach($_SESSION['collection'] as $el)
{
 $ids[] = $el['ExerciseID'];
}
}
// START OF THE QUERY USING THE EXERCISE ID'S FOR DISPLAYING IN THE COLLECTION
$the_query = new WP_Query(array('post__in' => $ids, 'post_type' => 'exercise',   'posts_per_page' =>'100')); ?>


<?php if ( $the_query->have_posts() ) : ?>

<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>


<div class="thumbnail">

<div class="caption" id="<?php the_ID(); ?>">

  <h5>
    <?php the_title(); ?>
  </h5>
  <?php the_post_thumbnail('collection_thumb'); ?>
  </div>
 <br />
  <form action="" method="post">
  <button class="btn btn-primary btn-m" type="submit" name="delete" value="Delete">Delete</button>
</form>
</div>

I am struggling matching the reps and sets to be displayed along side the exercise that has been queried.

Apologies for the poor explanation, by clicking on a exercise on this website and adding it to the collection hopefully you can see what I am trying to achieve.

7
  • 1
    If you can, I would set the array index to the exercise ID. This will allow you to easily call $_SESSION['collection'][$exerciseID]['sets'] and $_SESSION['collection'][$exerciseID]['reps'] respectively. Otherwise you will have to iterate over the collection array to find the proper ID. Commented Apr 8, 2014 at 20:11
  • So this is my array creation code ` if (isset($_POST['exerciseExpand'])) { $_SESSION['collection'][] = array('ExerciseID' => $_POST['ExerciseID'],'Sets' => $_POST['Sets'],'Reps' => $_POST['Reps']); } }` how can I give this the ExerciseID as the index? Commented Apr 8, 2014 at 20:15
  • if ( isset( $_POST['exerciseExpand'] ) ) { $_SESSION['collection'][$_POST['ExerciseID']] = array( 'ExerciseID' => $_POST['ExerciseID'], 'Sets' => $_POST['Sets'], 'Reps' => $_POST['Reps'] ); } - Note: All I did was define the index instead of leaving it as [] Commented Apr 8, 2014 at 20:26
  • Awesome so now my array structure is Array ( [75] => Array ( [ExerciseID] => 75 [Sets] => [Reps] => ) ) how can I pull the reps and sets that correspond with the ExerciseID into the wordpress loop? Really appreciate your help saved me hours. Commented Apr 8, 2014 at 20:44
  • 1
    No worries, this is in my first response. $_SESSION['collection'][$exerciseID]['sets'] you would have to define $exerciseID with the appropriate exerciseID. I would assume this is the post id for excersise post type. You should be able to use get_the_ID() Commented Apr 8, 2014 at 20:55

1 Answer 1

1

Set the array index to the ExerciseID like so:

if ( isset( $_POST['exerciseExpand'] ) ) {
    $_SESSION['collection'][$_POST['ExerciseID']] = array( 
        'ExerciseID' => $_POST['ExerciseID'],
        'Sets' => $_POST['Sets'],
        'Reps' => $_POST['Reps'] 
    );
}

This will allow you to use the data as: $_SESSION['collection'][$exerciseID]['sets'] and $_SESSION['collection'][$exerciseID]['reps'] respectively.

Sign up to request clarification or add additional context in comments.

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.