dougou8573 2013-05-26 17:38
浏览 30

如何在每个循环的php中使用多个表调用

I'd like to display results from two tables in my MYSQL database using the for each loop. Currently I have split both tables into two separate loops like this:

<?php
    $i = 0;
     foreach (array_reverse ($articles) as $article){?>
            <div class="greyvertical midtopmargin item leftpadding rightpadding">
                <a href="article.php?id=<?php echo $article['article_id']; ?>"><img src="../lifestyle/photos/articles/<?php echo $article['photo_folder']; ?>/<?php echo $article['photo_1']; ?>" alt="item">
                <h5 class="whitetext text2 extrabold smalltoppadding leftpadding"><?php echo $article['article_title']; ?></h5></a>
                <p class="meta whitetext leftpadding smalltoppadding">
                    <?php echo $article['article_summary']; ?></p>
                <a href="article.php?id=<?php echo $article['article_id']; ?>" class="whitetext text2 mediumfont leftpadding midbottommargin">READ ME</a>
            </div>
        <?php if (++$i == 5) break;
} ?>

    <?php
    $i = 0;
     foreach (array_reverse ($posts) as $post){?>
            <div class="greyvertical midtopmargin item leftpadding rightpadding">
                <a href="post.php?id=<?php echo $post['post_id']; ?>"><img src="../lifestyle/photos/blog/<?php echo $post['photo_folder']; ?>/<?php echo $post['photo_bg']; ?>" alt="item">
                <h5 class="whitetext extrabold text2 leftpadding smalltoppadding"><?php echo $post['post_title']; ?></h5></a>
                <p class="meta leftpadding whitetext smalltoppadding">
                    <?php echo $post['post_summary']; ?></p>
                <a href="post.php?id=<?php echo $post['post_id']; ?>" class="whitetext text2 mediumfont leftpadding midbottommargin">READ ME</a>
            </div>
            <?php if (++$i == 5) break;
            } ?>

As you can see they are almost identical with slight differences but I am completely stuck on how to combine the two without them being separate as it is now. Could anyone let me know in layman's terms how to combine the two loops into one? I.e I want to combine the articles and posts tables so they will be displayed as one rather than separately. thanks in advance guys.

  • 写回答

1条回答 默认 最新

  • douhuan2101 2013-05-26 17:55
    关注

    The comments made on your question make sense and you should take them into account, but there is a way of iterating over two arrays at once by using PHP's MultipleIterator. Here is a much simplified example of how this could work for you:-

    $articles = array('article 1','article 2','article 3');
    $posts = array('post 1', 'post 2', 'post 3');
    
    $iterator1 = new ArrayIterator($articles);
    $iterator2 = new ArrayIterator($posts);
    
    $multiIterator = new MultipleIterator();
    $multiIterator->attachIterator($iterator1);
    $multiIterator->attachIterator($iterator2);
    
    foreach($multiIterator as $combinedArray){
        echo $combinedArray[0] . "<br/>
    ";
        echo $combinedArray[1] . "<br/>
    ";
    }
    

    Output:-

    article 1
    post 1
    article 2
    post 2
    article 3
    post 3
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)