0

I have a create itinerary project. All the available destination is listed on my right panel and can be selected by button click. After the user select the desire destination/s it will be displayed on my right panel in the same page, in that part all is working great. I have also successfully pass the selected destination value into the url using session but when I am about to display it on another page what I'm getting is the only last value of selected destination. Can anyone know any solution or work around? Any help would be very much appreciated. Thank you in advance!

Below is the screenshot of my project and codes for your reference.

This is my right panel view where user can select a destination/s.

enter image description here

This is my codes for selecting a destination/s.

<?php
session_start();
if(!empty($_SESSION['destinationID'])){
        $destinationID = $_SESSION['destinationID'];
    }        
$dropdown_destination = $_GET['dropdown_destination']?:null;
$dropdown_tourdate = $_GET['dropdown_tourdate']?:null;
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
  case "add":
    if(!empty($_POST["quantity"])) {
      $productByCode = $db_handle->runQuery("SELECT * FROM tourist_spot WHERE TOURIST_SPOT_ID='" . $_GET["code"] . "'");
      $itemArray = array($productByCode[0]["TOURIST_SPOT_ID"]=>array('TOURIST_SPOT'=>$productByCode[0]["TOURIST_SPOT"], 
        'code'=>$productByCode[0]["TOURIST_SPOT_ID"], 
        'quantity'=>$_POST["quantity"], 
        'PRICE_PER_LOC'=>$productByCode[0]["PRICE_PER_LOC"]));

      if(!empty($_SESSION["cart_item"])) {
        if(in_array($productByCode[0]["TOURIST_SPOT_ID"],array_keys($_SESSION["cart_item"]))) {
          foreach($_SESSION["cart_item"] as $k => $v) {
              if($productByCode[0]["TOURIST_SPOT_ID"] == $k) {
                if(empty($_SESSION["cart_item"][$k]["quantity"])) {
                  $_SESSION["cart_item"][$k]["quantity"] = 0;
                }
                $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
              }
          }
        } else {
          $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
        }
      } else {
        $_SESSION["cart_item"] = $itemArray;
      }
    }
  break;
  case "remove":
    if(!empty($_SESSION["cart_item"])) {
      foreach($_SESSION["cart_item"] as $k => $v) {
          if($_GET["TOURIST_SPOT_ID"] == $k)
            unset($_SESSION["cart_item"][$k]);        
          if(empty($_SESSION["cart_item"]))
            unset($_SESSION["cart_item"]);
      }
    }
  break;
  case "empty":
    unset($_SESSION["cart_item"]);
  break;  
}
}
?>

If user click add the selected destination will be displayed on the left panel.

enter image description here

This is my code on how I display the selected destination to left panel and passing the parameters to URL.

<div class="ui teal segment">
          <?php
          if(isset($_SESSION["cart_item"])){
          $total_quantity = 0;
          ?>
           <div class="">
            <?php   
              foreach ($_SESSION["cart_item"] as $item){
            ?>
            <div class="ui clearing segment iti-details">
             <div class="ui left floated header">
                <h2 class="ui header">
              <img class=" large image" src="resources/images/bg6.jpg">
              <div class="content">

                <div class=""><?php echo $item["TOURIST_SPOT"]; ?>
                </div>
              </div>
            </h2>
              </div>
              <div class="ui right floated header">
                <i class="ellipsis vertical icon iti-mini"></i>
              </div>
            </div>
            <div class="ui divider"></div>
            <?php
              $total_quantity += $item["quantity"];
              ?>
              <form action ="checkprice.php">
                  <input type = "text" name = "destinationID" value = "<?php echo $item["code"];?>">
                  <?php
            }
            ?>
           </div>
            <?php
            } else {
            ?>
            <div class="no-records">Your Tour Destination is Empty</div>
            <?php 
            }
            ?>       
          </div>

And this is my URL from another page. In this view the selected destination/s is displayed in URL but not in page.

enter image description here

2
  • 1
    PHP overwrites GET parameters of the same name, unless you use [] in the name to create an array - ?foo[]=value1&foo[]=value2&… Commented Jan 13, 2020 at 13:08
  • @04FS Thank you for the suggestion sir. Also Just would like to ask Im going to select the destination and display on the other part without page refresh? Thank you Commented Jan 28, 2020 at 3:52

2 Answers 2

1

U can use explode() to help u out.

$url_array =  explode('/', $_SERVER['REQUEST_URI']) ;
$url = end($url_array);  

just assign it to a value then explode to an array of objects, the string u get u can use directly in a query.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the suggestion sir. Also Just would like to ask Im going to select the destination and display on the other part without page refresh? Thank you
As soon as that php page is requested by user u can grab the url as string. You could grab it on previous with params added to next.
Can you please elaborate? I mean, how I'm going to handle the page button clicked without page refresh? Thank you
You don't, php does. When user clicks say on www.example.com/home? and you have him select destinations etc. then when he goes to next page you could grab those items when opening new via session or use request uri from the server then explode into array. The uri grabs whatever is in top without www.example.com/home?etchere here is a link about globals php.net/manual/en/reserved.variables.globals.php
1

parse_str() function can be used to get URI parameters at once. More details can be found on this link. Also, above parameters can also directly assign to variables with following code.

<?php extract(parse_str($_GET)); ?>

with this you can directly check the get parameter and print its value.

For an example, if url will is http://www.example.com/test?param1=hi&param2=hello

you can directly print the value of param1 with following code.

<?php echo $param1; ?> 

1 Comment

Thank you for the suggestion sir. Also Just would like to ask Im going to select the destination and display on the other part without page refresh? Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.