I was following the instruction to get the entire record from table, and load them into html table. this is the model
private $namatabel;
public function __construct() {
parent::__construct();
$namatabel='ms_kategori_material';
}
function read()
{
$sql = $this->db->get($this->namatabel);
if($sql->num_rows() > 0)
{
foreach($sql->result() as $row)
{
$data[] = $row;
}
return $data;
}
else
{
return null;
}
}
then use the read() function on controller
public function __construct() {
parent::__construct();
$this->load->model('m_kategorimaterial');
}
function index()
{
$data['c_row'] = $this->m_kategorimaterial->read();
//pass the c_row into the views
$this->load->view('v/vkategorimaterial', $data);
}
to display them on the views
<?php
$no = 1;
foreach ($c_row as $row) { ?>
<tr id="row">
<td id="no"><?php echo $no;?></td>
<td id="judul"><?php echo $row->Kode_Kategori_Material_Jasa;?></td>
<td id="kategori"><?php echo $row->Nama_Material_Jasa;?></td>
</tr>
<?php
$no++;
}
?>
but then I got an error saying, undefined variable c_row and invalid argument supplied foreach(). I thought I have sent the c_row variable through the c_kategorimaterial/index and copy pasting foreach statement. what went wrong ?
1)Check whether you are getting the data right by using print_r($data). If that went wrong,you are probably missing something in the query.
2)If you got the db data perfectly,then just change the name of returning array in your model.
function read()
{
$sql = $this->db->get('ms_kategori_material');
if($sql->num_rows() > 0)
{
foreach($sql->result() as $row)
{
$c_row[] = $row;
}
return $c_row; //comment this return part while debugging
// print_r($c_row);
//exit;
}
else
{
return null;
}
}
It would be better if you print your data array to get exact idea of what you are getting in that array. Try following format, might be helpful
function getPosts() {
$query = $this->db->get('posts');
$posts = array();
foreach ($query->result() as $row) {
$posts[] = array(
'id' => $row->id,
'title' => $row->title,
'content' => $row->content
);
}
return $posts;
}
Related
i'm trying to learn codeigniter and i have problem here. There was an error Undefined variable and Invalid argument supplied for foreach() on view. here is the code :
Model (Model_Users.php) :
class Model_users extends CI_Model {
function __construct() {
parent::__construct();
}
function getFirstNames(){
$query = $this->db->query('SELECT firstname FROM users');
if($query->num_rows() > 0) {
return $query->result();
} else {
return NULL;
}
}
function getUsers(){
$query = $this->db->query('SELECT * FROM users');
if ($query->num_rows() > 0) {
return $query->result();
} else {
return NULL;
}
}
}
Controller (welcome.php) :
class Welcome extends CI_Controller {
public function index()
{
$this->home();
}
public function home()
{
$this->load->model('model_users');
$data['title'] = 'MVC Cool Title';
$data['page_header']='Intro to MVC Design';
$data['firstnames'] = $this->model_users->getFirstNames();
$data['users'] = $this->model_users->getUsers();
$this->load->view('welcome_message',$data);
}
}
View (Welcome_message.php) :
<div id="container">
<h1><?php echo $page_header ?></h1>
<div id="body">
<?php
foreach ($firstnames as $object) {
echo $object->firstname .'<br/>';
}
echo '<br/><hr/><br/>';
foreach ($users as $object){
echo $object->firstname . '\'s email address is'. $object->email . '<br/>';
}
?>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
</div>
Please help me what is wrong with my code ?
First check value return any value or not write this code in controller
use
var_dump($firstnames);
and
var_dump($users);
I think no data return from model
check any data in table
Thanks
You can eliminate the error by using return []; instead of return null; instead of checking for that case in every foreach call, because it appears that your user table might be empty based on the code provided.
Modify your model functions like below
function getFirstNames(){
$query = $this->db->get('users')->row()->firstname;
if($query->num_rows() > 0) {
return $query->result_array();
} else {
return NULL;
}
}
function getUsers(){
$query = $this->db->get('users');
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return NULL;
}
}
hope this will help
I got an error
"Cannot use object of type QueryResult as array"
I am simply trying to take state name from the records array but, I only manage to get this error "Cannot use object of type QueryResult as array"
Model.php
public function getstatename()
{
$statename = $this->salesforce->query ("SELECT State__c FROM RI_School_List__c group by State__c");
return $statename;
}
Controller.php
public function index() {
$data['getstatename'] = $this->model->getstatename();
$this->load->view('footer', $data);
echo "<pre>";
print_r($data['getstatename']['records']);
}
Actually $statename is result-set object not an array.
So you need to do like below:-
Model.php:-
public function getstatename(){
$statename = $this->salesforce->query ("SELECT State__c FROM RI_School_List__c group by State__c");
$state_array = array(); // create an array
foreach ($statename as $row){
$state_array[] = $row->State__c; // assign value to array
}
return $state_array; // return array
}
Controller.php:-
public function index() {
$data['getstatename'] = $this->Footer_model->getstatename();
echo "<pre/>";print_r($data['getstatename']); // check this and accordingly change you code further
$this->load->view('footer', $data);
}
Here is the solved code Thanks to #Anant
Model.php
public function getstatename()
{
$statename = $this->salesforce->query ("SELECT State__c FROM RI_School_List__c group by State__c");
$state_array = array();
foreach ($statename as $row){
$state_array[] = $row->State__c;
}
return $state_array;
}
Controller.php
public function index() {
$data['getstatename'] = $this->Footer_model->getstatename();
$this->load->view('footer', $data);
}
See if you can do
$data['getstatename']->fetchRow();
I am getting the below error on my code,
Call to a member function num_rows() on boolean in C:\xampp\htdocs\codeigniter\application\controllers\go.php on line 15
Code:
<?php if (!defined('BASEPATH')) {
exit('No direct script access
allowed');
}
class Go extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('string');
}
public function index()
{
if (!$this->uri->segment(1)) {
redirect(base_url());
} else {
$url_code = $this->uri->segment(1);
$this->load->model('Urls_model');
$query = $this->Urls_model->fetch_url($url_code);
if ($query->num_rows() == 1) {
foreach ($query->result() as $row) {
$url_address = $row->url_address;
}
redirect(prep_url($url_address));
} else {
$page_data = array(
'success_fail' => NULL,
'encoded_url' => FALSE,
);
$this->load->view('common/header');
$this->load->view('nav/top_nav');
$this->load->view('create/create', $page_data);
$this->load->view('common/footer');
}
}
}
}
#ahmed is correct that db code belongs in the model but the problem you're having is most likely from a db error.
Besides fixing the problem, the workaround is this.
if ($query->num_rows() == 1) {
should be changed to
if ($query && $query->num_rows() == 1) {
I would put an else in there to dump the $this-db->error() to the error_log so you can see what the problem is.
You should call the 'num_rows()' inside the Model, not the Controller, for this function is used often after the get() method:
//Model function:
$query = $this->db->get();
$data = Array();
if($query->num_rows()){
//Work with data:
foreach($query->result_array() AS $row){
$data[] = $row;
}
}
return $data;
I suppose if you want to use the num_rows inside the controller, you should return the query AS a result like this:
$query = $this->db->get();
return $query;
$data = [];
$this->db->where('', 0);
$query = $this->db->get('');
if ($query->num_rows()>0)
{
foreach ($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
First of all check that values come from database. if values come from table then put greater than 0 . if your value exist then query run forword. i thank this is help for you
This is caused by buggy Active Records in CI that adds an extra WHERE clause causing ambiguous column names and similar.
Try to get the actual query with $this->db->last_query(); and it will be instantly clear why $query is not an object.
Model
public function show()
{
$query = $this->db->get('text');
return $query->result();
}
2.Controller
public function inf()
{
$this->load->model('addm');
$data['results'] = $this->addm->show($query);
$this->load->view('backend/first',$data);
}
3.view
I am trying to get the data from my db but i can't solve this error:
Undefined variable: result
<?php
echo"<td>";
if (is_array($result)) {
foreach ($result() as $row) {
$id = $row->id;
$words = $row->words;
}
}
echo "</td>";
?>
Few things,
In your model function show is not expecting any parameters
Remove $query from $this->addm->show(); as not needed
In the view variable should be results not result
// controller
public function inf()
{
$this->load->model('addm');
$data['results'] = $this->addm->show();
$this->load->view('backend/first', $data);
}
// view
if (!empty($results)) {
foreach($results as $row) {
$id = $row->id;
$words = $row->words;
}
}
// model
public function show()
{
$query = $this->db->get('text');
return $query->result();
}
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