簡體   English   中英

選擇在CodeIgniter中從MySQL DB顯示值的位置

[英]Select where to display values from MySQL DB in 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使用選擇查詢時,它僅顯示最后一行。

試試這個(我在for循環之前已經定義了$result )另外,使用這樣的代碼(如use控制器和模型)來完成所有邏輯和數據庫工作也不是一個好習慣。

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


     ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM