Jump to content

Simple OOP Help


ryan.od

Recommended Posts

I'm attempting to use OOP for the first time in PHP and cannot get the following code to do anything. Any help would be greatly appreciated:

 

<?php
class Menu
{
	public $text;
	public $url;
}

$menu_footer_array = array();

$menu_main1 = new Menu();
$menu_main1 -> text = "Products";
$menu_main1 -> url = "products.html";
$menu_footer_array[0] = $menu_main1;

$menu_main2 = new Menu();
$menu_main2 -> text = "Services";
$menu_main2 -> url = "services.html";
$menu_footer_array[1] = $menu_main2;

$menu_main3 = new Menu();
$menu_main3 -> text = "Contact";
$menu_main3 -> url = "contact.html";
$menu_footer_array[2] = $menu_main3;
?>

 

Really, I'm going to be pulling the text and url info from a db, but until this straightforward attempt works, I'm stuck. I try to output this as follows:

 

<?php
include("../oop/menu.php");
?>
<div id="footer">
<ul>
<?php
	for(i=0; i<menu_footer_array.count(); i++){
		echo('<li><a href="' . menu_footer_array[i] -> url . '">'> . menu_footer_array[i] -> text . '</a></li>');
		echo('<li> | </li>');
	}
?>
	<li>© 2007 <span class="red">Made</span> Products Inc. All Rights Reserved</li>
</ul>
</div>

Link to comment
https://forums.phpfreaks.com/topic/70401-simple-oop-help/
Share on other sites

What is the purpose of your class? Is it just to act as an hash that you could access later? YOu could easily use an associative array to do the same thing.

 

But your code seems to be sound. The error may be what jesi pointed out

 

for(i=0; i<menu_footer_array.count(); i++){
		echo('<li><a href="' . menu_footer_array[i] -> url . '">'> . menu_footer_array[i] -> text . '</a></li>');
		echo('<li> | </li>');
	}

 

to

$count = count(menu_footer_array); //remove this from outside of the loop, it doesnt need to be evualted on every iteration
for(i=0; i<$count; i++){
		echo '<li><a href="' . $menu_footer_array[i] -> url . '">' . $menu_footer_array[i] -> text . '</a></li>';
		//echo('<li> | </li>'); im not sure what this line is doing and i removed the parens from the echo. WHile not illegal, it isnt an expression
	}

Link to comment
https://forums.phpfreaks.com/topic/70401-simple-oop-help/#findComment-353725
Share on other sites

I think include needs to be in the form

include ("/full/path/to/file.php");

instead of

include ("../to/file.php");

The main reason I can think of for this is that because with "includes" it is hard to be sure where any one file is actually being executed from.

 

To help though you could try to

 echo ("\n<p>I've been included</p>\n");

inside of the included files (at the bottom, for preferance ;))

Link to comment
https://forums.phpfreaks.com/topic/70401-simple-oop-help/#findComment-353726
Share on other sites

What is the purpose of your class? Is it just to act as an hash that you could access later? YOu could easily use an associative array to do the same thing.

 

But your code seems to be sound. The error may be what jesi pointed out

 

for(i=0; i<menu_footer_array.count(); i++){
		echo('<li><a href="' . menu_footer_array[i] -> url . '">'> . menu_footer_array[i] -> text . '</a></li>');
		echo('<li> | </li>');
	}

 

to

$count = count(menu_footer_array); //remove this from outside of the loop, it doesnt need to be evualted on every iteration
for(i=0; i<$count; i++){
		echo '<li><a href="' . $menu_footer_array[i] -> url . '">' . $menu_footer_array[i] -> text . '</a></li>';
		//echo('<li> | </li>'); im not sure what this line is doing and i removed the parens from the echo. WHile not illegal, it isnt an expression
	}

 

Furthermore you have to change

			echo '<li><a href="' . $menu_footer_array[i] -> url . '">' . $menu_footer_array[i] -> text . '</a></li>';

to

			echo '<li><a href="' . $menu_footer_array[$i] -> url . '">' . $menu_footer_array[$i] -> text . '</a></li>';

Link to comment
https://forums.phpfreaks.com/topic/70401-simple-oop-help/#findComment-354684
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.