[英]how to pass value from while loop to next page after clicking on menu item php
I have aa 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.
使用while循环仅从数据库中提取子类别。
When clicking on one of the dropdown items they will be redirected to dancerProfile.php. 当单击其中一个下拉项时,它们将被重定向到DancerProfile.php。
I want dancerProfile.php to pull the menu item name from the other pages. 我希望DancerProfile.php从其他页面中提取菜单项名称。
html 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.
我想要的是,当我单击舞者A时,该舞者Profile.php将显示舞者A信息。 And when I click on dancerB it will show dancerB info, etc.
当我单击“舞者B”时,将显示“舞者B”信息等。
But it will only show the last dancer's information no matter which name I click on. 但是无论我单击哪个名称,它都只会显示最后一位舞者的信息。
dancerProfile.php 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. 因此,当我在任何其他页面的导航栏上单击舞者A时,在舞者Profile.php中,$ dancerName应该会打印舞者A。 And it should print dancerB if I clicked on dancerB from the nav bar.
如果我从导航栏中单击舞者B,它应该打印出舞者B。
but it is only printing dancerB no matter which link I click. 但是无论我单击哪个链接,都仅打印舞者B。
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: 还有danceProfile.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.
因此,我提供的所有代码都在danceerProfile页面上。 I don't know if that matters
我不知道这是否重要
You need pass dancer id or which is unique in your dancer table. 您需要通过舞蹈演员ID或在舞蹈演员表中唯一。 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. 现在转到
dancerProfile.php
并尝试类似的操作。
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 您的
dancerProfile.php
应该是
<?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>
You are currently overriding the value of $DANCER
with each iteration, so the last will always be the used value. 当前,您每次迭代都覆盖
$DANCER
的值,因此最后一个将始终是使用的值。
Just change your loop a bit: 只需稍微改变一下循环即可:
while($data=mysqli_fetch_array($dres)){
echo '<li><a href="dancerProfile.php?dancer_name='.$data['dancer_name'].'">'.$data['dancer_name'].'</a></li>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.