简体   繁体   中英

PHP looping through array values in a query

I'm attempting to loop through an array and use the rows(values) as the conditions for my SQL query but I can't seem to get it loop through the rows. It only outputs the data for the first row where the form_name = ' V243823' then stops. i need all the rows, so 3 arrays total to be returned.

Campus Forms Array

[0] => Array
        (

            [PQ_Lookup] => V243823
            [RL_Lookup] => B3823RL
            [MA_Lookup] => F356823
        )

Query

    foreach( $campus_forms[0] as $key => $row )
    {
    $this->db->select('form_deadline,form_url,form_fullname'); 
    $this->db->from('form_deadlines');

    $this->db->where('form_name', $row);
        $query = $this->db->get();

         if ($query->num_rows() > 0)
            {
                $campus_forms = $query->result_array();
                  return $campus_forms;
            }
      }

In first loop you are returning for this reason it doesn't execute next loop.Please return your data after foreach loop.You can hold your data in an array in every loop.You can do it in this way:

$form_data_array = array();

foreach( $campus_forms[0] as $key => $row )
    {
    $this->db->select('form_deadline,form_url,form_fullname'); 
    $this->db->from('form_deadlines');

    $this->db->where('form_name', $row);
        $query = $this->db->get();

         if ($query->num_rows() > 0)
            {
                $campus_forms = $query->result_array();
                 // return $campus_forms;
$form_data_array[] = $campus_forms;
            }
      }
return $form_data_array;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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