Thilina H
Thilina H

Reputation: 217

Passing multiple values for a single variable between 2 pages in php

I m developing a php site for mobile phones. It's home page has images of several phones, all are having a unique Id.(1,2,3,...).So when I click on an Image a the page should be redirected to another page(Details.php) and display images and information related to the image that I just clicked. I tried doing this using a session variable but if so I'll have to use separate session variable to each phone. How can I solve this issue. Please help.

Thanks in advance.

Upvotes: 0

Views: 1354

Answers (2)

user1438599
user1438599

Reputation:

In details.php you could add code similar to $phoneId = $_GET["id"] and append ?id=# (where # is the phone's ID) to the end of each link.

Example

Listing the phones, this assumes you have an array $phones with the data

foreach ($phones as $phone) {
    echo "<a href='details.php?id=" . $phone["id"] . "'><img src='images/phone" . $phone["id"] . ".png'></a>";
}

Determine which phone the user selected (in details.php), also assuming you have a $phones array

// Check if an ID was sent
if (empty($_GET["id"])) {
    // The user didn't select a phone
} else {
    // Get the phone ID
    $phoneID = $_GET["id"];

    // Check if the ID is valid
    if (array_key_exists($phoneID, $phones)) {
        // Select the phone
        $phone = $phones[$phoneID];
    } else {
        // The ID is invalid
    }
}

Upvotes: 2

Juvar Abrera
Juvar Abrera

Reputation: 485

You could use a GET variable to pass those information.

index.php or whatever where the phones are listed

<a href="details.php?phoneid=1"><img src="phone01.jpg"></a>
<a href="details.php?phoneid=2"><img src="phone02.jpg"></a>
<a href="details.php?phoneid=3"><img src="phone03.jpg"></a>

And at the details.php. You can use this

if(isset($_GET['phoneid'])) {
    $phoneid = $_GET['phoneid'];
    switch ($phoneid) {
        case '1':
            echo 'Phone 1';
            break;
        case '2':
            echo 'Phone 2';
            break;
        case '3':
            echo 'Phone 3';
            break;
    }
} else {
    // no value of phoneid
}

Another easier way is to use mySQL to list all the phones.

Upvotes: 1

Related Questions