0

I am attempting to echo out a message associated with a specific key in an array. I am getting undefined index and I cannot figure out why. My code is below:

<h1>RocketSnail Dynamic Template Editor</h1>
<p>Here you can edit which shipping type should take priority, as well as what message will show.</p>
<?php
$messagesArray = (array) Mage::helper('GOED_RocketSnail')->readFromJson(Mage::getBaseDir('var').'/RocketSnail/typeandmessage.json');
$selectedShipType = $_GET['shiptype'];
$selectedMessage = $messagesArray[$selectedShipType];
foreach($messagesArray as $shipType => $message){
    echo "<a href='?shiptype=".$shipType."' >".$shipType."</a>";
    echo "<br />";
}
Mage::log($selectedMessage);
//Mage::log($selectedShipType);
//var_dump($messagesArray);
if(array_key_exists("16677", $messagesArray)){
    echo "Exists! String!";
}
if(array_key_exists(16677, $messagesArray)){
    echo "Exists! Int!";
}
if(array_key_exists(0, $messagesArray)){
    echo "Exists! What?!?";
}
echo $selectedMessage;
?>

readFromJson simply reads from a Json file and returns whatever the file contains. If I var_dump($messagesArray), I get:

array(17) { ["16677"]=> string(4) "Here" ["31842"]=> string(2) "Is" ["16672"]=> string(1) "A" ["16673"]=> string(7) "Message" ["16674"]=> string(3) "For" ["16675"]=> string(4) "Each" ["16676"]=> string(8) "Possible" ["28492"]=> string(13) "Shipping_Type" ["26028"]=> string(2) "On" ["26319"]=> string(4) "Each" ["8066"]=> string(8) "Possible" ["8067"]=> string(4) "Item" ["34003"]=> string(2) "So" ["34002"]=> string(2) "We" ["51572"]=> string(3) "Can" ["25020"]=> string(6) "Change" ["25019"]=> string(7) "Content" }

When I log my $_GET request I have

2016-05-31T14:37:59+00:00 DEBUG (7): 16677

BUT! When I log $selectedMessage, I get

2016-05-31T14:40:49+00:00 ERR (3): Notice: Undefined index: in /var/www/html/app/design/adminhtml/default/desktop/template/goed/RocketSnailFilter.phtml on line 6

I'm stumped on this one. Any help is appreciated!

  • EDIT: I tried typecasting $selectedShipType to both a string and an int but that didn't help either. I'm surprised that neither "Exists! Int!" nor "Exists! String!" show on the page... – Joshua Palan May 31 '16 at 18:50
0

you have this line:

$selectedMessage = $messagesArray[$selectedShipType];

but the variable $selectedShipType is not defined.
You only define it at the end of the template:

$selectedShipType = $_GET['shiptype'];
  • I apologize I realize I changed that while debugging. Moving that line to before $selectedMessage = $messagesArray[$selectedShipType]; changes nothing. :/ – Joshua Palan May 31 '16 at 14:58
  • but do you call the page like this? example.com/page/here/?shiptype=something_here ? Maybe the $_GET parameter is not set. – Marius May 31 '16 at 15:00
  • Correct! The url looks like this: mywebsite.com/index.php/admin/filterSnail/?shiptype=16677. Also, the log of selected ship type returns 16677 as expected. – Joshua Palan May 31 '16 at 15:01
0

Well I managed to figure this out. I'm sad to have been downvoted when this seems to be something others have experienced as well with no real answers. The error was coming from my readFromJson function. Apparently, json_decode doesn't return the array as an associative array by default, even though that is how the info was encoded. All I had to do was add the 'true' flag in my decode function call. Basically this

json_decode($input);

became this

json_decode($input, true);

and now everything works perfect. I hope this helps someone else who has the same issue. For more documentation, check here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.