Jump to content

[SOLVED] Display array key as key name, not an integer


bloodgoat

Recommended Posts

Is it at all possible this way?

<?php
$a_navi = array();
$a_navi["IP Bans"] = array("Add", "bans&do=add", "Add a new IP to the current ban list");
$a_navi["IP Bans"] = array("View", "bans&do=view", "View the current ban list");
$a_navi["IP Bans"] = array("Clear", "bans&do=clear", "Clear the current IP ban list");
$a_navi[] = array("Configure", "config", "Modify your over-ride username, associated IP address, administrative panel password, date- and time-stamp formats, and paths to key files");
$a_navi["IP Log"] = array("View", "ip_log&do=view", "View the log of IP addresses associated with invalid administrative login attempts");
$a_navi["IP Log"] = array("Clear", "ip_log&do=clear", "Clear the log of IP addresses associated with invalid administrative login attempts");
$a_navi[] = array("Clear Global Chat", "chat&do=clear", "Clear the global chat log");
sort($a_navi);
?>

Or would I have to do it the other way?

<?php
$a_navi = array(
   "IP Log" => array(
      array("View", "ip_log&do=view", "etc etc"),
      array("Clear", "ip_log&do=clear", "etc etc"),
   ),
   "IP Bans" => array(
      array("Clear", "ip_bans&do=clear", "etc etc"),
      array("View", "ip_bans&do=view", "etc etc"),
      array("Add", "ip_bans&do=add", "etc etc")
   )
);
?>

And even doing it the second way, I keep getting integers instead of the actual value (IP Bans, IP Log, etc).

Link to comment
Share on other sites

I wasn't even confident if the first way was a proper method until I tried it. All my values seemed to be returned fine. I was mostly just being lazy.

 

Regardless, even trying the second way, I'm still getting numeric value returns, which I don't want. I'm trying to use the keys as classification headers for the navigation, and "0", "1", and "2" are terrible classifications at best.

Link to comment
Share on other sites

Pretty much. This code:

<?php
$a_navi = array(
"General" => array(
	array("Configure", "config", "Modify your over-ride username, associated IP address, administrative panel password, date- and time-stamp formats, and paths to key files"),
	array("Clear Global Chat", "chat&do=clear", "Clear the global chat log")
),
"IP Log" => array(
	array("View", "ip_log&do=view", "View the log of IP addresses associated with invalid administrative login attempts"),
	array("Clear", "ip_log&do=clear", "Clear the log of IP addresses associated with invalid administrative login attempts"),
),
"IP Bans" => array(
	array("Clear", "ip_bans&do=clear", "Clear the current IP ban list"),
	array("View", "ip_bans&do=view", "View the current ban list"),
	array("Add", "ip_bans&do=add", "Add a new IP to the current ban list")
)
);
sort($a_navi);
foreach($a_navi as $a_key => $a_linkk){
$a_links .= $a_key;
foreach($a_linkk as $a_link){
	$a_links .= "<a href=\"?action=admin&est=".$a_link[1]."\">".$a_link[0]."</a> | ";
}
}
$a_links = substr($a_links, 0, -3);
?>

Outputs like this:

0Configure | Clear Global Chat | 1View | Clear | 2Clear | View | Add

When I want it to output with the actual key name instead of it's numeric value, like this:

General Configure | Clear Global Chat | IP Logs View | Clear | IP Bans Clear | View | Add
Link to comment
Share on other sites

Wouldn't this end up requiring an unnecessary amount of loops? And if I had enough configure options for it to be recognized as it's own category, I would, but as it stands, "config" is it's just own file with only a few variables that are going to be readily edited.

 

Aside from that, would it at all fix my problem? Or would I still be getting numeric value displays?

Link to comment
Share on other sites

Well your desired output is a bit tricky because you want the General to be combined with the first key, but not any of the keys after it. So you want General Configure but not General Clear Global Chat even though Clear Global Chat is in General.

 

I would probably use two loops. It's not like your original setup is any easier. At least I don't think so.

Link to comment
Share on other sites

OHH, I think you're misconstruing how I want the output to be displayed. Here, I'll throw in some line breaks a little more symbol-work so you see how I want the output.

 

Current:

0: Configure | Clear Global Chat

1: View | Clear

2: Clear | View | Add

Log out

 

Desired:

General: Configure | Clear Global Chat

IP Log: View | Clear

IP Bans: Clear | View | Add

Log out

 

Like, each key is a header for it's own navigational segment, and the "|" separates each link in that segment. So both "Clear Global Chat" and "Configure" are under the "General" header.

 

But disregarding all of this... I still can't get the keys to display themselves as themselves, not their numeric association. AHHHH

Link to comment
Share on other sites

I keep getting numeric output from that.

 

And I didn't even know about the array_keys() function, oh boy. I tried using that on the main page of the admin panel as sort of a test, with this code:

<?php
	$title = "Main Panel";
	$content .=	"\n<table border=\"0\" cellpadding=\"4\" cellspacing=\"0\" width=\"900\" align=\"center\">";
	foreach($a_navi as $a_key => $a_panel){
		$a_key = array_keys($a_navi);
		$content .= "\n	<tr>".
					"\n		<td colspan=\"2\" class=\"f_head\">".$a_key."</td>".
					"\n	</tr>";
		foreach($a_panel as $a_pan){
			$content .=	"\n	<tr>".
						"\n		<td width=\"20%\" class=\"apleft\"><a href=\"?action=admin&est=".$a_pan[1]."\">".$a_pan[0]."</a></td>".
						"\n		<td width=\"80%\" class=\"apright\">".$a_pan[2]."</td>".
						"\n	</tr>";
		}
	}
	$content .=	"\n	<tr>".
				"\n		<td class=\"apfoot\" colspan=\"2\"> </td>".
				"\n	</tr>".
				"\n</table>";
?>

And it just output this:

http://i204.photobucket.com/albums/bb303/img0t/Untitled-2.png

The horizontal navigation towards the top shows how it is pre-array_keys() and the actual table is post-array_keys().

Link to comment
Share on other sites

<?php
foreach ($a_navi as $key => $value) {
     $value_keys = array_keys($value);
     echo $key . ': ' . implode(' | ', $value_keys);
}
?>

Literal copy and paste, this was the output:

0: General1: IP Log2: IP Bans

I figured it out anyways. I had to put each key into it's own array, and then create an array for that key afterwards.

Basically just changed this:

<?php
$a_navi = array(
   "General" => array(
      array("Configure", "config", "Modify your over-ride username, associated IP address, administrative panel password, date- and time-stamp formats, and paths to key files"),
      array("Clear Global Chat", "chat&do=clear", "Clear the global chat log")
   )
// as well as my other arrays, just using one for the example
);
?>

To this:

<?php
$a_navi = array(
array("General" => array(
	array("Configure", "config", "Modify your over-ride username, associated IP address, administrative panel password, date- and time-stamp formats, and paths to key files"),
	array("Clear Global Chat", "chat&do=clear", "Clear the global chat log")
))
);
?>

And added a third loop. It displays just how I wanted it to, now.

Link to comment
Share on other sites

  • 2 weeks later...
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.