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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器