Search code examples
phphtmlvariablesmysqlisubmenu

how to pass value from while loop to next page after clicking on menu item php


I have a a navigation bar and one of the nav items is a dropdown with sub categories. Only the subcategories are being pulled from the database by using a while loop.

When clicking on one of the dropdown items they will be redirected to dancerProfile.php.

I want dancerProfile.php to pull the menu item name from the other pages.

html

 <li class="li-spacing"><a href="parentdashboard.php">Home</a></li>
                        <li class="dropdown li-spacing">
                            <a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                <?php
                            $dancers = "SELECT `dancer_name` FROM `dancers` WHERE name = '$name'";
                            $dres = mysqli_query($con,$dancers); 

                            //if($res==FALSE){
                            //die('there was an error running query [' . $con->error . ']');
                            //  }

                            while($data=mysqli_fetch_array($dres)){
                              $dancerName = $data['dancer_name'];
                              foreach ($dancerName as $DANCER){
                                echo '
                                   <li><a href="dancerProfile.php?dancer_name=<?php echo $DANCER; ?>">'.$data["dancer_name"].'</a></li>
                                   <li class="divider"><hr></li>
                                ';
                              }
                            }
                            ?>
                            <li><a href="addDancer.php">Add New</a></li>
                            </ul>
                        </li>

This works well as all dancers appear in the dropdown. What I want is that when I click on dancerA the dancerProfile.php will show dancerA information. And when I click on dancerB it will show dancerB info, etc.

But it will only show the last dancer's information no matter which name I click on.

dancerProfile.php

 <div class="col-sm-6 dancerInfo">
                <div class="row">
                    <div class="col-sm-6">
                        <div class="dancerName"><?php echo $dancerName;?></div>
                    </div>

So When I click on dancerA on the nav bar in any other page, in dancerProfile.php $dancerName should print dancerA. And it should print dancerB if I clicked on dancerB from the nav bar.

but it is only printing dancerB no matter which link I click.

I am using bootstrap. Can anyone help me?

EDIT Here's an update of what my code looks like now.

<li class="li-spacing"><a href="parentdashboard.php">Home</a></li>
                        <li class="dropdown li-spacing">
                            <a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                <?php
                            $dancers = "SELECT `dancer_name`, `id` FROM `dancers` WHERE name = '$name'";
                            $dres = mysqli_query($con,$dancers); 
                            $dancerId= 'id';
                             }

                            while($data=mysqli_fetch_array($dres)){
                              $dancerName = $data['dancer_name'];
                                echo '
                                   <li><a href="dancerProfile.php?id=<?php echo $dancerId; ?>">'.$data["dancer_name"].'</a></li>
                                   <li class="divider"><hr></li>
                                ';
                              }
                            ?>
                            <li><a href="addDancer.php">Add New</a></li>
                            </ul>
                        </li>

And dancerProfile.php:

<?PHP
if(isset($_GET['id'])) {
$dancerId=$_GET['id'];
$dancerquery = "SELECT `dancer_name` FROM `dancers` WHERE id = " . $_GET['id'] . ";";
                $dancer_res = mysqli_query($con,$dancers);
                if($dancer_res){
                    $DANCER='dancer_name';
                }
            }
            ?>
<div class="col-sm-6 dancerInfo">
                <div class="row">
                    <div class="col-sm-6">
                        <div class="dancerName"><?php echo " $DANCER ";?></div>
                    </div>

I also forgot to mention that this navigation is also on dancerProfile page. So all of the code I am providing is on dancerProfile page. I don't know if that matters

My database table


Solution

  • You need pass dancer id or which is unique in your dancer table. something like given below.

     <li><a href="dancerProfile.php?dancer_id=<?php echo $DANCER_id; ?>">'.$data["dancer_name"].'</a></li>
    

    And now go to dancerProfile.php and try something like this.

    if (isset($_GET['dancer_id'])) {
        $dancer_id=$_GET['dancer_id'];
        //your query
    }
    

    Your full code:

    <li class="li-spacing"><a href="parentdashboard.php">Home</a></li>
    <li class="dropdown li-spacing">
        <a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
        <ul class="dropdown-menu">
            <?php
        $dancers = "SELECT `dancer_name`, `id` FROM `dancers` WHERE name = '$name'";
        $dres = mysqli_query($con,$dancers); 
        $dancerId= 'id';
         }
    
        while($data=mysqli_fetch_array($dres)){ ?>
          $dancerName = $data['dancer_name'];
    
               <li><a href="dancerProfile.php?id=<?php echo $data['id'];?>"><?php echo $data['dancer_name']; ?>'</a></li>
               <li class="divider"><hr></li>
    
          <?php } ?>
        <li><a href="addDancer.php">Add New</a></li>
        </ul>
    </li>
    

    Your dancerProfile.php should be

    <?PHP
    if(isset($_GET['id'])) {
    $dancerId=$_GET['id'];
    $dancerquery = "SELECT `dancer_name` FROM `dancers` WHERE id = '$dancerId'";
        $dancer_res = mysqli_query($con,$dancerquery);
        $data=mysqli_fetch_array($dancer_res);
    }
    ?>
    
    
    <div class="col-sm-6 dancerInfo">
        <div class="row">
            <div class="col-sm-6">
                <div class="dancerName"><?php echo $data['dancer_name'];?></div>
    </div>