Page 1 of 1
Replace values in array.
Posted: Fri Feb 03, 2012 11:04 pm
by EcazS
Here I am again!
Let's get right into it; I have this array
Array
(
[0] => Array
(
[url] => Home
[name] => Home
)
[1] => Array
(
[url] => About
[name] => About
)
[2] => Array
(
[url] => contact.php
[name] => Contact
)
)
And this is how I output it,
foreach ($navigation as $nav) {
echo "<li><a href=\"{$nav["url"]}\">{$nav["name"]}</a></li>";
}
But I want to check if the page is at Home, About or Contact and if it is then I want the name value to change into this,
<strong>original_value</strong>
I know this can be done with a loop somehow, I just don't know where to start, so any pointers and/or suggestions would be awesome.
Re: Replace values in array.
Posted: Fri Feb 03, 2012 11:47 pm
by bowersbros
Well, you could check what the current page you are on is using these functions, ill let you try to figure out how to set it up.
Use the construct __FILE__
functions basename and maybe explode and end, depending on your setup.
Then you can compare that value to your array nav using in_array()
if true then do whatever, if false then it isnt part of your array so.. most likely your code is wrong or invalid page
Re: Replace values in array.
Posted: Sat Feb 04, 2012 12:33 am
by EcazS
But it's dynamic so I figured that I'd check the $_GET but I'm still stuck on changing the array value, since I want to echo them out in the right way too.
Re: Replace values in array.
Posted: Sat Feb 04, 2012 12:39 am
by jacek
You can use in_array() if you know what both of the keys will be, example
if (in_array(array('url' => 'Home', 'name' => 'Home'), $pages)){
// home page
}
would work.
Re: Replace values in array.
Posted: Sat Feb 04, 2012 12:46 am
by EcazS
Ye, but if I do it like that then I would still need a way to change that specific value of the array and then outputting it. And that's sort of where I'm stuck.
My idea right now is to check what page it is (which I know how to do) and then update the array and then use the foreach to output the array. But I don't know how to update the array I currently have.
Re: Replace values in array.
Posted: Sat Feb 04, 2012 1:36 am
by EcazS
I updated the code a little bit and did like this,
while ($mysql->fetch($row)){
$navigation_items[$row["page_name"]] = array(
"url" => $row["page_name"],
"name" => current(explode(".", ucwords($row["page_name"]))),
"home" => $row["is_home"]
);
}
which gives me this array,
Array
(
[Home] => Array
(
[url] => Home
[name] => Home
[home] => 1
)
[About] => Array
(
[url] => About
[name] => About
[home] => 0
)
[contact.php] => Array
(
[url] => contact.php
[name] => Contact
[home] => 0
)
)
and then finally I did like this,
if((isset($_GET["p"])) && (in_array($_GET["p"], $navigation[$_GET["p"]]))) {
$navigation[$_GET["p"]]["name"] = "<strong>{$navigation[$_GET["p"]]["name"]}</strong>";
}
foreach ($navigation as $nav) {
echo "<li><a href=\"{$nav["url"]}\">{$nav["name"]}</a></li>";
}
And this is working up to 33%; the problem is if you type in the wrong "case-lettering" say you type in "home" instead of "Home" or "HoMe" instead of "Home" (basically just the wrong "case") then you will get this,
Notice: Undefined index: homE in C:\xampp\htdocs\CMS\themes\default\layout\header.template.php on line 20
Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\CMS\themes\default\layout\header.template.php on line 20
Is there a way to make the in_array not case-sensitive? Or "go around" it or do this another way so it wouldn't cause a problem?
Re: Replace values in array.
Posted: Sat Feb 04, 2012 8:56 am
by bowersbros
you could change the GET to lowercase, and make sure that the array is always lower case, or if it is always first letter cappitalised then do
ucfirst() then strtolower inside it.
Re: Replace values in array.
Posted: Sat Feb 04, 2012 11:27 am
by EcazS
I tried doing strtolower in the while loop,
$navigation_items[strtolower($row["page_name"])]
and also,
in_array(strtolower($_GET["p"]), $navigation[$_GET["p"]])
but I'm still getting the error :/
Re: Replace values in array.
Posted: Sat Feb 04, 2012 11:55 am
by bowersbros
well your array has the first letter capitalised in the name bit, but lowercased in the URL i think
So
ucfirst(strtolower($whatever))
should do it.
Re: Replace values in array.
Posted: Sat Feb 04, 2012 12:20 pm
by EcazS
The problem was that I didn't do the string manipulation on every $_GET variable.
It's a rather messy solution,
while ($mysql->fetch($row)){
$navigation_items[current(explode(".", $row["page_name"]))] = array(
"url" => $row["page_name"],
"name" => current(explode(".", $row["page_name"])),
"is_home" => $row["is_home"]
);
}
$page_name = current(explode(".", ucfirst(strtolower($_GET["p"]))));
if(isset($_GET["p"])) {
if(in_array($page_name, $navigation[$page_name])) {
$navigation[$page_name]["name"] = "<strong>{$navigation[$page_name]["name"]}</strong>";
}
}
foreach ($navigation as $nav) {
echo "<li><a href=\"{$nav["url"]}\">{$nav["name"]}</a></li>";
}
Edit,
I updated my "page creation" script to capitalize the first letter so I don't have to do that in the array.
Re: Replace values in array.
Posted: Sat Feb 04, 2012 10:49 pm
by EcazS
Alright so the above works, however, I'd like to change it a bit; I don't know if this is possible at all but I would like to check what page it is and echo out something else.
foreach ($navigation as $nav) {
if(this_page = $nav["name"]) {
echo "<li class=\"active\"><a href=\"" . strtolower($nav["url"]) . "\">{$nav["name"]}</a></li>";
}else {
echo "<li><a href=\"" . strtolower($nav["url"]) . "\">{$nav["name"]}</a></li>";
}
}
I hope you get what I mean, don't really know how to explain it :/
This would work if the array WASN'T multidimensional...aargh
foreach( $current as $k => $v ) {
$active = $_GET['page'] == $k
? ' class="current_page_item"'
: '';
echo '<li'. $active .'><a href="./'. $k .'">'. $v .'</a></li>';
}
Re: Replace values in array.
Posted: Sun Feb 05, 2012 11:56 pm
by jacek
Using the array above you could do
foreach ($navigation as $page => $nav) {
if($page == $_GET['page']) {
echo "<li class=\"active\"><a href=\"" . strtolower($nav["url"]) . "\">{$nav["name"]}</a></li>";
}else {
echo "<li><a href=\"" . strtolower($nav["url"]) . "\">{$nav["name"]}</a></li>";
}
}
?
Re: Replace values in array.
Posted: Mon Feb 06, 2012 1:16 am
by EcazS
What the heck?!
I tried something like that before but the items were repeating itself twice; I must have messed it up
Re: Replace values in array.
Posted: Mon Feb 06, 2012 9:57 pm
by jacek
EcazS wrote:What the heck?!
I tried something like that before but the items were repeating itself twice; I must have messed it up
I'm not sure how you would have done that :s
Have another go