【问题标题】:How to fill blank array with in loop using PHP如何使用 PHP 在循环中填充空白数组
【发布时间】:2019-10-30 04:20:33
【问题描述】:

我想在mysql“taxiInfo”和“booking”中有两个表,现在我只想显示/获取每辆出租车的状态 (无论每辆出租车都是“预订”还是“可用”),

这是我的表“taxiInfo”

id          userId          TotalTaxi           
1           2               3   
2           4               2

这是我的“预订”表

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

现在我首先从 mysql 获取“TotalTaxi”并使用循环检查出租车是否被预订,这是我的代码

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

现在我得到以下结果,现在如果出租车是“可用”那么我得到空白数组,我想获得两种条件的结果(出租车是否被预订不可用)

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

现在我在数组中有四个值(1 辆出租车预订,3 辆可用) 但是如何将所有值合并到单个数组中?我想要像以下数组/格式的输出

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

【问题讨论】:

  • 如果taxiInfo 中有2 条记录user_id 2 怎么办?您的代码不应该工作。
  • @nmfzone: 不会,每个用户都会有 1 条记录
  • @nmfzone:我的问题是“我想显示每辆出租车的状态(是否存在于“预订”表中“)换句话说我想填充空白数组(如果出租车可用)
  • 好的,我现在有什么问题。现在,你能在哪里找到$date 吗? $this-&gt;db-&gt;where('taxiId', $i); 是什么意思?如何从循环中知道taxi_id
  • @nmfzone: 1) $date 是今天的日期 2) 是的,你说得对,taxi_id 来自循环(每个出租车状态/循环根据出租车数量工作)

标签: php mysql arrays codeigniter


【解决方案1】:

对不起,我必须根据你的信息来回答。因为我认为您的数据库很奇怪(或者可能是因为我的知识很少)。很多不清楚的信息,例如:

  • 怎么可能TotalTaxi 3 表示它是taxi_id 1 或2 或3。
  • 什么是taxinumber?没有关于它的信息。
$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();
}

【讨论】:

  • 显示每条记录“已预订”,但实际上只有“1”条记录应显示已预订
  • @deepikaaggarwal 这怎么可能?你能告诉我你现在的完整代码是什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
  • 2017-07-15
  • 2011-10-08
相关资源
最近更新 更多