将结果保存到数组中

时间:2013-09-19 09:46:13

标签: php codeigniter codeigniter-2

我试图在每个循环中使用新的'WHERE(AND)'子句查询数据库。新子句只是循环的编号。

我需要在我正在努力的数组中保存每个循环返回的行数。

例如,如果有两个循环,第一个循环返回2行,第二个循环3.我希望数组基本上是:

$portfolio_list = array(2,3); 

就我设法得到的而言,这并不奇怪,它无法正常工作。

任何帮助都会很棒!

$portfolios = 2;

$i = 1;

$portfolios_list = array();

while ($i <= $portfolios) {

    $portfolio_sql = "
        SELECT COUNT(portfolio_number) AS portfolio_uploaded_images
        FROM exp_submissions
        WHERE member_id = $member_id
        AND type_id = '1' 
        AND portfolio_number = $i
    ";

    $query = $this->EE->db->query($portfolio_sql);

    $return = $query->result();
    $portfolios_list[] = $return;

    $i++;
}

2 个答案:

答案 0 :(得分:1)

请试试这个

$portfolios = 2;

for ($i = 1; $i < $portfolios+1; $i++) { //this runs 2 times

    $where_array = array('member_id' => $member_id, 'type_id' => '1', 'portfolio_number' => $i);
    $this->db->where( $where_array );
    //$this->db->select('*'); // I guess you do not need select for this
    $portfolio_list[$i] = $this->db->get('exp_submissions')->num_rows(); 

}

var_dump($portfolio_list);

答案 1 :(得分:0)

试试这个

$portfolios = 2;

$i = 1;


while ($i <= $portfolios) {

    $portfolio_sql = "
        SELECT COUNT(portfolio_number) AS portfolio_uploaded_images
        FROM exp_submissions
        WHERE member_id = $member_id
        AND type_id = '1' 
        AND portfolio_number = $i
    ";

    $query = $this->db->query($portfolio_sql);
    $portfolios_list[$i] = $query->num_rows; // This will give the No of rows fetched 
    $i++;                                                                                         
}
foreach($portfolios_list as $row => $no)
{
   echo "The Results Fetched from query no ".$row." is ".$no;
}