I have a form tag which uses the GET method. I want to search an array in a php file. I want the value of specific keys to be echoed. I got the php code to run and access an array but the code for the array is on the same page, I did not use the file() function. When I do try to use the file() function to echo the input of the form, the page displays characters in the php code like quotes and =>. The whole array is echoed but not the specific values of the keys which I want. I am running windows 8.1 and using IIS as the web server. Here is my code which works without the file() function:
<?php
//this is the function which takes in the GET request from the form name='cake'
$cake = htmlspecialchars($_GET['cake']);
$cake2 = strtoupper($cake);
echo "<div id='cake'>$cake</div>";
//use cake2 for the search process
echo $cake2;
$artists = array(
'ED SHEERAN' => "<div class='Data'>This could display the search results</div>",
'ELLIE GOULDING' => "<div class='Data'>This xxxxxxxxx result</div>" );
foreach($artists as $key => $value) {
if($cake2 == $key)
echo $value;
}
?>
The code above echos what I want. Preferably I would like to use a separate php file with the array above so here is my code for the file() attempt:
$cake = htmlspecialchars($_GET['cake']);
$cake2 = strtoupper($cake);
echo "<div id='cake'>$cake</div>";
//use cake2 for the search process
echo $cake2;
$dataReq = file("phpData.php", FILE_IGNORE_NEW_LINES);
#echo $dataReq;
foreach($dataReq as $key => $value){
if ($key == $cake2)
echo $value;
}
Above when I submit anything into the form it is echoed but I can't quite get the phpData array to be searched like it is in the first example. If I remove the hashtag from 'echo $dataReq;' the web page returns 'Array',$artists, $keys, and $values. Here is what that phpData file looks like:
<?php
$artists = array(
'ED SHEERAN' => "<div class='Data'>This could display the search result</div>",
'ELLIE GOULDING' => "<div class='Data'>This xxxxxxxxx result</div>" );
?>
My question would be how can I use the file() function to access the keys I want in its array and echo it?
-Thanks for reading this post :D
Instead of using "File", you should include phpData.php and then loop through the array of artists. Take a look at the difference between "file" and "include" and it'll be apparent why you need include:
http://php.net/manual/en/function.include.php
http://php.net/manual/en/function.file.php
Your code would look like this: