Jump to content

In need of help with using PHP array


sam98brown

Recommended Posts

Hi

I have been asked by a client to build the frontend of an application for displaying data from their JSON source.

I have successfully turned the JSON into an array using json_decode, and I have managed to generate a list of "vehicleLabel" as an option/select dropdown. I have created a submit button that then takes the user to a page which I aim to display more information about said "vehicleLabel". I am just struggling to work out how to do this. For example, the user should select the vehicle they want to view info about, and then they can view the manufacturer, make, model etc that would be eventually added to the array. 

Can anyone point me in the right direction? Here is an example of how my array looks:

Array(

[0] =>
stdClass Object(
[id] => 
[carID] =>
stdClass Object(
[carID] => 1
)
[carDescription] => car1desc
[active] => 1
[vehicleLabel] => veh1lab
[vehicleDescription] => veh1des
)
[1] =>
stdClass Object(
[id] => 
[carID] =>
stdClass Object(
[carID] => 2
)
[carDescription] => car2desc
[active] => 2
[vehicleLabel] => veh2lab
[vehicleDescription] => veh2des
)

 

I have copied and pasted this from http://array.include-once.org/ and renamed some of the data for example case

Link to comment
Share on other sites

JSON, XML, CSV formats are fine for data transfer but not as a primary data storage medium.

I would advise loading the JSON data into database for use in your application.

If that is not an option, make the carID the key of your array to make it easy to find the data you want. In your option/select, make the option values = carID but display the labels EG

echo "<option value=\"$carID\">$vehicleLabel</option>";

 

Link to comment
Share on other sites

6 minutes ago, Barand said:

JSON, XML, CSV formats are fine for data transfer but not as a primary data storage medium.

I would advise loading the JSON data into database for use in your application.

If that is not an option, make the carID the key of your array to make it easy to find the data you want. In your option/select, make the option values = carID but display the labels EG


echo "<option value=\"$carID\">$vehicleLabel</option>";

 

Thanks for your reply. That is currently how I have set up. My issue is, I have no idea how to then display other details about that specific car on the next page. I can display the value on the next page using the form, but how do i display other information stored about that car? Other parts of the array? (sorry I'm not sure on what the terminology is)

Link to comment
Share on other sites

2 minutes ago, Barand said:

You would get the other info from the aray the same way as you get the label  and the id.

If your dropdown name is, say, "carID" then your second page will receive the carID as in $_GET['carID'].

Use that value as the key to the array element that you want.

2

Use that value as the key to the array element that you want. -  This is where I'm getting confused. Would it be possible for you to point me in the right direction of seeing an example for this?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.