i am trying to display data using 2 tables in codeigniter, i have a products table with some products and another table with the id of the products, i did the following in controller:
public function index(){
$selectfeatured = $this->product->selectfeatured();
foreach($selectfeatured as $val){
$id=$val->pid;
$data['featured'] = $this->product->featured($id);
}
$this->load->view('home', $data);
}
in my model,
function selectfeatured()
{
$this->db->select('*');
$this->db->from('featured');
$this->db->order_by("id", "desc");
$this->db->limit(4);
$query = $this->db->get();
$result = $query->result();
return $result;
}
function featured($pid)
{
$this->db->select('*');
$this->db->where("id", $pid);
$this->db->from('product');
$query = $this->db->get();
$result = $query->result();
return $result;
}
this code only displays one product even though i have multiple products in both the tables, can anyone please tell me what is wrong in here
Make your $data['feature'] as an array:
public function index()
{
$selectfeatured = $this->product->selectfeatured();
$data['featured'] = array();
foreach ($selectfeatured as $val) {
$id = $val->pid;
$data['featured'][] = $this->product->featured($id); // add new item
}
$this->load->view('home', $data);
}
And make sure you display the featured array right way in your view, something like this:
<?php foreach ($featured as $feature) : ?>
<?php foreach ($feature as $product) : ?>
<div class="item"><img style="height:250px" src="<?php echo base_url(); ?>admin/uploads/products/<?php echo $product->pimage; ?>" alt="" class="img-responsive"></div>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
The variable $data['featured'] is inside a loop and is getting new value on each iteration, and is overwritten on each iteration, that's why you are getting only one product, you need to use array_push or '+=' etc to append the other products.
Related
When i using this below without where condition query i get the values
Model:
public function get_teacher_number() {
$this->db->select('staff.*');
$this->db->from('staff');
$this->db->join('staff_roles','staff.id=staff_roles.staff_id','inner');
$query = $this->db->get();
return $query->result_array();
}
Output:
2323232,262626,32323,,26262623265265,
When i using this below with where condition query i can't get all the values it show me just one value but when i tried this sql in my mysql server its show me 2 values and here i get only one value.
Model:
public function get_teacher_number() {
$this->db->select('staff.*');
$this->db->from('staff');
$this->db->join('staff_roles','staff.id=staff_roles.staff_id','inner');
$this->db->where('staff_roles.role_id',2);
$query = $this->db->get();
return $query->result_array();
}
Output:
262626,,
one value is missing..
Here i give you controller code:
$all_teacher = $this->teacher_model->get_teacher_number();
$x = '';
foreach ($all_teacher as $val) {
$smsid= $val["id"];
$number = $val["contact_no"];
$x = $x.$number.","; //number separated by comma
}
echo "<pre>";
print_r($x);
exit();
Good I'm starting with php and codeigniter, I have the following problem I'm running an array from my model, which is the result of a query, how can I read one by one my records in the controller?
function getCustomers() {
$sql = "SELECT * FROM customers";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$i = 0;
foreach($query->result() as $row) {
$img[$i]['id'] = $row->id;
$img[$i]['name'] = $row->name;
$img[$i]['Location'] = $row->Location;
$img[$i]['telephone'] = $row->telephone;
$i++;
}
return $img;
}
}
You can return array to the controller by loading the model into the controller like this.
$this->load->model('yourmodel');
$array = $this->yourmodel->yourmodelsmethod();
First you have to return this array to your controller like
$this->load->model('yourmodel');
$array['test'] = $this->yourmodel->yourmodelsmethod();
foreach($test as $key=>$value)
{
echo $value;
}
By doing this you can easily print your data from model in controller
I want to pass multiple id in where condition how to do it?
A query fetches org_id from database.
now I want to pass that in my where condition, so how to do it?
I tried foreach loop:
Below is my code:
$devices = $this->activation_m->get_activated_devices();
$org_id = array(); // create org_id array
foreach ($devices as $row)
{
$org_id[]= $row['org_id']; // assign ids into array
//$companys =$this->data['devices'] = $this->activation_m->get_company($org_id); // don't call again and again
}
//Now $org_id is array
$companys =$this->data['devices'] = $this->activation_m->get_company($org_id);
echo $this->db->last_query();
Model Code
public function get_activated_devices()
{
$this->db->select('*');
$this->db->join('sitekey','sitekey.site_key = activation.site_key');
$this->db->from('activation');
$query =$this->db->get();
$result = $query->result_array();
return $result;
}
public function get_company($org_id)
{
$this->db->select('*');
$this->db->join('sitekey','sitekey.org_id = company.id');
$this->db->join('activation','sitekey.site_key = activation.site_key');
$this->db->where('company.id IN',(implode(',',$org_id))); // see the change here
$this->db->from('company');
$query =$this->db->get();
$result = $query->result_array();
return $result;
}
now currently my query passes only one org_id , I want to pass all the org_id I get from my first query.
You can use where_in from active-records codeigniter
as
$devices = $this->activation_m->get_activated_devices();
$org_id = array();
foreach ($devices as $row)
{
$org_id[] = $row['org_id'];
}
$companys =$this->data['devices'] = $this->activation_m->get_company($org_id);
if( $companys->num_rows() > 0 )
{
echo "<pre>";
print_r( $companys->result());
echo "</pre>";
}
And for Model
public function get_company($org_ids = array())
{
$this->db->select('*');
$this->db->join('sitekey','sitekey.org_id = company.id');
$this->db->join('activation','sitekey.site_key = activation.site_key');
$this->db->where_in('company.id', $org_ids ); //this is condition
$this->db->from('company');
return $this->db->get();
}
You can use codeigniter's $this->db->or_where() for the purpose. Just traverse the array of organization id's and apply or_where conditions.
$this->db->select('*');
$this->db->join('sitekey','sitekey.org_id = company.id');
$this->db->join('activation','sitekey.site_key = activation.site_key');
foreach($org_id as $org)
{ // where $org is the instance of one object of active record
$this->db->or_where('company.id',$org);
}
$this->db->from('company');
$query =$this->db->get();
$result = $query->result_array();
return $result;
Another way to do this is to make a custom query by traversing the array like this and appending where clauses in string.
I'm having trouble passing data to my view when using $query->row() I can only seem to get the value when using it in the controller.
Here's the model:
public function get_post($post_id) {
$this->db->select('id, date, title');
$this->db->from('posts');
$this->db->where('id', $post_id);
$this->db->limit(1);
$query = $this->db->get();
$row = $query->row();
$row->title = html_purify($row->title);
$row->date_posted_iso = date('c', $row->date);
return $row;
}
The controller:
public function post($post_id) {
$row = $this->post_model->get_post($post_id);
$this->data['post'] = $row;
$this->data['meta_title'] = $row->title;
$this->template->build('post/show_post', $this->data);
}
The view:
<?php
foreach ($post as $row):
echo $row->date;
echo $row->title;
endforeach;
?>
The line $this->data['meta_title'] = $row->title; in the controller works correctly, but I don't want to explicitly define all of those variables in the controller.
I've tried with the foreach and without and get Object of class stdClass could not be converted to string
How can I correctly echo the value in my view using $query->row()?
As you are returning a row, you don't need a foreach loop, try changing your view code to:
<?php
echo $post->date;
echo $post->title;
?>
The variable should be $post and not $row as you define post element in the data array in your controller.
Try to do like this.............
public function post($post_id)
{
$row = $this->post_model->get_post($post_id);
$model = array();
$model['post'] = $row;
$this->template->build('post/show_post', $model);
}
In view fie do like this
foreach($post as $data)
{
echo $data['title'];
}
Currently I have this url to view an image from the db (codeigniter) domain.com/view/id
I'd like to be able to accept multiple ids comma separated domain.com/view/id,id,id
Any idea how to go about it? Thanks
view controller part:
function view() {
$id = alphaID($this->uri->segment(1) ,true);
$this->load->model('Site_model');
if($query = $this->Site_model->get_images($id)) {
$data['records'] = $query;
}
$this->load->view('view', $data);
}
<?php if(isset($records)) : foreach($records as $row) : ?>
<?php if($row->alpha_id == $this->uri->segment(1)): ?>
<h1><?php echo $row->alpha_id.$row->file_ext; ?></h1>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
Use this in your controller
function view() {
$id = $this->uri->segment(1);
$id_array = explode(",", $id);
$this->load->model('Site_model');
foreach ($id_array as $key => $id) {
// use alphaID function
$id = alphaID($id ,true);
if($query = $this->Site_model->get_images($id)) {
$data['records_array'][$key] = $query;
// added second array for comparison in view
$data['id_array'][$key] = $id;
}
}
$this->load->view('view', $data);
}
For your view:
<?php
foreach ($records_array as $key => $records) {
if(isset($records)) : foreach($records as $row) : ?>
// removed uri and added array
<?php if($row->alpha_id == $id_array[$key]): ?>
<h1><?php echo $row->alpha_id.$row->file_ext; ?></h1>
<?php endif; ?>
<?php endforeach; ?>
<?php endif;
}
?>
Because commas aren't valid path elements you won't be able to get this to work without having your delimited data on the right side of a ? character. You'll need to come up with another scheme or go with the comment from #jprofitt.
You are right, you can add a comma in $config['permitted_uri_chars'] but you'll have to manipulate with that segment every time you need it, unless you hook into the system core.
Haven't tested this code but you'll get an idea:
<?php
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-,'; // Note a comma...
// Controller
class Blog extends CI_Controller
{
public function posts($ids = NULL)
{
// Check if $ids is passed and contains a comma in the string
if ($ids !== NULL AND strpos($ids, ',') !== FALSE)
{
$ids = explode(',', $ids);
}
// Convert $ids to array if it has no multiple ids
is_array($ids) OR $ids = array($ids);
// $ids is an array now...
}
public function new_posts()
{
// Check if $ids is passed and contains a comma in the string
$ids = $this->uri->segment(1);
if (!empty($ids) AND strpos($ids, ',') !== FALSE)
{
$ids = explode(',', $ids);
}
// Convert $ids to array if it has no multiple ids
is_array($ids) OR $ids = array($ids);
// $ids is an array now...
}
}
?>
example.com/index.php/blog/posts/2,4,6,8
Please note once again, code may not be accurate because I haven't tested it but think it will help you.