I am trying to make a filter for website of cars. I would like to be able to filter by color of car represented by exterior. I do this by Select Distinct from exterior colors to list all of the exterior colors.
<h6 class="text-info">Select Color</h6>
<ul class="list-group">
<?php
$sql="SELECT DISTINCT exterior FROM newcars ORDER BY exterior";
$result=$conn->query($sql);
while($row=$result->fetch_assoc()) {
?>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" name="" value="<?= $row['exterior']; ?>" id="exterior"><?= $row['exterior']; ?>
</label>
</div>
</li>
<?php
}
?>
</ul>
<div id="test">
<?php
$sql = "SELECT * FROM newcars";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo ("<a href='newcarindex.php?id={$row['id']}'><div class='car'>");
echo '
<tr>
<td>
<img src="data:image\jpeg;base64,'.base64_encode($row['photo']).'"/>
</td>
</tr>
';
echo "<h2 class=car-name>";
echo $row['name'];
echo "</h2>";
echo "<span class=stock>STOCK#";
echo $row['stock'];
echo "</span>";
echo "<h3 class=car-msrp>";
echo $row['msrp'];
echo "</h3>";
echo "</div></a>";
}
} else {
echo "There are no Comments!";
}
?>
</div>
action.php this page i link to get the the results from what color they select. But I still cannot get it filter to the results.
include 'dbh.php';
if(isset($_POST['action'])) {
$sql = "SELECT * FROM newcars WHERE class !=''";
if(isset($_POST['class'])) {
$class = implode("','", $_POST['class']);
$sql .="AND class IN('".$class."')";
}
if(isset($_POST['body'])) {
$body = implode("','", $_POST['body']);
$sql .="AND class IN('".$body."')";
}
if(isset($_POST['exterior'])) {
$exterior = implode("','", $_POST['exterior']);
$sql .="AND class IN('".$exterior."')";
}
$result = $conn->query($sql);
$output='';
if($result->num_rows>0){
while($row=$result->fetch_assoc()){
$output .='<a href='newcarindex.php?id={$row['id']}'><div class='car'>
<tr>
<td>
<img src="data:image\jpeg;base64,'.base64_encode($row['photo']).'"/>
</td>
</tr>
<h2 class=car-name>'.$row['name'].'</h2>
<span class=stock>STOCK#'.$row['stock'].'</span>
<h3 class=car-msrp>'.$row['msrp'].'</h3>
</div></a>'
}
} else {
$output ="<h3>No Results</h3>";
}
echo $output;
}
?>
First of all you need to wrap your checkboxes in a HTML form that will send data to action.php file:
<h6 class="text-info">Select Color</h6>
<form action="action.php">
<ul class="list-group">
<?php
$sql = "SELECT DISTINCT exterior FROM newcars ORDER BY exterior";
$result = $conn->query($sql);
while($row=$result->fetch_assoc()) {
?>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" name="" value="<?= $row['exterior']; ?>" id="exterior"><?= $row['exterior']; ?>
</label>
</div>
</li>
<?php
}
?>
</ul>
<input type="submit" value="Filter" />
</form>
<div id="test">
...
Then, in action.php file you need to revise your SQL query. At this moment, when you concatenate base $sql variable with the one from if(isset($_POST['exterior'])) condition, the query looks like:
SELECT * FROM newcars WHERE class !=''AND class IN( ...
making it invalid (notice no space between !='' and AND).
Then the while loop has mixed apostrophes and quotation marks, which makes PHP code invalid. It should look like this:
while($row=$result->fetch_assoc()){
$photo = base64_encode($row['photo']);
$output .= "<a href='newcarindex.php?id={$row['id']}'>
<div class='car'>
<tr>
<td>
<img src='data:image\jpeg;base64,{$photo}'/>
</td>
</tr>
<h2 class='car-name'>{$row['name']}</h2>
<span class='stock'>STOCK#{$row['stock']}</span>
<h3 class='car-msrp'>{$row['msrp']}</h3>
</div></a>";
}
Related
I have a search page and the pagination is set but each time i refresh the search variable sets back to default... It also restores to default if i try to navigate to within pagination... So i want to know how to keep the value of the search variable...
The search form is in another page.
I have tried using this session_start() method but it doesn't seem to retain the search variable...
<?php require_once("../includes/initialize.php"); ?>
<?php
session_start();
if(isset($_POST['submit'])){
// 1. the current page number ($current_page)
$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
// 2. records per page ($per_page)
$per_page = 2;
// 3. total record count ($total_count)
$_SESSION['search'] = $_POST['search'];
echo $_SESSION['search'];
$search = $database->escape_value($_SESSION['search']);
$sql = "SELECT COUNT(*) FROM products WHERE image LIKE '%$search%'";
$sql .= " OR title LIKE '%$search%'";
$sql .= " OR slug LIKE '%$search%'";
$sql .= " OR description LIKE '%$search%'";
$sql .= " OR price LIKE '%$search%'";
$sql = $database->query($sql);
$sql = $database->fetch_array($sql);
$total_count = array_shift($sql);
// Find all photos
// use pagination instead
$pagination = new Pagination($page, $per_page, $total_count);
// Instead of finding all records, just find the records
$sql = "SELECT * FROM products WHERE image LIKE '%$search%'";
$sql .= " OR title LIKE '%$search%'";
$sql .= " OR slug LIKE '%$search%'";
$sql .= " OR description LIKE '%$search%'";
$sql .= " OR price LIKE '%$search%'";
$sql .= " ORDER by id DESC";
$sql .= " LIMIT {$per_page}";
$sql .= " OFFSET {$pagination->offset()}";
$photos = Product::find_by_sql($sql);
$tcount = count($photos);
}
?>
<?php include_layout_template('header2.php'); ?>
<div class="container">
<P class="lead error-msg">There are <?php echo $tcount; ?> results out of <?php echo " " . $total_count; ?></P>
<div class="row">
<div id="pagination" style="clear: both;">
<nav aria-label="Page navigation example">
<ul class="pagination">
<?php
for($i=1; $i <= $pagination->total_pages(); $i++){
if($i == $page) {
echo "<li class='page-item'><a class='page-link'> <span class=\"selected\">{$i}</span></a></li> ";
} else{
echo " <li class='page-item'><a class='page-link' href=\"search.php?page={$i}\">{$i}</a></li> ";
}
}
if($pagination->total_pages() > 1) {
if($pagination->has_previous_page()) {
echo "<li class='page-item'><a class='page-link' href=\"search.php?page=";
echo $pagination->previous_page();
echo "\">« Previous</a></li> ";
}
if($pagination->has_next_page()){
echo "<li class='page-item'><a class='page-link' href=\"search.php?page=";
echo $pagination->next_page();
echo "\">Next »</a></li>";
}
}
?>
</ul>
</nav>
</div>
</div>
</div>
<div class="container">
<div class="row">
<?php foreach ($photos as $photo): ?>
<div class="col-md-4 col-sm-6">
<div class="thumbnail">
<form method="post" action="cart.php?action=add&id=<?php echo $photo->id; ?>" role="form" class="form-vertical">
<a href="order_review.php?id=<?php echo $photo->id; ?>"><img src="<?php echo $photo->image_path();
?>" class="img-thumbnail" alt="responsive image"></a>
<div class="caption">
<h3 class="text-info text-center"><?php echo $photo->title; ?></h3>
<p class="text-muted text-center price card-header"><span class="currency">N</span><?php echo $photo->price; ?></p>
<p class="text-center"><em><?php echo $photo->description; ?></em></p>
<input type="hidden" name="id" class="form-control" value="<?php echo $photo->id; ?>" />
<input type="hidden" name="title" class="form-control" value="<?php echo $photo->title; ?>" />
<input type="hidden" name="slug" class="form-control" value="<?php echo $photo->slug; ?>" />
<input type="hidden" name="description" class="form-control" value="<?php echo $photo->description; ?>" />
<input type="hidden" name="price" class="form-control" value="<?php echo $photo->price; ?>"/>
<div class="col-sm-4 mx-auto d-block">
<select name="quantity" class="form-control" name="quantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
</div>
<div class="col-sm-4 col-sm-push-3 mx-auto d-block">
<input type="submit" name="add_to_cart" class="btn bg-success" value="Add to cart" />
</div><br/>
</form>
</div>
</div>
<?php endforeach;
?>
</div>
</div>
<?php include_layout_template('footer2.php'); ?>
Try to call session_start(); at the beginn of any php code. If this doesnt work create new page to test, if any sessions are working. Maybe the configuration is not correct.
<?php
session_start();
$_SESSION['foo'] = "test";
var_dump($_SESSION);
<?php
//var_dump($_POST);
//var_dump($_SESSION);
if(isset($_POST['submit']) || isset($_SESSION['search'])){
// 1. the current page number ($current_page)
$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
// 2. records per page ($per_page)
$per_page = 2;
// 3. total record count ($total_count)
if(isset($_POST['submit'])) {
$_SESSION['search'] = $_POST['search'];
$searchvalue = $_POST['search'];
} else {
isset($_SESSION['search']);
$searchvalue = $_SESSION['search'];
}
$search = $database->escape_value($searchvalue);
$sql = "SELECT COUNT(*) FROM products WHERE image LIKE '%$search%'";
$sql .= " OR title LIKE '%$search%'";
$sql .= " OR slug LIKE '%$search%'";
$sql .= " OR description LIKE '%$search%'";
$sql .= " OR price LIKE '%$search%'";
$sql = $database->query($sql);
$sql = $database->fetch_array($sql);
$total_count = array_shift($sql);
// Find all photos
// use pagination instead
$pagination = new Pagination($page, $per_page, $total_count);
// Instead of finding all records, just find the records
$sql = "SELECT * FROM products WHERE image LIKE '%$search%'";
$sql .= " OR title LIKE '%$search%'";
$sql .= " OR slug LIKE '%$search%'";
$sql .= " OR description LIKE '%$search%'";
$sql .= " OR price LIKE '%$search%'";
$sql .= " ORDER by id DESC";
$sql .= " LIMIT {$per_page}";
$sql .= " OFFSET {$pagination->offset()}";
$photos = Product::find_by_sql($sql);
$tcount = count($photos);
}
// var_dump($_SESSION);
?>
I am creating a seat reservation system. In my system, the code check number of seats a bus contains then pass it inside a for loop. When a user pick 2 passengers, it means two seats will be booked. How can I validate the checkbox in the for loop depending on the number of passenger(s) selected.
Using the GUI for more explanation.
on the main page, 2 there indicates number of passenger(s) selected.
When you come to the second page where the values are passed to, you can see 2 Adults as the selected number of passengers. When you click on Submit Button it does not validate the checkbox based on the number of passenger(s) selected. And if I should put required in the checkbox it validates the whole checkbox since it is in a loop
$_SESSION['seat_no'] is the number of seat(s) a bus contains. Let assume a user that want to book a seat selected two passengers which means two seats is booked, how can I validate the checkbox based on the number of seat(s) selected?
Here is my code:
<?php
for ($i = 1; $i <= $_SESSION['seat_no']; $i++) {
if(in_array($i,$mseat)){
echo "<div class='checkbox_wrapper_pick'>
<label>".$i."</label>
</div>";
}else{
echo "<div class='checkbox_wrapper'>
<input type='checkbox' value=".$i." name='seat_book[]' />
<label>".$i."</label>
</div>";
}
}
?>
The full source code:
<?php include("header.php"); error_reporting(0); ?>
<?php
if(isset($_POST['submit'])){
$from = $_POST['from'];
$to = $_POST['to'];
$date = $_POST['d_date'];
$nop = $_POST['nop'];
$_SESSION['from'] = $from;
$_SESSION['to'] = $to;
$_SESSION['date'] = $date;
$_SESSION['nop'] = $nop;
$get = mysqli_query($mysqli,"SELECT * FROM routes WHERE present_loc = '$from' and destination = '$to' ");
while($roys = mysqli_fetch_array($get)){
//get bus details
$bno = $roys['bus_no'];
$ploc = $roys['present_loc'];
$des = $roys['destination'];
$time = $roys['dept_time'];
$_SESSION['time'] = $time;
$amt = $roys['amount'];
$_SESSION['amt'] = $amt;
$b = str_replace( ',', '',$_SESSION['amt'] );
if( is_numeric($b) ) {
$a = $b;
}
$bus = mysqli_query($mysqli,"select * from bus where bus_no = '$bno'");
while($bu = mysqli_fetch_array($bus)){
$_SESSION['model'] = $bu['model'];
$_SESSION['seat_no'] = $bu['seat_no'];
$_SESSION['ac'] = $bu['bus_type'];
$_SESSION['excess_luggage'] = $bu['excess_luggage'];
$_SESSION['more_legs'] = $bu['more_legs'];
$_SESSION['id'] = $bu['id'];
}
$coun = mysqli_query($mysqli, "select count(booking_id) as seat, seats from booking where bus_no = '$bno' and seats !='' GROUP by booking_id" );
$mseat = array();
while($e = mysqli_fetch_array($coun)){
$bseat = $e['seat'];
$mseat[] = $e['seats'];
}
//$seatss = array();
$seat_string = implode(",",$mseat);
//get seats
$couns = mysqli_query($mysqli, "select sum(counter) as seat from booking where bus_no = '$bno' and seats !='' GROUP by bus_no" );
$rseats = mysqli_fetch_array($couns);
$lseat = $rseats['seat'];
if($_SESSION['seat_no'] == $lseat){
$tell = " No more seat(s) available.";
}else{
$tell = $_SESSION['seat_no'] - $lseat. " Seat(s) remaining.";
}
}
}
?>
<!--Main layout-->
<main class="mt-5">
<!--Main container-->
<form action="details" method="POST">
<!--Grid row-->
<div class="row">
<div class="col-lg-12 title-header mb-3 mx-auto z-depth-1">
<div class="row">
<div class="col-lg-8">
<?php echo '<h2> '.$_SESSION['from']. ' to '. $_SESSION['to']. '</h2>'; ?><br/>
<b><?php echo $_SESSION['date']; ?> :: <?php if($_SESSION['nop'] < '2') { echo $_SESSION['nop'] . ' Adult'; }
elseif($_SESSION['nop'] > 1) { echo $_SESSION['nop'] . ' Adults'; }
?></b>
</div>
</div>
</div>
<div class="col-lg-12 mbody"> <label style="margin-left: 4%; font-weight:bolder; font-size:20px; color:#000;">Details </label> </div>
<div class="col-lg-12 mbody bg-white ">
<table class="table table_view" style = "width: 100%; margin-left: 4%; margin-right:4%;">
<tbody>
<tr>
<td><b><?php echo $_SESSION['model']; ?></b><br/><?php echo $_SESSION['from']. ' to '. $_SESSION['to']; ?>
<br/><?php if($_SESSION['ac'] == 'AC') { echo '<span class="alert-info ac">'. $_SESSION['ac'] .'</span>'; }
else{ echo '<span class="alert-warning">No AC</pan>'; } ?>
<?php if($_SESSION['more_legs'] == 'Yes') { echo '<span class="alert-info ac">More Leg Room</span>'; }
else{ echo '<span class="alert-warning no">More Leg Not Available</pan>'; } ?>
</td>
<td><b>Departing Time</b><br/><i class="fa fa-clock-o" aria-hidden="true"></i> <?php echo $_SESSION['time']; ?></td>
<td> <img id = "seatimg" src="../images/seatsLayout/av.gif" class="img-responsive"> <?php echo $tell; ?></td>
<td>Adult <b>₦<?php echo $_SESSION['amt']; ?></b></td>
</tr>
</tbody>
</table>
</div>
<div class="col-lg-12">
<div class="col-lg-12 mbody"> <label style="margin-left: 3%; font-weight:bolder; font-size:20px; color:#000;"><img id = "seatimg" src="../images/seatsLayout/av.gif" class="img-responsive"> Select Seat</label> </div>
<div class="row detail">
<!--Grid column-->
<div class="col-lg-7 animation slideUp" >
<div class="well" id="bus_seats_layout" >
<table class="table table-bordered" cellspacing = "1" id="seatstable">
<tr>
<td><img id = "driverimg" src="../images/seatsLayout/steering.png" class="img-responsive" width="25" height="25"></td>
<td colspan="2" rowspan="3">
<?php
for ($i = 1; $i <= $_SESSION['seat_no']; $i++) {
if(in_array($i,$mseat)){
echo "
<div class='checkbox_wrapper_pick'>
<label>".$i."</label>
</div>
";
}else{
echo "
<div class='checkbox_wrapper'>
<input type='checkbox' value=".$i." name='seat_book[]' />
<label>".$i."</label>
</div>
";
}
}
?>
</td>
</tr>
</table>
</div>
</div>
<div class="col-lg-5">
<ul class="bt">
<li><img src="../images/seatsLayout/seat_available.png" class="img-responsive"> Available</li>
<li><img src="../images/seatsLayout/picked.png" class="img-responsive"> Selected</li>
<li><img src="../images/seatsLayout/seat_booked.png" class="img-responsive"> Booked</li>
</ul>
</div>
</div>
<div class="col-lg-12">
<input type="hidden" name="bus_no" value="<?php echo $bno; ?>">
<input type="hidden" name="to" value="<?php echo $to; ?>">
<input type="hidden" name="from" value="<?php echo $from; ?>">
<input type="hidden" name="amt" value="<?php echo $nop*$a; ?>">
<input type="hidden" name="nop" value="<?php echo $nop; ?>">
<div class="form-group">
<div align="right">
<input type="submit" name="submit" class="bme" value="Continue">
</div>
</div>
</div>
</div>
</div>
</form>
</main>
<?php include("footer.php"); ?>
I want to display value in checkbox. i have multiple value in database as shown in pic above. please have a look. I want value in different checkbox not in same checkbox
]1]2
Code I am trying
<div class="list-group">
<h3>Name</h3>
<?php
$query = "select distinct(name) from info_user where user_status = '1'";
$rs = mysqli_query($con,$query) or die("Error : ".mysqli_error());
while($color_data = mysqli_fetch_assoc($rs)){
?>
<a href="javascript:void(0);" class="list-group-item">
<input type="checkbox" class="item_filter colour" value="<?php echo $color_data['name']; ?>" >
<?php echo $color_data['name']; ?></a>
<?php } ?>
</div>
and what i tried by myself
<div class="list-group">
<h3>Name</h3>
<?php
$column = array();
$query = "select name from info_user where user_status = '1'";
$rs = mysqli_query($con,$query) or die("Error : ".mysqli_error());
while($color_data = mysqli_fetch_assoc($rs)){
$column[] = $color_data['name'];
?>
<a href="javascript:void(0);" class="list-group-item">
<input type="checkbox" class="item_filter colour" value="<?php foreach($column as $value)echo $value['name']; ?>" >
<?php foreach($column as $value)echo $value['name']; ?></a>
<?php } ?>
</div>
Getting this error after trying code
I think this is what you need.
<div class="list-group">
<h3>Name</h3>
<?php
$column = array();
$query = "select name from info_user where user_status = '1'";
$rs = mysqli_query($con,$query);
while ($color_data = mysqli_fetch_assoc($rs)) {
$column = array_merge($column, explode(',', $color_data['name']));
}
$column = array_unique($column);
foreach ($column as $value) {
?>
<a href="javascript:void(0);" class="list-group-item">
<input type="checkbox" class="item_filter colour" value="<?php echo $value; ?>" >
<?php echo $value; ?>
</a>
<?php } ?>
</div>
you can try this way, update your code
<div class="list-group">
<h3>Name</h3>
<?php
$column = array();
$query = "select distinct(name) from info_user where user_status = '1'";
$rs = mysqli_query($con, $query);
while ($color_data = mysqli_fetch_assoc($rs)) {
$column = array_merge($column, explode(',',$color_data['name']));
}
// to remove repeated names
$column = array_filter($column);
?>
<a href="javascript:void(0);" class="list-group-item">
<input type="checkbox" class="item_filter colour" value="<?php
foreach ($column as $value) {
echo $value;
?>">
<?php
echo $value;
}
?>
</a>
</div>
Im a PHP newbie. I am creating a jobs website and my search function tells me when there is no result but if there is, it displays ALL the jobs I have entered on the database. Please assist, I have tried everything.
here is my code:
<?php
if(isset($_POST['submit']))
{
$search = $_POST['keyword'];
$query = "SELECT * FROM jobs WHERE job_tags LIKE '%$search%'";
$search_query = mysqli_query($connection, $query);
if(!$search_query) {
die ("query failed" . mysqli_error($connection));
}
$count = mysqli_num_rows($search_query);
if($count == 0){
echo "<h3> NO RESULT</h3>";
}else{
$query = "SELECT * FROM jobs";
$job_display = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($job_display)){
$job_title = $row['job_title'];
$employer = $row['employer'];
$job_date = $row['job_date'];
$job_logo = $row['job_logo'];
$job_desc = $row['job_desc'];
?>
<div class="row">
<div>
<div class="media img-responsive">
<div class="media-left media-middle">
<a href="#">
<img class="media-object" src="images/<?php echo $job_logo; ?>" class="img-responsive" alt="Absa Insurance Logo">
</a>
</div>
<div class="media-body">
<h4 class="media-heading"><span class="job-tittle"><?php echo "{$job_title}";?> </span>(<i class="glyphicon glyphicon-map-marker"> </i>Gauteng, <span class="type blue"> Short-Term Insurance</span>)</h4>
<P>
<?php echo $job_desc;?>
... <i class="glyphicon glyphicon-plus"> </i> Read More</P>
</div>
<div class=" media-right media-middle job-location">
<p> <?php echo $job_date;?> </p>
</div>
</div>
</div>
</div>
<?php }
}
}
?>
here is the Form
<form class=" form-inline" action="search.php" method="post">
<div class="form-group">
<input type="text" name="keyword" class="form-control" placeholder="Job Key Word">
</div>
</form>
Please let me know if you need more information.
you need to remove this line or just filter here
$query = "SELECT * FROM jobs";
In first query You are looking for only selected records
$query = "SELECT * FROM jobs WHERE job_tags LIKE '%$search%'";
$search_query = mysqli_query($connection, $query);
If it find something You search one more time with
$query = "SELECT * FROM jobs";
You don't put WHERE in this query.
My query printed on webpage
SELECT * FROM products WHERE (product_name like '%meat%' OR description like '%meat%' OR ingradients like '%meat%') AND hide!=1 ORDER BY id ASC
If I run the same query query in mysql its showing 2 results with my php loop code its showing only one result,
my php code
<?php
$keyword=mysql_real_escape_string($_GET['Keyword']);
$query2 = "SELECT * FROM products WHERE (product_name like '%$keyword%' OR description like '%$keyword%' OR ingradients like '%$keyword%') AND hide!=1 ORDER BY id ASC ";
echo $query2 ;
$result2 = mysql_query($query2) or die('Error, query failed2');
if (mysql_num_rows($result2)>0){
mysql_data_seek($result2, 0);
$row2 = mysql_fetch_array($result2, MYSQL_ASSOC)
?>
<ul id="product-listing">
<?php
$i=1;
while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)){ ?>
<li <?php $i; if ($i%3==0) {echo "class=\"last\"";} ?>>
<div class="img">
<?php if ( $row2['new'] ==1 ) { ?>
<div class="new"><img src="images/new.png" width="18" height="41" /></div>
<?php } ?>
<img src="images/products/284X190/<?php echo $row2['image_1']; ?>" width="284" height="190" alt="" title="" /> </div>
<div class="name"><?php echo $row2['product_name']; ?></div>
<form action="" method="post">
<div class="price">Price:
<?php if ( $row2['market_price'] !=0 ) { ?>
<span> $<?php echo $row2['market_price']; ?> </span>
<?php } ?>
$<?php echo $row2['price']; ?></div>
<div class="add-to-cart">
<input type="image" src="images/btn-1.jpg" />
</div>
<div class="clear"></div>
</form>
</li>
<?php $i++; } ?>
</ul>
<?php } else { ?>
No Products,
<?php } ?>
You should use a while loop in your php.
while($item = mysql_fetch_assoc($query))
{
print_r($item); // echo out whatever you need to for each item returned
}