james182 Posted January 8, 2010 Share Posted January 8, 2010 i am trying to pull out the correct item key in and array and store it in the "item_key" variable. class myClass{ var $item_key; function get_menu($page_name) { $menu_item = array("Dashboard", "Content", "Comments", "Media", "Syndication", "Newsletter", "Affiliate", "Appearance", "Members", "Settings"); $menu_item_link = array("dashboard", "content", "comments", "media", "syndication", "newsletter", "affiliate", "appearance", "members", "settings"); $menu_item_icon = array("dashboard", "edit", "comments", "media", "syndication", "send", "cash", "color", "users", "advanced"); echo 'Key: '. $this->item_key; // Testing purpose only. echo '<h2><span class="h-ico ico-'. $menu_item_icon[$this->item_key] .'"><span>'. self::show_page_name($page_name) .'</span></span> <span class="h-arrow"></span> </h2>'; echo '<ul class="clearfix">'; $i=0; foreach($menu_item as $key => $value): if($value != self::show_page_name($page_name)): echo '<li><a class="h-ico ico-'. $menu_item_icon[$i] .'" href="'. parent::get_site_url() .'cp/'. $menu_item_link[$i] .'"><span>'. $value .'</span></a></li>'; else: echo ''; $this->item_key = $key; endif; $i++; endforeach; echo '</ul>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/187687-foreach-problem/ Share on other sites More sharing options...
cags Posted January 8, 2010 Share Posted January 8, 2010 So what exactly is the problem your encountering? Quote Link to comment https://forums.phpfreaks.com/topic/187687-foreach-problem/#findComment-990858 Share on other sites More sharing options...
james182 Posted January 8, 2010 Author Share Posted January 8, 2010 The problem is that it's not storing the data. for example if i click the members btn (which is 8 in the array) the number 8 should be echo'd out, in my test echo (echo 'Key: '. $this->item_key; // Testing purpose only.). Quote Link to comment https://forums.phpfreaks.com/topic/187687-foreach-problem/#findComment-990861 Share on other sites More sharing options...
cags Posted January 8, 2010 Share Posted January 8, 2010 What data? Are you saying that after calling the function the $item_key variable doesn't contain a value? If that's the case then it's probably because... if($value != self::show_page_name($page_name)): ...never evaluates to false. Since we can't see what the function show_page_name does it's difficult to say, but it doesn't seem to return anything that is in the array $menu_item. Are you sure it returns a value which begins with a capital letter? Quote Link to comment https://forums.phpfreaks.com/topic/187687-foreach-problem/#findComment-990866 Share on other sites More sharing options...
james182 Posted January 8, 2010 Author Share Posted January 8, 2010 function show_page_name($string) { $page_name = $string; $words = split("[/]", $page_name); $page_name = $words[3]; $page_name = substr($page_name, 0, strrpos($page_name, '.')); $page_name = ucfirst($page_name); return $page_name; } show page name just remove slashes in string eg: "/to/be/members.php" becomes "members". Have i gone about storing the data in a var the right way??? Quote Link to comment https://forums.phpfreaks.com/topic/187687-foreach-problem/#findComment-990879 Share on other sites More sharing options...
cags Posted January 8, 2010 Share Posted January 8, 2010 At the start of your loop echo out something along the lines of... echo "$value = ". self::show_page_name($page_name) . '<br/>': ... and comment out the other echo statements in the function. That way you will see exactly what it's comparing and it should become obvious why a value doesn't match. Alternatively before doing that place a die statement in the else block to check it doesn't actually enter it. Quote Link to comment https://forums.phpfreaks.com/topic/187687-foreach-problem/#findComment-990889 Share on other sites More sharing options...
james182 Posted January 8, 2010 Author Share Posted January 8, 2010 ok let me say that my code all works except for the the storing of the array item number: if($value != self::show_page_name($page_name)): echo '<li><a class="h-ico ico-'. $menu_item_icon[$i] .'" href="'. parent::get_site_url() .'admin/'. $menu_item_link[$i] .'"><span>'. $value .'</span></a></li>'; else: echo ''; $this->item_key = $value; // PROBLEM IS HERE, IT'S NOT SENDING TO VAR OUTSIDE OF THIS FUNCTION. endif; Quote Link to comment https://forums.phpfreaks.com/topic/187687-foreach-problem/#findComment-990901 Share on other sites More sharing options...
cags Posted January 8, 2010 Share Posted January 8, 2010 So I take it you don't get the current page in the list then? In that case how are you trying to access the value of item_key? Quote Link to comment https://forums.phpfreaks.com/topic/187687-foreach-problem/#findComment-990905 Share on other sites More sharing options...
james182 Posted January 8, 2010 Author Share Posted January 8, 2010 So probably the question i should be asking is how to send data to a var. Is this right?? class myClass { var myVar; echo $myVar; function test() { $myVar = "test"; } } Quote Link to comment https://forums.phpfreaks.com/topic/187687-foreach-problem/#findComment-990908 Share on other sites More sharing options...
cags Posted January 8, 2010 Share Posted January 8, 2010 No, firstly you don't have a dollar sign on your var declaration but perhaps that's just a typo? I don't believe you can place that echo statement inside a class like that, code inside a class should be inside a function. Also to access a class level variable I believe you should be using $this just like in your original code, that code you just posted I believe will create a local variable inside the function with the same name as the class level variable. Having said all this I've never really done much OOP with PHP, I'm basing what I say on the brief experiments I have done. As a quick example of usage... class ExampleClass { var $class_level_var; public function set_var($var) { $this->class_level_var = $var; } } $bob = new ExampleClass(); $bob->set_var("hello"); echo $bob->class_level_var; Quote Link to comment https://forums.phpfreaks.com/topic/187687-foreach-problem/#findComment-990921 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.