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

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.

Related

how to get values from model and display in controller

I am making a site in codeigniter, here is my controller, model and view:
controller:
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();// assign your value to CI variable
$this->load->view('home', $data); //passing your value to view
}
model:
public function email()
{
$query = $this->db->query("SELECT email FROM change_password");
$result = $query->result_array();
return $result;
}
view:
foreach ( $dbemail as $new_dbemail )
{
echo $new_dbemail['database_field'];//in here you can get your table header.
//Ex if your table has name field and you need to sho it you can use $new_dbemail['name']
}
but i want to get $new_dbemail['name'] in my controller, how i can get the value of $new_dbemail['name'] in controller rather than view ???
To do so... you have to fetch data into a row_array not in result_array in Model.
Controller
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();
$database_field = $data['dbemail']['database_field']; //to get result in controller
$date['database_field'] = $data['dbemail']['database_field']; //to get result in view
$this->load->view('home', $data);
}
Model
public function email()
{
$query = $this->db->query("SELECT email FROM change_password");
return $query->row_array();
}
View
<p> Database Field value : <?=$database_field;?></p>
You are Calling the Model In A Variable :-
$data['dbemail']=$this->login_model->email();
So in this you have the results which you are fetching from Database. Than you passing this variable data to your View.
$this->load->view('home', $data);
So you already have the value in controller which u fetched from model which is stored in $data.
You can View the Values of $data , using
echo "<pre>";
print_r($data);
and it will show what data you are receiving
Try like this...
In controller...
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$dbemail=$this->login_model->email();//array containing all emails
//For each loop here for displaying emails
foreach($dbemail as $email){
$mail = $email; //assigning to variable
echo $mail."<br/>";//displaying email
}
$data['dbemail'] = $dbemail;//for passing to view
$this->load->view('home', $data); //passing your value to view
}
First you need to fetch the name from the database in the model. Change the query line:
$query = $this->db->query("SELECT name, email FROM change_password");
Then to use the name data in the controller you could try this:
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();
$name = $data['dbemail']['name']; // example usage to retrieve name in controller
$this->load->view('home', $data);
}
Now the name data is available as $name in controller.
Controller:
public function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemails']=$this->login_model->email();
$this->load->view('home', $data); //passing your value to view
}
Model
public function email()
{
$this->db->select("email"); // the row of the table you want to select.
$this->db->get('change_password'); //table name. you can still use your query, its just much neat when you use a specific query builder.
$result = $this->result(); //giving you a result of type object
return $result;
}
View
foreach ( $dbemails as $dbemail )
{
echo $dbemail->email; // the '->' will be the identifier of which row you want to get or display, i assume its email beacause you specify it in the model.
}
Hope this what your looking for.

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..?

Redirect Loop error in CodeIgniter

I worked on this for a day. I get this same problem, but I don't understand.
<?php
class Users extends CI_Controller
{
function index()
{
redirect('Users/login');
}
function login()
{
$data['title'] = 'Selamat datang di Sistem Informasi Koperasi';
$data['main_content'] = 'login';
$this->load->view('Users/template', $data);
}
function logout()
{
$this->session->sess_destroy();
$this->session->set_flashdata('info_login', 'Anda sudah keluar dari sistem');
redirect('Users/login');
}
function validate()
{
//Load User Model
$this->load->model('Users_Model');
//Validate User
$query = $this->Users_Model->validate();
if($query != '') {
//Mengambil Roles dari Groups
$roles = $this->Users_Model->roles($query->group_id);
//$this->login_model->last_login($query);
$data = array(
'username' => $query->username,
'roles' => $roles,
'is_logged_in' => true
);
$this->session->set_userdata($data);
if($roles == 'administrators') {
redirect('Administrators/index');
} elseif($roles == 'managers') {
redirect('Managers/index');
}
else {
$this->session->set_flashdata('info_login', 'Mohon maaf anda belum terdaftar sebagai Group! Silahkan hubungi admin!');
redirect('Users/login');
}
} else {
$this->session->set_flashdata('info_login', 'Maaf,username dan password yang anda masukkan salah,silahkan coba kembali!');
redirect('Users/login');
}
}
}
In Chrome and Firefox I get this message. What should i do?
This webpage has a redirect loop The webpage at
http://localhost/simpks/index.php/Users/login has resulted in too
many redirects. Clearing your cookies for this site or allowing
third-party cookies may fix the problem. If not, it is possibly a
server configuration issue and not a problem with your computer. Here
are some suggestions: Reload this webpage later. Learn more about this
problem. Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many
redirects.
this is my view template.php
<?php
$this->load->view('includes/header',$main_content);
$this->load->view('Users/'.$main_content);
$this->load->view('includes/footer');
?>
this is my model Users_Model.php
<?php
class Users_Model extends CI_Model{
function validate(){
$this->db->where('username',$this->input->post('username'));
$this->db->where('password',md5($this->input->post('password')));
$query = $this->db->get('Users');
if($query->num_rows == 1){
$row = $query->row();
return $row;
}
}
function roles($id){
$this->db->where('id',$id);
$query = $this->db->get('Groups');
if($query->num_rows == 1){
$row = $query->row();
return $row->name;
}
}
}
?>
use include instead loader if you call it in view.
ex : include 'includes/footer';
you don't have to put redirect('Users/login'); for session checking in your view class. Just erase it.
If you need redirect, put it in another page like users/test. If session is expired in users/test call redirect method in users/test controller. For better structure, i think you should minimize php function in view.
I am also facing that problem but both page's controllers i've redirects method so I add a refresh in redirect method, try it.
read at bottom of page CI redirect with refresh
<?php
class Users extends CI_Controller
{
function index()
{
redirect('another_controller/login');
}
}
Create another controller - another_controller.php
class another_controller extends CI_Controller
{
function login()
{
$this->load->view('home');
}
}

Array is not recognized in Codeigniter's View

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);
}

Fetching “id” from a view and pass to another view in Grocery Crud CodeIgniter

I am new in Grocery Crud with Code Igniter and need a help. I have table vaboteni (emploees) and it work well. But I get stuck with code add more action. When I click to add action button I got error 404 Page Not Found. I want to fetch "id" from one row in table and pass to another view in order to display data for only one employee. I have site in local server, address localhost/bis_resursi/index.php/vraboteni/vraboteni_managment
Here is my Controller vraboteni.php
function vraboteni_management()
{
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('vraboteni');
$crud->set_subject('вработен');
.....
$crud->add_action('Преглед', '', 'vraboteni/vraboten_managment/pregled','ui-icon-plus');
function pregled($id)
{
$this->load->model("vraboteni_pregled_model");
$data["result"] = $this->getVraboteniPregled($vrabotenID);
$this->load->view("pregled", $data);
}
$output = $crud->render();
$this->_example_output($output);
}
and Models: vraboteni_pregled_model.php
<?php
class Vraboteni_Pregled_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
function getVraboteniPregled($id){
$query = $this->db->query("SELECT * FROM vraboteni WHERE vraboteID = '$id' ");
return $query->result();
}
and in view vraboten_view.php I put
<?=$query['vrabotenID']?>
<br>
Hi, I'am <?=$query['ime']?>
<br>
from<?=$query['adresa']?>
I managed to find solution. Right code is:
Controller vraboteni.php
$crud->add_action('Преглед', '', 'vraboteni/get','ui-icon-plus');
$output = $crud->render();
$this->_example_output($output);
}
function vraboteni()
{
$crud = new grocery_crud();
$crud->set_table('vraboteni');
$output = $crud->render();
print_r($output);
}
function getall()
{
$this->load->model('vraboten_model');
$data['query']=$this->vraboten_model->vraboten_getall();
$this->load->view('vraboten_view',$data);
}
function get($vrabotenID)
{
$this->load->model('vraboten_model');
$data['query']=$this->vraboten_model->vraboten_get($vrabotenID);
$this->load->view('vraboten_view',$data);
}
Models vraboten_model.php
<?php
class Vraboten_model extends CI_Model{
function vraboten_model(){
parent::__Construct();
}
function vraboten_getall(){
$this->load->database();
$query=$this->db->get(' vraboteni');
return $query->result();
}
function vraboten_get($vrabotenID){
$this->load->database();
$query=$this->db->get_where(' vraboteni',array('vrabotenID'=>$vrabotenID));
return $query->row_array();
}
}
and view vraboten_view.php
<?=$query['vrabotenID']?>
<br>
Hi, I'am <?=$query['ime']?>
<br>
from<?=$query['adresa']?>

Resources