douzhi1924 2015-11-16 00:30
浏览 54
已采纳

如何将此查询中的数据添加到数组中?

Is there a way that I can put the data from the while loop into an array and output it using echo? I've tried defining an array and just appending it with .= but it does not add.

$query = "SELECT * FROM coils WHERE name like '%$term%' or 
                                            resistance like '%$term%' or 
                                            wraps like '%$term%' or
                                            wire_one like '%$term%' or
                                            wire_two like '%$term%' or
                                            wire_three like '%$term%' or
                                            wire_four like '%$term%' or
                                            wire_five like '%$term%' or
                                            wire_six like '%$term%' 
                                            LIMIT 25";
        $prep = $db->getConnection()->prepare($query);
        $result = $prep->execute();
        $rowCount = $prep->rowCount();

        if ($rowCount <= 0) {
            echo "<script>alert('No Results, please try another search');</script>";
        } 

        while($row = $prep->fetch(PDO::FETCH_ASSOC)) {
            echo "<a href='coil.php?id=" . $row['uniqueid'] . "'>";
            echo "<div id='search_result'>";
            echo "<div id='search_title'>Name: " . $row['name'] . "</div>";
            echo "<div id='search_ohms'>Resistance: " . $row['resistance'] . "</div>";
            echo "<div id='search_wraps'>Wraps: " . $row['wraps'] . "</div>";
            echo "<div id='search_around'>Wrapped Around: " . $row['wrapped'] . "</div>";
            echo "<div id='search_description'>" . $row['description'] . "</div>";
            echo "</div>";
            echo "</a>";
        }
  • 写回答

2条回答 默认 最新

  • dongwai4434 2015-11-16 00:33
    关注

    Try to add output in an array and then use implode().

    $temp = array();
    
    while($row = $prep->fetch(PDO::FETCH_ASSOC)) {
        $temp[] = "<a href='coil.php?id=" . $row['uniqueid'] . "'>";
        $temp[] = "<div id='search_result'>";
        $temp[] = "<div id='search_title'>Name: " . $row['name'] . "</div>";
        $temp[] = "<div id='search_ohms'>Resistance: " . $row['resistance'] . "</div>";
        $temp[] = "<div id='search_wraps'>Wraps: " . $row['wraps'] . "</div>";
        $temp[] = "<div id='search_around'>Wrapped Around: " . $row['wrapped'] . "</div>";
        $temp[] = "<div id='search_description'>" . $row['description'] . "</div>";
        $temp[] = "</div>";
        $temp[] = "</a>";
    }
    
    echo implode(' ', $temp); //with or without space
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置