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
Related
I am writing a program that basically does stuff with a database, I am having trouble with some of the functionality though, this is what it is supposed to do
public function clist() {
$this->load->model('list_model');
$fields = $this->list_model->listcli();
if ($fields === $this->list_model->listcli()) {
$fieldl = $fields;
$this->load->view('clientlist');
$this->clientlist->display_clients($fieldl);
}
}
This loads the model which looks like this:
public function listcli()
{
$this->db->list_fields('clients');
}
}
Then runs the model function listcli so that it will list all the fields in the clients database and puts the value into $fields I then call it fieldl and load a view that will show the data, the view looks like this:
<html>
<body>
<p> sup </p>
<?php
function display_clients($fieldl)
{
?>
<html>
<body>
<p> sup2 </p>
<ul>
<?php
foreach ($fieldl as $l) {
?>
<li>
<?php echo $l;?>
</li>
<?php
}
}
Then calls the function inside the view and passed the data from $fieldl into it.
but I am getting the error " Call to a member function display_clients() on a non-object in /codeigniter/src/application/controllers/clientlist.php on line 40"
line 40 is
$this->clientlist->display_clients($fieldl);
Can you help? Please and thank you.
(I know this kind of question has been asked before but they are always very specific to the code at hand so doesn't help me, I am really new to CodeIgniter so if you can keep any answer relatively simple I will be grateful).
I needed to use the second parameter possible to pass my data over into the view, not my own makeshift solution for it, CI accepts an object as a second parameter for the CI_Loader::view()
Why can't you prepare the list in the controller and pass it to view via $data array?
$my_list = '<ul>';
foreach ($fieldl as $l) {
$my_list .= '<li>'.$l.'</li>';
}
$my_list .= '</ul>';
$data['my_list'] = $my_list;
then you can access it in view as $my_list. Just echo it where you need it.
EDIT:
AS per your own answer, yes - exactly. Using example above you would pass the $data to view as the second parameter:
$this->load->view('my_view',$data);
Then in the View, you access $data['my_list'] as $my_list, $data['foo'] as $foo etc.
I do not want to be asking a question that has already been answered, however I have done a ton of research and I am stuck. Any help would be appreciated.
I am trying to query and add database results to OpenCart's addproduct.tpl
In the MODEL file I have:
public function units() { //function that gets database info and returns it
$unit_variables = $this->db->query("SELECT unit FROM ". DB_PREFIX ."
weight_class_description");
if (!$unit_variables) {
die(mysql_error());
}
else {
foreach ($unit_variables->rows as $temp_var) {
print_r($temp_var['unit'], true);
}
}
}
In the CONTROLLER file I have:
$this->load->model('catalog/product'); //where my function is located
$this->model_catalog_product->units();
$weight_variables = units();
if (isset($this->request->post['weight_variables'])){
$this->data['weight_variables'] = $this->request->post['weight_variables'];
}
In the VIEW I have:
<?php echo $weight_variables ?>
I get the following error:
Call to undefined function units() in /path/to/controller/file on line etc.
Note: When I print_r($temp_var); instead of returning print_R($temp_var, true) and delete these lines of code $weight_variables = units(); if (isset($this->request->post['weight_variables'])){ $this->data['weight_variables'] = $this->request->post['weight_variables'] } in the controller file my model file will display the query results on the addproduct.tpl
units() is a METHOD of your object, yet you're calling it as a standalone regular function:
$weight_variables = units();
^^^^^^^
Shouldn't it be:
$weight_variables = $this->model_catalog_product->units();
instead? And note that as-written, your method doesn't actually return anything, so $weight_variables will simply get assigned null..
So I'm playing around with CodeIgniter for a bit. Right now I'm trying to build a "page overview" page. I got another page where i can create pages, and i want to display those pages in an overview.
Right now i my model i got the following:
public function page_list(){
$query = $this->db->get('pages');
if ($query){
$pageListing = "<table class='table table-striped'>";
foreach ($query->result() as $row){
$pageListing .= "<tr>";
$pageListing .= "<td>".$row->title."</td>";
$pageListing .= "<td>".$row->category."</td>";
$pageListing .= "<td>".$row->date."</td>";
$pageListing .= "<td><a href='delete_page/".$row->id."'>delete</a></td>";
$pageListing .= "<td><a href='edit_page/".$row->id."'>edit</a></td>";
$pageListing .= "</tr>";
}
$pageListing .= "</table>";
} else {
$pageListing = "There are no pages to display";
}
}
I know it's not the best way of doing it. I'm obviously writing html code that belongs in the view in a model. Right now i don't bother about that. I'm just trying to figure out how to use the variable $pageListing in my controller, and from there load in into a view.
I tried using the variable in the following way:
public function displayPages {
$this->load->model('model_pages');
$this->model_pages->page_list();
echo $this->model_pages->pageListing;
}
When i can do this successfully i can use $pageListing in my view.
Right now i get the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Main::$pageListing
Filename: core/Model.php
Line Number: 51
As i said before, i know this is not the best practice, to code html in the model, and echo it from the controller. This is pure for experimenting, and I'm just curious how i can make this work. Since i will have to work with other variable from the model in the future, and that will work the exact sameway, except they won't contain whole html codes.
Return the $pageListing from the model back to the controller. Add this line at the end of your model function:
return $pageListing;
Edit:
public function displayPages {
$this->load->model('model_pages');
echo $this->model_pages->page_list();
//echo $this->model_pages->pageListing;
}
OR
public function displayPages {
$this->load->model('model_pages');
$data['pageList'] = $this->model_pages->page_list(); //assign to a variable
$this->load->view('pagelist', $data); //is a file pagelist.php in your views folder
}
Now if you do the second way(correct way) in your pagelist.php, you will get the variable $pageList. Then just echo $pageList in your viewfile.
I'm new to PHP and CodeIgniter, and saw that there is many questions mentioning this and been trying them all but nothing seems to work. Everything is auto-loaded in configuration, the database is running and function for posting to database are working but writing to view page doesn't work at all. Except for displaying username, but for that I create a new variable on view page.
Controller
public function ShowNews()
{
$data = array();
$this->load->model('user');
$data['news'] = $this->user->getNews();
$this->load->vars($data);
}
Model
function getNews(){
$q = $this->db->get('News');
if($q->num_rows() > 0){
return $q->result();
}
return FALSE;
}
View
<?php foreach($news as $row) : ?>
<li><?php echo $row->Title; ?> </li>
<li><?php echo $row->Date; ?></li>
<?php endforeach; ?>
This is the error EDIT ves to news
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: news
Filename: admin/Pocetna.php
Line Number: 64
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: admin/Pocetna.php
Line Number: 64
Using WAMP, NetBeans with CodeIgniter. I saw it has to be something with passing data from controller to view but I can't figure it out and been trying few days already, but always having problems.
You're not passing $data to your view. Your controller should be like this:
public function show_news()
{
$this->load->model('user');
$data = [];
$data['news'] = $this->user->get_news();
$this->load->view('news', $data);
}
Your view should also be checking if $news is FALSE, because you'll have some issues with foreach if you loop over the value FALSE. Your model should also be returning result_array not result, foreach cam't loop over objects..
public function get_news()
{
$q = $this->db->get('News');
return($q->num_rows() > 0) ? $q->result_array() : FALSE;
}
Your view should look something like this:
<?php
if($news !== FALSE)
{
foreach($news as $row)
{
echo "<li>{$row['title']}</li>";
echo "<li>{$row['date']}</li>";
}
}
else
{
echo "No news to see here!";
}
?>
Your title also doesn't link up with the error in the post, so that's the solution to the one in the title.
Hi I've recently started yet another project and my boss is insisting that we use the MVC model, the problem being that due to many articles showing different ways to do this we havent managed to agree on what a proper MVC model should look like.
So here's my problem for this project (whether this is the correct way to do it or not) I am using the following baseline rules
Controller classes manage both getting the data from the model classes and passing the data to the view classes and retrieving the view and displaying it
Model classes managhe all database actions and return the data using mysql_fetch_assoc
View classes create the views using the data etc.
So my issue is with processing the information from mysql_fetch_assoc normally you would do something like this (assuming we have already run a query)
while ($row = mysql_fetch_assoc($result)) {
echo $row["username"];
}
but as I'm processing the results in the view class rather than the model how do I cycle through all of the results when I have already passed the assoc array to the view, currently I'm getting a problem where it keeps looping through the results until it hits a memory size error so for some reason it isn't able to figure out how many results it needs to cycle through
My current code snippets are below sorry for the bad explainations.
Controller
require_once 'admin_model.php';
require_once 'admin_view.php';
class admin_controller {
public $model;
public $view;
public function __construct() {
$this->model = new admin_model;
$this->view = new admin_view;
}
public function get_group_view() {
$in_model = $this->model->get_group_view();
$in_view = $this->view->get_group_view ($in_model);
echo $in_view;
}
Model
class admin_model {
public function get_group_view() {
$query = mysql_query("
SELECT
group_id,
group_name
FROM
user_groups
");
return mysql_fetch_assoc($query);
}
}
View
class admin_view {
public function get_group_view($group_data) {
while($group_data) {
$output .= $group_data['group_id'] . '###' . $group_data['group_name'] . '<hr />';
}
return $output;
}
}
Which currently returns the error:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 133693393 bytes)
So can someone please advise me on the best way to go through the results without moving 'mysql_fetch_assoc' function from the model class?
PS I know I'm probably doing MVC completely wrong but it works for us and we don't want to have to research and change our code yet again thanks.
You should not return the MySQL Result - you should do:
$return = array();
$query = mysql_query("SELECT group_id, group_name FROM user_groups");
while($row = mysql_fetch_assoc($query)) {
$return[] = $row;
}
mysql_free_result($row);
return $return;
And you should fix the $group_data bug per #Roman_S . The correct use, along with the above code is
public function get_group_view($group_data) {
$output = '';
foreach($group_data as $group) {
$output .= $group['group_id'] . '###' . $group['group_name'] . '<hr />';
}
return $output;
}
Finally you should migrate to MySQLi or PDO if possible.
You have en error here
while($group_data) {
$output .= $group_data['group_id'] . '###' . $group_data['group_name'] . '<hr />';
}
If $group_data is not empty - your loop will never end.
To give a suggestion on how to handle database control.
When using PDO for instance
$pdoInst = new PDO( .. );
and we have a method somewhere that validates every statement the $pdoInst produces
abstract class .. {
public static function validateStmt($stmt) {
if($stmt !== false) { .. }
// everything else you like, even error handling, log files, etc.
}
}
}
a prepared statement like the get_group_view method will look like the following
public function get_group_view {
$stmt = $pdoInst->prepare(" .. QUERY .. ");
// the return can be wrapped in a method to handle errors, etc, which can be done
// here or else where.
$stmt->execute() // returns true or false
return $stmt;
}
now for iteration
public function get_group_view($group_data) {
$output = "";
// validate the statement, can be done here or else where as said before
if($pdoInst::validateStmt($group_data)) {
// many ways how to iterate, foreach is just one.
foreach($group_data as $index => $group) {
$output .= $group['group_id'] . '###' . $group['group_name'] . '<hr />';
}
}
return $output;
}
The nicest thing about PDO is that you can extend the classes with custom ones. You can add functionality that adds more value to your Model.