# help modifying 'onclick' to call variable url from data file

**URL:** <https://www.phphelp.com/t/help-modifying-onclick-to-call-variable-url-from-data-file/20651>\
**Category:** General PHP Help\
**Created:** [July 9, 2013, 11:23pm UTC](https://www.phphelp.com/t/help-modifying-onclick-to-call-variable-url-from-data-file/20651 "2013-07-09T23:23:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://www.phphelp.com/uploads/default/original/2X/2/243e9ed5d93c5653ce3186f18df16e019ce38445.png) [@system](https://www.phphelp.com/u/system)\
**Post date:** [July 9, 2013, 11:23pm UTC](https://www.phphelp.com/t/help-modifying-onclick-to-call-variable-url-from-data-file/20651/1 "2013-07-09T23:23:44Z")

</div>

Hello, I have 2 php files which are used for the data for an autocomplete search box (search\_munliple.php & pricedata.php), I want to modify the ‘onclick’ command line which currently reads:

‘onclick’ =\> ‘alert(‘You clicked on the ‘.$name.’ fruit!’);’,

To open a url for each data record which is specified in the data file which reads:

‘Apple’ =\> array(‘image’ =\> ‘assets/images/fruits/apple.jpg’, ‘description’ =\> ‘One of America’s favorite fruits.’, ‘url’ =\> ‘[http://google.co.uk/](http://google.co.uk/)’),

The full code for data file (pricedata.php) (only apple has a url field at the min) is:

[php]\<?php  
$fruits = array(  
‘Apple’ =\> array(‘image’ =\> ‘assets/images/fruits/apple.jpg’, ‘description’ =\> ‘One of America’s favorite fruits.’, ‘url’ =\> ‘[http://google.co.uk/](http://google.co.uk/)’),  
‘Avocado’ =\> array(‘image’ =\> ‘assets/images/fruits/avocado.jpg’, ‘description’ =\> ‘The avocado is a dense, evergreen tree, shedding many leaves in early spring.’),  
‘Banana’ =\> array(‘image’ =\> ‘assets/images/fruits/banana.jpg’, ‘description’ =\> ‘Bananas are fast-growing herbaceous perennials arising from underground rhizomes.’),  
‘Gooseberry’ =\> array(‘image’ =\> ‘assets/images/fruits/gooseberry.jpg’, ‘description’ =\> ‘Gooseberries are deciduous shrubs.’),  
‘Grape’ =\> array(‘image’ =\> ‘assets/images/fruits/grape.jpg’, ‘description’ =\> ‘Grapes come in large clusters.’),  
‘Jackfruit’ =\> array(‘image’ =\> ‘assets/images/fruits/jackfruit.jpg’, ‘description’ =\> ‘The jackfruit tree is handsome and stately.’),  
‘Mango’ =\> array(‘image’ =\> ‘assets/images/fruits/mango.jpg’, ‘description’ =\> ‘Mango trees make handsome landscape specimens and shade trees.’),  
‘Papaya’ =\> array(‘image’ =\> ‘assets/images/fruits/papaya.jpg’, ‘description’ =\> ‘The papaya is a short-lived, fast-growing, woody, large herb to 10 or 12 feet in height. It is also regarded by some as being delicious.’),  
‘Peach’ =\> array(‘image’ =\> ‘assets/images/fruits/peach.jpg’, ‘description’ =\> ‘These are great in the summertime.’),  
‘Pear’ =\> array(‘image’ =\> ‘assets/images/fruits/pear.jpg’, ‘description’ =\> ‘Pears are delicious fruits.’),  
‘Pineapple’ =\> array(‘image’ =\> ‘assets/images/fruits/pineapple.jpg’, ‘description’ =\> ‘The pineapple plant is a herbaceous perennial, 2-1/2 to 5 ft.’),  
‘Rose Apple’ =\> array(‘image’ =\> ‘assets/images/fruits/rose\_apple.jpg’, ‘description’ =\> ‘The rose apple is a highly decorative evergreen large shrub.’),  
‘Tamarind’ =\> array(‘image’ =\> ‘assets/images/fruits/tamarind.jpg’, ‘description’ =\> ‘The bright green, pinnate foliage is dense and feathery in appearance.’),  
‘White Sapote’ =\> array(‘image’ =\> ‘assets/images/fruits/white\_sapote.jpg’, ‘description’ =\> ‘The white sapote forms a medium to very large evergreen tree.’),  
);  
$vegetables = array(  
‘Alfalfa’ =\> array(‘image’ =\> ‘assets/images/fruits/alfalfa.jpg’, ‘description’ =\> ‘One cup of raw, sprouted alfalfa seeds, contains 1.32 grams of protein.’),  
‘Artichoke’ =\> array(‘image’ =\> ‘assets/images/fruits/artichoke.jpg’, ‘description’ =\> ‘One medium artichoke cooked with no added salt has 3.47 grams protein.’),  
‘Asparagus’ =\> array(‘image’ =\> ‘assets/images/fruits/asparagus.jpg’, ‘description’ =\> ‘Half cup (about 6 spears) cooked with no added salt contains 2.16 grams of protein.’),  
‘Broccoli’ =\> array(‘image’ =\> ‘assets/images/fruits/broccoli.jpg’, ‘description’ =\> ‘Half cup of broccoli, cooked with no added salt contains 1.86 grams protein.’),  
‘Carrots’ =\> array(‘image’ =\> ‘assets/images/fruits/carrots.jpg’, ‘description’ =\> ‘Half cup cooked with no added salt contains 0.59 grams protein.’),  
‘Celery’ =\> array(‘image’ =\> ‘assets/images/fruits/celery.jpg’, ‘description’ =\> ‘One cup of celery, cooked, boiled, drained with no added salt has 1.25 grams protein.’),  
‘Corn’ =\> array(‘image’ =\> ‘assets/images/fruits/corn.jpg’, ‘description’ =\> ‘One large ear of yellow corn, cooked with no salt contains 4.02 grams protein.’),  
‘Green Pepper’ =\> array(‘image’ =\> ‘assets/images/fruits/green\_pepper.jpg’, ‘description’ =\> ‘One small raw pepper contains 0.64 grams protein.’),  
‘Mushroom’ =\> array(‘image’ =\> ‘assets/images/fruits/mushroom.jpg’, ‘description’ =\> ‘Half a cup of raw mushrooms contains 1.08 grams of protein.’),  
‘Onion’ =\> array(‘image’ =\> ‘assets/images/fruits/onion.jpg’, ‘description’ =\> ‘One small onion cooked without salt contains 0.82 grams protein.’),  
‘Potato’ =\> array(‘image’ =\> ‘assets/images/fruits/potato.jpg’, ‘description’ =\> ‘One medium baked potato without salt contains 4.33 grams of protein.’),  
‘Spinach’ =\> array(‘image’ =\> ‘assets/images/fruits/spinach.jpg’, ‘description’ =\> ‘One cup of raw spinach contains 0.86 grams of protein.’),  
‘Squash’ =\> array(‘image’ =\> ‘assets/images/fruits/squash.jpg’, ‘description’ =\> ‘One cup of sliced summer squash, boiled with no added salt contains 1.87 grams of protein.’),  
);  
?\>[/php]

and the searchmultipal.php file is:

[php]\<?php  
/\* Get the query string “q” variable – this is what the user typed in. \*/  
$q = $\_GET[‘q’];

/\* Run some sort of searching operation.  
- Usually you will be searching a database and then pulling back results.  
- In this case, we’re going to just use a simple array as our data source and find any matches. \*/  
include(‘pricedata.php’);  
$results = array(‘fruits’ =\> array(), ‘vegetables’ =\> array());  
foreach ($fruits as $name =\> $data)  
{  
if (stripos($name, $q) !== false)  
{  
$results[‘fruits’][$name] = $data;  
}  
}  
foreach ($vegetables as $name =\> $data)  
{  
if (stripos($name, $q) !== false)  
{  
$results[‘vegetables’][$name] = $data;  
}  
}

/\* Get the data into a format that Smart Suggest will read (see documentation). \*/  
$final\_fruits = array(‘header’ =\> array(), ‘data’ =\> array());  
$final\_fruits[‘header’] = array(  
‘title’ =\> ‘Fruits’, # Appears at the top of this category  
‘num’ =\> count($results[‘fruits’]), # Displayed as the total number of results.  
‘limit’ =\> 5 # An arbitrary number that you want to limit the results to.  
);  
foreach ($results[‘fruits’] as $name =\> $data)  
{  
$final\_fruits[‘data’][] = array(  
‘primary’ =\> $name, # Title of result row  
‘secondary’ =\> $data[‘description’], # Description below title on result row  
‘image’ =\> $data[‘image’], # Optional URL of 40x40px image  
‘onclick’ =\> ‘alert(‘You clicked on the ‘.$name.’ fruit!’);’, # JavaScript to call when this result is clicked on  
‘fill\_text’ =\> strtolower($name) # Used for “auto-complete fill style” example  
);  
}

$final\_vegetables = array(‘header’ =\> array(), ‘data’ =\> array());  
$final\_vegetables[‘header’] = array(  
‘title’ =\> ‘Vegetables’, # Appears at the top of this category  
‘num’ =\> count($results[‘vegetables’]), # Displayed as the total number of results.  
‘limit’ =\> 5 # An arbitrary number that you want to limit the results to.  
);  
foreach ($results[‘vegetables’] as $name =\> $data)  
{  
$final\_vegetables[‘data’][] = array(  
‘primary’ =\> $name, # Title of result row  
‘secondary’ =\> $data[‘description’], # Description below title on result row  
‘image’ =\> $data[‘image’], # Optional URL of 40x40px image  
‘onclick’ =\> ‘alert(‘You clicked on the ‘.$name.’ vegetable!’);’, # JavaScript to call when this result is clicked on  
‘fill\_text’ =\> strtolower($name) # Used for “auto-complete fill style” example  
);  
}

/\* Output JSON \*/  
$final = array($final\_fruits, $final\_vegetables);  
header(‘Content-type: application/json’);  
echo json\_encode($final);  
die();  
?\>[/php]

Its my first post so hope its right! hope you guys can help 🙂
