Replace values in array.

Ask about a PHP problem here.
Post Reply
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Replace values in array.

Post 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.
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Replace values in array.

Post 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 :D
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Replace values in array.

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Replace values in array.

Post 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.
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Replace values in array.

Post 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.
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Replace values in array.

Post 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?
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Replace values in array.

Post 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.
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Replace values in array.

Post 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 :/
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Replace values in array.

Post 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.
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Replace values in array.

Post 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.
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Replace values in array.

Post 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>';
    }
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Replace values in array.

Post 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>";
     }
}
?
Image
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Replace values in array.

Post by EcazS »

What the heck?!
I tried something like that before but the items were repeating itself twice; I must have messed it up
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Replace values in array.

Post 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 :)
Image
Post Reply