如何使用php在数组中使用while循环?

时间:2017-11-20 06:05:32

标签: php

如何将结果值列表添加到数组中?我的代码是这样的:

function report_details($display = null) {
    if($display){
        $imagePath = SITEURL . "images/";
    } else {
        $imagePath = SITEPATH . "images/";
    }   
    include("includes/db.php");
    $query = $con->query("SELECT * FROM survey_details");

    while($row = $query->fetch_assoc()){
        $sqlimg = $con->query("SELECT * FROM photo_img WHERE surv_id='$row[survey_id]'");   
        $resimg = $sqlimg->fetch_assoc();

        $reportdetails = array(
            array('BrandIcon' => $imagePath . "facebook.png",'Comapany' => "facebook",'Rank' => "2",'Link' => "http://www.facebook.com"),

            );
        return $reportdetails;  
    }

}

2 个答案:

答案 0 :(得分:0)

function report_details($display = null) {
    if($display){
        $imagePath = SITEURL . "images/";
    } else {
        $imagePath = SITEPATH . "images/";
    }   
    include("includes/db.php");
$query = $con->query("SELECT * FROM survey_details");

while($row = $query->fetch_assoc()){
$sqlimg = $con->query("SELECT * FROM photo_img WHERE surv_id='$row[survey_id]'");   
$resimg = $sqlimg->fetch_assoc();

    $reportdetails[] = array(
        array('BrandIcon' => $imagePath . "facebook.png",'Comapany' => "facebook",'Rank' => "2",'Link' => "http://www.facebook.com"),

    );

}
return $reportdetails;      
}

答案 1 :(得分:0)

        <?php
        function report_details($display = null) {                
            if($display){
                $imagePath = SITEURL . "images/";
            } else {
                $imagePath = SITEPATH . "images/";
            }   
            include("includes/db.php");
        $query = $con->query("SELECT * FROM survey_details");

        while($row = $query->fetch_assoc()){
        $sqlimg = $con->query("SELECT * FROM photo_img WHERE surv_id='$row[survey_id]'");   
        $resimg = $sqlimg->fetch_assoc();
// hoping you want to save all the details to an array
            $reportdetails[] = array( //this array saves the details each time the loop runs.
            'BrandIcon' => $imagePath . "facebook.png",
            'Comapany' => "facebook",
            'Rank' => "2",
            'Link' => "http://www.facebook.com"
            );    
        }    
    return $reportdetails;  
        }

希望这有效