douxingsuo8809 2014-12-30 01:44
浏览 61
已采纳

PHP显示来自2个SQLite数组的foreach()结果,其中一个是随机的

I have 2 SQLite- tables, one containing articles and one containing image URL's. What I'd like to do is to do a foreach() to show the articles in order and show a set number of random images next to the article.

The images should be selected by checking similarity between the Article's and image's categories (column in the table) and then randomized. (The article catergory could be for example 'motorboats' and the img category 'boat').

How do I show the results from two different arrays? The code for the articles is:

    $stmt = $db->prepare('SELECT * FROM Article WHERE category = "article" ORDER BY pubdate DESC;');
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<div id="artikelLista">

  <?php foreach($res as $article): ?>
  <div class="artikelContent">
 <?php echo $article['title']; ?><br>
<?php echo $article['content'];?>
    <div class="floatRight clear"><?php echo "Artikel skriven " . $article['author'] . " " . $article['pubdate']; ?></div>
  </div>
  <?php endforeach; ?>

To add the second array, I tried this, which didn't work, it only showed the results from the first array multiple times:

$stmt = $db->prepare('SELECT * FROM Article WHERE category = "article" ORDER BY pubdate DESC;');
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt2 = $db->prepare('SELECT * FROM Object;');
$stmt2->execute();
$res2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
?>

<div id="artikelLista">

  <?php foreach($res as $article): ?>
<?php foreach($res2 as $object): ?>

  <div class="artikelContent">
 <?php echo $article['title']; ?><br>
<?php echo $article['content'];?><br>
<?php echo $object['img'];?> <!-- For testing. The images should be filtered and a a few random images would be shown here -->
    <div class="floatRight clear"><?php echo "Artikel skriven " . $article['author'] . " " . $article['pubdate']; ?></div>
  </div>
  <?php endforeach; ?>
  <?php endforeach; ?>

I guess it's not possible to use nested foreach() like this. How will I manage this?

Also, If anyone knows on top of their head how to match the similarities between the arrays as described above, I'd be greatful. If not, I'll deal with this later.

  • 写回答

1条回答 默认 最新

  • dqt20140129 2014-12-30 03:23
    关注

    You can fall a function inside the first foreach a pass category of the article to that function. Now in that functino collect all images in an array related to the passed category and then return it by randomizing it. Code below ..

    <?php foreach($res as $article): ?>
        <?php
    
        $img = get_img($article['category']);
    
        ?>
    
        <div class="artikelContent">
            <?php echo $article['title']; ?><br>
            <?php echo $article['content'];?><br>
            <img src="<?php echo $img; ?>" /> <!-- The return result will be shown here -->
            <div class="floatRight clear"><?php echo "Artikel skriven " . $article['author'] . " " . $article['pubdate']; ?></div>
        </div>
    
    <?php endforeach; ?>
    
    <?php
    
    function get_img($category){
    
    
        $stmt2 = $db->prepare('SELECT * FROM Object WHERE category = '.$category);
        $stmt2->execute();
        $res2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
        $rnd = array();
        foreach($res2 as $object){
            $rnd[] = $object['img'];
        }
    
        $n = rand(0,(count($rnd)-1));
    
        return $rnd[$n];
    
    }
    
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算