Skip to content
Advertisement

How to fill blank array with in loop using PHP

I want have two tables in mysql “taxiInfo” and “booking”,Now i just want to show/fetch status of every taxi (whether every taxi is “booked” or “aviliable” ),

Here is my table “taxiInfo”

id          userId          TotalTaxi           
1           2               3   
2           4               2

Here is my table “booking”

id      userId          taxiId          serviceDate
1       2               1               2019-10-30  

Now first of all i fetch “TotalTaxi” from mysql and check whether taxi is booked or not using loop,Here is my code

$this->db->select('*');
        $this->db->from('taxiInfo');
        $this->db->where('userId', $_POST['userId']);
        $query = $this->db->get();
        if ( $query->num_rows() > 0 )
            {
                $rows = $query->row_array();
                 $totaltaxi=$rows['TotalTaxi'];
                $team=array();
                for($i=1;$i<=$totaltaxi;$i++)
                {
                    $date = date('Y-m-d');
                    $this->db->select('b.*');
                    $this->db->from('booking b');
                    $this->db->where('userId', $_POST['userId']);
                    $this->db->where('taxiId', $i);
                    $this->db->where('serviceDate', $date);
                    $querys = $this->db->get();
                    $res = $querys->result_array();
                    echo "<pre>";print_R($res);
                }

Now i am getting following result , now if taxi is “aviliable” then i am getting blank array, i want to get result for both condition (whether taxi is booked not aviliable)

Array
(
    [0] => Array
        (
            [userId] => 2
            [taxiId] => 1
        )
)
Array
(
)
Array
(
)
Array
(
)

Right now i have four values in array (1 taxi booked and 3 are aviliable) But how can i merge all values in single array ? I want output like following array/format

Array
(
    [0] => Array
        (
            [userId] => 2
            [taxinumber] => 1
            [status] => booked
        )
)
Array
(
    [1] => Array
        (
            [userId] => 2
            [taxinumber] => 2
            [status] => aviliable
        )
)
...

Advertisement

Answer

Sorry, I must answer based on your information. Since I think your DB is weird (or maybe it’s because my little knowledge). A lot information that’s not clear, for example:

  • How is it possible TotalTaxi 3 means it’s taxi_id 1 or 2 or 3.
  • What is taxinumber? There is no information about it.
$userId = $_POST['userId'];
$this->db->select('*');
$this->db->from('taxiInfo');
$this->db->where('userId', $userId);
$query = $this->db->get();

if ($query->num_rows() > 0) {
    $rows = $query->row_array();
    $totaltaxi = $rows['TotalTaxi'];
    $date = date('Y-m-d');
    $taxiStatuses = [];

    for ($i=1; $i <= $totaltaxi; $i++) {
        $this->db->select('b.*');
        $this->db->from('booking b');
        $this->db->where('userId', $userId);
        $this->db->where('taxiId', $i);
        $this->db->where('serviceDate', $date);
        $query = $this->db->get();

        $taxiStatuses[] = [
            'user_id' => $userId,
            'taxi_id' => $i,
            'status' => $query->num_rows() > 0 ? 'booked' : 'available',
        ];
    }

    var_dump($taxiStatuses);die();
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement