I am trying to generate a table to display my websites enquiries and add a column containing links(actions) for each of the enquiries.
Upon creating my table I am receiving the following error:
Message: Trying to get property of non-object (line 55)
My controller is as follows:
$this->table->set_heading('ID', 'Name', 'Surname', 'Email', 'Phone','Message','Date','Actions');
$enquiries = $this->contact_model->get_table_enquiries($per_page,$offset);
foreach($enquiries as $row) {
$links = anchor('admin/enquiries/edit/' ,'Edit');
$links .= anchor('admin/enquiries/delete/', 'Delete');
$this->table->add_row(
$row->id, //line 55
$row->first_name,
$row->last_name,
$row->email_address,
$row->phone_number,
$row->message,
$links
);
}
$viewdata['enquiries_table'] = $this->table->generate();
And the function in the model that gets the results:
function get_table_enquiries($per_page,$offset)
{
$this->db->order_by('date','desc');
$query=$this->db->get('contact',$per_page,$offset);
return $query;
}
How can I get my foreach loop to work and create the necessary rows and append the links???
Why am I receiving the error message?
I think you need to try
foreach ($enquiries->result() as $row)
function get_table_enquiries($per_page,$offset)
{
$this->db->order_by('date','desc');
$query=$this->db->get('contact',$per_page,$offset);
return $query->result(); //do this
}
Related
I have an error in my controller. I am trying to use the result coming from model function to call another function in my model. I am using Codeigniter Framework. hope you can help me out. Thanks
Controller:
function photographer_campaign_details(){
$camp_id = $this->uri->segment(4);
$data['page_title'] = 'Photographer Campaign Details';
$adminId = $this->session->userdata('adminid');
$campaign = $this->Admin_model->get_all_photographer_campaign_details($camp_id);
$data['seller'] = $this->Admin_model->get_seller_name_by_id($campaign['uid']);//error is here: Undefined index: uid
$data['campaign'] = $campaign;
$this->load->view('admin/photographer_campaign_details',$data);
}
My Model:
function get_all_photographer_campaign_details($camp_id) {
$this->db->select('*');
$this->db->where('campaign_id',$camp_id);
$query = $this->db->get('ps_campaigns');
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row) {
$data[] = $row;
}
return $data;
}
return array();
}
//get seller name by id
function get_seller_name_by_id($uid)
{
$this->db->select('firstname, lastname');
$this->db->where('id', $uid);
$query = $this->db->get('ps_users');
//return $query->row();
return $query->result_array();
}
an error is coming from the controller: Undefined index: uid
Looking at your get_all_photographer_campaign_details, if no rows are found then you return an empty array.
In your controller, you never check to see if a valid entry was found. As a result you get your undefined index: uid when an id is referenced in the URL that doesn't correspond to an entry, because $campaign is empty and doesn't have a uid key. Try something like this:
function photographer_campaign_details(){
$camp_id = $this->uri->segment(4);
$data['page_title'] = 'Photographer Campaign Details';
$adminId = $this->session->userdata('adminid');
$campaign = $this->Admin_model->get_all_photographer_campaign_details($camp_id);
if (!$campaign ){
show_404();
}
$data['seller'] = $this->Admin_model->get_seller_name_by_id($campaign['uid']);//error is here: Undefined index: uid
$data['campaign'] = $campaign;
$this->load->view('admin/photographer_campaign_details',$data);
}
Additionally, you are returning data wrong in the event that you do find data. Namely this bit in get_all_photographer_campaign_details:
foreach ($query->result_array() as $row) {
$data[] = $row;
}
Should be something like:
foreach ($query->result_array() as $row) {
$data = $row;
break;
}
The problem is that you are appending the row as one row in $data, but your controller is expecting to get the actual data itself. I.e. your controller is expecting this:
[
'campaignid' => 1,
'uid' => 'ijerjeire'
]
But you are returning this:
[
[
'campaignid' => 1,
'uid' => 'ijerjeire'
]
]
Note the extra array that everything is wrapped around. Basically, your model is returning an array of results, when your controller is just expecting results. My above suggestion will work if there is only ever supposed to be one campaign returned. If that is not the case, then you need to adjust your controller instead of your model method.
To reiterate my other point: make sure and validate the user input that comes from the URL. Otherwise you will return PHP errors instead of 404's.
I'm using Magento which is on the zend framework and the following code currently outputs the first row matching the criteria is_read != 1', 'is_remove != 1'. I need to modify this code to output the last 4 table rows that matches said criteria. I tried a few things but none worked. Please Help!
ModuleName/Model/Resource/
public function loadLatestNotice(Mage_AdminNotification_Model_Inbox $object)
{
$adapter = $this->_getReadAdapter();
$select = $adapter->select()
->from($this->getMainTable())
->order($this->getIdFieldName() . ' DESC')
->where('is_read != 1')
->where('is_remove != 1')
->limit(1);
$data = $adapter->fetchRow($select);
if ($data) {
$object->setData($data);
}
$this->_afterLoad($object);
return $this;
}
Here are some other codes that are used...
ModuleName/Model/
public function loadLatestNotice()
{
$this->setData(array());
$this->getResource()->loadLatestNotice($this);
return $this;
}
ModuleName/Block/
public function getLatestNotice()
{
return $this->_getHelper()
->getLatestNotice()->getTitle();
}
Template/
href="<?php echo $latestNoticeUrl ?>" onclick="this.target='_blank';"><?php echo $this->getLatestNotice() ?>
I was able to solve the problem myself, by using the following method.
The first thing i tried to produce is 4 notification table rows instead of 1, is to change ->limit(1); to ->limit(4); and $adapter->fetchRow($select); to $adapter->fetchAll($select);. The issue is, the solution requires more than just changing these 2 values.
ModuleName/Model/Resource/
public function loadLatestNotice(Mage_AdminNotification_Model_Inbox $object)
{
$adapter = $this->_getReadAdapter();
$select = $adapter->select()
->from($this->getMainTable())
->order($this->getIdFieldName() . ' DESC')
->where('is_read != 1')
->where('is_remove != 1')
->limit(4);
$data = $adapter->fetchAll($select);
if ($data) {
$object->setData($data);
}
$this->_afterLoad($object);
return $this;
}
After changing this, the template will stop outputting information, In order for the template to output the new array, you must duplicate some code and remove ->getTitle() line in the block file, then change a few line of codes in the template .phtml file as follows.
ModuleName/Block/
public function getNewFuncName()
{
return $this->_getHelper()
->getLatestNotice();
}
Template/
<?php
$notice = $this->getNewFuncName();
foreach ($notice as $item) {
foreach ($item as $value) {
echo '<div class="notemssg"><p id="notetitle" href='.$value['url'].' >'.$value['title'].'</p><p id="notedate">'.$value['date_added'].'</p></div>';
}
}
?>
Changing the code to properly call and display the array will result it 4 table rows being displayed. the code can be modified to be used and any way you would like to display the info on the fronted.
Hope this helps Someone!
I am facing the following error:
Trying to get property of non-object and Undefined variable php
errors in my code
Controller:
function showDoctorInformation(){
$this->load->model('PatientModel');
$data['doctorinfo'] = $this->PatientModel->getDoctorInformation();
$this->parser->parse('patient_msgview', $data);
}
Model:
function getDoctorId() {
$this->db->from('person');
$this->db->select('doctorId');
$doctorId = $this->db->get()->result();
return $doctorId;
}
function getDoctorInformation() {
$doctorId = $this->getDoctorId();
$this->db->from('DoctorInfo');
$this->db->where('doctorId', $doctorId);
$this->db->select('name', 'surname', 'Bio', 'Address', 'img');
$doctorinfo = $this->db->get()->result();
return $doctorinfo;
}
View:
<?= $doctorinfo->name ?>
I have displayed information from the database before with this method and I can't see the error now.
result() return
This method returns the query result as an array of objects, or an
empty array on failure
So you need to fetch single data form your database using ->row()
function getDoctorId() {
$this->db->select('doctorId');
$this->db->from('person');
$this->db->select('doctorId');
$query = $this->db->get();
if ($query->num_rows == 1) {
$row=$query->row();// fetch single row
return $row->doctorId;// get doctor id
} else {
return FALSE;
}
}
And in viwe you have to get your data using foreach loop
For exm
foreach ($doctorinfo as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
I have just started using codeigniter, PHP and facing issue while trying to build UI dynamically by fetching data from DB:
Requirement:
I need to create a list of parent - child dynamically by reading info from DB.
I have two tables admin_menu and admin_menu_item. Admin menu contains parent menu options and admin_menu_item contains each parent's detail option. admin_menu_item has AD_MENU_ID column storing parent's id giving me hierarchical structure.
My Controller:
class Home extends CI_Controller {
public function index()
{
$this->load->helper('url');
$this->load->helper('array');
$this->load->model('Menu');
$header_data['base_url'] = base_url();
$data['menu'] = $this->Menu->get_admin_menu_data();
$data['menu_item'] = array();
foreach($data['menu'] as $row){
array_push($data['menu_item'],
$this->Menu->get_admin_menu_item_data($row->ad_menu_id));
}
$this->load->view('homepage/header');
$this->load->view('homepage/admin_menu',$data);
}
}
Here is the structure of the code
My Model:
class Menu extends CI_Model{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_admin_menu_data()
{
$this->db->select('ad_menu_id,ad_menu_name');
$query = $this->db->get('admin_menu');
return $query->result();
}
function get_admin_menu_item_data($menu_id)
{
$query = $this->db->query("SELECT ad_menu_item_name FROM ADMIN_MENU_ITEM WHERE AD_MENU_ID = " . $menu_id);
if( $query->num_rows() > 0 ){
return $query->result();
}
}
}
When I use $data variable in my view I get a blank parent row resulting in an extra item where parent doesn't have a value and I get following error
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: homepage/admin_menu.php
Line Number: 9
which is basically my view line where I access parent element
My View:
echo "<ul class=\"accordion\" id=\"accordion-1\">";
foreach ($menu as $key => $value) {
print "<li class=\"dcjq-current-parent\">"; //add li tag
print "" . $value->ad_menu_name . "\n";
print "<ul>";
foreach($menu_item as $key_item => $value_item){
print "<li><a href=\"#\">";
print $value_item->ad_menu_item_name . "</a></li>\n";
$action_type = '';
}//end of menu item for each
print "</ul></li>\n";
}//end of menu for each
print "</ul>";
Sorry, for the long post but I am really stuck here and cannot figure out what is wrong with this?
Update:
I was finally able to make it work ny changing the code in model and view. The issue was objects within objects causing the hierarchy to become too complex.
The final solution was to make just one database call and process data in the view.
Thanks Yan for working with me and guiding to the solution.
It seems you have two different objects in the $menu variable in the view which is not recommened. My suggestion is to send one of them as a second parameter to the view.
Notice how you are overwriting the data in '$data['menu']['menu_item']' with each iteration of the foreach loop.
In the controller:
$data['menu_item'] = array();
foreach($data['menu'] as $row){
array_push($data['menu_item'],
$this->Menu->get_admin_menu_item_data($row->ad_menu_id));
}
In the view:
foreach ($menu_item as $key => $value) {
print "<li class=\"dcjq-current-parent\">"; //add li tag
print "" . $value->ad_menu_name . "\n";
}
EDIT:
The solution was merging the SQL queries:
SELECT ADMIN_MENU_ITEM
.ad_menu_item_name FROM ADMIN_MENU_ITEM JOIN admin_menu ON ADMIN_MENU_ITEM.AD_MENU_ID = admin_menu.ad_menu_id
i'am have same problem
how to fix error Message: Trying to get property of non-object
if i'am inject "-" from URL.
to fix edit your artikel_model.php
function get_by_id($id){
$query = $this->db->get_where('tbl_artikel', array('id' => $id));
if ($query->num_rows() > 0){
return $query->row();
}else{
print_r('No Found Artikel');
error_reporting(0);
}
}
print_r('No Found Artikel'); <-- if URL not found show this message
error_reporting(0); <-- to hiden error report
./eoc
Hey guys !
Well I was trying out codeigniter but it seems to me that I have made some kind of mess while trying to retrieve and display the data from the tables
here is the code snippet.
I want to retrieve all the articles stored in my article table along with that I need to pull out all the tags associated with each article from the relationship table and the tag table called articleTagRelation and tags respectively
Table structure :
Article table : articleID, articleContent, date
Tags table : tagID, tagName
articleTagRelation : aricleID,tagID {Combination of both is my primary key}
CI model :
article_model.php
public function getAllTags($postId){
$this->db->select('articleTagRelation.tagId as tagId, articleTagRelation.postId as postId, article.tagName as tagName,');
$this->db->from('articleTagRelation');
$this->db->join('Tags','Tags.tagId = articleTagRelation.tagId');
$this->db->where('ArticleTagRelation.articleId',$postId);
$qTag = $this->db->get();
if($qTag->num_rows() > 0){
foreach ($qTag->result() as $tag) {
return $tag;
}
}
}
public function getAllArticles(){
$this->db->select('*');
$this->db->from('Article');
$this->db->order_by('date','desc');
$query=$this->db->get();
if($query->num_rows()>0){
foreach ($query->result() as $row) {
$data['row'] = $row;
$data['articletags'] = $this->getAllTags($row->articleId); // I'm trying to get get a array of all the associate tags.
$post=array($data['row'],$data['articletags']);
}
}else{
echo 'nothing found !';
}
return $post;
}
my controller file
article.php
I'm calling this function in the index function
$data['rows'] = $this->blog_model->getAllArticles();
and then loading the view by passing the data array
now the part where things get messy
in my view
echo $r->articleId // works fine
echo $r->articletags->tagId //gives me a error message
Can any one help me out in printing those tagIds
First you don't need the foreach at all to get the tag information, it comes back from a query_result.
like this...
if($qTag->num_rows() > 0){
return $qTag->result();
}
else {
return array(); //return empty array if no tags
}
Then to build your article, do this with getAllArticles()
public function getAllArticles(){
// removed uneccessary select and from
// the below accomplishes the same thing
$this->db->order_by('date','desc');
$query = $this->db->get('Article');
if ( $query->num_rows() > 0 ) {
// store the result in a variable you will end up returning
$articles = $query->result();
// make sure you foreach by reference so that changes you make
// to the interated $article will be made to the actual article
// result
foreach ($articles as &$article) {
// create a new property of your article "tags" and save an
// array of tags in it
$article->tags = $this->getAllTags( $article->articleId );
}
} else {
echo 'nothing found !';
}
return $articles;
}
The last thing to note is that when you now reference $r->tags that is an array, so you can foreach it to process all tags, OR reference an index like $r->tags[3]->tagId
if($qTag->num_rows() > 0){
foreach ($qTag->result() as $tag) {
$tags[] = $tag; //this create the array with the result
}
return $tags;
}
"$r->articletags->tagId" only works if you return results as an object, use "$r->articletags['tagId']" instead.