Jump to content

foreach problem


james182

Recommended Posts

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>';
}
}

Link to comment
https://forums.phpfreaks.com/topic/187687-foreach-problem/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/187687-foreach-problem/#findComment-990866
Share on other sites

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???

Link to comment
https://forums.phpfreaks.com/topic/187687-foreach-problem/#findComment-990879
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/187687-foreach-problem/#findComment-990889
Share on other sites

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;

Link to comment
https://forums.phpfreaks.com/topic/187687-foreach-problem/#findComment-990901
Share on other sites

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;

Link to comment
https://forums.phpfreaks.com/topic/187687-foreach-problem/#findComment-990921
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.