where and while loop not seem to be working - php

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.

Related

After clicking the submit button in a form, everything became undefined

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: &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<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.

how to view submenu on click using php,html

I'm trying to view submenu after click on parent.
Classes->Primary School->(Grade1,Grade2,Grade3,Grade4,Grade5,Grade6)
Classes->Middle School->(Grade7,Grade8,Grade9)
Classes->Secondary School->(Grade10,Grade11,Grade12)
<div class="collapse navbar-collapse" id="main-navigation">
<ul class="nav navbar-nav navbar-right">
<?php foreach ($data as $menu) { ?>
<?php if(!$menu->children) { ?>
<li><?php echo $menu->name; ?></li>
<?php }
else {
?>
<li class="dropdown open">
<a href="#" class="dropdown-toggle " data-toggle="dropdown">
<?php echo $menu->name; ?>
<span class="fa fa-angle-down"></span>
</a>
<ul class="dropdown-menu open" role="menu">
<?php
foreach ($menu->children as $child) {
?>
<li><?php echo $child->name; ?></li>
<?php
foreach ($child->children as $sub_child) {
?>
<li class="dropdown-submenu">
<?php echo $sub_child->name; ?><span class="fa fa-angle-down"></span>
<ul class="dropdown-submenu" role="menu">
<?php } ?>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php } ?>
</ul>
</li>
</ul>
</div>
The result:
Problem with result
I want to view Grade 1 to Grade 6 after click on Primary school and from Grade 7 to Grade 9 after click on Middle school ...
Please help!
You have an open <ul class="dropdown-submenu" role="menu"> tag without any <li> or <a>'s being generated inside of it. Then you close two each statements before closing the <ul> tag.
<?php foreach ($child->children as $sub_child) { ?>
<li class="dropdown-submenu">
<a href="#" style="color:black;" class="dropdown-toggle " data-toggle="dropdown">
<?php echo $sub_child->name; ?><span class="fa fa-angle-down"></span>
</a>
<ul class="dropdown-submenu" role="menu">
<?php } ?>
<?php } ?>
</ul>
</li>
This will be causing all sorts of unintended markup output. Fix that and it should fix your problem. If not, you'll atleast be a lot closer.

How To Display In A List Using PHP

How do you repeatedly display the ID numbers in PHP then in a list?
This is the code I have in View
<ul class="dropdown-menu" role="menu">
<li><?php echo ?></li>
</ul>
In Controller
$query=$this->mdl_mainmenu->notif_items();
$data['notifs'] = $query;
$this->load->view('vw_main_menu',$data);
In Model
function notif_items(){
$user = $this->session->userdata("username");
$this->csbrms->select("deleted_user_request.sdp_no,
deleted_user_request.approval_status");
$this->csbrms->from('deleted_user_request');
$this->csbrms->join('m_employee_masters', 'm_employee_masters.emp_no = user_request.emp_no');
$this->csbrms->where('deleted_user_request.created_by',$user);
$this->csbrms->where('deleted_user_request.approval_status' , 'Closed');
$this->csbrms->where('deleted_user_request.approval_status' , 'Resolved');
$query = $this->csbrms->get();
return $query->result();
}
changed ul tag to :
<ul class="dropdown-menu" role="menu">
<?php foreach($vw_main_menu['notifs'] as $row):?>
<li>
<?php echo $row['deleted_user_request.sdp_no']?>
</li>
<?php endforeach;?>
</ul>
<ul class="dropdown-menu" role="menu">
<?php foreach($notifs as $row):?>
<li>
<?php echo $row->deleted_user_request.sdp_no; ?>
</li>
<?php endforeach;?>
</ul>

Select from table echoes one instead of more rows

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 :)

Add a class to <li> if sub menu exist using mysqli query

I want to know, how do i add a class to a <li> if it has a sub_menu?
I have a proper HTML menu populated from mysql database.
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li>About us
<ul class="dropdown-menu">
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
<li>Link 5</li>
</ul>
</li>
<li class="dropdown">Contact</li>
</ul>
</div>
I am using bootstrap framework, so for the drop down menu i have to replace my <li> to this:
<li class='dropdown'>
<a aria-expanded='false' aria-haspopup='true' class='dropdown-toggle' data-toggle='dropdown' href='/about-us/' role='button'>
About us <span class='caret'></span>
</a>
</li>
I want to know how do i check if the <li> has any sub_menu before running a sub menu query in the loop? so that i can add markup for dropdown menu.
Here's the PHP Code that generates MENU:
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<?php
$res1 = $connection->query("SELECT * FROM primary_nav ORDER BY m_menu_id ASC");
while($menu=$res1->fetch_array())
{
?>
<li>
<a href="<?php echo $domain; ?><?php echo $menu['m_menu_link']; ?>">
<?php echo $menu['m_menu_name']; ?>
</a>
<?php
$res1_pro=$connection->query("SELECT * FROM primary_subnav WHERE m_menu_id=".$menu['m_menu_id']." ORDER BY seq ASC");
//echo "\xA";
?>
<?php
if(mysqli_num_rows($res1_pro) > 0) {
echo '<ul class="dropdown-menu">' ."\xA";
while ($pro1_row = $res1_pro->fetch_array()) {
?>
<li>
<a href="<?php echo $domain; ?><?php echo $pro1_row['s_menu_link']; ?>">
<?php echo $pro1_row['s_menu_name']; ?>
</a>
</li>
<?php
}
echo '</ul>' ."\xA";
}
?>
</li>
<?php
echo "\xA";
}
?>
</ul>
</div>
I could do thi with the jQuery, but i want to do it the HTML way.
You have to add the <li> with the class in the if check for the number of rows. Then you have to add an else clause to echo out the <li> without the class:
<?php
$res1 = $connection->query("SELECT * FROM primary_nav ORDER BY m_menu_id ASC");
while($menu=$res1->fetch_array())
{
$res1_pro=$connection->query("SELECT * FROM primary_subnav WHERE m_menu_id=".$menu['m_menu_id']." ORDER BY seq ASC");
//echo "\xA";
if(mysqli_num_rows($res1_pro) > 0) { // echo the list element with the class here
?>
<li class="foo">
<?php echo $menu['m_menu_name']; ?>
<?php
echo '<ul class="dropdown-menu">' ."\xA";
while ($pro1_row = $res1_pro->fetch_array()) {
?>
<li>
<?php echo $pro1_row['s_menu_name']; ?>
</li>
<?php
}
echo '</ul>' ."\xA";
} else { // echo the normal list element if number of rows is 0
?>
<li>
<?php echo $menu['m_menu_name']; ?>
<?php
}
?>
</li>
<?php
echo "\xA";
}
?>
Note: This particular coding style makes it very hard to write and maintain code should the need arise. You may want to look into ways to combine PHP and HTML to make it easier to update and maintain your code.

Categories