doukenqiong0588 2014-04-08 20:07
浏览 36
已采纳

返回相应的数组值,以便在wordpress循环中使用

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.

  • 写回答

1条回答 默认 最新

  • doubairan4213 2014-04-09 00:02
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度