简体   繁体   English

PHP foreach及其之后的while循环

[英]PHP foreach and after that while loop

I currently have a query which fetches data and then runs another query. 我目前有一个查询,该查询可获取数据,然后运行另一个查询。 I then echo out the results of the second query. 然后,我回显出第二个查询的结果。 What I would like to do is to move the echo outside of the foreach loop please but I don't know how to do that. 我想做的是将回声移到foreach循环之外,但是我不知道该怎么做。

$ids = 2,3;
$id = explode(",",$ids);

$barcode = array();

foreach($id as $value) {
    $sql_query = $db->prepare("SELECT barcode FROM product WHERE id=:value");
    $sql_query->bindParam(":value", $value);
    $sql_query->execute();
    $row = $sql_query->fetch(PDO::FETCH_ASSOC);
    $barcode = $row['barcode'];


    /* I want to move this part starting from here outside the foreach */ 

    $sql_select_all = $db->prepare("SELECT * FROM inventory WHERE barcode=:barcode");
    $sql_select_all->bindParam(":barcode", $barcode);
    $sql_select_all->execute();
    while($row = $sql_select_all->(PDO::FETCH_ASSOC)){

        $name = $row['name'];
        $img = $row['image'];
        $desc = $row['desc'];

        echo $name.$img.$desc;
    }
    /* ending here */

}

How can I move the echo line out of the foreach loop? 如何将回声线移出foreach循环?

try 尝试

foreach($id as $value){
$sql_query = $db->prepare("SELECT barcode FROM product WHERE id=:value");
$sql_query->bindParam(":value", $value);
$sql_query->execute();
$row = $sql_query->fetch(PDO::FETCH_ASSOC);
// make array with the entry
$barcode[] = '"'.$row['barcode'].'"';    
}
// after foreach we explode the array and add comma , 
$barcode = explode(',',$barcode)

// using mysql ( [IN][1] ) query instead of ( = )
$sql_select_all = $db->prepare("SELECT * FROM inventory WHERE barcode in ($barcode)'");
$sql_select_all->bindParam(":barcode", $barcode);
$sql_select_all->execute();
while($row = $sql_select_all->(PDO::FETCH_ASSOC)){

$name = $row['name'];
$img = $row['image'];
$desc = $row['desc'];

echo $name.$img.$desc;

using IN() Function 使用IN()函数

What you need to do is store the results of your second query into an array. 您需要做的是将第二个查询的结果存储到一个数组中。 Then you can move the echo out of the for loop and run a loop on your new array like this: 然后,您可以将echo移出for循环,并在新数组上运行循环,如下所示:

$ids = 2,3;
$id = explode(",",$ids);

$barcode = $results = array();

foreach($id as $value){
    $sql_query = $db->prepare("SELECT barcode FROM product WHERE id=:value");
    $sql_query->bindParam(":value", $value);
    $sql_query->execute();
    $row = $sql_query->fetch(PDO::FETCH_ASSOC);
    $barcode = $row['barcode'];


    /*i want to move this part starting from here outside the foreach*/ 

    $sql_select_all = $db->prepare("SELECT * FROM inventory WHERE barcode=:barcode");
    $sql_select_all->bindParam(":barcode", $barcode);
    $sql_select_all->execute();
    while($row = $sql_select_all->(PDO::FETCH_ASSOC)){
        $results[] = $row;
        //$name = $row['name'];
        //$img = $row['image'];
        //$desc = $row['desc'];

        //echo $name.$img.$desc;
    }
    /*ending here*/
    foreach($results as $row) {
        echo $row['name'] . $row['image'] . $row['desc'];
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM