【问题标题】:PHP Show foreach() results from 2 SQLite-arrays, one as randomPHP 显示来自 2 个 SQLite 数组的 foreach() 结果,其中一个是随机的
【发布时间】:2014-12-30 01:44:30
【问题描述】:

我有 2 个 SQLite- 表,一个包含文章,一个包含图像 URL。 我想做的是做一个 foreach() 来按顺序显示文章,并在文章旁边显示一组随机图像。

应通过检查文章和图像类别(表中的列)之间的相似性来选择图像,然后随机化。 (例如,文章类别可以是“motorboats”和 img 类别“boat”)。

如何显示两个不同数组的结果? 文章代码为:

    $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; ?>

为了添加第二个数组,我尝试了这个,但没有成功,它只显示了第一个数组的结果多次:

$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; ?>

我猜不可能像这样使用嵌套的 foreach()。 我将如何管理这个?

另外,如果有人知道如何匹配上述数组之间的相似性,我将非常感激。如果没有,我稍后再处理。

【问题讨论】:

    标签: php arrays sqlite foreach


    【解决方案1】:

    您可以将一个函数放在第一个 foreach 文章的传递类别中到该函数。现在在那个功能中,将所有图像收集到一个相关的数组中 到传递的类别,然后通过随机化将其返回。下面的代码..

    <?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];
    
    }
    
    
    ?>
    

    【讨论】:

    • 很抱歉花了这么长时间才接受,我的电脑崩溃了,所以我一直在忙于格式化和重新安装.. 没有尝试过这段代码,但看起来它应该可以解决问题。但是,这将匹配具有精确字符串的类别(“motorboats”和“boat”不会匹配),对吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    相关资源
    最近更新 更多