ryan.od Posted September 23, 2007 Share Posted September 23, 2007 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> Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 23, 2007 Share Posted September 23, 2007 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 Quote Link to comment Share on other sites More sharing options...
ryan.od Posted September 23, 2007 Author Share Posted September 23, 2007 Right. Oops. I guess I left off a couple $. Thanks for calling my attention to it. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted September 24, 2007 Share Posted September 24, 2007 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 } Quote Link to comment Share on other sites More sharing options...
Gamic Posted September 24, 2007 Share Posted September 24, 2007 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 ) Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted September 25, 2007 Share Posted September 25, 2007 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>'; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.