I am facing a problem and I would like your help!
I have built this:
$stmt_select = $conn->prepare("SELECT * FROM `stores` WHERE `user_id`=$id;");
$stmt_select->execute();
while ($row_select = $stmt_select->fetch(PDO::FETCH_ASSOC)) {
echo '<ul class="dropdown-menu dropdown-alerts">
<li>
<a href="#">
<div>
<i class="fa fa-home"></i> <strong>'. $row_select['area_store'] .'</strong>
<span class="pull-right text-muted small"><i>'. $row_select['name_store'] .'</i></span>
</div>
</a>
</li>
</ul>';
};
I have two rows into the table but echo displays only one result.Any ideas?
try this code:
$stmt_select = $conn->prepare("SELECT * FROM `stores` WHERE `user_id`=$id;");
$stmt_select->execute();
$res = $stmt_select->get_result();
while ($row_select = $res->fetch_assoc()) {
echo '<ul class="dropdown-menu dropdown-alerts">
<li>
<a href="#">
<div>
<i class="fa fa-home"></i> <strong>'. $row_select['area_store'] .'</strong>
<span class="pull-right text-muted small"><i>'. $row_select['name_store'] .'</i></span>
</div>
</a>
</li>
</ul>';
};
You could fetchAll the data and than do a foreach loop like so
$stmt_select = $conn->prepare("SELECT * FROM `stores` WHERE `user_id`=$id;");
$stmt_select->execute();
$stmt_select = $stmt_select->fetchAll(PDO::FETCH_ASSOC);
foreach ($stmt_select as $row) {
echo '<ul class="dropdown-menu dropdown-alerts">
<li>
<a href="#">
<div>
<i class="fa fa-home"></i> <strong>'. $row['area_store'] .'</strong>
<span class="pull-right text-muted small"><i>'. $row['name_store'] .'</i></span>
</div>
</a>
</li>
</ul>';
}
also i would suggest you to escape your variables before entering the sql query.
$stmt_select = $conn->prepare("SELECT * FROM `stores` WHERE `user_id`= :id");
$stmt_select->execute(["id" => $id]);
$stmt_select = $stmt_select->fetchAll(PDO::FETCH_ASSOC);
Try this it will help solve ur probem
$stmt =$conn->prepare("SELECT * FROM stores where user_id=:user_id")
$stmt->bindValue(":user_id", $id);
$stmt->execute();
$results=$stmt->fetchall(PDO::FETCH_ASSOC);
if(count($results) > 0 ){
foreach ($row as $results) {
echo "<ul class=\"dropdown-menu dropdown-alerts\">
<li>
<a href=\"#\">
<div>
<i class=\"fa fa-home\"></i> <strong>".$row['area_store'] ."</strong>
<span class=\"pull-right text-muted small\"><i>".$row['name_store'] ."</i></span>
</div>
</a>
</li>
</ul>";
}
}elseif (count($results) <= 0) {
echo "no records found";
}
if you found this helpful please mark the answer :)
Related
Hi. Everything is working perfectly until I clicked the submit button.
This is what it looks like before clicking the submit button. But after clicking it, this is what it will look like. After clicking it, all the information became undefined. Can someone please help me.
I dont know what went wrong. Here is my code.
<?php
session_start();
if(!isset($_SESSION["user"]))
{
header("location:index.php");
}
ob_start();
include ('db.php');
$pid = $_GET['pid'];
$sql ="select * from reservation where reservationno = '$pid' ";
$re = mysqli_query($con,$sql);
while($row=mysqli_fetch_array($re))
{
$clientid = $row['clientid'];
$name = $row['name'];
$reservationno = $row['reservationno'];
$invoiceno = $row['invoiceno'];
$reservationdate = $row['reservationdate'];
$totalamount = $row['totalamount'];
$netamount = $row['netamount'];
$cin = $row['reservefrom'];
$cout = $row['reserveto'];
}
$asql ="select * from reservationdetails where reservationno = '$pid' ";
$are = mysqli_query($con,$asql);
while($row=mysqli_fetch_array($are))
{
$cout2 = $row['checkout2'];
$cout3 = $row['checkout3'];
$days = $row['days'];
$days2 = $row['days2'];
$days3 = $row['days3'];
$roomid = $row['roomid'];
$roomid2 = $row['roomid2'];
$roomid3 = $row['roomid3'];
$qty = $row['qty'];
$qty2 = $row['qty2'];
$qty3 = $row['qty3'];
}
?>
<div id="wrapper">
<nav class="navbar navbar-default top-navbar" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".sidebar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="home.php"><?php echo $_SESSION["user"]; ?> </a>
</div>
<ul class="nav navbar-top-links navbar-right">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#" aria-expanded="false">
<i class="fa fa-user fa-fw"></i> <i class="fa fa-caret-down"></i>
</a>
<ul class="dropdown-menu dropdown-user">
<li><i class="fa fa-user fa-fw"></i> User Profile
</li>
<li><i class="fa fa-gear fa-fw"> </i> Settings
</li>
<li class="divider"></li>
<li><i class="fa fa-sign-out fa-fw"></i> Logout
</li>
</ul>
<!-- /.dropdown-user -->
</li>
<!-- /.dropdown -->
</ul>
</nav>
<!--/. NAV TOP -->
<nav class="navbar-default navbar-side" role="navigation">
<div class="sidebar-collapse">
<ul class="nav" id="main-menu">
<li>
<i class="fa fa-dashboard"></i> Status
</li>
<li>
<i class="fa fa-desktop"></i> News Letters
</li>
<li>
<i class="fa fa-bar-chart-o"></i>Room Booking
</li>
<li>
<a class="active-menu" href="Payment.php"><i class="fa fa-qrcode"></i> Payment</a>
</li>
<li>
<i class="fa fa-qrcode"></i> Reports
</li>
<li>
</i> Logout
</li>
</div>
</nav>
<!-- /. NAV SIDE -->
<div id="page-wrapper" >
<div id="page-inner">
<div class="row">
<div class="col-md-12">
<h3 class="page-header">
Update Payment
</h3>
</div>
</div>
<!-- /. ROW -->`
<?php
include('db.php');
$mail = "SELECT * FROM `contact`";
$rew = mysqli_query($con,$mail);
?>
<div class="row">
<div class="col-md-12">
<div class="jumbotron">
<div class="panel-body">
<form>
<?php echo $clientid; ?><br>
<?php echo $name; ?><br>
Invoice no: <?php echo $invoiceno; ?><br>
Total Amount: ₱<?php echo $totalamount; ?><br>
Remaining Balance:
<br><br><br>
<h5>Payment:               <h5>
<input type="text" name="updatetextbox" /><br><br>
<h5>Confirm Payment:</h5>
<input type="text" name="updatetextbox1" />
<br>
<center><input type="submit" class="btn btn-primary" />
<?php
if(isset($_POST['submit'])){
$code1=$_POST['updatetextbox1'];
$code=$_POST['updatetextbox'];
if($code1!="$code"){
//$msg="Invalid code";
echo "<script type='text/javascript'> alert('Error')</script>";
}
else
$curdate=date("Y/m/d");
$paymentmode = "Cash";
$amountpaid_cash = 1.00;
$paymentdetails = "INSERT INTO `payments` (`clientid`, `name`, `reservationno`, `paymentmode`, `creditcardno`, `bankname`, `amountpaid_cc`, `amountpaid_cash`, `invoiceno`, `datepaid`) VALUES ('$clientid', '$name', '$reservationno', '$paymentmode', 'NULL', 'NULL', 'NULL', '$amountpaid_cash', '$invoiceno', '$curdate')";
if (mysqli_query($con,$paymentdetails))
{
echo "<script type='text/javascript'> alert('Your Booking application has been sent')</script>";
}
else
{
echo "<script type='text/javascript'> alert('Error adding user in database')</script>";
}
}
?>
</form>
</div>
</div>
</div>
<?php
$sql = "SELECT * FROM `contact`";
$re = mysqli_query($con,$sql);
?>
<!-- /. ROW -->
</div>
</div>
</div>
<!-- /. PAGE INNER -->
</div>
The problem is that you don't pass any of that information to the page after you submit it. Your first example shows pid as an HTTP get variable and that's what you use to define everything else. In your second example (after submit), there is no pid variable. Your code has no way to get the information unless you pass it along with the submit request or you include pid again, so that it can do the same lookup.
for those who came too late here is the answer to this question
you just need to put the insertion statement inside the :
This image will explain the solution
this happened because of the scope of the variables can't be reached outside the if statement block.
I want to select some values using 'where' and 'and' clause in bootstrap. The code selects only one value meanwhile there are more than one in the database. Here goes the code:
<ul class="collapsible collapsible-accordion">
<li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i>ENGLISH LANGUAGE<i class="fa fa-angle-down rotate-icon"></i></a>
<?php
$sql = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE subject = 'ENGLISH LANGUAGE' AND class = 'JHS 2'";
$result = $pdo->query($sql);
$result->setFetchMode(PDO::FETCH_ASSOC);
while($row=$result->fetch()){
?>
<div class="collapsible-body">
<ul class="list-unstyled">
<?php echo '<li>'?><a data-id="<?php echo $key['note_id']?>" href="<?php echo $key['note_id']?>" class="waves-effect" id="getUser"><?php echo $key['topic']?></a>
<?php echo '</li>';?>
<li>Registration form
</li>
</ul>
</div>
</li>
<?php
}
?>
</ul>
If you want to fetch multiple records in a loop, you should use method ::fetch_row() (PHP.net).
So your code should look like this:
<ul class="collapsible collapsible-accordion">
<li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i>ENGLISH LANGUAGE<i class="fa fa-angle-down rotate-icon"></i></a>
<?php
$sql = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE subject = 'ENGLISH LANGUAGE' AND class = 'JHS 2'";
$result = $pdo->query($sql);
$result->setFetchMode(PDO::FETCH_ASSOC);
while($row=$result->fetch_row()){
?>
<div class="collapsible-body">
<ul class="list-unstyled">
<?php echo '<li>'?><a data-id="<?php echo $key['note_id']?>" href="<?php echo $key['note_id']?>" class="waves-effect" id="getUser"><?php echo $key['topic']?></a>
<?php echo '</li>';?>
<li>Registration form
</li>
</ul>
</div>
</li>
<?php
}
?>
</ul>
By using ::fetch() method, you are fetching whole results array/object.
I'm working on one webpage project and using php to show information of computers. I stored the urls of images in one relation and could successfully retrieve them out with php. But they cannot be shown in the webpage. Here's my code:
<?php
require_once('db_setup_hetty.php');
$sql = "USE hzhu24;";
if ($conn->query($sql) === TRUE){
//echo "using Database hzhu24"
}else{
echo "Error using database:" . $conn->error;
}
$C1 = $_POST["C1"];
$C2 = $_POST["C2"];
$query = "SELECT * FROM Items where Category1 Like '%$C1%' and Category2 Like '%$C2%';";
$result = $conn->query($query);
if($result->num_rows > 0){
?>
<?php
while($row = $result->fetch_assoc()){
$url = $row['Pic_url'];
?>
<div class="col-sm-4">
<div class="thumbnail">
<!--span class="e-label"><div>Sale</div></span-->
<span class="service-link text-center">
<img src= $url>
<div class="list-inline">
<i class="fa fa-eye"></i>
<i class="fa fa-link"></i>
</div>
</span>
<div class="caption">
<div class="category"> <?php echo $C2; ?>
<div class="pull-right">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star-o"></i>
</div>
</div>
<h3><?php echo $row['Item_name']; ?></h3>
<h4><?php echo $row['Brand']; ?></h4>
<!--strong>$899.00</strong-->
<div>Details</div>
</div>
</div>
</div>
<?php
}
}
else {
echo "Item not found";
}
echo "</table>"
?>
And the webpage looks like:
If your php version >= 5.4, you can just use short echo tag like this:
<?php
require_once('db_setup_hetty.php');
$sql = "USE hzhu24;";
if ($conn->query($sql) === TRUE){
//echo "using Database hzhu24"
}else{
echo "Error using database:" . $conn->error;
}
$C1 = $_POST["C1"];
$C2 = $_POST["C2"];
$query = "SELECT * FROM Items where Category1 Like '%$C1%' and Category2 Like '%$C2%';";
$result = $conn->query($query);
if($result->num_rows > 0){
?>
<?php
while($row = $result->fetch_assoc()){
$url = $row['Pic_url'];
?>
<div class="col-sm-4">
<div class="thumbnail">
<!--span class="e-label"><div>Sale</div></span-->
<span class="service-link text-center">
<img src=<?=$url;?>>
<div class="list-inline">
<i class="fa fa-eye"></i>
<i class="fa fa-link"></i>
</div>
</span>
<div class="caption">
<div class="category">
<?=$C2;?>
<div class="pull-right">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star-o"></i>
</div>
</div>
<h3><?=$row['Item_name'];?></h3>
<h4><?=$row['Brand'];?></h4>
<!--strong>$899.00</strong-->
<div>Details</div>
</div>
</div>
</div>
<?php
}
}
else {
echo "Item not found";
}
echo "</table>"
I have a HTML structure with bootstrap 3 columns, which all div repeat 3 times only, but all div have one ul and each ul has 7 li.
I want to show the dynamic list according to the below HTML structure:
<div class="col-md-4 padding-left">
<ul class="unstyled">
<li><i class="fa fa-angle-right"></i> Mechanical Engineering Jobs</li>
<li><i class="fa fa-angle-right"></i> BPO Jobs</li>
<li><i class="fa fa-angle-right"></i> Networking Jobs</li>
<li><i class="fa fa-angle-right"></i> Java Jobs</li>
<li><i class="fa fa-angle-right"></i> Online Marketing Jobs</li>
<li><i class="fa fa-angle-right"></i> Animation Jobs</li>
<li><i class="fa fa-angle-right"></i> Design Engineer Jobs</li>
</ul>
</div>
<div class="col-md-4 padding-left">
<ul class="unstyled">
<li><i class="fa fa-angle-right"></i> Analytics Jobs</li>
<li><i class="fa fa-angle-right"></i> UI/UX Jobs</li>
<li><i class="fa fa-angle-right"></i> NLP Jobs</li>
<li><i class="fa fa-angle-right"></i> Marketing Jobs</li>
<li><i class="fa fa-angle-right"></i> Banking Jobs</li>
<li><i class="fa fa-angle-right"></i> MBA Jobs</li>
<li><i class="fa fa-angle-right"></i> Teaching Jobs</li>
</ul>
</div>
<div class="col-md-4 padding-left">
<ul class="unstyled">
<li><i class="fa fa-angle-right"></i> Accounting Jobs</li>
<li><i class="fa fa-angle-right"></i> Retail Jobs</li>
<li><i class="fa fa-angle-right"></i> Travel Jobs</li>
<li><i class="fa fa-angle-right"></i> Merchandiser Jobs</li>
<li><i class="fa fa-angle-right"></i> Architecture Jobs</li>
<li><i class="fa fa-angle-right"></i> Banking Insurance Jobs</li>
<li><i class="fa fa-angle-right"></i> Music Jobs</li>
</ul>
</div>
For this I have seen the below answers and tried with the below code.
I have tried with the below code but it's not working as is should.
<?php
$sqlEng = mysql_query('select * from jobs_category');
$count = 1;
while($resEng = mysql_fetch_array($sqlEng)){
if ($count%3 == 1)
{
?>
<div class="col-md-4 padding-left">
<?php } ?>
<ul class="unstyled">
<li><i class="fa fa-angle-right"></i> <?php echo $resEng['name'];?></li>
</ul>
<?php if ($count%3 == 0)
{
?>
</div>
<?php } $count++; } ?>
Assuming you have only 21 records in the table.
try this.
<?php
//$sqlEng = mysql_query('select * from jobs_category');
$servername = "localhost:3306";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password, "stack1");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sqlEng = $conn->query('select * from jobs_category');
$count = 1;
while($resEng = $sqlEng->fetch_assoc()){
if ($count%7 == 1)
{
?>
<div class="col-md-4 padding-left">
<ul class="unstyled">
<?php } ?>
<li><i class="fa fa-angle-right"></i> <?php echo $resEng['name'];?></li>
<?php if ($count%7 == 0)
{
?>
</ul>
</div>
<?php } $count++;
} ?>
tested in my local.
If I get your question correctly, you are trying to spread the list items <li> A Job </li> across three lists <div> <ul> List Items </ul> </div>
<?php
$sqlEng = mysql_query('select * from jobs_category');
$count = 1;
$group_one = "
<div class='col-md-4 padding-left'>
<ul class='unstyled'>
";
$group_two = "
<div class='col-md-4 padding-left'>
<ul class='unstyled'>
";
$group_three = "
<div class='col-md-4 padding-left'>
<ul class='unstyled'>
";
while($resEng = mysql_fetch_array($sqlEng)){
if($count > 3) {
$count = 1;
}
switch ($count) {
case 1:
$group_one .= "
<li><a href='#'><i class='fa fa-angle-right'></i> " .$resEng['name'] . "</a></li>";
break;
case 2:
$group_two .= "
<li><a href='#'><i class='fa fa-angle-right'></i> " .$resEng['name'] . "</a></li>";
break;
case 3:
$group_three .= "
<li><a href='#'><i class='fa fa-angle-right'></i> " .$resEng['name'] . "</a></li>";
}
$count++;
}
$group_one .= "
</ul>
</div>";
$group_two .= "
</ul>
</div>";
$group_three .= "
</ul>
</div>";
echo $group_one . $group_two . $group_three;
?>
You want to consider using MySQl improved mysqli https://www.w3schools.com/php/php_ref_mysqli.asp
I want to return multiple notifications within a loop. I have 2 functions, the first one which pulls the notifications from the database and the second one for the dashboard header. The dashboard header is where the notifications will be displayed.
Now, the issue is, for some reason only one notification will be displayed. I've tried changing the return inside the loop to echo but that outputs the notifications at the beginning of dashboardTop().
What's going wrong and how can I resolve this?
public function loopNotifications() {
$stmt = $this->conn->prepare("SELECT * FROM notification WHERE isto=:user_id");
$stmt->execute(array(':user_id'=>$_SESSION['user_id']));
while ($notification = $stmt->fetch(PDO::FETCH_ASSOC)) {
$from = $this->pullName($notification['isto']);
return '<li>' . $notification['content'] . '</li>';
}
}
public function dashboardTop() {
echo '
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="material-icons">notifications</i>
<span class="notification">' . $this->notificationCount() . '</span>
<p class="hidden-lg hidden-md">Notifications</p>
</a>
<ul class="dropdown-menu">
' . $this->loopNotifications() . '
</ul>
</li>
<li>
<a href="index.php?page=profile" class="dropdown-toggle" data-toggle="dropdown">
<i class="material-icons">person</i>
<p class="hidden-lg hidden-md">Profile</p>
</a>
</li>
</ul>
</div>
';
}
public function loopNotifications() {
$stmt = $this->conn->prepare("SELECT * FROM notification WHERE isto=:user_id");
$stmt->execute(array(':user_id'=>$_SESSION['user_id']));
$out=''; //empty var tof concaternation
while ($notification = $stmt->fetch(PDO::FETCH_ASSOC)) {
$from = $this->pullName($notification['isto']);
$out.= '<li>' . $notification['content'] . '</li>'; //concaternate each line in to string
}
return $out; //return whole string
}
public function dashboardTop() {
echo '
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="material-icons">notifications</i>
<span class="notification">' . $this->notificationCount() . '</span>
<p class="hidden-lg hidden-md">Notifications</p>
</a>
<ul class="dropdown-menu">
' . $this->loopNotifications() . '
</ul>
</li>
<li>
<a href="index.php?page=profile" class="dropdown-toggle" data-toggle="dropdown">
<i class="material-icons">person</i>
<p class="hidden-lg hidden-md">Profile</p>
</a>
</li>
</ul>
</div>
';
}