0

In the code below, I am trying to use echo $r_id[$d_id]['name'];" where $r_id = china and $d_id = china1 was defined in a previous page, so as to replicate echo $china['china1']['name'];, which will give me the value of Beef and Broccoli from the array.

<?php
$china = array(
'china1'=> array(
'cat'=> "Chinese",
'id'=>"C1",
'name'=>"Beef and Broccoli",
'instruction'=> "Placeholder")
);

if(isset($_GET["r_id"])){
        if(isset($_GET["d_id"])){
            $r_id = $_GET["r_id"];
            $d_id = $_GET["d_id"];
        }
    }

    echo $r_id[$d_id]['name'];

?>

But all I am getting is this:

Warning: Illegal string offset 'china1' in....

Warning: Illegal string offset 'name' in....

Where did I get it wrong? Code for the previous page:

        <table border = 1px align = center style = "margin-top:100px;">
        <tr>
            <td><a href="recipes.php?r_id=china&d_id=china1"><img src="images/china1icon.jpg"/></a></td>
            <td><img src="images/vietnam1icon.jpg"/></td>
            <td><img src ="images/japan1icon.jpg"/></td>
            <td><img src ="images/korea1icon.jpg"/></td>
        </tr>
    </table>
2
  • Are you certain $d_id set? This error would occurr if $r_id[$d_id] is not set, i.e. the key $d_id is not there. Best guess would be that if(isset($_GET["r_id"])) is being bypassed because r_id is not set. Commented Jan 3, 2015 at 12:23
  • @dwhite.me This was what I used from the previous page: <a href="recipes.php?r_id=$china&d_id=china1"></a> Commented Jan 3, 2015 at 12:28

1 Answer 1

2

You cannot pass a variable directly into the URL: <a href="recipes.php?r_id=$china&d_id=china1"></a>. You should print out the variable in the URL like <a href="recipes.php?r_id=" . $china . "&d_id=china1"></a>, and then it'd work.

And on this page, $r_id is now 'china', if you want echo $r_id[$d_id]['name']; to become echo $china['china1']['name'];, then you need to do $$r_id[$d_id]['name'], note the double dollar sign, which defines a variable variable.

Sign up to request clarification or add additional context in comments.

7 Comments

It doesn't seem to be the issue here, as I got the error message "Notice: Undefined variable: $" on top of the previous 2 error messages.
Hi, I'm sorry but when I replaced your content of the <a> tag into mine, I got Notice: Undefined variable: r_id and d_id now.
Don't be sorry, but my solution should work. It's really hard to see where the root error is. Perhaps it'd help if you post your code in your previous page, this page, as well as the URL of this page.
localhost/webp/cooking/recipes.php?r_id=china&d_id=china1 is the URL of the current page. Code for previous page now in the question. Thanks for taking your time to respond to me.
It should work. My guess is there's some silly mistake somewhere. Undefined variable: r_id and d_id means your code never got inside the if statements, which means isset($_GET["r_id"]) or if(isset($_GET["d_id"])) returned false. To avoid this in the future, you can try defining the variable as an empty string first, so it never returns as undefined. For now, try var_dump-ing $_GET, isset($_GET["r_id"]) etc, and see if you find anything useful? It's most likely a silly typo.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.