Array is not recognized in Codeigniter's View - codeigniter

I'm trying to pass an array to a view from a controller but I can't and I don't know why.
I have a model, a controller and a view.
The model:
<?php
class Modelo_bd extends CI_Model
{
public function datos()
{
$cnb=$this->db->query("SELECT * from anuncios");
return $cnb->result();
}
}
?>
The controller:
if($this->modelo_usuarios->puede_entrar($usr))
{
$this->load->model("modelo_bd");
$cbd=$this->modelo_bd->datos();
$this->load->view('datos',$cbd);
return true;
}
The view:
<?php
echo $cbd->titulo_a;
echo $cbd->contenido;
?>
The error is in the view.
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: cbd
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Why $cbd variable isn't recognized in the view if it is an array? How can I fix it?
Thanks.

In the controller :
if($this->modelo_usuarios->puede_entrar($usr))
{
$this->load->model("modelo_bd");
$cbd=$this->modelo_bd->datos();
$this->load->view('datos', array('cbd' => $cbd);
return true;
}
The second parameter should be array.
You can use in the model:
public function datos()
{
return $this->db->query("SELECT * from anuncios");
}
and in the view:
<?php
foreach ($cdb->result() as $item) {
echo $item->titulo_a;
echo $item->contenido;
}
?>

Shouldn't you rather do:
The controller:
if($this->modelo_usuarios->puede_entrar($usr))
{
$this->load->model("modelo_bd");
$data['cbd']=$this->modelo_bd->datos();
$this->load->view('datos',$data);
return true;
}
The view:
<?php
foreach($cbd as $key => $row){
echo $row->titulo_a;
echo $row->contenido;
}
?>
I think this works better, your choice.

You should do it like this
if($this->modelo_usuarios->puede_entrar($usr))
{
$this->load->model("modelo_bd");
$data['cbd'] = $this->modelo_bd->datos();
$this->load->view('datos',$data);
}

Related

validation in Datamapper orm

hello below is my model
<?php
class Orm_demo_model extends DataMapper{
var $table= 'orm_demo';
var $has_many=array('post_model');
var $validation =array(
'name'=>array(
'label'=>'Name',
'rules'=>array('required','trim'),
),
'description'=>array(
'label'=>'Description',
'rules'=>array('required','trim'),
)
);
}
//End Of Orm_demo .php model ..
And this is my controler
<?php
class OrmCtrl extends CI_Controller{
function __construct() {
parent::__construct();
}
function index(){
$data['title']='Add data';
$this->template->load('templates/template','ormDemoForm',$data);
}
function save(){
$obj = new Orm_demo_model();
$obj->name=$this->input->post('name');
$obj->description= $this->input->post('description');
if($obj->save()){
echo '<script>alert("Record Added..");</script>';
redirect('ormCtrl/','refresh');
}
else{
echo $obj->error->name;
echo $obj->error->description;
// echo $obj->error->string;
}
}
}
//end of ormCtrl.php file
now problem is that it can't show error message when validation failed...
it returns following message.
Unable to access an error message corresponding to your rule name: required.
Unable to access an error message corresponding to your rule name: required.
please tell me whats problem is there..?

Why no return value from function named 'link_to' with PHPUnit in Laravel?

Currently following a Laravel 4 book on writing tests, the following returns no output from PHPUnit if the function is named link_to(), but does return the result if it is named something else like link_tox(). Why is this?
app/helpers.php
<?php
function link_to($url, $body) {
$url = url($url);
return "<a href='{$url}'>{$body}</a>";
}
app/tests/ExampleTests.php
<?php
class FunctionsTest extends TestCase {
public function testGeneratesAnchorTag() {
$actual = link_to('dogs/1', 'Show Dog');
$expected = "<a href='http://localhost/dogs/1'>Show Dog</a>";
$this->assertEquals($expected, $actual);
}
}
Laravel already has a link_to function.

Use a function in $this->set() with CakePHP 2.1

I'm just wondering how I can use/define my own function using the $this->set() method in CakePHP? I want to do something like this...
AppController.php
<?php
function checkSetup() {
if ($this->Auth->user('setup') == 'notcomplete') { return true; }
}
$this->set('isSetup', checkSetup());
?>
And then I will be able to access and call it in my view file:
<?php if ($isSetup): ?>
You haven't setup your profile yet!
<?php endif; ?>
I've tried that, but It clearly doesn't work as I get a massive fatal error. Any ideas/suggestions on how I can do this?
$this->set('isSetup', checkSetup());
That line needs to be inside some function in order to be called. Presumably you want it in the beforFilter of your app controller - something like this:
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
function beforeFilter() {
$this->set('isSetup', checkSetup());
}
function checkSetup() {
if ($this->Auth->user('setup') == 'notcomplete') { return true; }
}
}
?>

Move the category list into the products view

This is the sample controller
public function products()
{
$category['catlist']=$this->usermodel->catlist();
$this->load->view('admin/products',$category);
}
this is the model
public function catlist()
{
$query=$this->db->get('category');
if($query->num_rows()>0)
{
return $query->result();
}
else
{
return null;
}
}
This is the view
<select name="categoryname">
<?php
foreach($catlist as $categorylist)
{
?>
<option><?php echo $categorylist->categoryname;?> </option>
<?php
}
?>
</select>
I want to display the categorylist but getting the error like
Message: Undefined variable: catlist
Message: Invalid argument supplied for foreach()
if (is_array($catlist))
{
foreach ($catlist as $categorylist){
...
}
}
The reasons is so you're not allocating an empty array when you've got nothing to begin with anyway.

How do I display adodb data in Codeigniter in my view?

I have this code in my model in codeigniter:
<?php
class User_model extends Model {
function User_model()
{
parent::Model();
}
function get_data()
{
$pages = false;
// Get the pages from the database using adodb if needed
$this->adodb->connect();
$recordSet = $this->adodb->execute('SELECT id, genreID, artist, albumName FROM album' );
if ( ! $recordSet )
{
log_message( 'error', 'Error connecting to the database' );
log_message( 'debug', $this->adodb->getErrorMsg());
}
else
{
unset( $this->pages );
while (!$recordSet->EOF)
{
$pages[] = array(
'id' => $recordSet->fields[0],
'genreID' => $recordSet->fields[1],
'artist' => $recordSet->fields[2],
'albumName' => $recordSet->fields[3]
);
$recordSet->MoveNext();
}
$this->pages = $pages;
}
$this->adodb->disconnect();
}
}
?>
I have this in my controller:
<?php
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
//
$this->load->model('User_model');
$data['query'] = $this->User_model->get_data();
$this->load->view('welcome_message',$data);
}
}
What I cannot do is get my model results into my view. There is no result() object because I used adodb so this:
<?php foreach($query->result() as $row): ?>
<p>
<?=$row->albumName;?>
</p>
<?php endforeach; ?>
gets me an error.
What do I put in my view to get the query results of my model. I know the model works because I can echo $recordSet->fields[3]; from the model and see the album name.
Thank you for any help.
edit: I don't understand why my get_data() call in my view returns nothing.
For one you are not returning anything from get_data so that will cause $query to be null. Also you shouldn't be executing the query in the view because view content should not be handling data layer operations, also you've already disconnected from the db.

Resources