Stop button to repet in loop - php

Is there any way to stop a button from submit button to stop looping in while loop?
it just looks terrible to have like 15 submit buttons insted of 1. whitout moving it from the from
$sql = "SELECT * FROM `bestalning` WHERE lev=0
ORDER BY tid";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
echo "<div class='continer bg-vit table-rsponsive-sm '>";
echo "<table class='table'>";
echo "<thead class='thead-dark'>";
echo "<tr><th>Lev</th>";
echo "<th> </th>";
echo "<th>Artikelnr</th>";
echo "<th>Antal</th>";
echo "<th>Singnatur</th>";
echo "<th>Önskad ankomst</th>";
echo "<th>Skickad</th>";
echo "</tr></thead><tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<form method='POST' action='ID_change.php'>";
echo "<td><input style='margin-right:5px;' name='levJA' type='checkbox' value='1'></td>";
echo "<td><input style='margin-right:5px;' name='RowID' type='hidden' value='".$row['ID']."'></td>";
echo "<td>".$row['artikelnr']."</td>";
echo "<td>".$row['antal']."</td>";
echo "<td>".$row['ovrigt']."</td>";
echo "<td>".$row['Tid']."</td>";
echo "<td>".$row['date']."</td>";
echo "<input class='btn btn-dark m-1' type='submit' value='submit'>;
echo "</form>";
echo "</tr>";
}
echo "</tbody></table>";
echo "</div>";
mysqli_close($conn);

Related

while loop only returning 1 product row

Evening all,
I have the following script setup on the backend of an eCommerce platform I am developing. Essentially I want to pull the products through and also pull all of the current categories through so that products can be assigned to a particular category.
However my product edit page is only displaying 1 result.
Its interesting to note that if i remove the script relating to the last SQL query my products show but with no category options. With the script only 1 product shows with the correct categories.
<?php
$myusername= $_SESSION['login_user'];
include '../ecommerce/connection.php';
$sql="SELECT * FROM products where status='yes' ";
// Posting Result
$result = mysqli_query($connection, $sql);
// Counting Results
$count=mysqli_num_rows($result);
if($count==0) {
echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No products setup</h1></div>";
} else {
if ($result = mysqli_query($connection, $sql)){
echo "<table id='order_table_small'>";
echo "<th>Image</th>";
echo "<th>Product ID</th>";
echo "<th>Name</th>";
echo "<th>Description</th>";
echo "<th>Price</th>";
echo "<th>Weight</th>";
echo "<th>Options </th>";
while($row = $result->fetch_array())
{
$product_no = $row['product_id'];
echo "<tr>";
echo '<td><img src="images/'. $row['image'] . ' " id="product_image_admin">';
echo "</td> ";
echo "<td>" .$row['product_id'];
echo "</td> ";
echo "<td>" .$row['product_name'];
echo "</td> ";
echo "<td>" .$row['product_description'];
echo "</td> ";
echo "<td>" .'&pound'.$row['product_price'];
echo "</td> ";
echo "<td>" .$row['product_weight'].'kg';
echo "</td> ";
echo "<td><form action='store_configuration/edit_product' method='post' id='delivery_change_form'>";
echo "<input type='text' name='product' value='$product_no' style='opacity: 0;'/>";
echo "<input type='Submit' value='Edit Product' >";
echo "</form></td>";
echo "<td><form action='store_configuration/change_parent_category' method='post' id='delivery_change_form'>";
echo "Parent Category <select name='category' style='height: auto;' >";
**include '../ecommerce/connection.php';
$sql="SELECT category_name FROM categories where status='yes' ";
// Posting Result
$result = mysqli_query($connection, $sql);
// Counting Results
$count=mysqli_num_rows($result);
if($count==0) {
echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No categories setup</h1></div>";
} else {
if ($result = mysqli_query($connection, $sql)){
while($row = $result->fetch_array()) {
$category = $row['category_name'];
echo" <option value='$category' >".$category ."</option>";
}
}
}**
echo "</select>";
echo "<br><br><input type='Submit' value='Update Category' >";
echo "</form></td>";
echo "</tr>";
}
echo "</table>";
}
}
?>
I would love any help on how I can show all products with all categories.
I have tried everything I can think of.
Thanks.
Stan.
You've used the same names of variables for products and categories queries. While the categories are inside of products loop you reassign that variable with new query and that's why it's crashes. Change name of variables for second query and that should fix your script.
<?php
$myusername= $_SESSION['login_user'];
include '../ecommerce/connection.php';
$sql="SELECT * FROM products where status='yes' ";
// Posting Result
$result = mysqli_query($connection, $sql);
// Counting Results
$count=mysqli_num_rows($result);
if($count==0) {
echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No products setup</h1></div>";
} else {
if ($result = mysqli_query($connection, $sql)){
echo "<table id='order_table_small'>";
echo "<th>Image</th>";
echo "<th>Product ID</th>";
echo "<th>Name</th>";
echo "<th>Description</th>";
echo "<th>Price</th>";
echo "<th>Weight</th>";
echo "<th>Options </th>";
while($row = $result->fetch_array())
{
$product_no = $row['product_id'];
echo "<tr>";
echo '<td><img src="images/'. $row['image'] . ' " id="product_image_admin">';
echo "</td> ";
echo "<td>" .$row['product_id'];
echo "</td> ";
echo "<td>" .$row['product_name'];
echo "</td> ";
echo "<td>" .$row['product_description'];
echo "</td> ";
echo "<td>" .'&pound'.$row['product_price'];
echo "</td> ";
echo "<td>" .$row['product_weight'].'kg';
echo "</td> ";
echo "<td><form action='store_configuration/edit_product' method='post' id='delivery_change_form'>";
echo "<input type='text' name='product' value='$product_no' style='opacity: 0;'/>";
echo "<input type='Submit' value='Edit Product' >";
echo "</form></td>";
echo "<td><form action='store_configuration/change_parent_category' method='post' id='delivery_change_form'>";
echo "Parent Category <select name='category' style='height: auto;' >";
**include '../ecommerce/connection.php';
$sql2="SELECT category_name FROM categories where status='yes' ";
// Posting Result
$result2 = mysqli_query($connection, $sql2);
// Counting Results
$count=mysqli_num_rows($result2);
if($count==0) {
echo "<div class='no_order_info_box'><br><h1 id='order_h1'>No categories setup</h1></div>";
} else {
if ($result2 = mysqli_query($connection, $sql2)){
while($row2 = $result2->fetch_array()) {
$category = $row2['category_name'];
echo" <option value='$category' >".$category ."</option>";
}
}
}**
echo "</select>";
echo "<br><br><input type='Submit' value='Update Category' >";
echo "</form></td>";
echo "</tr>";
}
echo "</table>";
}
}
?>

Submit multiple values only from one button

Hi i am trying to submit the multiple form values only from one button i have 10 records and one record has one button of submit and 10 records have 10 buttons of submit what i want there should be only one button of submit and i can submit he whole 10 records from one button:
echo "<table class='table table-hover table-responsive table-bordered'>";
// our table heading
echo "<tr>";
echo "<th class='textAlignLeft'>Product Name</th>";
echo "<th>Price</th>";
echo "<th style='width:5em;'>Quantity</th>";
echo "<th>Image</th>";
echo "<th>Action</th>";
echo "</tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>";
echo "<div class='product-id' style='display:none;'>{$id}</div>";
echo "<div class='product-name'>{$name}</div>";
echo "</td>";
echo "<td>$" . number_format($price, 2, '.' , ',') . "</td>";
if(isset($quantity)){
echo "<td>";
echo "<input type='text' name='quantity' value='{$quantity}' disabled class='form-control' />";
echo "</td>";
echo "<td>";
echo "<td>";
echo "<button class='btn btn-success' disabled>";
echo "<span></span> Submitted!";
echo "</button>";
echo "</td>";
echo "</td>";
}else{
echo "<td>";
echo "<input type='number' name='quantity[]' value='1' class='form-control'/>";
echo "</td>";
echo "<td><img src='product-images/{$image}' width='60' height='60'</td>";
echo "<td>";
echo "<button class='btn btn-primary add-to-cart'>";
echo "<span></span>Submit to cart";
echo "</button>";
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
}
what i want only one button from i can submit all my values: Any help would be high appreciated:
$action = isset($_GET['action']) ? $_GET['action'] : "";
$product_id = isset($_GET['product_id']) ? $_GET['product_id'] : "1";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$quantity = isset($_GET['quantity']) ? $_GET['quantity'] : "1";
$image = isset($_GET['image']) ? $_GET['image'] : "1";
if($action=='added'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> added to your cart!";
echo "</div>";
}
else if($action=='failed'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> failed to add to your cart!";
echo "</div>";
}
// left join to select products
$query = "SELECT p.id, p.name, p.price, p.image, ci.quantity
FROM products p
LEFT JOIN cart_items ci
ON p.id = ci.product_id
ORDER BY p.name";
$stmt = $con->prepare( $query );
$stmt->execute();
// count number of products returned
$num = $stmt->rowCount();
HTML CODE
<?php
// connect to database
include 'config/db_connect.php';
// page headers
$page_title="Cart";
include 'layout_head.php';
// parameters
$action = isset($_GET['action']) ? $_GET['action'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$image = isset($_GET['image']) ? $_GET['image'] : "";
// display a message
if($action=='removed'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> was removed from your cart!";
echo "</div>";
}
else if($action=='quantity_updated'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> quantity was updated!";
echo "</div>";
}
else if($action=='failed'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> quantity failed to updated!";
echo "</div>";
}
else if($action=='invalid_value'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> quantity is invalid!";
echo "</div>";
}
// select products in the cart
$query="SELECT p.id, p.name, p.price, p.image, ci.quantity, ci.quantity * p.price AS subtotal
FROM cart_items ci
LEFT JOIN products p
ON ci.product_id = p.id";
$stmt=$con->prepare( $query );
$stmt->execute();
// count number of rows returned
$num=$stmt->rowCount();
if($num>0){
//start table
echo "<table class='table table-hover table-responsive table-bordered'>";
// our table heading
echo "<tr>";
echo "<th class='textAlignLeft'>Product Name</th>";
echo "<th>Price</th>";
echo "<th style='width:15em;'>Quantity</th>";
echo "<th>Sub Total</th>";
echo "<th>Action</th>";
echo "</tr>";
$total=0;
while( $row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>";
echo "<div class='product-id' style='display:none;'>{$id}</div>";
echo "<div class='product-name'>{$name}</div>";
echo "</td>";
echo "<td>$" . number_format($price, 2, '.', ',') . "</td>";
echo "<td>";
echo "<div class='input-group'>";
echo "<input type='number' name='quantity[]' value='{$quantity}' class='form-control'>";
echo "<span class='input-group-btn'>";
echo "<button class='btn btn-default update-quantity' type='button'>Update</button>";
echo "</span>";
echo "</div>";
echo "</td>";
echo "<td>$" . number_format($subtotal, 2, '.', ',') . "</td>";
echo "<td>";
echo "<a href='remove_from_cart.php?id={$id}&name={$name}' class='btn btn-danger'>";
echo "<span></span> Delete from cart";
echo "</a>";
echo "</td>";
echo "</tr>";
$total += $subtotal;
}
echo "<tr>";
echo "<td><b>Total</b></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td>$" . number_format($total, 2, '.', ',') . "</td>";
echo "<td>";
echo "</a>";
echo "</td>";
echo "</tr>";
echo "</table>";
}else{
echo "<div class='alert alert-danger'>";
echo "<strong>No products found</strong> in your cart!";
echo "</div>";
}
include 'layout_foot.php';
?>
Use name quantity[] instead of quantity.By this way you can receive post data as an array. After the loop use only one submit button.
echo "<input type='number' name='quantity[]' value='1' class='form-control'/>";
Then receive it by using foreach
foreach($_POST['quantity'] as $qty)
{
echo $qty;
//Here you will receive all the quantities one by one.
}
Form the code
<?php
echo "<table class='table table-hover table-responsive table-bordered'>";
// our table heading
echo "<tr>";
echo "<th class='textAlignLeft'>Product Name</th>";
echo "<th>Price</th>";
echo "<th style='width:5em;'>Quantity</th>";
echo "<th>Image</th>";
echo "</tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>";
echo "<div class='product-id' style='display:none;'>{$id}</div>";
echo "<div class='product-name'>{$name}</div>";
echo "</td>";
echo "<td>$" . number_format($price, 2, '.' , ',') . "</td>";
if(isset($quantity)){
echo "<td>";
echo "<input type='text' name='quantity[]' value='{$quantity}' disabled class='form-control' />";
echo "</td>";
echo "<td>";
echo "</td>";
echo "<td>";
echo "</td>";
}else{
echo "<td>";
echo "<input type='number' name='quantity[]' value='1' class='form-control'/>";
echo "</td>";
echo "<td><img src='product-images/{$image}' width='60' height='60'</td>";
echo "<td>";
echo "</td>";
}
echo "</tr>";
}
echo "<tr><td colspan='4'>";
echo "<button class='btn btn-primary add-to-cart' type='submit'>";
echo "<span></span>Submit to cart";
echo "</button>";
echo "</td></tr>";
echo "</table>";

How to random the choices in php

I am creating a random generated quiz with 10 questions. In generating the random question is fine, but I want to display also the choices in random, same with the questions.
This is my code that I am currently working with:
<?php
generate();
function generate(){
include('connection.php');
mysql_select_db('exam');
$result=mysql_query("SELECT * FROM questionaires
INNER JOIN choices ON questionaires.q_id=choices.q_id
WHERE RAND()<(SELECT ((10/COUNT(*))*10) FROM questionaires)
ORDER BY RAND() LIMIT 10");
$c=0;
echo "<table border='3' align='center' bordercolor='#CCCCCC'>
<tr>
<th>Number:</th>
<th>Question</th>
</tr>
";
while($row = mysql_fetch_array($result)){
$c++;
echo "<tr>";
echo "<td>" . $c . "</td>";
echo "<td>";
echo $row['question'] . "<br>";
echo "<input type='radio' name='ans'>".$row['choice_a']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_b']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_c']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_d']."</input><br>";
echo "</td>";
echo "</tr>";
//}
//}
}
echo "</table>";
}
?>
Little help will highly appreciated.
You could change script to this:
echo "<td>";
echo $row['question'] . "<br>";
$ans=array($row['choice_a'],$row['choice_b'],$row['choice_c'],$row['choice_d']);
shuffle($ans);
foreach ($ans as $choice) {
echo "<input type='radio' name='ans'>".$choice."</input><br>";
} unset($choice);
echo "</td>";

I want to know how to get the selected data on another webpage using PHP

I have use loop to show limited data and each loop i have button to select it.
I have a syntax error in my view.php i dont know what to put in that query
How do i select a row in index.php to view more details in view.php. I tried giving it a name in the echo $rows; loop but i got syntax error too.
This is my code in index.php
<?php
$sqlQuery = mysql_query("SELECT fname FROM info");
while ($rows = mysql_fetch_array($sqlQuery)){
echo $rows['fname'];
echo "<input type='submit' value='Show more info' name='submit' class='btn btn-info'>";
}
?>
-----------------------end code for index------------------------------
this is my code in view
<?php
$sqlQuery = mysql_query("SELECT * FROM info WHERE fname=$_POST['fname']");
echo "<table border='1' width='50%'>";
echo "<tr>";
echo "<td>First Name</td>";
echo "<td>last Name</td>";
echo "<td>Age Name</td>";
echo "</tr>";
while ($rows = mysql_fetch_array($sqlQuery)){
echo "<tr>";
echo "<td>";
echo $rows['fname'];
echo "</td>";
echo "<td>";
echo $rows['lname'];
echo "</td>";
echo "<td>";
echo $rows['age'];
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
-------------------------------end code for view--------------------
<form method="POST" action="view.php">
<?php
$sqlQuery = mysql_query("SELECT fname FROM info");
while ($rows = mysql_fetch_array($sqlQuery)){
echo "<input type='text' value='".$rows['fname']."' name='fname'>";
echo "<input type='submit' value='Show more info' name='submit' class='btn btn-info'>";
}
?>
</form>
Try this in view.php
echo $_POST['fname'];
$query = "SELECT * FROM info WHERE fname LIKE ".$_POST['fname']."";
echo $query;
check what output are you getting

PHP postback on submit

I have a question on php postback
My code is :
<?php
if(isset($_POST["Delete"]))
{
echo "DELETE";
}
if(isset($_POST["Modifier"]))
{
echo "Modifier";
}
if(!empty($_SESSION["Status"]))
{
if($_SESSION["Status"] == "u")
{
header("Location: Index.php?Action=Acceuil");
}
if($_SESSION["Status"] == "a")
{
$Connection = mysql_connect("localhost","root") or die(mysql_error());
mysql_select_db("tpw34") or die("Nope.");
$query = "Select * From Products";
$result = mysql_query($query);
While($ligne = mysql_fetch_assoc($result))
{
//Index.php?Action=AdminDeleteProduct&Delete=".$ligne["ProductID"]."
echo "<form method='POST' Action='#'>";
echo "<table border='1'>";
echo "<tr>";
echo "<td colspan='2'><center><img width='250' height='250' src='".$ligne["Image"]."'/></center></td>";
echo "</tr>";
echo "<tr>";
echo "<th>Nom du produit :</th>";
echo "<td>".$ligne["ProductName"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Prix :</th>";
echo "<td>".$ligne["Prix"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Description :</th>";
echo "<td>".$ligne["Description"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2'h><input type='Submit' value='Delete' name='Delete'/><input type='Submit' value='Modifier' name='Modifier'/></td>";
echo "</tr>";
echo "</table>";
echo "<br>";
echo "</form>";
}
}
}
?>
My Question is : I want to get the ProductID of the item ( in the table ) to be in the $_POST["Delete"] and $_POST["Modifier"] but i dont wanna change the text on the button. I want to keep DELETE and MODIFIER. I have read many things on the web but i dont find the correct answer.
Include a hidden form value for ProductID. Then you can retrieve the value in $_POST['ProductID']
echo "<input type=hidden name='ProductID' value='" . $ligne["ProductID"] . "'>";
You could work with sessions, where you can temporarily save your information.
Sessions
Or like Tim Dearborn suggested, use a hidden input to send it with the next form submit.

Categories