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.
Related
I have been having a hella of a time trying to get an array to initialize inside a class. What I am trying to attempt is to create a side menu using an array and a child class. I have tried to solve this problem for several days and now I'm asking for help. I have a feeling I am missing something basic as I am just learning PHP.
Below is my code and the commented out lines are attempts at a solution that have not worked.
<?php
/************* global variables ******************************/
//global $house_array, $car_array, $vacation_array, $company_address;
$company_name = "Heaven HVAC";
$street = '12345 Blue Canyon Rd.';
$company_citystatezip = "Heaven, CA 91777";
/************* end global variables **************************/
echo '<H1 align="center">Calling Array inside Child Class</H1>';
class Company{
//// insert object variables (properties) HERE
var $company_url = "http://localhost";
var $company_email = "example#example.com ";
//// insert methods here
function getHeader($company_name, $color) {
$topheader = "<TABLE align='center'; style='background-color:$color; width:50%'><TR><TD>";
$topheader .= "<H1 style='text-align:center'>$company_name</H1>";
$topheader .= "</TD></TR></TABLE>";
return $topheader;
}
function getFooter($color) {
$this->address;
$bottomfooter = "<TABLE align='center'; style='background-color:$color;width:50%'><TR><TD>";
$bottomfooter .= "<center><b><u>$this->address</center></b></u>";
$bottomfooter .= "</TD></TR></TABLE>";
return $bottomfooter;
}
} // end class Company
//// Working On child class - If commented out, then the code has been tried and failed
class AirCondition extends Company {
var $navbar_array;
//// create 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" );
}
//create_navbar_array();
// return $navbar_array;
function getLeftNavBar($array){
// create_navbar_array();
## Create a table to display arrays
print "<TABLE BORDER='1'>";
echo '<tr><td>Navigation Menu</td></tr>';
foreach($array as $Page){
echo "<tr><td>{$Page}</td></tr>";
return $array;
}
echo "</TABLE>";
}
}
$HVACcompany = new AirCondition();
$HVACcompany->address = "777 Estate Rd <br /> $company_citystatezip";
echo $HVACcompany->getHeader($company_name, orange);
echo "<br/>";
echo $HVACcompany->getFooter(green);
//echo $HVACcompany->getLeftNavBar();
//echo $HVACcompnay->create_navbar_array();
//$HVACcompnay->create_navbar_array();
$HVACcompany->getLeftNavBar($navbar_array);
?>
I am able to create the web page layout, but the array isn't initializing. What am I doing wrong/missing?
The following line:
$HVACcompany->getLeftNavBar($navbar_array);
should be:
$HVACcompany->getLeftNavBar($HVACcompany->navbar_array);
Because $navbar_array was never declared in the global namespace, however it was declared as a public variable within the class AirCondition which was initialized with the variable $HVACcompany.
Additionally you need to call the function $HVACcompany->create_navbar_array();
before calling $HVACcompany->getLeftNavBar($HVACcompany->navbar_array); in order to create the array $navbar_array.
Also remove the return $array; statement inside the foreach loop.
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 have a question regarding displaying the contents of a function, this function displaying a while loop.
Here is a function within my model:
function get_results($id)
{
$stmt = "select * where ... "
$stmt = $this->BEAR->Database->query($stmt);
$result = '';
while($row = mysqli_fetch_array($stmt))
{
$result .= '<div>';
$result .= $row['name'];
$result .= '</div>';
}
$this->BEAR->Template->setData('loop', $result, FALSE);
}
This is my Controller:
$BEAR->Webprofile->get_results(Template->getData('id'));
And this is my view:
<?php echo $this->getData('loop');?>
This displays the Loop within my view with no problem. But what I wish for is not to have any HTMl within my Model, Is there anyway of doing this (As this can cause a large amount of HTML in my Model). Maybe a way I can set the data within the Model and then get the data within my view.
I tried setting within the Model functions while loop individually like the following:
while($row = mysqli_fetch_array($stmt))
{
$this->BEAR->Template->setData('name', $row['name']);
$this->BEAR->Template->setData('name', $row['age']);
}
Then call the function in the Controller and call each setData, but this only displayed the first result not the full while loop of contents.
Therefore I wish to display all the contents of my while loop in my view (with HTML) but wish my function to just be getting and setting the Data. Can this be done? Any thoughts or guidance would be appreciated.
You need to apply some discipline to your MVC. Your models need to return raw data. It should return only objects or arrays of data. The key is consistency.
Your views need to include all the code to add your html formatting. Having a view that simply calls a model function you wrote that spits out a div or an ordered list, makes the entire concept of the view useless. Your views should provide all the HTML code.
Since you're using PHP, you can easily drop in and out of HTML.
Start with something like this in your model:
function get_results($id)
{
$stmt = "select * where ... "
$stmt = $this->BEAR->Database->query($stmt);
$results = array();
while($row = mysqli_fetch_array($stmt))
{
$results[] = $row['name'];
}
return results;
}
From there, you should be able to figure out that your controller should call this function, and pass the $results into your view/template along with the specific view file for rendering.
function get_results($id)
{
$stmt = "select * where ... "
$stmt = $this->BEAR->Database->query($stmt);
$result = '';
$result = mysqli_fetch_array($stmt);
return $result;
}
Then in your controller:
$this->BEAR->Template->setData('loop', $model->get_results($id), FALSE);
Then in your template
foreach($rows as $row){
....do something with each row
}
full example of how to get the data from the model and then pass to the template
class MyController {
function controller_showResults(){
$model = new Model();
$results = $model->get_results($_GET['id']);
$this->BEAR->Template->setData('loop', $results, FALSE);
}
}
Now the view assuming that the first argument to setData in template is a variable passed to the view and that variable is $results
<?php foreach($loop as $l): ?>
<div><?php echo $l['name'] ?></div>
<?php endforeach; ?>
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.
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