[英]Select where to display values from MySQL DB in CodeIgniter
I want to display the values from MySQL database using CodeIgniter. 我想使用CodeIgniter从MySQL数据库显示值。 Here, I am using
SELECT
to query to compare the multiple values using the WHERE
condition. 在这里,我使用
SELECT
查询以使用WHERE
条件比较多个值。
I want to display the checked rows values from one page to another. 我想显示从一页到另一页的选中的行值。 I am passing the the checked value via URL.
我通过URL传递检查的值。 In the second page, I am getting the value from the URL, and using the
explode()
function to split the value and display using a for
loop. 在第二页中,我从URL获取值,并使用
explode()
函数拆分值并使用for
循环显示。 Here, values are displaying correctly. 在这里,值显示正确。 But, when passing the array values to the
SELECT
query, it displays only the last rows. 但是,将数组值传递给
SELECT
查询时,它仅显示最后一行。
I want to display all checked rows from the database. 我想显示数据库中所有选中的行。
For example, sample code: 例如,示例代码:
<?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);
?>
For example: 例如:
$array = array("1","2","6");
Normally using explode()
and for
loop, it display all the values. 通常使用
explode()
和for
循环,它将显示所有值。 When I use select query using CodeIgniter, it displays the last row only. 当我通过CodeIgniter使用选择查询时,它仅显示最后一行。
Try this (I have defined $result
before for loop) Also this is not a good practice to use your code like this use controller and models to do all the logical and database work.... 试试这个(我在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.