skip to Main Content

I basically have it so that my while loop makes a bunch of tags that link to a page called car.php. On car.php I want it so that the vehicle_id is transferred from the clicked tag within the original page to car.php so I can use that primary key to display all of the other information on the car.php.

Currently, this is my extremely messy while loop, what it currently does is give me the final result only, meaning when I go to car.php it simply gives me the final car owned by a user and none of the other ones. If I remove $i++ it gives the very first car. This happens with every button also, no matter which tag I click I get the same result, either the first or final result. I basically just want a way to know which tag was clicked.

<?php
                $clicked = 0;
                $i = 0;
                if ($result = mysqli_query($conn, $sql)) {

                    while ($row = mysqli_fetch_array($result)) {
                        $cars[] = array(
                            'vehicle_id' => $row['vehicle_id']
                        );
                        ?>
                        
                        <a $i++; href="car"<?php $_SESSION["vehicle_id"]=$cars[$i]['vehicle_id'];?>; class="carrow col-10";?><?php echo $row['vehicle_make']; echo $row['vehicle_model']; echo $row['vehicle_id'];?><hr></a>
                        

                        <?php
                        $i++;
                    }
                    
                    
                    
                    echo var_dump($cars);
                    }   
                    ?>

2

Answers


  1. You could mean this (broken across several lines for readability)

    <a 
        href="car.php?vehicle_id=<?php echo $row['vehicle_id']; ?>"
        class="carrow col-10"
    >
        <?php echo "{$row['vehicle_make']} {$row['vehicle_model']} {$row['vehicle_id']}"; ?>
        <hr>
    </a>
    

    On the next page (car.php) you can retrieve it by using $_GET[‘vehicle_id’]

    Example

    if (isset($_GET['vehicle_id'])) {
        $vehicle_id = $_GET['vehicle_id'];
    }
    
    Login or Signup to reply.
  2. I think you should do the following:

    declare $cars array outside the loop:

    $cars = [];

    And within the loop, add vehicle id to $cars array;

    Updated solution:

    <?php
                $clicked = 0;
                $i = 0;
                if ($result = mysqli_query($conn, $sql)) {
                    $cars = [];
                    while ($row = mysqli_fetch_array($result)) {
                        $temp = [];
                        $temp['vehicle_id'] = $row['vehicle_id'];
                        $cars[] = $temp;
                        ?>
                        
                        <a $i++; href="car"<?php $_SESSION["vehicle_id"]=$cars[$i]['vehicle_id'];?>; class="carrow col-10";?><?php echo $row['vehicle_make']; echo $row['vehicle_model']; echo $row['vehicle_id'];?><hr></a>
                        
    
                        <?php
                        $i++;
                    }
                    
                    
                    
                    echo var_dump($cars);
                    }   
                    ?>
    

    Hope this helps.

    <?php
                $clicked = 0;
                $i = 0;
                if ($result = mysqli_query($conn, $sql)) {
    
                    while ($row = mysqli_fetch_array($result)) {
                        $cars[] = array(
                            'vehicle_id' => $row['vehicle_id']
                        );
                        ?>
                        
                        <a $i++; href="car"<?php $_SESSION["vehicle_id"]=$cars[$i]['vehicle_id'];?>; class="carrow col-10";?><?php echo $row['vehicle_make']; echo $row['vehicle_model']; echo $row['vehicle_id'];?><hr></a>
                        
    
                        <?php
                        $i++;
                    }
                    
                    
                    
                    echo var_dump($cars);
                    }   
                    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search