view in codeigniter passing of array not working - php

So my problem is that the values from this specific id won't print. I really don't know what's the problem since there is no error. please help guys. Still a newbie at using this framework. thanks!
controller:
public function teacher(){
$this->load->model('model_teacher');
$id = $this->input->post('idnum');
$data['result'] = $this->model_teacher->scoreboard($id);
$this->load->view('teacher/teacher', $data);
}
model:
class Model_teacher extends CI_Model {
public function scoreboard($id) {
//$this->db->where('login_id', $this->input->post('idnum'));
$query = $this->db->query("SELECT * FROM teacher WHERE login_id = '".$id."'");
return $query->result();
}
}
view:
<?php
foreach ($result as $a) {
echo $a['login_id'];
echo $a['lname'];
echo $a['mname'];
echo $a['fname'];
}
?>

Alternative to Ghost's answer:
To keep you model the same you would just need to change your view file from:
foreach ($result as $a) {
echo $a['login_id'];
echo $a['lname'];
echo $a['mname'];
echo $a['fname'];
}
To:
foreach ($result as $a) {
echo $a->login_id;
echo $a->lname;
echo $a->mname;
echo $a->fname;
}
This is because result() with the DB driver returns an array of Objects where as result_array() returns an array of arrays.
Hope this helps!

Related

how to get all the records from mysql table in codeigniter

I try the following code but only one recorde will come
function redeem()
{
$this->db->select('questionEng');
$this->db->from('question');
$query=$this->db->get();
foreach($query->result_array() as $result)
{
print_r($result);die;
}
$this->load->view('admin/redeem');
}
Only one record is coming.please help me
function redeem()
{
$this->db->select('questionEng');
$this->db->from('question');
$results = $this->db->get()->result_array();
foreach($results as $result)
{
echo "<pre>";
print_r($result);
echo "<pre>";
}
die;
$this->load->view('admin/redeem');
}
In your code you have used die in for loop. So you script will stop working after first looping. So you script printing only one record.
function redeem()
{
$this->db->select('questionEng');
$this->db->from('question');
$query=$this->db->get();
foreach($query->result_array() as $result)
{
print_r($result);
}
die;
$this->load->view('admin/redeem');
}
public function REDEEM()
$this->db->select('questionEng' );
$this->db->from('question');
$this->db->where(select any colum you want);
$query=$this->db->get();
$list=$query->result();
return $list;
Then load the model in controller as
$res = $this->your model name->REDEEM();
echo json_encode($res);exit; or load view in place of json
remove die; at all, also why you print result you should pass it to your view then read in view:
function redeem()
{
$this->db->select('questionEng');
$this->db->from('question');
$query=$this->db->get();
$arr_data = $query->result_array() ;
$this->load->view('admin/redeem', $arr_data);
}
You can do this.
function redeem()
{
$this->db->select('*');
$this->db->from('question');
$query=$this->db->get();
$results = $query->result_array();
echo "<pre>";
print_r($results); // to print results
echo "<pre>";
$this->load->view('admin/redeem',$results); //pass to view
}

Code Igniter, array printing in view

This is my model
<?php
class home_model extends CI_Model {
function login()
{
$q = $this->db->query('SELECT * FROM users');
$count = $q->num_rows();
if ($count > 0)
{
foreach ($q->result() as $row)
{
$data[]['username'] = $row->username;
$data[]['email'] = $row->email;
$data[]['sex'] = $row->sex;
$data[]['dob'] = $row->dob;
$data[]['mobile'] = $row->mobile;
}
return $data;
}
?>
This is my controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class home extends CI_Controller
{
public function index()
{
$this->load->model('home_model');
$data['records'] = $this->home_model->login();
$this->load->view('home', $data);
}
}
I am only able to print_r and that is not how i want it, how will i echo this? I don't know what format I should use in my echo,
echo($records[0]);
This format is what I tried but it returns the error
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: views/home.php
Line Number: 4 Array
How should I code my view?
first of all your login function is need to corrected as per codeigniter.
function login() {
$q = $this->db->query('SELECT * FROM users');
$count = $q->num_rows();
if ($count > 0) {
return $q->result_array();
}
it will provide an array as you required.
then to use in views, you need to use it following.
suppose you want to echo first user name
echo $records[0]['username'];
Here $records[0] is an array and hence you need to use as above mention and similarly for other field.
Use following process
Your Modal
function login()
{
$q = $this->db->query('SELECT * FROM users');
$count = $q->num_rows();
if ($count > 0)
{
return $q->result();
}
}
?>
No change for controller
On View
foreach ($records as $row)
{
$username = $row->username;
$email = $row->email;
$sex = $row->sex;
$dob = $row->dob;
$mobile = $row->mobile;
}
In your views
foreach($records as $users)
{
echo $users['username'];
}
Use the builtin function print_r() and provide the array is an argument

Select Data Not Work on CodeIgniter?

I have MVC on CI that not show error but cant get the result.
This the Script
Controller
customer.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Customer extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('customer_view');
}
function tampil_customer()
{
$this->load->model('customer_model');
$data = array('tests' => $this->customer_model->tampil_data_customer());
$this->load->view('customer_view', $data);
}//end fucnntion
}//end class
?>
Model
customer_model.php
<?php
class Customer_model extends CI_Models
{
public function tampil_data_customer()
{
$results = array();
$this->db->select('*');
$this->db->from('customer');
$query = $this->db->get();
if($query->num_rows() > 0)
{
$results = $query->result();
}
return $results;
}
}
?>
View
customer_view.php
<table border="1" align="center" widht="900">
<tr>
<td colspan="5" align="center"><h2>Data Customer</h2></td>
</tr>
<tr>
<th>Nama</th>
<th>Company</th>
<th>Alamat</th>
<th>Telepon</th>
<th>HP</th>
</tr>
<?php
//$result = $this->result();
if( !empty($results) )
{
foreach($results as $isi)
{
echo "<tr>";
echo "<td>$isi->nama</td>";
echo "<td>$isi->company</td>";
echo "<td>$isi->alamat</td>";
echo "<td>$isi->telepon</td>";
echo "<td>$isi->hp</td>";
//echo anchor('customer/edit_customer/'.$isi->ID, 'Edit')."|";
//echo anchor('customer/hapus_customer/'.$isi->ID, 'Hapus')."|";
echo "</tr>";
}
}
?>
</table>
the Result only show the HTML like this
Can Anyone Help me to Fix this Problem?
Im very new Using CodeIgniter. I never use Framework before.
Im very appreciated your Answer.
Thanks
You are storing the result from database to array tests here
$data['tests'] = $this->customer_model->tampil_data_customer();
and trying to call it in view with an undefined array $results.
Try with this.
if(!empty($tests->result())){
foreach($tests->result() as $isi)
{
echo "<tr>";
echo "<td>".$isi->nama."</td>";
echo "<td>".$isi->company."</td>";
echo "<td>".$isi->alamat."</td>";
echo "<td>".$isi->telepon."</td>";
echo "<td>".$isi->hp."</td>";
echo "</tr>";
}
}
In model can you make it simple like this,
public function tampil_data_customer()
{
$query=$this->db->query('select * from customer');
$results = $query->result();
return $results;
}
when you pass in arguments using $this->load->view() function,you are using the view() function with the instance of Loader load,it's in /system/core/Loader.php. As the block comments say
An associative array of data to be extracted for use in the view.
if you pass in an object,you can get the public properties by simply using the property name,you can't get the protected and private property.
if you pass in an Multi-dimensional Array,you can get the value by using key/keys in the Multi-dimensional Array.
By the way,you must pass in an array or object other a variable.
sample below
model.php(application\models\test)
class model extends CI_Model {
public $ss=1;
protected $bb=1;
private $vv=3;
}
controller.php(application\controllers)
class Hello extends CI_Controller
public function __construct() {
parent::__construct ();
}
public function index() {
$this->load->model('test/model');
$model=new model();
$this->load->view ( 'test/view', $model );
}
view.php(application\views\test)
echo $ss;
then you get the value of object.

Passing mysql result $row to another method in a class

I Have a Main class Employee which fetches Employees Details,Passed Inputs it fetches mysql results and RETURNS into a associate array into $row.
public function Result(){
$this->IsEmptyCheck();
$this->ConnectDb();
$this->QueryDb();
$this->RowCount();
$this->IfEmployeeFound();
}
public function IfEmployeeFound(){
if ($this->row_cnt > 0)
{
while ($row = $this->result->fetch_assoc()){
return($row);
}
}else{echo "No Results" ;}
}
public function CustomhtmlTabledisplay($row){
foreach ( $row as $key => $value ) {
echo .....
echo "<td>".$value['employee_name']."</td>\n";
echo "<td>".$value['age']."</td>\n";
echo "<td>".$value['familydetails']."</td>\n";
echo .....
}
}
I am running the below php call calling the employee class and executing above functions in it like this.
$check = new Employee($employeeid);
$check->Result()->CustomhtmlTabledisplay();
$check->CloseDb();
How can i achieve it ?
I would like to fetch the data by passing the mysql returned rows array from
IfEmployeeFound() into CustomhtmlTabledisplay();
I would like to display employee details by executing this type of query
$check->Result()->CustomhtmlTabledisplay();
(If I understood). For doing next - by executing this type of query:
$check->Result()->CustomhtmlTabledisplay();
You need return $this in end of every method for chaining methods. And store need data in property-fields of your Main Class.
EDIT
If you want use your class like this
$check->CustomhtmlTabledisplay(($check->Result());
change class methods to:
public function Result(){
$this->IsEmptyCheck();
$this->ConnectDb();
$this->QueryDb();
$this->RowCount();
return $this->IfEmployeeFound();
}
public function IfEmployeeFound(){
$out = array();
if ($this->row_cnt > 0){
while ($row = $this->result->fetch_assoc())
$out[] = $row;
}
return empty($out)? null: $out;
}
public function CustomhtmlTabledisplay($rows){
if($rows){
foreach ( $rows as $row) {
echo .....
echo "<td>".$row['employee_name']."</td>\n";
echo "<td>".$row['age']."</td>\n";
echo "<td>".$row['familydetails']."</td>\n";
echo .....
}
}
}
You can use
require_once (employee.php); //employee is the file where you have the methods
And instance the class employee

Codeigniter, How to store value from query inside foreach

I have a code like this
$this->load->model("m_crud");
$test= $this->m_crud->get_querry; //this model already have query and result() to return
foreach($test as $retest)
{
echo $retest->primary_id;
$test2=$this->db->query("select * from table2 where id='$retest");
foreach($test2 as $retest2)
{
echo $retest2->name;
}
}
Its easy to just put all this code to "views" but I want to use MCV model.
I tried to store the result using array like this:
$data['test']=$test
foreach($test as $retest)
{
echo $retest->primary_id;
$test2=$this->db->query("select * from table2 where id='$retest");
$data['test2']=$test2
foreach($test2 as $retest2)
{
echo $retest2->name;
}
}
$this->load->view("test_view",$data);
What I got in the view is I have the same value from the echo of $test2, which should be differently by each of $test
thank you, sorry for my bad english
You can use in view page
foreach($test2 as $newarray)
{
foreach($newarray->result() as $row)
{
echo $row->columnnames;//
}
}
It is easy here is an example
Controller Method
function getResult()
{
$this->load->model('mymodel');
$results = $this->mymodel->getRecords();
$data['results'] = $results;
$this->load->view('myview',$data);
}
Model Method
function getRecords()
{
return $this->db->query("select * from table2 where id='$retest")->result_array();
}
And view
foreach($results as $row){
echo $row['id'];
echo '<br>';
echo $row['othercolumn'];
}

Categories