Undefined variable id in CodeIgniter - php

I have this todo list project.
My model
class Model_lists extends CI_Model {
function Model_list(){
parent::__construct();
}
function list_get($id){
$this->load->database();
$query = $this->db->get_where('lists', array('id'=>$id));
return $query->row_array();
}
My Controller
public function view_list($id){
$this->load->model('model_lists');
$data["query"] = $this->model_lists->list_get($id);
$this->load->view('lists/view_list',$data);
}
List view
<?php
echo $query['list_by'];
?>
However, when I access the view I get 2 PHP errors
1) Message: Missing argument 1 for Lists::view_list()
2) Message: Undefined variable: id
This is how I call the list:
<a class="view_list" href="<?php echo site_url("lists/view_list?lid={$row->list_id}");?>"><i class="icon-eye"> </i></a>

Your site url is not correct. The error is because no id is being passed and in CI (unless you have set to use query arrays) then the url is of the format www.example.com/controller/method/argument
So try:
href="<?php echo site_url('lists/view_list/'.$row->list_id); ?>"
In the docs:
http://www.codeigniter.com/user_guide/helpers/url_helper.html#site_url

Related

How to pass argument for function in controller from view

I have function in controller with one argument and now I want to pass value for it from view through url.
courses.php
public function index($id){
print_r();
exit;
}
coursesview.php
<a class="jscroll-next" href="<?php echo base_url(); ?>/courses/index?page=<?php echo $nextPage; ?>&id=<?php echo $parentId ?>">next page</a>
How it possible?
From the user guide,
Simply put the parameter in an array and pass it to the view by using the second parameter of load->view().
Afterwards, in the view, the variable will be available as the key.
In this example: $id or $anothervar.
courses.php
public function index($id){
$data = array('id' => $id, 'anothervar' => 'yeahi');
$this->load->view('coursesview', $data);
// exit;
}
coursesview.php
echo $id; // the ID
echo $anothervar; // "yeahi"
You are going to use minimum arguments in URL, here is my suggestions
1.<a class="jscroll-next" href="<?php echo base_url('courses/$nextPage/$parentId/'); ?>">next page</a> //controller followed by arguments - if you call index method
(OR)
2.<a class="jscroll-next" href="<?php echo base_url('courses/methodname/$nextPage/$parentId/'); ?>">next page</a> //controller,method name and followed by arguments
How to get those arguments in controller
Example:
$nextPage = $this->uri->segment(3);
$parentId = $this->uri->segment(4);

cant get passed parameter by url in php

please help me, this is simple thing but I don't know why this still keep error for an hour,
on my view I've got :
<a href="admin/editProduct?idd=$id">
on my controller that directed from above :
public function editProduct(){
$data["id"] = $_GET['idd'];
$data["produk"] = $this->model_get->get_data_list(2);
//this below doesn't work either, i just want to past the parameter to my model
//$data["produk"] = $this->model_get->get_data_list($_GET['idd']);
$this->adminHeader();
$this->load->view("adminPages/editProduct", $data);
$this->adminFooter();
}
I can not use the array id. It keeps telling me undefined variable idd.
I don't know what to do anymore, please anyone help!
I am using Codeigniter framework
Change your view as:
<a href="admin/editProduct/<?php echo $id;?>">
And in the controller, either get the id as parameter,
public function editProduct($id) {
}
or as uri segment
public function editProduct() {
$id = $this->uri->segment(3);
}
Change your link (in view) with this
<a href="admin/editProduct/$id">
And change your controller as
public function editProduct($id) {
}
Then user $id inside your controller
Make the href link as follows:
...
And edit the controller like this:
public function editProduct($id){
...
$this->model_get->get_data_list($id);
}
Where $id will be the passed $id.
make
try this this works fine
public function editProduct()
{
$id=$this->uri->segment(3);
$data["produk"] = $this->model_get->get_data_list($id);
$this->adminHeader();
$this->load->view("adminPages/editProduct", $data);
$this->adminFooter();
}

Trying to get property of non object is the error what iam getting in codeigniter

I want to display individual values passed from controller to view page but when i try to do so iam getting error has TRYING TO GET PROPERTY OF NON OBJECT so request ci developers to help me
This is my controller page
$data['showdata'] = $this->searchresultss->login($per_page,$look,$age, $age_to,$age_from,$se_ct,$subsect,$coun_try,$sta_te, $ci_ty,$qualification);
$this->load->view('searchresult',$data);
**This is my view page**
<?php
echo "<pre>";
print_r($showdata);
if (isset($showdata)){
foreach ($showdata as $key) {
?>
<?php echo($key->gender); ?>// This is the line where iam getting error
<?php
}
}
?>
**Here is my model page**
<?php
Class Searchresultss extends CI_Model
{
function login($per_page=3,$look,$age,$age_to,$age_from,$se_ct,$subsect,$coun_try, $sta_te, $ci_ty,$qualification)
{
$query="SELECT *
FROM users
";
$data=array();
$query=$this->db->query($query);
$data['results']=$query->result_array();
$data['count']=$query->num_rows();
$data['pages']=ceil($data['count']/3);
return $data;
}
}
I am geting error has trying to get property of non object. I am trying to print individual values passed from controller to view page ..but I am not able to do so.
You are not load the model in controler. Load the model searchresultss in controller before calling the model function. Like,
$this->load->model('searchresultss');
And
Change
echo($key->gender);
To
echo $key['gender'];
This will solve the issue
your echo is not wrong.
but i guess you didnt take the result of your query so:
try this :
if (isset($showdata)){
foreach ($showdata->result() as $key){ // just add ->result() after $showdata
echo($key->gender); ?>
}
}

Zend getting database record false

I have a link http://mywebsite.com/referral/dafa60b2b96c366e165be58649755742
With a parameter dafa60b2b96c366e165be58649755742
Was try to get that parameter to connect to database to get that user information, but it end up getting no object: Notice: Trying to get property of non-object in line.....
here's my code:
application.ini:
resources.router.routes.referralSelectProduct.route = "/referral/:url"
resources.router.routes.referralSelectProduct.defaults.controller = Index
resources.router.routes.referralSelectProduct.defaults.action = referral-select-product
resources.router.routes.referralSelectProduct.reqs.url = "[0-9a-zA-Z]+"
controller:
public function referralSelectProductAction()
{
$referral_url = $this->getRequest()->getParam('url');
$user = $this->_helper->model('Users')->fetchRowByFields(array('url' => $referral_url));
setcookie('referral_url', $referral_url, time() + 3600*24*30, '/');
}
model:
class Application_Model_DbTable_Users extends Sayka_Db_Table_Abstract{
protected $_rowClass = 'Application_Model_DbTable_Row_User';protected $_name = 'users';}
view:
<div id="content_bottom" class="content"><br/>
<h1>Select the product to refer your friend <span class="colorset_orange"><?php echo $this->user()->first_name; ?></span>.</h1>
<div><a class="refer_submit" href="?email=<?php echo $this->user()->email ?>&first_name=<?php echo $this->user()->first_name; ?>&last_name=<?php echo $this->user()->last_name; ?>" id="btn_free_download">Free Download</a></div>
<div><a class="refer_submit" href="#" id="btn_buy_now">Buy Now</a></div>
My view end up getting no object of that user in the database.
If i remember it correctly (for zend framework 1.*):
// Controller
...
$this->view->user = $this->_helper->model('Users')->fetchRowByFields(array('url' => $referral_url));
// View
echo $this->user->email;
Make sure that this $this->_helper->model('Users')->fetchRowByFields(array('url' => $referral_url)) returns you what you expect. var_dump it in controller

Query not passing with View in Codeigniter

I have the foloowing controller (supernavigationloggedin):
<?php
class Supernavigationloggedin extends CI_Controller {
function index(){
#get current session id
$currentSessionID = $this->session->userdata('session_id');
#get all the account row for the given sessionID
$data['info'] = $this->db->get_where('Client', array('session_id'=>$currentSessionID))->row();
#views
$this->load->view('supernavigationloggedin',$data);
}
}
?>
and the following view named(supernavigationloggedin):
<div id="superNavigation">
<h5><strong>Welcome</strong>, <?php $info->fname; ?> Account Settings</h5>
<div class="clearL"> </div>
</div
>
It keeps throwing an error on line:<h5><strong>Welcome</strong>, <?php echo $info['fname']; ?> <a href="#">Account that Message: >>> Trying to get property of non-object
I've tried : <?php echo $info['fname']; ?> <?php echo $info->fname; ?> but neither seem to work.
It's because $info  is empty and no database request are made. You have to do this this way if you want your query to return an object:
$data['info'] = $this->db->get_where('Client', array('session_id'=>$currentSessionID))->row();
Or this way, if you prefer to get it into an array:
$data['info'] = $this->db->get_where('Client', array('session_id'=>$currentSessionID))->row_array();
This way it should work. row() or row_array() is necessary to execute your query.

Categories