选择在CodeIgniter中显示MySQL DB的值的位置

时间:2017-06-15 12:21:23

标签: php mysql codeigniter

我想使用CodeIgniter显示MySQL数据库中的值。在这里,我使用SELECT来查询以使用WHERE条件比较多个值。

我想将已检查的行值从一个页面显示到另一个页面。我通过URL传递了检查值。在第二页中,我从URL获取值,并使用explode()函数拆分值并使用for循环显示。这里,值正确显示。但是,在将数组值传递给SELECT查询时,它只显示最后一行。

我想显示数据库中所有已检查的行。

例如,示例代码:

<?php

$id  = $this->uri->segment(4);
$arr = explode(',', $id);
for ($kk = 0; $kk < count($arr); $kk++) {
    echo $id_val13 = $arr[$kk];
    $basicUrl                = $this->config->item("basicUrl");
    $basicUrl['bread_crumb'] = $this->breadcrumb();
    $query                   = $this->db->query("SELECT aa.id as id,customer_type_id,start_time,close_time,dd.name as customer_name,bb.name as vehicle_name,cc.name as driver_name,other_expensive,depature_city,destination_city,journey_status,payment,tour_file,DATE_FORMAT(aa.created_date, '%d/%m/%Y') as created_date FROM " . $this->tbl . " as aa
                                left join vehicle as bb on aa.vehicle_id=bb.id
                                left join driver as cc on aa.driver_id=cc.id
                                left join tbl_customer as dd on aa.customer_id=dd.id
                                WHERE aa.id = $id_val13");

    $result = array();

    if ($query->num_rows()) {
        $i = 1;
        foreach ($query->result() as $row) {
            $result_row   = array();
            $result_row[] = '<input type="checkbox" name="chk_id" id="chk_id" value="' . $row->id . '" />';
            $result_row[] = $row->id;
            $result_row[] = $row->customer_name;
            if ($row->customer_type_id == "1")
                $result_row[] = "Tour";
            else if ($row->customer_type_id == "2")
                $result_row[] = "Company";
            else if ($row->customer_type_id == "3")
                $result_row[] = "Guest";
            $result_row[] = $row->driver_name;
            $result_row[] = $row->vehicle_name;

            $result_row[] = $row->destination_city;
            $result_row[] = $row->start_time;
            $result_row[] = $row->close_time;
            $result_row[] = $row->payment;
            $result_row[] = $row->other_expensive;
            $result_row[] = $row->journey_status == "1" ? "Complete" : "On Progress";
            if ($row->tour_file != "") {
                $result_row[] = '<a href="' . base_url('uploads/' . $row->tour_file) . '">' . $row->tour_file . '</a>';
            } else {
                $result_row[] = "No File";
            }
            $result_row[] = $row->created_date;
            $action_btn   = action_button("Edit", $row->id) . action_button("Delete", $row->id) . action_button("Select", $row->id);
            $result_row[] = $action_btn;

            $result[] = $result_row;

            $i++;                                
        }                        
    }                
}


$tbl_header = array(
    "",
    "S NO",
    "Customer Name",
    "Type",
    "Driver Name",
    'Vehicle Name',
    "Destination City",
    "Start Time",
    "Close Time",
    "Rupee",
    "Other Expensive",
    "Journey Status",
    "Itinerary",
    "Created Date",
    array(
        "Edit",
        "Delete"
    )
);
$basicUrl['table'] = $this->makeTable($result, $tbl_header, $bool = TRUE);

$this->parser->parse("admin/center", $basicUrl);

?>

例如:

$array = array("1","2","6");

通常使用explode()for循环,它会显示所有值。当我使用CodeIgniter使用select查询时,它只显示最后一行。

1 个答案:

答案 0 :(得分:0)

尝试这个(我在for循环之前定义了$result)这也不是一个很好的做法,使用你的代码这样使用控制器和模型来完成所有的逻辑和数据库工作....

 <?php

    $id = $this->uri->segment(4);
    $arr = explode(',', $id);
    $result=array();
    for ($kk = 0; $kk < count($arr); $kk++)
        {
          echo $id_val13 =  $arr[$kk];
            $basicUrl = $this->config->item("basicUrl");
            $basicUrl['bread_crumb'] = $this->breadcrumb();
            $query=$this->db->query("SELECT aa.id as id,customer_type_id,start_time,close_time,dd.name as customer_name,bb.name as vehicle_name,cc.name as driver_name,other_expensive,depature_city,destination_city,journey_status,payment,tour_file,DATE_FORMAT(aa.created_date, '%d/%m/%Y') as created_date FROM ".$this->tbl." as aa
                            left join vehicle as bb on aa.vehicle_id=bb.id
                            left join driver as cc on aa.driver_id=cc.id
                            left join tbl_customer as dd on aa.customer_id=dd.id
                            WHERE aa.id = $id_val13"); 


    if($query->num_rows()){
        $i=1;
        foreach($query->result() as $row){
            $result_row=array();
            $result_row[]='<input type="checkbox" name="chk_id" id="chk_id" value="'.$row->id.'" />';
            $result_row[]=$row->id;
            $result_row[]=$row->customer_name;
            if($row->customer_type_id=="1")
            $result_row[]="Tour";
            else if($row->customer_type_id=="2")
            $result_row[]="Company";
            else if($row->customer_type_id=="3")
            $result_row[]="Guest";
            $result_row[]=$row->driver_name;
            $result_row[]=$row->vehicle_name;

            $result_row[]=$row->destination_city;
            $result_row[]=$row->start_time;
            $result_row[]=$row->close_time;
            $result_row[]=$row->payment;
            $result_row[]=$row->other_expensive;
            $result_row[]=$row->journey_status=="1" ? "Complete" : "On Progress";
            if($row->tour_file!="")
            {
            $result_row[]='<a href="'.base_url('uploads/'.$row->tour_file).'">'.$row->tour_file.'</a>';
            }
            else
            {
            $result_row[]="No File";
            }
            $result_row[]=$row->created_date;
            $action_btn=action_button("Edit",$row->id).action_button("Delete",$row->id).action_button("Select",$row->id);
            $result_row[]=$action_btn;

            $result[]=$result_row;


            $i++;


    }


        }


         }


        $tbl_header=array("","S NO","Customer Name","Type","Driver Name",'Vehicle Name',"Destination City","Start Time","Close Time","Rupee","Other Expensive","Journey Status","Itinerary","Created Date",array("Edit","Delete"));
    $basicUrl['table']=$this->makeTable($result,$tbl_header,$bool=TRUE);

    $this->parser->parse("admin/center",$basicUrl);


     ?>