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
Share on other sites

You're using count wrong - that is how you might do it in Java. In PHP you need to pass the array you want to count as an argument to count. Also, in PHP variable names have $ in front.

These are correct:

$menu_footer_array

$this->text

 

 

Link to comment
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.