Notice: Undefined variable: pages in menu.php on 7

Hi, I have setup a basic Model View Presenter and have implemented my website theme into it.
Menu links are being populated via SQL query.

The menu works fine on the homepage, however when I click one of the SQL generated links, it gives me the error:
Notice: Undefined variable: pages in view\menu.php on line 7
SCREENSHOT

Myself and my tutor have tried to fix this problem, without any luck.

Appreciate any advice thanks.

So, where is the code in the menu.php page that populates $pages with the data that the code expects?

It is in my presenter model
SCREENSHOT http://imgur.com/a/ULIBr

But I am not too sure how to connect that, maybe I done my homepage differently? How would I be able to fix this?

Sorry still new to php.

Please post your code inline in the forum using the forum’s bbcode [nobbc][php][/php][/nobbc] tags… Pictures of your code are of little help.

PRESENTER PHP FILE
[php]<?php
include_once(“model/model.php”);

class Presenter {
public $model;

public function __construct()  
{  
    $this->model = new Model();

} 

public function invoke()
{
	if (!isset($_GET['page']))
	{
		// if no special page is requested, we'll show a menu of all pages or load the default page
		$pages = $this->model->getPages();
		//include 'view/pages.php';
		include 'home.php';
	}
	else
	{
		// otherwise show the requested page
		$page = $this->model->getPage($_GET['page']);
		$display='Title:' . $page->title . '<br/>'
		. 'Image:' . $page->image . '<br/>'
		. 'Description:' . $page->description . '<br/>';
		//include 'view/viewpage.php';
		include 'view/layout.php';
	}
}

}

?>[/php]

MODEL PHP FILE
[php]<?php

include_once(“model/page.php”);

class Model {

public $result;
public function getPages()
{
	include_once('model/conn.php');
	
	
	$sql = "SELECT * FROM pages";
	$result = mysqli_query($conn, $sql);
	
	if(!$result ) {echo 'error finding any pages';}
	// or if table is emtpty
	else if (mysqli_num_rows($result) == 0)
	{ echo '<p>There are no pages</p>';}
	// if not display results
	else {
		$pages_array = array();
		while ($row = mysqli_fetch_array($result)){
			// the row array elements are fields in the database
			// assign records to php variables
			$page_title = $row['PageTitle'];
			$page_image  = $row['Image1'];
			$page_description = $row['Description'];
			$page_obj = new Page($page_title, $page_image, $page_description);
			$pages_array[] = $page_obj;
		}
		// here goes some hardcoded values to simulate the database
	return $pages_array;
	
	} 
}

public function getPage($title)
{
	// we use the previous function to get all the books and then we return the requested one.
	// in a real life scenario this will be done through a db select command
	$allPages = $this->getPages();
	
	foreach( $allPages as $value ){
		if( $value->title == $title){
			return $value;
		}
	}
	
}

}

?>[/php]

PAGE PHP FILE if it helps
[php]<?php

class Page {
public $title;
public $image;
public $description;

public function __construct($title, $image, $description)  
{  
    $this->title = $title;
    $this->image = $image;
    $this->description = $description;
} 

}

?>[/php]

MENU PHP FILE
[php]<?php

	foreach ($pages as $title => $page)
	{
		echo '<li><a href="index.php?page='.$page->title.'">'.$page->title.'</a></li>';
	}

?>[/php]

If you’re new to php then I would suggest NOT starting with Object-Oriented Programming. There’s nothing wrong in starting with procedural coding to begin with and you can still develop some very nice scripts. As a matter of fact I believe I did what you are currently trying to do, but only using procedural PHP. My advice to anyone new to PHP is to be sure you know HTML/CSS really well before tackling PHP, for it will save you a lot of headaches. I also suggest be careful of tutorials that you find online for they are probably old and obsolete. I know when I was first beginning programming in php that I felled for that trap and there is nothing more annoying than throwing away 6 to 8 hours of coding because you were using an obsolete tutorial. I think you were taking some of the code from a book or online tutorial? That’s another problem when first learning to code and that is piecemealing other people’s code to make it your own. I know I have falling into the same trap and it usually ends in disaster unless I followed the tutorial the way they did it and then wrote my own code taking what I have learn. This is just my opinion, but I’m just trying to save you some headaches and time.

Refrain from going off topic. If all you are going to provide is put downs and incorrect advice than I suggest you not comment at all.

Did I ask for the information you gave? NO I asked for advice, not negative advice.

AND FYI! I am professional in HTML/CSS having been a web designer for almost 6 years now.

I maybe new to php, but I do know quit a bit about it. And our tutor would not be teaching us PHP if he didnt think we werent confident.

So take your insecure comment and you go learn php. And I dont have a headache, I made that mvp all by myself. So I think I am capable enough in php.

These forums always get people who make comments about others skill and where they think they should start or what they think they should be doing simply because I stated I was a beginner to php.

If you are not going to help than why bother sticking on these forums for?

Ory, you can rant all you want and talk down one of the best helpers on this site all you want.

You still will not get an answer because you did not ask a question. You created some classes and posted them here.
You want to know how you “connect” to a class? What does that mean? Stop ranting and let us know how to help you.

What errors are you getting? If you need tutorials on how to program in Object Orientated PHP, we can steer to them.
Hard to know what you want help with! But, with that said…

First, have you looked at your code? Your “rendered” code? Rendered means what the browser sees after you have ran thru your PHP code. I suggest you RIGHT-CLICK on your page where it shows the menus. Then, select SHOW-PAGE-SOURCE or whatever depending which browser you are using. Then, in the source for that page (remember, this is after PHP, so it is not your PHP source code, but, your final browser product!) and review the syntax of the differences between the two links. I bet one is wrong. Or, the data you are storing in the SQL menu data is incorrect. Either way, it should show you in the displayed code. Hope that helps…

Oh, 6 years is NOT a profession level for programming. 15 to 20 years minimum… But, don’t rant back at me on that comment as it is just my opinion, not something to get mad about! Good luck!

stick to topic or I will report to moderator. And I did ask a question, if you are a selective reader, I suggest you refer back to the original thread post. In regards to OOP I am doing a course in Algorithms and Data Structures for C#! I have done a introductory course to VB.net I fail to see what your comment is in regards too.

Ory, I AM the moderator. Did you not understand my help I posted?

Your links are inaccurately being posted to the browsers. Otherwise they would work.
To understand why they are not working look at the results your code is creating and fix it.

We want to help you, but, you need to do the work.

Well, yes, I reread your first post. It talks about an error in code you did not show us. Hmmm…

code was there in screenshot link, error is the thread name itself. Sorry for ranting.

Will appreciate any advice you can give please, doesnt necessarily have to be the solution, im sure I can figure that out myself if I am put on the right track thanks.

Line 7 in menu.php has changed as I removed the html doctype and head and body tags

Well, again, it is hard to debug a picture. I will retype it, but, I shouldn’t have to…

Okay, did not need to. Your code is badly formed. In objects your create them and access parts of them as in the form
$something->$somepartofobject… Etc. (Badly formed example, but, you get it…)

BUT, you pull out the table as $title => $page. So the $page is just the page, nothing more.
Using the foreach ($pages as $title=>$page) you are really separating the data into an index named $title and the data as $page. Therefore, I think you want to just use the $title and $page variables not $page-> …

So, using the foreach is not an object. Now, if the data inside the $page part is an object, then it is usable your way.

thank you. i am getting somewhere now. i commented put the foreach statement and the menu now shows on the layout view but is no longer showing on the home.php file. so slowly getting somewhere. I really enjoy learning this mvp its a good experience.

Well, good that you are getting it solved. Please be patient with those who respond to you. We only want to help.

When you get to another problem, ask away. Someone will assist you.

Remember you can see what your program is sending to the browser by looking at the code. Just right-click and show-source.
This is handy as you can see where the errors are if they do not throw out an error on the screen. Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service