Zend getting database record false - php

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

Related

Can't set CGI Parameter (PHP)

I'm new to PHP and have a problem. I have a navbar on the side of a homepage that displays 4 links. I'm supposed to write a method setWhichPage in a child class that will access a CGI parameter named 'whichpage' from a parent class and use it to determine which of the 4 linked pages will display in the main section of the homepage, using another method called getMainFunction. The navbar was created with the methods getLeftNavBar and createNavbarArray, which exist in the child class (called TravelAgent), but get their variables from the parent class (called Company). The method setWhichPage is supposed to be in this child class also. I'm supposed to use the value $_GET or $_REQUEST array to set the value in the property $whichpage. The acceptable values for the $whichpage property should match the getLeftNavBar method. The methods getLeftNavBar and setWhichPage work together so that when the user clicks on a link in the navbar the URL they are supposed to create these links:
http://amazingadventures.000webhostapp.com/?whichpage=home
http://amazingadventures.000webhostapp.com/?whichpage=sales
http://amazingadventures.000webhostapp.com/?whichpage=support
http://amazingadventures.000webhostapp.com/?whichpage=contact
Where I'm having trouble is when I try to get setWhichPage to set the variable $whichpage to one of the four values in the links, I get this error message:
Notice: Undefined index: whichpage in /storage/ssd1/873/8888873/public_html/index.php
Can you tell me what I'm doing wrong? Thanks in advance for any help. Here's the relevant code:
<?php
class Company { //start of parent class Company
protected $company_name;
protected $company_address;
protected $company_url;
protected $company_email;
protected $whichpage;
public function __construct(){
$this->company_name = "Amazing Adventures Travel";
$this->company_address = "13999 Turtle Way, Los Angeles, California, 90001";
$this->company_url = "http://amazingadventures.000webhostapp.com/";
$this->company_email = "email.edu";
$this->whichpage = "home";
}
//other methods...
} //end of parent class Company
class TravelAgent extends Company { //start of child class TravelAgent
var $navbar_array;
function create_navbar_array() {
$mainurl = $this->company_url;
$this->navbar_array = array("Home Page"=>"$mainurl?whichpage=home", "Sales"=>"$mainurl?whichpage=sales",
"Support" => "$mainurl?whichpage=support", "Contacts" => "$mainurl?whichpage=contact");
}
function getLeftNavBar() {
echo "<table border=1, td width=100, height = 40><tr>";
foreach($this->navbar_array as $x => $x_value) {
echo '<tr><td>'. $x ."</td></tr>";
echo "<br>";
}
echo "</tr></table>";
}
function setWhichPage(){
$whichpage = $_GET['whichpage'];
}
function getMainSection() {
echo "The ".$whichpage." page";
}
}
//end of child class TravelAgent
$travelobject = new TravelAgent(); //object creation
$travelobject->getHeader(); //function calls
$travelobject->create_navbar_array();
$travelobject->getLeftNavBar();
$travelobject->setWhichPage();
$travelobject->getFooter();
?>
<table style='width:100%' border='1'> //HTML
<tr>
<td style='width:15%'>Left Navigation Bar</td>
<td> getMainSection()</td> //function call
</tr>
</table>
You have to reference it with $this->whichpage and check for $_GET['whichpage'] if is set. Something along the lines like this:
function setWhichPage(){
if(isset($_GET['whichpage'])) {
$this->whichpage = $_GET['whichpage'];
}
}
function getMainSection() {
echo "The ".$this->whichpage." page";
}
Notice: Undefined index: whichpage in /storage/ssd1/873/8888873/public_html/index.php
This error means $_GET array does not have the key whichpage.
In your case if you open http://amazingadventures.000webhostapp.com/ the $_GET array will be empty.
The best practice is to check whether the key exists before accessing.
Example of setWhichPage:
$this->whichpage = isset($_GET['whichpage']) ? $_GET['whichpage'] : $this->whichpage; // "home" by default

Undefined variable id in CodeIgniter

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

Display an array data from two tables in codeigniter

I've two tables on my database, monitor [pk = idMonitor] and monitor_data [pk = idMonitor_data].
Please click you can see the tables fields here. As you can see i put the array data in table monitor_data.
I want to Update the condition for every idinventory where monitor_data.idMonitor = $id.
But first i want to display the current data of 'monitordate','idinventory', and 'condition' from database to my view.
My controller
public function edit($id=0) {
$dataa = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->data->monitordate = $dataa->monitordate;
$this->data->condition = $dataa->condition; <-line 20
$this->data->detail = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->template->set_title('SMIB | Monitoring')
->render('monitor_edit',$this->data);
}
My View (monitor_edit)
<?php echo form_open(site_url("monitor/ubah"),'data-ajax="false"'); ?>
<?php foreach ($detail as $items): ?>
<h4><?php echo '[ '.$items['idinventory'].' ] '?> </h4>
<?php echo form_label ('Condition : ');
echo form_dropdown('condition', array('good'=>'Good','broke'=>'Broken','lost'=>'Lost'),#$items['condition']);
?>
<?php endforeach; ?>
<?php echo form_close(); ?>
My Model
class Monitor_m extends MY_Model {
public function __construct(){
parent::__construct();
parent::set_table('monitor','idMonitor');
}
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
$data = parent::get_array();
return $this->improve_data($data);
}
Here is my problem : its work fine for monitordate code in my controller, BUT i keep getting an error for condition code
Maybe because i use 'monitor_data.idMonitor' as my parameter $id not idinventory. how can i use 2 parameters for example like where idMonitor=$id and idinventory=$idiventory.
Do i explain it right ?
Severity: Notice Message: Trying to get property of non-object
Filename: controllers/monitor.php Line Number: 20
Please Please help me, i dont know what is wrong with my controller :( i've searching the solution but none of those work. :(
It's weird if you can get monitordate but can't get condition.
Can you edit your controller to be like this?
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
// $data = parent::get_array();
$data = $this->db->result_array();
print_r($data);
echo $this->db->last_query();
exit;
return $this->improve_data($data);
}

Codeigniter passing data controller to view

Here is my controller:
class CommonController extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('common_model'); //load your model my model is "common model"
}
public function add_work(){
$names = $_POST['name'];
$works = $_POST['work'];
$allValues = array(); // array to contains inserted rows
foreach($names as $key => $name){
$name= "your specified name";
$insertdata = array();
$insertdata['work'] = $works[$key];
$insertdata['name'] = $name;
$this->common_model->insert($insertdata);
array_push($allValues,$insertdata);
//$insert = mysql_query("INSERT INTO work(name,work) values ( '$name','$work')");
}
foreach($allValues as $insertRow){
echo $insertRow['work'];
echo $insertRow['name'];//this shows data well. but how to pass data in view.php
}
//view code will add here to show data in browser
}
Basically I want to pass all data to view.php for printing or exporting purpose. How can I do so.
To load a view you should do like this.
$this->load->view("filename");
If you want to pass data to view, you should do like this.
$this->load->view("filename",$data);
$data should have all parameters which you want to print in view.
The syntax goes like this.
$this->load->view("filename","data to view","Returning views as data(true / false");
If third parameter is true, view will come as data. It will not go to browser as output.
Edit:
Change
$this->load->view('print_view',$insertdata);
to
$data['insertdata'] = $insertdata;
$this->load->view('print_view',$data);
For more info, check this link
How CI Classes Pass Information and Control to Each Other
Calling Views
We will see.how the controller calls a view and passes data to it:
First it creates an array of data ($data) to pass to the view; then it loads and calls the view in the same expression:
$this->load->view('testview', $data);
You can call libraries, models, plug-ins, or helpers from within any controller, and models and libraries can also call each other as well as plug-ins and helpers.
However, you can't call one controller from another, or call a controller from a
model or library. There are only two ways that a model or a library can refer back to a controller:
Firstly, it can return data. If the controller assigns a value like this:
$foo = $this->mymodel->myfunction();
and the function is set to return a value, then that value will be passed to the variable $foo inside the controller.
//sample
public function display()
{
$data['text_to_display'] = $this->text_to_display;
$data['text_color'] = $this->text_color;
$this->load->view('display_view',$data);
}
Adding Dynamic Data to the View
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view
loading method. Here is an example using an array:
$data = array(
’title’ => ’some’,
’heading’ => ’another some’,
’message’ => ’and another some’
);
$this->load->view(’view’, $data);
And here’s an example using an object:
$data = new Someclass();
$this->load->view(’view’, $data);
Sending Multiple Dimensional array
if we pull data from your database it will typically be
in the form of a multi-dimensional array.
<?php
class foo extends CI_Controller {
public function index()
{
$data[’Books’] = array(’POEAA’, ’TDD’, ’Clean C’);
$data[’title’] = "Title";
$data[’heading’] = "Heading";
$this->load->view(’view’, $data);
}
}
in view
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $heading;?></h1>
<h3>My Books List</h3>
<ul>
<?php foreach ($Books as $item):?>
<li><?php echo $item;?></li>
<?php endforeach;?>
</ul>
</body>
</html>
More Learning
NOTE:
There is a third optional parameter lets you change the behavior of the method so that it returns data as a string rather
than sending it to your browser.The default behavior is false, which sends it to your browser. Remember to
assign it to a variable if you want the data returned:
$string = $this->load->view(’view’, ’’, TRUE);
Above will not solve your problem directly but definetly help in understanding concepts.

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