how to pass a retried database data to another page? - php

I am trying to make a simple booking form.
1st showing available cities and packages in a page from database
when the user clicks on the BOOK button in the page, the page will redirect to the action page where the selected form/package should be added to the booking table automatically and send a confirmation mail.
The following code generates the available packages from the database:
include 'inc/db_connect.php';
//working connection to the DB
$sql="SELECT pkg, fcity, dcity, price, details FROM tour_package WHERE fcity='".$fcity_search."' ORDER BY fcity ASC";
$sqlresult=mysqli_query($con,$sql);
$afct=mysqli_affected_rows($con);
while($pkg=mysqli_fetch_array($sqlresult,MYSQLI_ASSOC)){
$num[]=$pkg['pkg'];
$fcity[]=$pkg['fcity'];
$dcity[]=$pkg['dcity'];
$price[]=$pkg['price'];
$details[]=$pkg['details'];
$num++;
}
$i=0;
$book_btn_id = 1;
while($i < $afct)
{
echo '<form action="book_tour_action.php">';
$num_var_to_db = $num[$i];
echo '<span class="search-class-row"><div class="search-results-label">Package : </div>'.$num[$i].'<br/></span>';
echo '<span class="search-class-row"><div class="search-results-label">From : </div>'. $fcity[$i].'<br/></span>';
echo '<span class="search-class-row"><div class="search-results-label">Destination : </div>' . $dcity[$i].'<br/></span>';
echo '<span class="search-class-row"><div class="search-results-label">Price : </div>' .$price[$i].'<br/></span>';
echo '<span class="search-class-row"><div class="search-results-label">Details :</div>' . $details[$i].'<br/></span>';
//echo ' BOOK ';
echo '<input type="submit" value="BOOK" class="booking-btn" name="bb'.$book_btn_id++.'">';
echo '<br><br/><hr/>';
$i++;
echo '</form>';
}
I've set a dynamic name to the BOOK button.
book_tour_action.php:
echo $num_var_to_db;
$sql = "INSERT INTO booking (num_var_to_db)
VALUES ('$num_var_to_db')";
But it says UNDEFINED VARIABLE when the BOOK button is clicked.
So that means the data is not passed to book_tour_action.php I guess.
How can I make it work?

You need to keep $num_var_to_db value in hidden input field in the form to get the value. Try do like below :
echo '<form action="book_tour_action.php">';
$num_var_to_db = $num[$i];
echo '<input type="hidden" name="num_var_to_db" value="'.$num_var_to_db.'">'
echo '<span class="search-class-row"><div class="search-results-label">Package : </div>'.$num[$i].'<br/></span>';
echo '<span class="search-class-row"><div class="search-results-label">From : </div>'. $fcity[$i].'<br/></span>';
echo '<span class="search-class-row"><div class="search-results-label">Destination : </div>' . $dcity[$i].'<br/></span>';
echo '<span class="search-class-row"><div class="search-results-label">Price : </div>' .$price[$i].'<br/></span>';
echo '<span class="search-class-row"><div class="search-results-label">Details :</div>' . $details[$i].'<br/></span>';
//echo ' BOOK ';
echo '<input type="submit" value="BOOK" class="booking-btn" name="bb'.$book_btn_id++.'">';
echo '<br><br/><hr/>';
$i++;
echo '</form>';
and in the php page you can do following to get the value:
$num_var_to_db = $_REQUEST['num_var_to_db'];

Related

Check box is toggled when I click on any text

I am dynamically creating the checkbox input fields with text lables, and trying to create a treeview when I click any item in the tree the first check box is always toggling.What can be the possible reason.
if($category->visible==1){
echo '<div class="categorylist level'.$depth.' cat">';
$count=$DB->count_records('course',['category'=>$category->id]);
if($count>0){
echo '<div class="parent">';
if($depth>0){
$indent=10;
for($i=0;$i<=$depth;$i++){
$indent=$indent+$depth;
echo '<div style="margin-left:'.$indent.'px">';
}
}
echo '<input type="checkbox" id="category" name="cat[]" value='.$category->id.'/>';
echo '<label for="category"'.$category->id.'"><span class="">'.$category->name.' ('.$count.')</span></label><br>';
if($depth>0){
for($i=0;$i<=$depth;$i++){
echo '</div>';
}
}
echo '</div>';
}
else{
echo '<div class="parent">';
if($depth>0){
$indent=10;
for($i=0;$i<=$depth;$i++){
$indent=$indent+$depth;
echo '<div style="margin-left:'.$indent.'px">';
}
}
echo '<label for="category"><span class="">'.$category->name.' ('.$count.')</span></label><br>';
if($depth>0){
for($i=0;$i<=$depth;$i++){
echo '</div>';
}
}
echo '</div>';
}
echo '</div>';
}
Just checked this <label for="category"'.$category->id.'"> could be your issue, label with duplicate for attribute can cause this issue, make sure that you have no label with same $category->id

SQL While loop select all rows that have a column checked

I'm trying to do a featured car section in my used car lot website.
What I want to do is to use a while loop and select only the rows that have a specific column that returns true. For example, in my database, I have a column that is marked true if a checkbox on the submit form is ticked indicating that the car is supposed to be "featured".
In my while loop I only want those cars to appear in this particular section. Using the code below, I can return all vehicles in the inventory but no way to select only the ones with the "featured" column that returns a true value.
<?php
$sql = "SELECT * FROM inventory LIMIT 10";
$result = mysqli_query($conn, $sql);
while ($row = $result->fetch_assoc())
{
echo '<div class="slide">';
echo '<div class="car-block">';
echo '<div class="img-flex">';
echo '<a href="inventory-listing.php?vin='.$row['vin'].'">';
echo '<span class="align-center">';
echo '<i class="fa fa-3x fa-plus-square-o"></i></span></a>';
echo '<img src="images/inventory/'.$row['vin'].'-main.png" alt="" class="img-responsive">';
echo '</div>';
echo '<div class="car-block-bottom">';
echo '<h6><strong>'.$row['year'].' '.$row['make'].' '.$row['model'].'</strong></h6>';
echo '<h6>'.$row['body'].', '.$row['milage'].' miles</h6>';
echo '<h5>$ '.$row['price'].'</h5>';
echo '</div>';
echo '</div>';
echo '</div>';
}
?>
What would I be able to add to my sql query to filter it only the rows where the "featured" column returns true?
Alternative to Goleztrols suggestion, you could just insert an IF clause before echo-ing the results.
<?php
$sql = "SELECT * FROM inventory LIMIT 10";
$result = mysqli_query($conn, $sql);
while ($row = $result->fetch_assoc())
{
if ($row['featured']=="true")
{
echo '<div class="slide">';
echo '<div class="car-block">';
echo '<div class="img-flex">';
echo '<a href="inventory-listing.php?vin='.$row['vin'].'">';
echo '<span class="align-center">';
echo '<i class="fa fa-3x fa-plus-square-o"></i></span></a>';
echo '<img src="images/inventory/'.$row['vin'].'-main.png" alt="" class="img-responsive">';
echo '</div>';
echo '<div class="car-block-bottom">';
echo '<h6><strong>'.$row['year'].' '.$row['make'].' '.$row['model'].'</strong></h6>';
echo '<h6>'.$row['body'].', '.$row['milage'].' miles</h6>';
echo '<h5>$ '.$row['price'].'</h5>';
echo '</div>';
echo '</div>';
echo '</div>';
}
else
{
echo "nothing to be shown";
}
}

trying to show the same content in page and also in fancybox

Im trying to fill my articles with content that is coming from the database.
I have my articles with: title, image, date, content and with a link (read more) to open a fancybox that will show the same content of this article but in the fancybox.
So I have my div id="show-container" that corresponds to the div that shows the content in fancybox.
This div have display:none in css and it only appears when the user click in the link with #show href and class="fancybox", here:
<a class="fancybox" href="#show">Read more</a>
But I want to show the same content in fancybox so Im fill this "show" div with the same info that I put in the article.
My articles are working fine, each article is with right name,image and content.
But when I click to open the fancybox of each article all the fancyboxs have the same content, that is the content of my first article.
Anyone there know how I can fix this?
$readPost = $pdo->prepare("SELECT * FROM posts ORDER BY date DESC LIMIT 0,4");
$readPost->execute();
$folder = '../images/';
while ($readPostResult = $readPost->fetch(PDO::FETCH_ASSOC))
{
echo '<article id="loop-news">';
echo '<img src="'.$folder.$readPostResult['thumb'].'" title="'.$readPostResult['title'].'"/>';
echo '<h2>';
echo '<a href="#show" class="x" >'.$readPostResult['title'].'</a><br />';
echo '</h2>';
echo '<p>'.$readPostResult['content'].'<a class="fancybox" href="#show">Read more</a></p>';
echo '<div id="show-container">';
echo '<div id="show">';
echo '<h2>'.$readPostResult['title'].'</h2>';
echo '<br />';
echo '<img src="'.$folder.$readPostResult['thumb'].'" title="'.$readPostResult['title'].'"/>';
echo '<p>'.$readPostResult['content'].'<br /></p>';
echo '<a class="button" href="index.html">Back</a>';
echo '</div>';
echo '</div>';
echo '</article>';
}
?>
I think I need to pass the id of each news when I click in my a href:
echo '<p>'.$readPostResult['content'].'<a class="fancybox" href="#show">Read more</a></p>';
Do you know How I can do that?? Because Im already using "#show#" in my link to open the fancybox..
You have to use class for css instead of id
Your ID values are meant to be unique to the element. Because you're generate the content in a loop, you will have multiple duplicate ID values in your elements. Try changing it to this:
$i = 0;
while ($readPostResult = $readPost->fetch(PDO::FETCH_ASSOC))
{
echo '<article id="loop-news-' . $i . '">';
echo '<img src="'.$folder.$readPostResult['thumb'].'" title="'.$readPostResult['title'].'"/>';
echo '<i class="fa fa-download"></i>';
echo '<h2>';
echo '<a href="#show" class="x" >'.$readPostResult['title'].'</a><br />';
echo '</h2>';
echo '<span>'.date('d/m/Y H:i',strtotime($readPostResult['date'])).'</span>';
echo '<p>'.lmWord($readPostResult['content'],270).'<a class="fancybox" href="#show-' . $i . '" id="showFancy-' . $i . '">Read more</a></p>';
echo '<div id="show-container-' . $i . '">';
echo '<div id="show-' . $i . '">';
echo '<h2>'.$readPostResult['title'].'</h2>';
echo '<br />';
echo '<img src="'.$folder.$readPostResult['thumb'].'" title="'.$readPostResult['title'].'"/>';
echo '<p>'.$readPostResult['content'].'<br /></p>';
echo '<a class="button" href="index.html">Back</a>';
echo '</div>';
echo '</div>';
echo '</article>';
++$i;
}
You'll notice, I've created an $i variable outside the while loop, and it appends the value of $i to all your ID elements. Once at the end of the loop, it will increase $i by 1, so that all your IDs in your loop will be unique.

MySQL separate in to DIV elements

I would greatly enjoy knowing how to separate the results of my query based on the "season" using PHP only. The products should be placed in separate divs according to what season they are for. I would also like to do this in the most efficient way possible. I have thought about his for some time and have failed both in a solution and in finding someone else's similar results on the internet. Please ignore the jquery, it is a leftover for something I may or may not use.
<?php
session_start();
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>Shop</title>
<link rel="stylesheet" type="text/css" href="style/main.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0
/jquery.min.js"></script>
<script type="text/javascript" src="script/jquery.simplyscroll.min.js"></script>
<script type="text/javascript">
(function($) {
$(function() {
$("#scroller").simplyScroll();
});
})(jQuery);
</script>
</head>
<body>
<div id="container">
<div id="header">
<div id="menu">
<ul>
<li>Home</li>
<li>About</li>
<li>Shop</li>
<li>Contact</li>
</ul>
</div>
</div>
<div id="content">
<?php
//current URL of the Page. cart_update.php redirects back to this URL
$current_url = base64_encode("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
//$results = $db->query('SELECT id, name, thumb, description, price, season FROM products
BY season ASC');
foreach($db->query('SELECT id, name, thumb, description, price FROM products BY season
ASC') as $results){
if ($results) {
//output results from database
$last_season = 1;
echo '<div class="products">';
while($obj = $results->fetch_object())
{
if ($last_season != $obj->season){
echo '</div><div class="products">';
}
echo '<div class="product">';
echo '<form method="post" action="cart_update.php">';
echo '<div class="product-thumb"><img src="image/'.$obj->product_img_name.'"></div>';
echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
echo '<div class="product-desc">'.$obj->product_desc.'</div>';
echo '<div class="product-info">Price '.$currency.$obj->price.' <button
class="add_to_cart">Add To Cart</button></div>';
echo '</div>';
echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
$last_season = $obj->season;
}
echo '</div>';
}
}
?>
<div id="shopping-cart">
<h2>Your Shopping Cart</h2>
<?php
if(isset($_SESSION["products"]))
{
$total = 0;
echo '<ol>';
foreach ($_SESSION["products"] as $cart_itm)
{
echo '<li class="cart-itm">';
echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&
return_url='.$current_url.'">×</a></span>';
echo '<h3>'.$cart_itm["name"].'</h3>';
echo '<div class="p-code">P code : '.$cart_itm["code"].'</div>';
echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
echo '<div class="p-price">Price :'.$currency.$cart_itm["price"].'</div>';
echo '</li>';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
}
echo '</ol>';
echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong> Check-out!</span>';
echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&
return_url='.$current_url.'">Empty Cart</a></span>';
}else{
echo 'Your Cart is empty';
}
?>
</div>
</div>
<div id="footer">Footer goes here.</div>
</div>
</body>
</html>
I am getting an error on line 44, which is the start of the "foreach" statement. I am again at a loss. Thank you a ton for your help so far. If I can get this working, with help of course, I will be extremely excited and in your debt.
Here's a loop that will create a new div each time the season changes in a new row. You will need to change the query to order the results by season, e.g.
ORDER BY season ASC
Here's the loop:
if ($results) {
//output results from database
$last_season = 1; //initial value
echo '<div class="season">'; //opens first season div
while($obj = $results->fetch_object()){
if ($last_season != $obj->season){
echo '</div><div class="season">';
}
echo '<div class="product">';
echo '<form method="post" action="cart_update.php">';
echo '<div class="product-thumb"><img src="image/'.$obj->product_img_name.'"></div>';
echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
echo '<div class="product-desc">'.$obj->product_desc.'</div>';
echo '<div class="product-info">Price '.$currency.$obj->price.' <button class="add_to_cart">Add To Cart</button></div>';
echo '</div>';
echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
$last_season = $obj->season;
}
echo '</div>'; //closes final season div
}
Regarding the line 44 error in the foreach, I believe what you want there instead of a foreach is simply:
$results = $db->query('SELECT id, name, thumb, description, price FROM products BY season
ASC'); //remember to remove the closing bracket of the foreach

Paginating a `View Cart` Page using PHP

I am trying to paginate my PHP page by ten results per page. Currently the page shows all results at once. I did look up some tutorials on pagination but I cannot seem to figure out how to implement them into the current PHP page. The PHP page is called view_cart.php and it essentially retrieves the cached products that were added by the user via a shopping cart page and displays them on this page. I am just looking for a few hints to point me in the right direction, not for someone to do my work for me.
This is the view_cart.php page code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
session_start();
include_once("config.php");
if(isset($_SESSION["products"]))
{
$total = 0;
echo '<form method="post" action="PAYMENT-GATEWAY">';
echo '<ul>';
$cart_items = 0;
foreach ($_SESSION["products"] as $cart_itm)
{
$product_code = $cart_itm["code"];
$queryy = "SELECT TOP 1 product_name,product_desc, price FROM products WHERE product_code='$product_code'";
$results = mssql_query($queryy, $mysqli);
$obj = mssql_fetch_object($results);
echo '<li class="cart-itm">';
echo '<span class="remove-itm">×</span>';
echo '<div class="p-price">'.$currency.$obj->price.'</div>';
echo '<div class="product-info">';
echo '<h3>'.$obj->product_name.' (Code :'.$product_code.')</h3> ';
echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
echo '<div>'.$obj->product_desc.'</div>';
echo '</div>';
echo '</li>';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->product_name.'" />';
echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$product_code.'" />';
echo '<input type="hidden" name="item_desc['.$cart_items.']" value="'.$obj->product_desc.'" />';
echo '<input type="hidden" name="item_qty['.$cart_items.']" value="'.$cart_itm["qty"].'" />';
$cart_items ++;
}
echo '</ul>';
echo '<span class="check-out-txt">';
echo '<strong>Total : '.$currency.$total.'</strong> ';
echo '</span>';
echo '</form>';
echo 'Checkout';
}else{
echo 'Your Cart is empty';
}
?>
<?php
date_default_timezone_set('America/Edmonton');
?>
<?php echo date("D M d, Y G:i a"); ?>
</body>
</html>
This is the config.php page code:
<?php
$mysqli = mssql_connect('gd','Gad','Rdgaf!');
$objConnectee = mssql_select_db('Gdaddf',$mysqli );
?>
Thank you for any help. All help is appreciated.
You will have url parameters: start and count.
Use them in the sql query, like LIMIT $_GET['start'], $_GET['count']
The last thing is to render links to the "Next" and "Previous" page. (and to "First", "Last", 1,2,3,4....). Those links will include parameters start and count.
For example, the "Next" link will contain url with parameters:
"view_cart.php?start=" . ($_GET['start'] + $_GET['count']) . "&count=" . $_GET['count']
Beware of the total record count to not overflow.

Categories