All menu is not displayed - php

There are 4 sub menus in "menu" button which are drinks, main, appetizers and desserts. When users click drinks, they will be directed to drinks and the same when they go other sub menu buttons. But I want to display all menu when users click "menu" button. I know I have to use "else" but the menu does not display at all.
menu.php
<div class="products">
<?php
if (isset($_GET['id'])) {
//current URL of the Page. basket_update.php redirects back to this URL
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$results = $mysqli->query("SELECT * FROM menu WHERE MenuTypeID = ".$_GET['id']);
$currency = '$';
if ($results) {
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
echo '<div class="product">';
echo '<form method="post" action="basket_update.php">';
echo '<div class="product-thumb"><img src="'.$obj->MenuPicture.'" /></div>';
echo '<div class="product-content"><h3>'.$obj->MenuName.'</h3>';
echo '<div class="product-desc">'.$obj->MenuDescription.'</div>';
echo '<div class="product-info">';
echo 'Price: '.$currency.$obj->MenuPrice.' | ';
echo 'Qty <input type="text" name="menu_qty" value="1" size="1" />';
echo '<button class="add_to_basket">Add To Basket</button>';
echo '</div></div>';
echo '<input type="hidden" name="MenuID" value="'.$obj->MenuID.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
}
}
?>
<?php
}else{
$query2 = "SELECT * FROM Menu";
$result2 = mysql_query($query2);
}
?>
</div>

Try this code.
<div class="products">
<?php
if (isset($_GET['id'])) {
$results = $mysqli->query("SELECT * FROM menu WHERE MenuTypeID = ".$_GET['id']);
}
else{
$results = $mysqli->query("SELECT * FROM menu");
}
//current URL of the Page. basket_update.php redirects back to this URL
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$currency = '$';
if ($results) {
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
echo '<div class="product">';
echo '<form method="post" action="basket_update.php">';
echo '<div class="product-thumb"><img src="'.$obj->MenuPicture.'" /></div>';
echo '<div class="product-content"><h3>'.$obj->MenuName.'</h3>';
echo '<div class="product-desc">'.$obj->MenuDescription.'</div>';
echo '<div class="product-info">';
echo 'Price: '.$currency.$obj->MenuPrice.' | ';
echo 'Qty <input type="text" name="menu_qty" value="1" size="1" />';
echo '<button class="add_to_basket">Add To Basket</button>';
echo '</div></div>';
echo '<input type="hidden" name="MenuID" value="'.$obj->MenuID.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
}
}
?>

Related

Page not found when my submitting my php form

i have this form i built using php the form is supposed to search for a user in formation in my database, but when i fill in the search box and click ok it will try to load process.php instead of the search.php i assigned it to redirect to and for that reason it shows me "PAGE NOT FOUND" *** and for the refrence it is a wordpress plugin that i want to use on my website that i am trying to build
this is the code
add_action( 'admin_menu', 'addAdminMenu' );
function addAdminMenu(){
add_menu_page('ClassifiedBr Settings', 'ClassifiedBr', 'manage_options', 'classifiedbr_settings_page', 'classifiedbr_init', '', 3);
add_submenu_page('classifiedbr_settings_page','Classified Sub', 'Classified Sub','manage_options', 'classified_sub_slug', 'stracking_init');
}
function classifiedbr_init(){
echo '<form action="demo.php" method="post" />';
echo "<h1>Britchi Tracking</h1>";
echo '<p>';
echo 'Costumers Name (required) <br/>';
echo '<input type="text" name="cname" placeholder="Emmanuel John"/>';
echo '</p>';
echo '<p>';
echo 'Tracking number (required) <br/>';
echo '<input type="text" name="ctracking" placeholder="EEX28PDS"/>';
echo '</p>';
echo '<p>';
echo 'Email (required) <br/>';
echo '<input type="email" name="cemail" placeholder="Email"/>';
echo '</p>';
echo '<p>';
echo 'Recived Port (required) <br/>';
echo '<input type="text" name="crport" placeholder="Brazil Port"/>';
echo '</p>';
echo '<p>';
echo 'Delivered Port (required) <br/>';
echo '<input type="text" name="cdport" placeholder="Lagos Port"/>';
echo '</p>';
echo '<p>';
echo 'Current Location (required) <br/>';
echo '<input type="text" name="clocation" placeholder="Abuja" />';
echo '</p>';
echo '<p>';
echo 'Destination (required) <br/>';
echo '<input type="text" name="cdestination" placeholder="Total filling Station, Abuja"/>';
echo '</p>';
echo '<p><input type="submit" name="cregistered" value="Register"></p>';
echo '</form>';
}
function stracking_init(){
echo '<form action="search.php" method="post" />';
echo "<h1>Britchi Tracking</h1>";
echo '<p>';
echo 'Costumers Name (required) <br/>';
echo '<input type="text" name="search" placeholder="Emmanuel John"' . '" size="70"/>';
echo '</p>';
echo '<p><input type="submit" name="csearch" value="Search"></p>';
echo '</form>';
}
add_shortcode('TRACSEARCH', 'stracking_init');
?>
in this code I have 2 forms the first one (classifiedbr_init)sending to demo.php and the second one (strackinh_init) sending to search.php
as you can see the classifiedbr_init and the stracking_init functions will show on the admin page but only the stracking_init will show on the the frontend becouse i created a shortcode for it
this is the search.php code
<table border="">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>RecivedPort</th>
<th>DestinationPort</th>
<th>Location</th>
<th>Destination</th>
</tr>
<?php
$conn=mysqli_connect("localhost","root","","class");
$set=$_POST['search'];
if($set) {
$show="SELECT * FROM demo where fname='$set'";
$result=mysqli_query($conn, $show);
while($rows=mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>";
echo $rows['cname'];
echo "</td>";
echo "<td>";
echo $rows['ctracking'];
echo "</td>";
echo "<td>";
echo $rows['cemail'];
echo "</td>";
echo "<td>";
echo $rows['crport'];
echo "</td>";
echo "<td>";
echo $rows['cdport'];
echo "</td>";
echo "<td>";
echo $rows['clocation'];
echo "</td>";
echo "<td>";
echo $rows['cdestination'];
echo "</td>";
echo "</tr>";
echo "</br>";
}
}
else{
echo "nothing found";
}
?>
</table>
thanks as you help me out

search not found message after run query

I'm making a search query for my webs, and i get an error for my query after i add the if ($results==0) ......
Object of class mysqli_result could not be converted to int in line 65 which refer to if ($results==0)
$results = $mysqli->query("SELECT * FROM produk2 WHERE $construct" );
if ($results==0)
{
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1.
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
meaning</br>3. Please check your spelling";
}
else{
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
echo '<div class="product">';
echo '<form method="post" action="cart_update.php">';
echo '<div class="product-thumb"><img src="images/'.$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">';
echo 'Price '.$currency.$obj->price.' | ';
echo 'Qty <input type="text" name="product_qty" value="1" size="3" />';
echo '<button class="add_to_cart">Add To Cart</button>';
echo '</div></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>';
}
}
$mysqli->query don't have the data count until you ask for it
Try like this
if($results ->num_rows == 0){
// no result found
}

calculating the sum of an array php

Hi im currently doing making a website that sells games as a project but im having problems calculating the sum for the price of the games
I have this loop which displays the games added into the basket
cart.php
<?php
$count = 0;
while ($count < $numrow)
{
$row = $results -> fetch_assoc();
extract($row);
echo"<div>";
echo"<div class='recommended_games'>";
echo "<img src='images/".$gameIMG."' />";
echo "</div>";
echo '<div class="price_tag">';
echo '<div class="price_tag" name="price" method="POST">£'.$gamePrice. '</div>';
echo'</div>';
echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform">';
echo '<select name="quantity" id="quantity" />';
echo '<option value="1">1</option>';
echo '<option value="2">2</option>';
echo '<option value="3">3</option>';
echo '<option value="4">4</option>';
echo '<option value="5">5</option>';
echo '</select>';
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
echo '<input type="submit" value="update" />';
echo '</form>';
echo '<div class="quantity_update">';
echo '<form action="remove_item.php" method="POST">';
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
echo '<input type="submit" value="Remove Item" />';
echo '</form>';
echo '</div>';
echo '</div>';
echo"<img class='box1' src='Images/Grey-Banners.png' />";
echo"</div>";
$count = $count + 1;
}
echo '<div id="delete_all">';
echo '<form action="delete_cart.php" method="POST">';
echo '<input id="hide_button" type="submit" value="Clear All" />';
$a=array($gamePrice);
echo array_sum($a);
echo '</form>';
echo '</div>';
?>
this is where im trying to calculate the total price
$a=array($gamePrice);
echo array_sum($a);
The reason this doesnt work, is because $gamePrice never is an array (unless you didnt provide all code). In the loop, it gets set to a new value, after the loop only the last one is stored.
Based on some hints in your code, I guessing this is a cart and you're looping through the cart. An easy way to get a total is like this:
$total = 0;
while( $itemsThatWeLoop){
// Some code here
$total+= $gamePrice*$quantity;
}
You add a variable which increments with the product's price
To explain the while-only-last-value-saved:
$i=0;
while( $i<=10){
$i= $i+1;
}
echo $i;
Will give 10. All other iterations $i gets set to a new value. The original value is not saved

Update the quantity of cart using PHP & SQL

I've this problem in which I can't update the cart for some reason. I've looked for many solutions to see if they can solve my problem but no luck. I've 2 files one called cart.php which contains the form and a updatebasket.php file which contains query.
Cart file
<?php
$count = 0;
while ($count < $numrow)
{
$row = $results -> fetch_assoc();
extract($row);
echo"<div>";
echo"<div class='recommended_games'>";
echo "<img src='images/".$gameIMG."' />";
echo "</div>";
echo '<div class="price_tag">';
echo '<div class="price_tag">£'.$gamePrice. '</div>';
echo'</div>';
echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform">';
echo '<input type="text" value="1" name="quantity" id="quantity" />';
echo '<input type="hidden" value="'.$gameID.'" name='.$gameID.' id="gameid" />';
echo '<input type="submit" value="update" />';
echo '</form>';
echo '</div>';
echo"<img class='box1' src='Images/Grey-Banners.png' />";
echo"</div>";
$count = $count + 1;
}
?>
updatebasket file
<?php
session_start();
require "dbconnect.php";
$memberID = $_SESSION['id'];
$quantity = $_POST['quantity'];
$gameID = $_POST['gameid'];
mysqli_autocommit($con,FALSE);
$connect->query($query);
$query = "UPDATE basket SET quantity = ".$quantity." WHERE gameid = ".$gameID." AND id = ".$memberID."";
$results = $connect->query($query);
mysqli_commit($con);
header('Location: cart.php');
?>
Change cart.php to this:
<?php
$count = 0;
while ($count < $numrow)
{
$row = $results -> fetch_assoc();
extract($row);
echo"<div>";
echo"<div class='recommended_games'>";
echo "<img src='images/".$gameIMG."' />";
echo "</div>";
echo '<div class="price_tag">';
echo '<div class="price_tag">£'.$gamePrice. '</div>';
echo'</div>';
echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform">';
echo '<input type="text" value="1" name="quantity" id="quantity" />';
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
echo '<input type="submit" value="update" />';
echo '</form>';
echo '</div>';
echo"<img class='box1' src='Images/Grey-Banners.png' />";
echo"</div>";
$count = $count + 1;
}
?>
The big change is this:
BEFORE:
echo '<input type="hidden" value="'.$gameID.'" name='.$gameID.' id="gameid" />';
AFTER:
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
php uses the name attribute as the key for post and get, not id.
id is used by javascript, jquery (also javascript), css, and probably a few other things.
But in forms, name is the one you want for the post and get key.

update quantity with one button php

Hello i am currently doing a project to make a website that sells game however one of my problems right now is updating the quantity with a single button. i have got it working with multiple update buttons next to the items that i would like to update but to make it more realistic, i would like to just have one button that will update all the item quantities in the basket table in my database.
This is my cart file, i have commented out the bit where it works with multiple update buttons.
<?php
$count = 0;
while ($count < $numrow)
{
$row = $results -> fetch_assoc();
extract($row);
echo"<div>";
echo"<div class='recommended_games'>";
echo "<img src='images/".$gameIMG."' />";
echo "</div>";
echo '<div class="price_tag">';
echo '<div class="price_tag">£'.$gamePrice. '</div>';
echo'</div>';
echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform" id="update_all">';
echo '<select name="quantity" id="quantity" />';
echo '<option value="1">1</option>';
echo '<option value="2">2</option>';
echo '<option value="3">3</option>';
echo '<option value="4">4</option>';
echo '<option value="5">5</option>';
echo '</select>';
//echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
//echo '<input type="submit" value="update" />';
echo '</form>';
echo '</div>';
echo '</form>';
echo"<img class='box1' src='Images/Grey-Banners.png' />";
echo"</div>";
$count = $count + 1;
}
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" form="update_all"/>';
echo '<input type="submit" value="update" form="update_all"/>';
?>
This is my updatebasket file which updates the quantity in the database
<?php
session_start();
require "dbconnect.php";
$memberID = $_SESSION['id'];
$quantity = $_POST['quantity'];
$gameID = $_POST['gameid'];
$connect->query($query);
$query = "UPDATE basket SET quantity = ".$quantity." WHERE gameid = ".$gameID." AND id = ".$memberID."";
$results = $connect->query($query);
mysqli_commit($con);
header('Location: cart.php');
?>
If I understand you correctly, you need to work with an array for this to be achieved, by changing your form and you can use a foreach to assemble your query to update each entry in the cart. So one button will update each item in the cart with the respective quantities. I did not test this code, this is how I would approach it.
Eample of the HTML Form Changes:
echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform" id="update_all">';
echo '<select name="quantity[]" id="quantity" />';
echo '<option value="1">1</option>';
echo '<option value="2">2</option>';
echo '<option value="3">3</option>';
echo '<option value="4">4</option>';
echo '<option value="5">5</option>';
echo '</select>';
//echo '<input type="hidden" value="'.$gameID.'" name="gameid[]" id="gameid" />';
//echo '<input type="submit" value="update" />';
echo '</form>';
echo '</div>';
echo '</form>';
echo"<img class='box1' src='Images/Grey-Banners.png' />";
echo"</div>";
$count = $count + 1;
}
echo '<input type="hidden" value="'.$gameID.'" name="gameid[]" id="gameid" form="update_all"/>';
echo '<input type="submit" value="update" form="update_all"/>';
?>
Query Eample:
foreach ($_POST['gameid'] as $row=>$id) {
$gameid = $id;
$newquantity = ($_POST['quantity'][$row]);
$query = "UPDATE basket SET quantity = ".$newquantity." WHERE gameid = ".$gameID." AND id = ".$memberID."";
$connect->query($query);
}
One solution would be to give your quantity <select> inputs unique names with the Game ID attached to the end (eg. "quantity_32"). This makes it easy to know how much of each game is in the cart.
// POST Example
$_POST['quantity_32'] = 1;
$_POST['quantity_31'] = 3;
$_POST['quantity_37'] = 2;
Front-End Form Change
echo '<select name="quantity_<?=$gameID?>" id="quantity" />';
Back-End Processing
Then on the processing page loop through the $_POST variables and find the quantity fields and grab their Game ID's.
foreach ($_POST as $key => $quantity) {
// Ignore non-quantity fields
if (preg_match('/quantity_([0-9]+)/i', $key, $result) !== 1) continue;
$quantity = (int)$quantity;
$game_id = (int)$result[1];
// Update Cart Quantity in DB
// ...
}
Important!
Please ensure you SQL-Escape all values you save into the DB. Hackers could do some nasty stuff if you don't.
This example shows how you can keep things safe using MySQLi Prepared Statements.
http://mattbango.com/notebook/code/prepared-statements-in-php-and-mysqli/

Categories