All I want is to get some place_ids from db, put them in array and echo that array in view. Could you please check the code below and help me to find the mistake. I think the problem is in view part.
Model:
$this->db->select('place_id');
$this->db->from('table');
$this->db->where('ses_id', $ses_id);
$query = $this->db->get();
if ($query && $query->num_rows() > 0) {
return $query->result_array();
}
else {return false;}
Controller:
$ses_id = $this->uri->segment(3);
$data["results"] = $this->mymodel->did_get($ses_id);
$this->load->view("my_view", $data);
View:
<?php
$place_id = array();
foreach($results as $row){
$place_id[] = $row['place_id'];
}
print_r($place_id);
?>
checking the $results is empty or not
<?php
$place_id = array();
if(!empty($results)
{
foreach($results as $row)
{
$place_id[] = $row['place_id'];
}
}else
{ echo "no data found";}
print_r($place_id);
?>
Related
I want to loop through the results of a query and then compare each results to an input field, if the value is repeated it must return false if not the result is true, I know that I could use unique index in database to avoid repeated values and then catch the error, but in this case repeated values are possible but they can´t both be "active" there a column called "status" with two values A or I (active and inactive) so "clabe" value can be repeated only when one is active and the other inactive.
Controller:
function agregar()
{
$this->load->helper('date');
date_default_timezone_set('America/Mexico_City');
$now = date('Y-m-d H:i:s');
$Interno = $this->input->post('intern');
$clabe = $this->input->post('clabe2') . $this->input->post('control2');
$cve_banco = $this->input->post('cve_banco');
$Banco = $this->input->post('Banco2');
$Observaciones = $this->input->post('Observaciones');
$data = array(
'Interno' => $Interno,
'clabe' => $clabe,
'cve_banco' => $cve_banco,
'Banco' => $Banco,
'Observaciones' => $Observaciones,
'Fecha_alta' => $now,
);
if($this->consultas_M->insert($data) == true){
//redirect('Inicio/busqueda', 'refresh');
} else{
echo "Hubo un problema al insertar";
}
}
Model:
function insert($data)
{
$clabe = $this->input->post('clabe2') . $this->input->post('control2');
$this->db->select('Clabe');
$this->db->where('Status =', 'A');
$query= $this->db ->get('cuentas');
return $query->row();
foreach ($query as $row) {
$i = $row;
if ($clabe = $i) {
return false;
} else {
$this-> db -> insert('cuentas', $data);
if ($this->db->affected_rows() > 0) {
return true;
}
}
}
}
Why would you want to have possible duplicated index ? If you want to have different rows with a common "index" for some reason you can create a column called index or something and add in your WHERE clause that index.
PS: in your model in the foreach loop's if condition use :
if ($clabe == $row){...
instead of
$i = $row
if ($clabe = $i){...
I solved myself the issue, i´m posting in in case it helps anybody, it was easier than I thought.
function insert($data, $clabe)
{
$Status = 'A';
$clabe = $this->input->post('clabe2') . $this->input->post('control2');
$this->db->select('Clabe');
$this->db->where('Status =', $Status);
$this->db->where('Clabe =', $clabe);
$q= $this->db ->get('cuentas');
if($q -> num_rows() > 0){
return false;
}else{
$this-> db -> insert('cuentas', $data);
return true;
}
And this is the controller:
if($this->consultas_M->insert($data, $clabe) == true){
redirect('Inicio/busqueda', 'refresh');
} else{
echo "There was a problem";
$this->load->view('errors/error');
}
The only thing I needed and wasn´t doing was to select the "clabe" I wanted and then compare it with the input where user writes this "clabe". And then just a simple if-else statement.
function insert($data)
{
$Status = 'A';
$clabe = $this->input->post('clabe2') . $this->input->post('control2');
$this->db->select('Clabe');
$this->db->where('Status', $Status);
$query= $this->db ->get('cuentas');
$flag=true;
foreach ($query as $row) {
$i = $row;
if ($clabe = $i){
$flag=false;
}
}
if($flag){
$this-> db -> insert('cuentas', $data);
if ($this->db->affected_rows() > 0) {
$flag= true;
}
}
return $flag;
}
Your code will add it on first else block ad return. Instead it should check all the rows first to decide.
I have a problem,im trying to convert php standard to CodeIgniter, But I dont know how to convert ths code, please help, and thanks a lot.
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("ardefa");
$borneo=mysql_query("select* from borneo");
while($row=mysql_fetch_array($borneo))
{
?>
<a href="#"><li><img src="
<?php
$page = isset($_GET['page']) ? ($_GET['page']):"";
if ($page =='borneo')
{
echo $row["img"];
}
?>">
</li></a>
<?php
}
?>
Hope this will help you :
You don’t need to use db_select if you have single database, if multiple database you only need to use a different database on the same connection. You can switch to a different database when you need to using this $this->db->db_select('ardefa');
You can do like this :
//$this->db->db_select('ardefa');
$this->db->select('*');
$this->db->from('borneo');
$query = $this->db->get();
if ($query->num_rows() > 0 )
{
/*for multiple array*/
$result = $query->result_array();
/*print here to see the result
print_r($result);
*/
}
Use $result like this :
foreach($result as $row)
{
echo $row;
}
Or can also do it like this :
//$this->db->db_select('ardefa');
$query = $this->db->get('borneo');
if ($query->num_rows() > 0 )
{
/*for multiple array*/
$result = $query->result_array();
/*for single array
$row = $query->row_array();
*/
}
For more : https://www.codeigniter.com/user_guide/database/
Try this hope it will help you
MODEL
public function your_function(){
return $this->db->get('borneo')->reslut_array();
}
CONTROLLER
<?php
$this->load->model('model-name');
$data = $this->model-name->model_function();
foreach($data as $row){
if(isset($_GET['page']) && $_GET['page'] == "borneo"){ ?>
<li><img src="<?php echo $row['img']?>" /></li>
<?php } } ?>
My Code :
$this->db->join('followers','followers.id_follower = post.id_account','LEFT');
$id_account = $this->session->userdata('id');
$where = ("followers.id_following=$id_account or post.id_account = $id_account");
$this->db->where($where);
Please help me
Something like this?
In you controller:
// Set variables
$page_data = array();
$id_account = $this->session->userdata('id');
// Get data
$this->db->from('post');
$this->db->join('followers','followers.id_follower = post.id_account','LEFT');
$this->db->where('id_following', $id_account);
$this->db->or_where('id_account', $id_account);
$page_data['followers'] = $this->db->get();
// Load views
$this->load->view('myview', $data);
In your view somewhere
<?php if (!empty($followers)) {
foreach ($followers as $item) {
echo $item['follower_id'];
}
} ?>
You may want to use short forms of php in your view, but you get the idea.
function mostViewedTracks() {
$this->load->database();
$this->db->select("*");
$this->db->from("file");
$this->db->order_by("views", "desc");
$this->db->limit(8);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$results = $query->result();
}
return array();
}
when i run this model it gives error that invalid argument supplied for foreach()
this is my view code:
<?php if( !empty($most_viewed) ) { ?>
<?php foreach($most_viewed as $row): ?>
<a href="<?php echo base_url('index.php/home/track');?>/<?php echo $row->id;?>" class="list-group-item">
<img class="track-list-img" src="<?php echo base_url('assets/img/music');?>/<?php echo $row->id;?>.jpg">
<div class="list-track-infoo">
<h4 class="list-group-item-heading"><?php echo $row->title;?><span class="badge"><?php echo $row->views;?><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></span> </h4>
<p class="list-group-item-text"><?php echo $row->singer;?></p>
</div>
</a>
<?php endforeach; ?>
<?php} ?>
Updated
Controller
function track() {
$id = $this->uri->segment(3);
$data1['track'] = $this->view_models->track($id);
$data1['most_viewed'] = $this->view_models->mostViewedTracks();
$this->load->view('includes/header');
$this->load->view('track', $data1);
$this->load->view('includes/footer');
}
I have tried my best to solve this problem but all in vain
You need to change two mistakes,
You should return the `$result` array in your model like,
$results = $query->result();
return $results;
And in foreach change $row1 to $row which produces the error. see,
<?php foreach($most_viewed as $row): ?>
Instead of return array(); you have to return result set from models file
if($query->num_rows() > 0)
{
return $query->result();// return result set
}else{
return FALSE;//
}
IN controller you have to call your models function as
$data1['most_viewed'] = $this->view_models->getFeaturedTracks();// change mostViewedTracks to getFeaturedTracks
In view use it as
<?php foreach($most_viewed as $row): ?>// remove $row1 to $row
Function should like this....
function getFeaturedTracks() {
$results = array();
$this->load->database();
$this->db->select("*");
$this->db->from("file");
$this->db->order_by("views", "desc");
$this->db->limit(8);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$results = $query->result();
}
return $results;
}
I am working in codeigniter.I have created one function in model.The function is like this :
function pr($id)
{
//$query5 = $this->db->query("select max(to_date) as last_date from agent_print_details where agent_id = ".$id);
//$result5 = $query5->result();
$this->db->select('max(to_date) as last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id',$id);
$result = $this->db->get();
if($result->num_rows()>0)
return $result->result();
else
return "empty";
}
my controller is :
$pr_detail = $this->cashier1_model->pr($role['id']);
if($pr_detail != 0)
{
echo "nisarg";
}
else
{
echo "123";
}
when I print pr_detail then it will display output like this:
Array ( [0] => stdClass Object ( [last_date] => ) )
it will give blank data so it has to print 123 but it is display nisarg.
So What should i have to do to print 123?
If result not found just return FALSE in model file. Also use CI select_max to get max value
MODEL
$this->db->select_max('to_date','last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id', $id);
$result = $this->db->get();
if ($result->num_rows() > 0) {
return $result->result();
} else {
return FALSE;
}
CONTROLLER
$pr_detail = $this->cashier1_model->pr($role['id']);
if($pr_detail)
{
echo "nisarg";
}
else
{
echo "123";
}
you could use count function, also, do you have defined a table name in get function?
...
$result = $this->db->get('agent_print_details');
//if($result->num_rows() > 0)
if(count($result) > 0)
...
When you use $result->num_rows() > 0 you CANT use $result->result(). If u want to return these results, you need to check if its not empty first, and then query again to get results.
$this->db->select_max('to_date','last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id', $id);
$result = $this->db->get();
if ($result->num_rows() > 0) {
$this->db->select_max('to_date','last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id', $id);
$result = $this->db->get();
return $result->result();
} else {
return FALSE;
}