I'm trying to get all results from my database. I have an OOP structure set up and created a couple functions in my DB class which get all results from the database.
public function getAll($table, $order) {
return $this->actionAll('SELECT *', $table, $order);
}
public function actionAll($action, $table, $order) {
$sql = "{$action} FROM {$table} ORDER BY {$order} DESC";
if(!$this->queryAll($sql)) {
return $this;
}
}
public function queryAll($sql) {
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql)) {
if($this->_query->execute()) {
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
} else {
$this->_error = true;
}
}
}
In my Page class I'm calling the functions from my DB class and counting the results.
public function get() {
$data = $this->_db->getAll('pages', 'created');
if($data->count()) {
$this->_data = $data->first();
return true;
}
}
Next, in my list PHP file I'm calling the get function from my Page class and creating an foreach loop to loop through the results.
<?php $page = new Page();
$pages = $page->get();
if(empty($pages)): ?>
<p>No pages at the moment</p>
<?php else: ?>
<?php foreach($pages as $item): ?>
<tr>
<td><?php echo e($item->data()->label); ?></td>
<td><?php echo e($item->data()->title); ?></td>
<td><?php echo e($item->data()->slug); ?></td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php endforeach;
endif; ?>
The problem is that I'm getting the error Invalid argument supplied for foreach(). I don't really understand why I'm getting this error.
I've tried to fix this, but couldn't think of a solution.
Instead of checking empty($pages) check !is_array($pages).
<?php $page = new Page();
$pages = $page->get();
if(!is_array($pages)): ?>
<p>No pages at the moment</p>
<?php else: ?>
<?php foreach($pages as $item): ?>
<tr>
<td><?php echo e($item->data()->label); ?></td>
<td><?php echo e($item->data()->title); ?></td>
<td><?php echo e($item->data()->slug); ?></td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php endforeach;
endif; ?>
Related
I have a table that store the data of ID, ID Login, Name, etc. But i just want to show the data based on ID Login
Here My Controller :
function index(){
$data['hasil'] = $this->M_user_lapor->index_model();
$this->load->view('v_user_lapor/index', $data);
}
My Model :
function index_model(){
$baca = $this->db->query('select * from user_lapor');
if($baca->num_rows() > 0){
foreach ($baca->result() as $data){
$hasil[] = array(
'id_login'=>$data->id_login,
'id_lapor'=>$data->id_lapor,
'nm_unit'=>$data->nm_unit,
'pic_1'=>$data->pic_1,
'pic_2'=>$data->pic_2,
'ip_wan'=>$data->ip_wan,
'ip_lan'=>$data->ip_lan,
'prov'=>$data->prov,
'icn_sid'=>$data->icn_sid,
'tlkm_sid'=>$data->tlkm_sid,
'status'=>$data->status,
);
}
return json_encode ($hasil);
}else{
return false;
}
}
View :
<tbody>
<?php
if ($hasil){
$no = 1;
$array = json_decode($hasil, true);
foreach($array as $data) {
?>
<tr>
<td class="text-center"><?php echo $no++;?></td>
<td><?php echo $data['nm_unit'];?></td>
<td><?php echo $data['pic_1'];?></td>
<td><?php echo $data['pic_2'];?></td>
<td><?php echo $data['ip_wan'];?></td>
<td><?php echo $data['ip_lan'];?></td>
<td><?php echo $data['prov'];?></td>
<td><?php echo $data['icn_sid'];?></td>
<td><?php echo $data['tlkm_sid'];?></td>
</tr>
<?php
}
}
?>
</tbody>
As you can see, there is id_login inside my model, and i want to show the table data based on it, hopefully somebody can help me because i'm just using the codeigniter, thnaks
I solved this by myself, lol. I just pass the id_login value from session, so i add this to my controller :
function index(){
$id_login = $this->session->id_login;
$data['hasil'] = $this->M_user_lapor->index_model($id_login);
$this->load->view('v_user_lapor/index', $data);
}
And call it to my model :
function index_model($id_login){
$baca = $this->db->query('select * from user_lapor where id_login='.$id_login);
if($baca->num_rows() > 0){
foreach ($baca->result() as $data){
$hasil[] = array(
'id_login'=>$data->id_login,
'id_lapor'=>$data->id_lapor,
'nm_unit'=>$data->nm_unit,
'pic_1'=>$data->pic_1,
'pic_2'=>$data->pic_2,
'ip_wan'=>$data->ip_wan,
'ip_lan'=>$data->ip_lan,
'prov'=>$data->prov,
'icn_sid'=>$data->icn_sid,
'tlkm_sid'=>$data->tlkm_sid,
'status'=>$data->status,
);
}
return json_encode ($hasil);
}else{
return false;
}
}
I want to validate and insert multiple records from single form.
I tried following solution, but it is not validating each records.
Yii - multiple records in one form submission
I've used something like this in my form:
<td><?php echo CHtml::activeTextField($item,"[$i]name"); ?></td>
<td><?php echo CHtml::activeTextField($item,"[$i]price"); ?></td>
<td><?php echo CHtml::activeTextField($item,"[$i]count"); ?></td>
<td><?php echo CHtml::activeTextArea($item,"[$i]description"); ?></td>
And in my controller I've done similar to this:
public function actionBatchCreate() {
$models=array();
// since you know how many models
$i=0;
while($i<5) {
$models[]=Modelname::model();
// you can also allocate memory for the model with `new Modelname` instead
// of assigning the static model
}
if (isset($_POST['Modelname'])) {
$valid=true;
foreach ($_POST['Modelname'] as $j=>$model) {
if (isset($_POST['Modelname'][$j])) {
$models[$j]=new Modelname; // if you had static model only
$models[$j]->attributes=$model;
$valid=$models[$j]->validate() && $valid;
}
}
if ($valid) {
$i=0;
while (isset($models[$i])) {
$models[$i++]->save(false);// models have already been validated
}
// anything else that you want to do, for example a redirect to admin page
$this->redirect(array('modelname/admin'));
}
}
$this->render('batch-create-form',array('models'=>$models));
}
<?php
public function actionBatchCreate()
{
$arrItems = array();
$valid = true;
if(isset($_POST['Modelname'])) {
foreach ($_POST['Modelname'] as $i => $notUsed)
{
$objItem = new Modelname();
$objItem->attributes=$_POST['Modelname'][$i];
$valid = $objItem->validate() && $valid;
$arrItems[] = $objItem;
}
if($valid) {
foreach ($arrItems as $objItemValidated) {
$objItemValidated->save();
}
$this->redirect(array('modelname/admin'));
}
}
// optional create a initial empty row in View
if(!count($arrItems)) {
$arrItems[] = new Modelname();
}
$this->render('batch-create-form',array('models'=>$arrItems));
}
View-File batch-create-form.php
<table>
<?php foreach($models AS $i => $item):?>
<tr>
<td><?php echo CHtml::activeTextField($item,"[$i]name"); ?></td>
<td><?php echo CHtml::activeTextField($item,"[$i]price"); ?></td>
<td><?php echo CHtml::activeTextField($item,"[$i]count"); ?></td>
<td><?php echo CHtml::activeTextArea($item,"[$i]description"); ?></td>
<tr>
<?php endforeach;?>
</table>
Hello all I have a problem that my information is not being send to my view file.
What mistake do I have?
My Model:
public function viewbds($duanid, $bdsid)
{
$q = $this->db->query("SELECT bds.tenduan, bds.id, bds.tieude FROM bds WHERE bds.tenduan=$duanid AND bds.id=$bdsid);
if($q->num_rows() > 0)
return $q->result();
return false;
}
My Controller:
public function chothue($duanid, $bdsid)
{
$this->load->model('admin_model');
$date[chothue] = $this->admin_model->viewbds('$duanid', '$bdsid');
$this->load->view('admin/view', $data);
}
My View: admin/chothue/1/1
<?php foreach($chothue as $d) { ?>
<tr>
<td><?php echo $d->id; ?></td>
<td><?php echo $d->tenduan; ?></td>
<td><?php echo $d->tieude; ?></td>
<?php } ?>
$this->db->query("SELECT bds.tenduan, bds.id, bds.tieude FROM bds WHERE bds.tenduan=$duanid AND bds.id=$bdsid")->get();
Don't forget to use ->get()
AND use active record
$this->db->select("tenduan, id, tieude")->from("bds")->where("tenduan", $duanid)->where("id", $bdsid)->get();
Here is my model - I want to assign the rows and echo out on the view page e.g
My Model:
public function dsduan(){
$data = array();
$q = $this->db->query("SELECT duan.id, duan.tenduan, duan.diachi, quan.quan, duan.tdt, duan.mdxd, duan.tt, duan.cdt, duan.dvql, duan.dvtk, duan.dvtc, duan.dvgs, duan.loai, duan.tienich, duan.noidung, duan.hinhanh FROM duan LEFT JOIN quan ON duan.quan=quan.id ORDER BY duan.tenduan ASC");
if ($q->num_rows == 1)
{
foreach ($q->result() as $row)
{
$data[]['id'] = $row->id;
$data[]['tenduan'] = $row->tenduan;
$data[]['diachi'] = $row->diachi;
$data[]['quan'] = $row->quan;
}
}
return $data;
}
My Controller:
public function dsduan() {
$this->load->model('madmin');
$duan = $this->madmin->dsduan();
$this->load->view('danhsachduan', $duan);
}
My View:
<td><?php echo $id; ?></td>
<td><?php echo $tenduan; ?></td>
<td><?php echo $diachi; ?></td>
<td><?php echo $quan; ?></td>
How do I properly pass my model array to my controller so that I can echo out on my view?
My view Error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: id
Filename: views/danhsachduan.php
Line Number: 44
In your model you use two dimensional array:
$data[]['id'] = $row->id;
then in your controller you pass this array to view.
so if you want to access two dimensional array into your view, you have to use foreach loop.
such as change your array in model like following:
$data['id'][] = $row->id;
and in your view access array using:
foreach($id as $res)
{
echo $res;
}
update your view like this
<?php foreach($duan as $d) { ?>
<td><?php echo $d['id']; ?></td>
<td><?php echo $d['tenduan']; ?></td>
<td><?php echo $d['diachi']; ?></td>
<td><?php echo $d['quan']; ?></td>
<?php } ?>
public function dsduan(){
$data = array();
$q = $this->db->query("SELECT duan.id, duan.tenduan, duan.diachi, quan.quan, duan.tdt, duan.mdxd, duan.tt, duan.cdt, duan.dvql, duan.dvtk, duan.dvtc, duan.dvgs, duan.loai, duan.tienich, duan.noidung, duan.hinhanh FROM duan LEFT JOIN quan ON duan.quan=quan.id ORDER BY duan.tenduan ASC");
if ($q->num_rows == 1)
{
foreach ($q->result() as $row)
{
$data['id'] = $row->id;
$data['tenduan'] = $row->tenduan;
$data['diachi'] = $row->diachi;
$data['quan'] = $row->quan;
}
}
return $data;
}
In codeigniter, it use extract() method when binding variable from controller to view. In this case, you can do like this.
In Model
public function dsduan()
{
$data = array();
$q = $this->db->query("SELECT duan.id, duan.tenduan, duan.diachi, quan.quan, duan.tdt, duan.mdxd, duan.tt, duan.cdt, duan.dvql, duan.dvtk, duan.dvtc, duan.dvgs, duan.loai, duan.tienich, duan.noidung, duan.hinhanh FROM duan LEFT JOIN quan ON duan.quan=quan.id ORDER BY duan.tenduan ASC");
if ($q->num_rows)
{
$data = $q->result_array();
}
return $data;
}
In Controller
public function dsduan()
{
$this->load->model('madmin');
$data['duan'] = $this->madmin->dsduan();
$this->load->view('danhsachduan', $data);
}
In View, if your array is multi-dimensional, you can use like this,
<?php foreach($duan as $d) { ?>
<td><?php echo $d['id']; ?></td>
<td><?php echo $d['tenduan']; ?></td>
<td><?php echo $d['diachi']; ?></td>
<td><?php echo $d['quan']; ?></td>
<?php } ?>
if your array is one-dimensional, you can use like this,
<td><?php echo $duan['id']; ?></td>
<td><?php echo $duan['tenduan']; ?></td>
<td><?php echo $duan['diachi']; ?></td>
<td><?php echo $duan['quan']; ?></td>
Have modified your code. Kindly replace below with your code.
Model:
public function dsduan()
{
$q = $this->db->query("SELECT duan.id, duan.tenduan, duan.diachi, quan.quan, duan.tdt, duan.mdxd, duan.tt, duan.cdt, duan.dvql, duan.dvtk, duan.dvtc, duan.dvgs, duan.loai, duan.tienich, duan.noidung, duan.hinhanh FROM duan LEFT JOIN quan ON duan.quan=quan.id ORDER BY duan.tenduan ASC");
if($q->num_rows() > 0)
return $q->result();
return false;
}
Controller:
public function dsduan()
{
$this->load->model('madmin');
$data['duan'] = $this->madmin->dsduan();
$this->load->view('danhsachduan', $data);
}
View:
<?php foreach($duan as $d) { ?>
<td><?php echo $d->id; ?></td>
<td><?php echo $d->tenduan; ?></td>
<td><?php echo $d->diachi; ?></td>
<td><?php echo $d->quan; ?></td>
<?php } ?>
So, i've been using codeigniter for a while and I've never had an issue running a query in my model and returning it to my controller which then passes it into my view where I access it as an object, like so:
MODEL (somemodel)
function getdata()
{
$query = $this->db->get('sometable');
return $query;
}
CONTROLLER (somecontroller)
function controldata()
{
$this->load->model('somemodel');
$data['dbdata'] = $this->somemodel->getdata();
$this->load->view('someview',$data);
}
VIEW (someview)
<?php
foreach($dbdata->result() as $row) {
echo $row->id;
echo $row->name;
echo $row->whatever;
echo "<br />";
}
?>
But now for whatever reason in 2.0 it's making use $dbdata->result_array() as $row instead.
Basically it's returning the results as an array instead.
It doesn't make any sense, has something changed in the new version?
Thanks,
So here's my code:
MODEL (admin_model)
function show_allproducts($sort,$order,$limit,$offset)
{
$this->db->select('products.id,productcategories.categoryname,products.name,products.internetspecial,products.added,products.modified');
$this->db->from('products');
$this->db->join('productcategories','products.productcategories_id = productcategories.id');
$this->db->order_by($sort,$order);
$this->db->limit($limit,$offset);
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query;
}
else
{
return FALSE;
}
}
CONTROLLER (admin)
function show_allproducts()
{
$data['title'] = 'All Products';
$this->load->library('pagination');
$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/admin/show_allproducts';
$config['total_rows'] = $this->db->get('products')->num_rows();
$config['per_page'] = 10;
$config['num_links'] = 5;
$config['full_tag_open'] = '<div class="paggination right">';
$config['full_tag_close'] = '</div>';
$config['prev_link'] = '< prev';
$config['next_link'] = 'next >';
$config['cur_tag_open'] = '<a class="active">';
$config['cur_tag_close'] = '</a>';
$this->pagination->initialize($config);
$this->load->model('admin_model');
$data['tabledata'] = $this->admin_model->show_allproducts('name','ASC',$config['per_page'],$this->uri->segment(3));
$data['nav'] = 'admin/pagesections/nav';
$data['maincontent'] = 'admin/pages/show_allproducts';
$this->load->view('admin/template',$data);
}
VIEW (show_all_products)
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<?php if($tabledata) { ?>
<tr>
<th>ID</th>
<th>CATEGORY</th>
<th>NAME</th>
<th>ADDED</th>
</tr>
<?php foreach($tabledata->result() as $row) { ?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->categoryname; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->added;) ?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td>NO DATA</td>
</tr>
<?php } ?>
</table>
Unless you specify $dbdata->result_array() as $row , It wont return the results as array. The same code which you've posted must be working in CI 2.0.
If not, may be the problem with $this->db->get('sometable').
Try with $this->db->query('select * from sometable'). But as far as i know, both are same...
Ok folks, no lie, I found the freaking problem.
Looking at the fourth td where $row->added; is at, there's a close parantheses at the end of it that was causing 4 days of mayhem.
Kindest regards to all those who tried to help. Sometimes it doesn't matter how many eyes are on it I guess.