Codeigniter Array Display - codeigniter

I want to display values of an array, but it only displays one value of the array rather than all values of the array.
Model:
function get_subject_position($exam_id , $class_id , $subject_id ) {
$this->db->where('exam_id' , $exam_id);
$this->db->where('class_id' , $class_id);
$this->db->where('subject_id' , $subject_id);
$this->db->select('mark_obtained');
$this->db->order_by('mark_obtained DESC');
$subject_position = $this->db->get('mark')->result_array();
foreach($subject_position as $row) {
return $row;
}
}
View:
$subject_pos = $this->crud_model->get_subject_position($row2['exam_id'], $class_id, $row3['subject_id']);
<td style="text-align: center;"><?php echo $subject_pos['mark_obtained'];?></td>

You need to move the "foreach" loop into your view, because right now it's returning the first $row, and then stopping the foreach loop ("return" stops looping)
Example:
View
<?php
$subject_pos = $this->crud_model->get_subject_position($row2['exam_id'], $class_id, $row3['subject_id']);
foreach($subject_pos as $row) {
?>
<tr>
<td style="text-align: center;"><?=$row['mark_obtained']?></td>
</tr>
<?php } ?>
Function
function get_subject_position($exam_id , $class_id , $subject_id ) {
$this->db->where('exam_id' , $exam_id);
$this->db->where('class_id' , $class_id);
$this->db->where('subject_id' , $subject_id);
$this->db->select('mark_obtained');
$this->db->order_by('mark_obtained DESC');
return $this->db->get('mark')->result_array(); // Get ALL rows
}

Related

Want to display db value in my page. The Add and view should in one page

public function unit_list()
{
//Redirect in case of session out
if (!$this->session->userdata ( 'user_id' )) {
redirect ( $this->config->base_url (), 'refresh' );
return false;
}
$this->load->model('Unit_model');
$arrWhere = array("vchr_unit_status"=>"A");
$data ["unit"]=$this->Unit_model->get_all($arrWhere);
$this->load->view('includes/header');
$this->load->view('includes/left_menu');
$this->load->view('unit/manage',$data);
$this->load->view('includes/footer');
}
This is my controller . The unit_list is the function i wrote to get all data from db and view that in my view page
class Unit_model extends CI_Model{
protected $strTableName = 'suc_unit';
function __construct(){
parent::__construct ();
$CI = &get_instance();
$this->db->from($this->strTableName);
}
/**
*
*/
function get_all($arrWhere)
{
$this->db->where($arrWhere);
$q1 = $this->db->get();
$result = $q1->result_array();
return $result;
}
This is my model
<table id="tblListOfUnits" class="table table-bordered table-striped">
<tr>
<th style="width: 10px">Sl No</th>
<th>Unit Name</th>
<th>Unit Symbol</th>
</tr>
<?php
if(!isset($unit)){
$count = 1;
foreach ($unit as $key => $units) {
?>
<tr id="tr_<?php echo $units->pk_int_unit_id; ?>">
<td id="tdUntSlNo_<?php echo isset($units) ? $units['int_no_decimal']: ' '; ?>><a href="javascript:unitEdit(<?php echo $unit->pk_int_unit_id; ?>)" ><?php echo $count; ?>.</a></td>
<td id="tdUntNme_<?php echo $units->pk_int_unit_id; ?>"><a href="javascript:unitEdit(<?php echo $unit->pk_int_unit_id; ?>)" ><?php echo $unit->vchr_unit_name; ?></a></td>
<td id="tdUntSymbl_<?php echo $units->pk_int_unit_id; ?>"><a href="javascript:unitEdit(<?php echo $unit->pk_int_unit_id; ?>)" ><?php echo $unit->vchr_unit_symbol; ?></a></td>
</tr>
<?php
$count ++;
}
}?>
</table>
This is my view page. please help me to view all the data in my view page. Im new to codeigniter so i dont know how to use get_all function to view the data in my page.
CodeIgniter allows you to pass the data to the view in a couple of ways. First, if you would simply use the second parameter of the view method, then your data would be available as $unit in the view.
In your code, it seems that you are doing that. That means that in your unit/manage.php view, the result of your query should be available as $unit.
A couple of things though. When you load a model, calls to that model are lowercased.
$this->unit_model->get_all( $arrWhere );
It may not matter, but that line should be:
$data["unit"] = $this->unit_model->get_all( $arrWhere );
I want to point out that in your model's method, you are assuming that you will always have a result, and that's not OK. You need to make sure there's a result before trying to return it. Also, you need to provide a table name to get():
function get_all( $arrWhere )
{
// Make sure $arrWhere is an array
if( ! is_array( $arrWhere ) )
return FALSE;
$this->db->where( $arrWhere );
// Make sure you set the table name!
$q1 = $this->db->get( $this->strTableName );
// Make sure there is a result set!
if( $q1->num_rows() > 0 )
return $q1->result_array();
return FALSE; // or NULL, or array(), or whatever you want.
}
Sure, you are trying to set the table in the constructor of the model, but that would only work for the first query to the database. That's not right, and you should avoid that because typically you don't create a model just for a single query.
function __construct()
{
parent::__construct();
// If you are in a CodeIgniter model, you don't need this line
// $CI = &get_instance();
// This is bad. Don't do this ever.
// $this->db->from($this->strTableName);
}
Now in your view you have a problem. $unit will ALWAYS be set, so when you do this:
if( ! isset( $unit ) ) ...
You will never enter that block of code. What you should be doing there is:
if( ! empty( $unit ) ) ...
In your foreach loop, this code will throw an error:
$units->pk_int_unit_id
It's because you are returning an array of arrays from your db call, so it should be:
$units['pk_int_unit_id']
You've got other instances where you're trying to use an object that is actually an array. Basically anywhere that you have $units-> it should be $units['...'].
If you wany to use an object instead of an array, then inside get_all() you should use:
return $q1->result();
Because result_array() is an array of arrays, but result() is an array of objects.
Anyways, hope this helps.

Codeigniter db->where() not working in foreach loop

On my view I need to be able to get the hidden banner_image_id and use that in my $this->db->where('banner_image_id', $id) on my model function
When I update the image. It does not update to the correct row. Instead it update all rows with the same filename.
Question: On my model how can I make sure that my $this->db->where('banner_image_id', $id) updates correct rows.
I have done a vardump($id) and result is string(2) "63" which is correct but still updates every row the same. I tried update_batch also no luck.
Model Function
public function edit_banner_image($file_name) {
$banner_image_id = $this->input->post('banner_image_id');
if (isset($banner_image_id)) {
foreach ($banner_image_id as $id) {
//var_dump($id);
//exit;
$data = array('banner_id' => $this->uri->segment(4),'banner_image' => $file_name);
$this->db->where('banner_image_id', $id);
//$this->db->where_in('banner_image_id', $id); // Tried No Luck
//$this->db->or_where_in('banner_image_id', $id); // Tried No Luck
$this->db->update($this->db->dbprefix . 'banner_img', $data);
}
}
}
View Table
<table>
<tbody>
<?php foreach ($banner_images as $img) { ?>
<tr>
<td>
<?php echo $img['banner_image_id'];?>
<input type="hidden" name="banner_image_id[]" value="<?php echo $img['banner_image_id'];?>">
</td>
<td>
<input type="file" name="banner_image[]" multiple size="20">
</td>
<td>
<img src="<?php echo base_url() . 'uploads/' . $img['banner_image'];?>" />
<input type="hidden" name="banner_image" value="<?php echo $img['banner_image'];?>">
</td>
</tr>
<?php }?>
<tbody>
</table>
Try to concatenate the id numbers in foreach loop and pass it where function at once.
Assuming that your id numbers are array(1,2,5,6).
public function edit_banner_image($file_name) {
$banner_image_id = $this->input->post('banner_image_id');
if (isset($banner_image_id)) {
$bid = 'IN(';
foreach ($banner_image_id as $id) {
$bid .= $id . ',';
}
$bid = substr($bid, 0, -1) . ')'; //remove last comma and add closing paranthesis.
//The content of the variable $bid would be like
//IN(1,2,5,6)
$data = array('banner_id' => $this->uri->segment(4),'banner_image' => $file_name);
$this->db->where('banner_image_id', $id);
$this->db->update($this->db->dbprefix . 'banner_img', $data);
}
}
Another Suggestion:
Convert $banner_image_id to a normal array in case of it is an associative array.
public function edit_banner_image($file_name) {
$banner_image_id = $this->input->post('banner_image_id');
$bid = array();
if (isset($banner_image_id)) {
foreach ($banner_image_id as $id) {
$bid[] = $id;
}
$data = array('banner_id' => $this->uri->segment(4),
'banner_image' => $file_name);
$this->db->where_in('banner_image_id', $bid);
$this->db->update($this->db->dbprefix . 'banner_img', $data);
}
}
I suggest you to make convertion in the controller section, not in model. The lines ending with // should be in the controller. You can easily pass $bid to edit_banner_image($file_name, $bid) function then.
public function edit_banner_image($file_name, $bid) {
$banner_image_id = $this->input->post('banner_image_id'); //
$bid = array();//
if (isset($banner_image_id)) {//
foreach ($banner_image_id as $id) { //
$bid[] = $id;//
}
$data = array('banner_id' => $this->uri->segment(4),
'banner_image' => $file_name);
$this->db->where_in('banner_image_id', $bid);
$this->db->update($this->db->dbprefix . 'banner_img', $data);
}
}
Thus, you will have more readable code.

Undefined variable, how to retrieve data from database in codeigniter?

My model is:
function get_all_pages(){
$query= $this->db->query("SELECT Title FROM news");
return $query->result_array();
}
My view is:
<table cellpadding="0" cellspacing="0">
<tr>
<th>Title</th>
</tr>
<?php
$count=1;
if(!empty($content)) {
foreach ($content as $content1)
{
?>
<tr>
<td align="left"><?php echo $content1['Title']; ?></td>
</tr>
<?php
}
} else{?>
<tr>
<td align="center" colspan="4"><?php echo "No Record Added Yet!!!";?></td>
</tr>
<?php }?>
</table>
My controller is:
public function manage_news()
{
$this->load->model('users_model');
$result_nb = $this->users_model->get_all_pages();
$data['content'] = $result_nb;
$data['page_title']="Manage Pages";
$this->load->view("manage_news",$data);
}
I am using codeigniter. my exact error is:
Call to undefined method users_model::get_all_pages()
how can i fix this error? i want the title row data to appear in a table (from the db)
Q: use Correct naming structure for CI? Which version do u use?
Filename
models/user_model.php
Examples (for demo purpose)
<?php
class User_model extends CI_Model {
// example one
function get_all_pages() {
$query = $this->db->query("SELECT Title FROM news")->result_array();
return ( is_array($query) && sizeof($query) > 0 ? $query : FALSE);
}
// example second
function get_all_pages_second() {
$data = FALSE;
$query = $this->db->query("SELECT Title FROM news");
if ($query->num_rows() > 0){
$data = $query->result_array();
}
return $data;
}
// example third
function get_all_pages_third() {
$data = FALSE;
$query = $this->db->select('Title')->get('news');
if ($query->num_rows() > 0){
$data = $query->result_array();
}
return $data;
}
}

codeigniter grabbing array from database

I'm new to codeigniter and followed the guides but seem to be missing something. I have a simple database with customer records in it. My first goal in codeigniter is to simple list all my customers.
here is my controller:
public function index()
{
$this->load->model('HomeModel'); //loads the HomeModel model
$data = $this->HomeModel->function1(); //loads the function (function1) from the model
$this->load->view('index', $data);
}
Here is my model:
public function function1()
{
$query = $this->db->get('work_orders');
return $query->result();
}
here is my view:
<table class="table table-bordered table-striped table-highlight" id="invoice-details">
<thead>
<tr>
<th>Work Order ID</th>
<th>Status</th>
<th>Description</th>
</tr>
</thead>
<?php
foreach ($data->result() as $row);?>
<tr class="even gradeC">
<td><a href="/WorkOrders/viewWo/<?php echo $row['id']; ?>">
<?php echo $row['id']; ?></a></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo Logic\System\Lib\Helper::trunc(htmlentities($row['description']), 8); ?></td>
</tr>
<?php endforeach; ?>
</table>
Change on model :
public function function1()
{
$query = $this->db->get('work_orders');
//return $query->result();
return $query->result_array();
}
Change on controller :
public function index()
{
$this->load->model('HomeModel');
//$data = $this->HomeModel->function1();
$data['result'] = $this->HomeModel->function1();
$this->load->view('index', $data);
}
Change on view :
//foreach ($data->result() as $row);
if(is_array($result)&&!empty($result))
foreach ($result as $row);
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading function. Here is an example using an array:
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('blogview', $data);
Views : Codeigniter User Guide
And Since you are using array on your view like $row['id'], you got to use result_array on model to return result set array:
This function returns the query result as a pure array, or an empty array when no result is produced. Typically you'll use this in a foreach loop, like this:
$query = $this->db->query("YOUR QUERY");
foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
Generating Query Results : Codeigniter
You can use $this->db->join();
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
Produces:
SELECT * FROM blogs
JOIN comments ON comments.id = blogs.id
Go through active record class guide .
Active Record Class
In ur controller,u must pass the values as subarray of data array$data['result'].After which u can simply call it in view as $result .
Controller
$data['result'] = $this->HomeModel->function1();
And In view,
foreach ($result as $row){
<?php echo $row->id; ?>
..
}

accessing array in array data pass from controller to view of codeigniter

I have this following code in my controller. I want to print the data i have my array.. should it be double forloop or foreach?
CONTROLLER:
public function index()
{
$in_cart = array();
if (!isset($_SESSION['cartProducts'])){
$in_cart['list'] = "No Products";
}
else{
foreach ($_SESSION['cartProducts'] as $key => $value) {
$in_cart[$key] = $this->shopmod->get_one_data($key);
}
$cart['list'] = $in_cart;
}
$this->load->vars($cart);
$data['cart'] = $this->load->view('shop/cart', '', TRUE);
$this->load->view('layout/default', $data);
}
VIEW:
<?php if(is_array($list)): ?>
<?php foreach($list as $row):?>
<tr>
<td><?=$row->name?></td>
</tr>
<?php endforeach ?>
<?php endif;?>
but i have this following error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: shop/cart.php
Line Number: 18
anyhelp guys? :(
I see you are using Code igniter (nice!)
I would do the following to ensure the $key is taking 'name'
public function index() {
foreach ($_SESSION['cartProducts'] as $key => $value) {
echo $key;
} }
Then
public function index() {
$in_cart = array();
if (!isset($_SESSION['cartProducts'])){
$in_cart['list'] = "No Products";
}
else{
foreach ($_SESSION['cartProducts'] as $key => $value) {
$in_cart[$key] = $this->shopmod->get_one_data($key);
}
}
$this->load->vars($cart);
$data['cart'] = $this->load->view('shop/cart', '', TRUE);
$data['list'] = $in_cart;
$this->load->view('layout/default', $data); }
PHP is telling you exactly what is wrong.
You are trying to access the name property of the $row array when the -> syntax is specifically reserved for objects.
To access array values, you use square brackets and keys: <?=$row['name']?>
Helpful hint to understand - What you're doing is quasi-equivalent to: 7->name, insofar as the left value (7) has no idea how to use the arrow syntax. It's reserved for when the left value is an object. Not an integer, or in your case, an array.
Update after your comment:
You would get at the data like this:
<? foreach($row['list'] as $r):?>
<tr>
<td><?=$r[0]->name;?></td>
</tr>
<? endforeach;?>

Resources