ccdavies
ccdavies

Reputation: 1606

Saving while results into an array

I am trying to query a database with a new 'WHERE (AND)' clause on each loop. The new clause is simply the number of the loop.

I need to save the count of rows returned per loop in an array which I am struggling with.

If for instance there is two loops, the first loop returns 2 rows, the second 3. I want the array to essentially be:

$portfolio_list = array(2,3); 

Here is as far as I have managed to get, and no surprise its not working correctly.

Any help would be great!

$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++;
}

Upvotes: 0

Views: 80

Answers (2)

Kyslik
Kyslik

Reputation: 8385

please try this

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

Upvotes: 1

Genocide_Hoax
Genocide_Hoax

Reputation: 853

Try this

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

Upvotes: 0

Related Questions