I created a website selling components. Each component has 9 products. How can I add a dropdown button which can arrange the product showing by its Price or Name?
<?php
$query = "SELECT * FROM products ORDER BY id ASC";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
?>
<div class="col-md-4">
<form method="post" action="shop.php?action=add&id=<?php echo $row["id"]; ?>">
<div style="border: 1px solid #eaeaec; margin: -1px 19px 3px -1px; box-shadow: 0 1px 2px rgba(0,0,0,0.05); padding:10px;" align="center">
<h5 class="text-info"><?php echo $row["p_name"]; ?></h5>
<img src="<?php echo $row["image"]; ?>" class="img-responsive">
<h5 class="text-danger">$ <?php echo $row["price"]; ?></h5>
<div class="col-xs-8">
<input type="text" name="quantity" class="form-control" value="1">
</div>
<input type="hidden" name="hidden_name" value="<?php echo $row["p_name"]; ?>">
<input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>">
<input type="submit" name="add" class="btn btn-primary" value="Add to Cart" align="right">
</div>
</form>
</div>
<?php
}
}
?>
<?php
}
?>
</div>
</div>
Perhaps you could use Javascript as well and add a few select boxes to give Users the chance to choose the Attribute they want to sort by and also the Direction of the Sorting as in ASC or DESC. The Code Snippet below might shed some light:
<?php
$query = "SELECT * FROM products ORDER BY id ASC";
$result = mysqli_query($connect, $query);
// CREATE VARIABLES TO HOLD THE SELECT OPTIONS VALUES FOR THE SORTING...
$sortParam1 = 'price';
$sortParam2 = 'name';
$sortDir1 = 'ASC';
$sortDir2 = 'DESC';
// START BUILDING THE OUTPUT...
$output = "<div class='col-md-12'>"; //<== WRAPPER FOR SORING & DIRECTION BOXES
$output .= "<form method='post' action='' id='sorting_form'>"; //<== FORM FOR THE SORTING: SUBMITS TO SAME SCRIPT
// SORTING
$output .= "<div class='col-md-6'>";
$output .= "<select name='sorting' id='sorting'class='form-control'>";
$output .= "<option value='id'>Sorting</option>";
$output .= "<option value='{$sortParam1}'>{$sortParam1}</option>";
$output .= "<option value='{$sortParam2}'>{$sortParam2}</option>";
$output .= "</select>";
$output .= "</div>";
// DIRECTION
$output .= "<div class='col-md-6'>";
$output .= "<select name='direction' id='direction'class='form-control'>";
$output .= "<option value='ASC'>Sort Direction</option>";
$output .= "<option value='{$sortDir1}'>{$sortDir1}</option>";
$output .= "<option value='{$sortDir2}'>{$sortDir2}</option>";
$output .= "</select>";
$output .= "</div>";
$output .= "</form>"; //<== CLOSE SORTING FORM...
$output .= "</div>"; //<== CLOSE WRAPPER...
$rowData = "";
// WE USE JQUERY BELOW TO SUBMIT THE SORTING FORM ONCE USER
// SELECTS ANY OPTION FROM.... SO WE NOW HANDLE THAT SCENARIO HERE USING PHP
if(isset($_POST['sorting']) || isset($_POST['direction']) ){
$sortDirection = isset($_POST['direction']) ? $_POST['direction'] : $sortDir1; // DEFAULTS TO ASC
$sortField = isset($_POST['sorting']) ? $_POST['sorting'] : 'id'; // DEFAULTS TO id
$query = "SELECT * FROM products ORDER BY {$sortField} {$sortDirection}";
$result = mysqli_query($connect, $query);
}
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)) {
// USE HEREDOC TO CAPTURE THE DATA WITHIN THE LOOP...
$rowData .=<<<R_DATA
<div class="col-md-4">
<form method="post" action="shop.php?action=add&id={$row["id"]}">
<div style="border: 1px solid #eaeaec; margin: -1px 19px 3px -1px; box-shadow: 0 1px 2px rgba(0,0,0,0.05); padding:10px;" align="center">
<h5 class="text-info">{$row["p_name"]}</h5>
<img src="{$row["image"]}" class="img-responsive">
<h5 class="text-danger">\$ {$row["price"]}</h5>
<div class="col-xs-8">
<input type="text" name="quantity" class="form-control" value="1">
</div>
<input type="hidden" name="hidden_name" value="{$row["p_name"]}">
<input type="hidden" name="hidden_price" value={$row["price"]}">
<input type="submit" name="add" class="btn btn-primary" value="Add to Cart" align="right">
</div>
</form>
</div>
R_DATA;
} // CLOSE WHILE LOOP
} // CLOSE IF CONDITIONAL LOGIC
// ADD THE DATA GATHERED FROM WHILE LOOP ($rowData) TO THE OUTPUT: $output
$output .= $rowData;
$output .= "</div>\n</div>"; // THESE APPEAR IN YOUR CODE BUT SEEM IRRELEVANT HERE...
// DISPLAY THE OUTPUT
echo $output;
?>
<!-- ENTER JAVASCRIPT: JQUERY -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js" integrity="sha384-I6F5OKECLVtK/BL+8iSLDEHowSAfUo76ZL9+kGAgTRdiByINKJaqTPH/QVNS1VDb" crossorigin="anonymous"></script>
<script type="text/javascript">
(function($){
$(document).ready(function(e){
var sortForm = $("#sorting_form");
var sortBox = $("#sorting");
var directionBox = $("#direction");
var bothBoxes = sortBox.add(directionBox);
// IF EITHER OF THE SORTING OR DIRECTION IS CHANGED
// JUST SUBMIT THE FORM
bothBoxes.on("change", function(evt){
sortForm.submit();
});
});
})(jQuery);
</script>
Related
Is it possible to add color on approve and decline, basically green for approve and red for declined? I've tried looking it up but I can't seem to find a way to do it.
Here's the php code:
<?php
if(isset($_POST['approve']))
{
$msg = "Approved";
$approval="Approved";
}
if(isset($_POST['decline']))
{
$msg = "Declined";
$approval="Declined";
}
$reqnumber=$_POST['reqnumber'];
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'pcrequest');
$sql = "UPDATE request SET approval = '$approval' WHERE reqnumber = '$reqnumber'";
if(mysqli_query($con, $sql))
header("refresh:1; url=messages-admin.php?msg=$msg");
else
var_dump(mysqli_error($con));
?>
HTML:
<div class="container" style="width: 1370px; margin-left: -40px;">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Search</span>
<input type="text" name="search_text" id="search_text" placeholder="Search by Employee Name, Account, Platform, etc." class="form-control" />
</div>
</div>
<div id="result"></div>
</div>
</div>
</div>
Fetch:
$output .= '<tr>
<td>'.$row["reqname"].'</td>
<td>'.$row["month"]."/".$row["day"]."/".$row["year"].'</td>
<td>'.$row["empname"].'</td>
<td>'.$row["position"].'</td>
<td>'.$row["account"].'</td>
<td>'.$row["platform"].'</td>
<td>'.$row["processor"].'</td>
<td>'.$row["ram"].'</td>
<td>'.$row["monitor"].'</td>
<td>'.$row["phone"].'</td>
<td>'.$row["phonetype"].'</td>
<td>'.$row["headset"].'</td>
<td>'.$row["approval"].'</td>';
if ($row['status']) :
$output .= '<td>'.$row["status"].'</td> ';
else:
$output .= '
<td>
<form method="post" action="update-request-status.php">
<input type="hidden" name="reqnumber" value="'.$row['reqnumber'].'" />
<button class="button" type="submit" name="completed" value=""><span>New Request!</span></button>
</form>
</td>
<td><i class="fa fa-edit" style="color: black; font-size: 25px;"></i></td>
<td><i class="fa fa-trash" style="color: red; font-size: 25px;"></i></td>
</tr>
';
endif;
I've added the HTML and PHP code, as you can see HTML is fetching data from PHP.
Store your color in a variable and use it wherever you want to colorize
$color = $row["approval"] == "Approved" ? "green" : "red";
//Then use $color in any element you want to colorize
'<td style="color:' . $color . ';">' .$row["approval"] . '</td>';
or use that
'<td style="color:' . ($row["approval"] == "Approved" ? "green" : "red") . ';">' .$row["approval"] . '</td>';
For multiple colors, use an array()
$colors = array();
$colors["Approved"] = "green";
$colors["Declined"] = "red";
$colors["Pending"] = "blue";
<td style="color:' . $colors[$row["approval"]] . ';">' .$row["approval"] . '</td>';
You can use php to generate css and html.
<?php
if(isset($_POST['approve']))
{
$msg = "Approved";
$approval="Approved";
$color = "green";
}
else if(isset($_POST['decline']))
{
$msg = "Declined";
$approval="Declined";
$color="red";
}
?>
$output .= '<tr>
<td>'.$row["reqname"].'</td>
<td>'.$row["month"]."/".$row["day"]."/".$row["year"].'</td>
<td>'.$row["empname"].'</td>
<td>'.$row["position"].'</td>
<td>'.$row["account"].'</td>
<td>'.$row["platform"].'</td>
<td>'.$row["processor"].'</td>
<td>'.$row["ram"].'</td>
<td>'.$row["monitor"].'</td>
<td>'.$row["phone"].'</td>
<td>'.$row["phonetype"].'</td>
<td>'.$row["headset"].'</td>
<td' . 'style="color:' . $color . '"><?=$msg?>>'.$row["approval"].'</td>';
if ($row['status']) :
$output .= '<td>'.$row["status"].'</td> ';
else:
$output .= '
<td>
<form method="post" action="update-request-status.php">
<input type="hidden" name="reqnumber" value="'.$row['reqnumber'].'" />
<button class="button" type="submit" name="completed" value=""><span>New Request!</span></button>
</form>
</td>
<td><i class="fa fa-edit" style="color: black; font-size: 25px;"></i></td>
<td><i class="fa fa-trash" style="color: red; font-size: 25px;"></i></td>
</tr>
';
endif;
in this php code i have foreach loop create textarea with checkbox for each statement
i wanna to enable textarea if his checkbox is checked
but they have same id
<?php
foreach($Arows as $row){
echo '<tr class="row">
<th class="col-sm-12"><h4>'.$row['Type_A'].'</h4></th>
</tr>';
$stmt3 = $con->prepare("SELECT * FROM sous_analyse WHERE Id_A =".$row['Id_A']);
$stmt3->execute();
$SArows = $stmt3->fetchAll();//Analyse rows
foreach($SArows as $Srow){
echo '<tr class="row">
<td class="col-sm-4">'.$Srow['Parameter'].'</td>
<td class=" col-sm-5"><input type="text" class="form-control col-xl-12 border-secondary" name="Fonctionnalite" placeholder="RESULTATS" required=""></td>
<td class="col-sm-3 text-center">'.$Srow['Normal'].'</td>
</tr>';
}
echo '<tr class="row">
<th class="col-sm-12">
<div class="input-group">
<span class="input-group-addon form-check">
<span class="checkbox-custom checkbox-default">
<input type="checkbox" class="icheckbox-grey" id="inputUnchecked" name="inputUnchecked">
<label for="inputUnchecked"></label>
</span>
</span>
<textarea class="form-control" id="textareaDefault" rows="3" style="margin-top: 0px; margin-bottom: 0px; height: 60px;" disabled></textarea>
</div>
</th>
</tr>';
}
?>
i use this script but is working in first textarea
<script >
$(document).ready(function() {
$("#inputUnchecked").on('click', function() {
if($(this).is(':checked')){
$("#textareaDefault").prop('disabled',false);
} else {
$("#textareaDefault").prop('disabled',true);
}
alert("fdg");
})
});
</script>
Expanding on my comment, you will want to adjust your PHP. This is easily done with a counter ($c).
<?php
foreach($Arows as $row){
$html = "";
$html .= '<tr class="row">'."\r\n";
$html .= '<th class="col-sm-12"><h4>'.$row['Type_A'].'</h4></th>'."\r\n";
$html .= '</tr>'."\r\n";
$stmt3 = $con->prepare("SELECT * FROM sous_analyse WHERE Id_A =".$row['Id_A']);
$stmt3->execute();
$SArows = $stmt3->fetchAll();
$c = 1;
foreach($SArows as $Srow){
$html .= '<tr class="row">'."\r\n";
$html .= '<td class="col-sm-4">'.$Srow['Parameter'].'</td>';
$html .= '<td class=" col-sm-5"><input type="text" class="form-control col-xl-12 border-secondary" name="Fonctionnalite" placeholder="RESULTATS" required=""></td>';
$html .= '<td class="col-sm-3 text-center">'.$Srow['Normal'].'</td>';
$html .= '</tr>';
}
$html .= '<tr class="row">';
$html .= '<th class="col-sm-12">';
$html .= '<div class="input-group">';
$html .= '<span class="input-group-addon form-check">';
$html .= '<span class="checkbox-custom checkbox-default">';
$html .= '<input type="checkbox" class="icheckbox-grey" id="inputUnchecked-$c" name="inputUnchecked">';
$html .= '<label for="inputUnchecked-$c"></label>';
$html .= '</span></span>';
$html .= '<textarea class="form-control" id="textareaDefault-$c" rows="3" style="margin-top: 0px; margin-bottom: 0px; height: 60px;" disabled></textarea>';
$html .= '</div></th></tr>';
echo $html;
$c++;
}
?>
Your JavaScript can then use the ID since it is unique or can simply be relative to nearby elements.
$(function() {
$("[id^='inputUnchecked-']").on('click', function() {
if($(this).is(':checked')){
$(this).parent().parent().find("[id^='textareaDefault-']").prop('disabled',false);
} else {
$(this).parent().parent().find("[id^='textareaDefault-']").prop('disabled',true);
}
alert("fdg");
})
});
My intention is to display the content(which is stored in mysql) if the picture is clicked.
<?php
$query = "SELECT * FROM pet where pet_cat = 'D' ORDER BY petid ";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result))
{
$_SESSION['petname'] = $row['petname'];
$_SESSION['petdesc'] = $row['petdesc'];
$_SESSION['petimg'] = $row['petimg'];
echo '<li style="
padding-right: 20px;
padding-left: 20px;">';
echo '<a style= "cursor: pointer;"onclick= "document.getElementById(\'dogmod\').style.display=\'block\'">';
echo '<img src="data:image/jpeg;base64,'.base64_encode($_SESSION['petimg'] ).'" />';
echo '<h4>';
echo $_SESSION['petname'] ;
echo '</h4>';
include 'desca.php';
echo '</a>';
echo '</li>';
}
?>
The modal only display one content and that is the first content of the first picture.(sorry for bad english )
This is the code of my modal:
<div id="dogmod" class="modal">
<center>
<form class="modal-content animate" style="margin: 0; padding-left:50px; padding-right:50px; padding-bottom:50px;">
<div class="imgcontainer">
<span onclick="document.getElementById('dogmod').style.display='none'" class="close" title="Close">×</span>
<h1 align=center>Description</h1>
<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($_SESSION['petimg'] ).'" style="margin-top:50px;float:left; margin-right:50px;"/>'; ?>
<h1 style="margin-top:50px;margin-bottom:50px">
<?php echo $_SESSION['petname'] ;?>
</h1>
<p style="margin-bottom:50px;">
<?php echo $_SESSION['petdesc'] ;?>
</p>
<input type="button" value="Back" onclick=location.href='doga.php' class="button_1" style=" width: auto;padding: 10px 18px; background-color: #f44336; border:0px; color:white;">
<h2> </h2>
</form>
</center>
<script>
// Get the modal
var modal = document.getElementById('dogmod');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</div>
The problem is that you are generating many modals with the same "id".
What happens in this case is that you will be displayed only the last image in the table.
Generate your modal id dynamically.
Use php and add an affix to the id attribute of the modal and call that one in your onclick event.
Here is how you do it:
$query = "SELECT * FROM pet where pet_cat = 'D' ORDER BY petid ";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result))
{
$_SESSION['petname'] = $row['petname'];
$_SESSION['petdesc'] = $row['petdesc'];
$_SESSION['petimg'] = $row['petimg'];
$_SESSION['petid'] = $row['petid'];
echo '<li style="
padding-right: 20px;
padding-left: 20px;">';
echo '<a style= "cursor: pointer;"onclick= "document.getElementById(\'dogmod'.$row['petid'].'\').style.display=\'block\'">';
echo '<img src="data:image/jpeg;base64,'.base64_encode($_SESSION['petimg'] ).'" />';
echo '<h4>';
echo $_SESSION['petname'] ;
echo '</h4>';
include 'desca.php';
echo '</a>';
echo '</li>';
}
And in your modal php file, change only your first line to:
<div id="dogmod<?=$_SESSION['petid']?>" class="modal">
You should be good to go.
To make it close just fine, do the following in your modal php template find the 'close' link:
<span onclick="document.getElementById('dogmod<?= $_SESSION['petid'] ?>').style.display='none'" class="close" title="Close">×</span>
I have a products table where I have products being listed into. I want to create a peice of code that will show the same style but different price, name, description, picture for each product. I have create a peice of code that sort of does this. My code displays only the first in the row. I need to list all of them. My code is :
function grabProducts($con) {
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="col-md-12" id="please_wait">
<div class="panel panel-warning">
<div class="panel-heading">
<h3 class="panel-title"><i>Loading, Please Wait...</i></h3>
</div>
<div class="panel-body">
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('#please_wait').fadeOut(7000);
$('#content_purchase').hide(0).delay(5000).fadeIn("slow");
});
</script>
<?php
$users = $this->grabUserInfos($con);
foreach ($users as $user) {
$username = $user[1];
$email = $user[3];
}
$site_config = new site_config();
$member_config = new member_config();
$result = mysqli_query($con, "SELECT * FROM products");
$count = mysqli_num_rows($result);
// See if there are any products in the database
if ($count > 0) {
// While loop for each product element
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$name = $row['name'];
$description = $row['description'];
$picture = $row['picture_location'];
$price = $row['price'];
$stock_monitering = $row['stock_monitering'];
$stock = $row['stock'];
$new_stock = $stock - 1;
$url_path = 'http' . (empty($_SERVER['HTTPS']) ? '' : 's') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url_path_naked = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH).'/pizza/products.php';
$getValidatedCheck_ = mysqli_query($con, "SELECT txn_id, hasValidated, item_name, amount, currency, payment_date FROM payment_logs WHERE userId = '$username' ORDER BY id DESC LIMIT 1");
$count_success_payment = mysqli_num_rows($getValidatedCheck_);
// While loop for our successful payment_logs for each user
while ($row = mysqli_fetch_array($getValidatedCheck_)) {
$txn_id = $row['txn_id'];
$validated_check = $row['hasValidated'];
$item_name = $row['item_name'];
$item_price = $row['amount'];
$item_currency = $row['currency'];
$payment_date = $row['payment_date'];
$now = strtotime("-10 minutes");
// If there is not enough stock, show out of stock
if($stock <= 0) {
$display = 'out_of_stock';
} else if($stock > 0 && $now > strtotime($payment_date)) {
$display = 'show_products';
}
// If there are no transactions for that user show the products like normal
if($count_success_payment == 0) {
// If they've not already seen the success message, show them it now and update their hasValidated from 0 to 1 so they don't see it again
if ($validated_check == '0') {
// If stock monitering is on, update our stock to 1 less than we had before the purchase
if($stock_monitering == '1') {
mysqli_query($con, "UPDATE products SET stock = '$new_stock' WHERE name = '$name'");
}
$display = 'show_success';
}
}
}
switch($display) {
case "show_products":
?>
<div class="col-md-3" id="content_purchase" style="display: none;">
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title">Price: <i>$<?php echo $price; ?></i>
<div class="fRight" style="float: right;"><i><?php echo $name; ?></i> Stock
(<?php echo $stock; ?>)
</div>
</h3>
</div>
<center>
<div class="panel-body">
<img src="<?php echo $picture; ?>"
style="width: 85%; height: 100px; margin: 15px 0px 15px 0px; border-radius: 5px; border: 2px solid #ED4949;">
<form name="paypal_form" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business"
value="<?php $site_config->grabSiteSettings($con, 'paypal_address'); ?>">
<input type="hidden" name="item_name" value="<?php echo $name; ?>">
<input type="hidden" name="item_number" value="<?php echo $id; ?>">
<input type="hidden" name="amount" value="<?php echo $price; ?>">
<input type="hidden" name="quantity" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="custom" value="username=<? echo $username; ?>&status=<? echo $checkout_status; ?>&product=<? echo $name; ?>">
<input type="hidden" name="notify_url" value="https://benzaofficial.com/pizza/includes/checkout.php">
<input type="hidden" name="return"
value="<?php $site_config->grabSiteSettings($con, 'site_url'); ?>/pizza/products.php?status=complete">
<input type="hidden" name="cancel_return"
value="<?php $site_config->grabSiteSettings($con, 'site_url'); ?>/pizza/products.php?status=canceled">
<button type="submit" class="btn btn-danger"
style="vertical-align : bottom; margin-bottom: 15px; display: block; width: 85%;">
<i class="fa fa-paypal"></i>aypal
</button>
<form>
</div>
</center>
</div>
</div>
<?php
break;
case "show_success":
?>
<div class="col-md-12" id="content_purchase" style="display: none;">
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title"><i>PAYMENT SUCCESS!</i></h3>
</div>
<div class="panel-body">
<p>You've successfully purchased <i><?php echo $item_name; ?></i> for <i><?php echo $item_price; ?> <i><?php echo $item_currency; ?></i></i> We have emailed you your receipt to <?php echo $email; ?>. You can click here to download your purchased files.<br>Click here to purchase again.</p>
</div>
</div>
</div>
<?php
mysqli_query($con, "UPDATE payment_logs SET hasValidated = '1' WHERE userId = '$username' ORDER BY id DESC LIMIT 1");
break;
case "no_products":
?>
<div class="col-md-12" id="content_purchase" style="display: none;">
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><i>Currently No Products For Sale</i></h3>
</div>
<div class="panel-body">
<p>There are currently no products up for sale.</p>
</div>
</div>
</div>
<?php
break;
case "out_of_stock":
?>
<div class="col-md-3" id="content_purchase" style="display: none;">
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title">Price: <i>$<?php echo $price; ?></i>
<div class="fRight" style="float: right;"><i><?php echo $name; ?></i> Stock
(<?php echo $stock; ?>)
</div>
</h3>
</div>
<center>
<div class="panel-body">
<img src="<?php echo $picture; ?>"
style="width: 85%; height: 100px; margin: 15px 0px 15px 0px; border-radius: 5px; border: 2px solid #ED4949;">
<p><i><?php echo $name; ?></i> is currently out of stock. Please come back later and try again.</p>
</div>
</center>
</div>
</div>
<?php
break;
}
}
} else {
$display = 'no_products';
}
}
first you need a function dat select all of your products like this one
function showproducts($conn,$id){
$req="SELECT * FROM products ";
$liste=$conn->query($req);
return $liste->fetchAll();
}
than you should call this function :
$list=$cc->showproducts($cc->conn);
and foreach row you can show all columns
<?php
foreach ($list as $l){ ?>
<tr>
<td><?php echo $l[0] ;?> </td>
<td><?php echo $l[1] ;?> </td>
<td><?php echo $l[2] ;?> </td>
<td><?php echo $l[3] ;?> </td>
<td><?php echo $l[7] ;?> </td>
</tr>
<?php } ?>
I'm trying to display one of these boxes : (Image here) for every product. But when I add another product in the database, it doesn't seem to add another on view for my products page. Here you see my database, but only one image on my products page. The page looks as same as the first image posted above. If anyone knows why my products aren't showing for each product, only the first one, then please let me know. I know my code is not secure, not neat. I will do that after I'm done getting this to work so don't judge me on that. Only the issue I asked. Thanks.
My PHP code is all in this one function which I call in my div containers :
function grabProducts($con) {
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="col-md-12" id="please_wait">
<div class="panel panel-warning">
<div class="panel-heading">
<h3 class="panel-title"><i>Loading, Please Wait...</i></h3>
</div>
<div class="panel-body">
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('#please_wait').fadeOut(7000);
$('#content_purchase').hide(0).delay(5000).fadeIn("slow");
});
</script>
<?php
$users = $this->grabUserInfos($con);
foreach ($users as $user) {
$username = $user[1];
$email = $user[3];
}
$site_config = new site_config();
$member_config = new member_config();
$result = mysqli_query($con, "SELECT * FROM products");
$count = mysqli_num_rows($result);
$site_callback = $site_config->grabSiteSettings_manual($con, 'site_url').'/pizza/includes/checkout.php';
$site_return_success = $site_config->grabSiteSettings_manual($con, 'site_url').'/pizza/products.php?status=complete';
$site_return_canceled = $site_config->grabSiteSettings_manual($con, 'site_url').'/pizza/products.php?status=canceled';
// See if there are any products in the database
if ($count > 0) {
// While loop for each product element
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$name = $row['name'];
$description = $row['description'];
$picture = $row['picture_location'];
$price = $row['price'];
$stock_monitering = $row['stock_monitering'];
//$stock = $row['stock'];
// Count the number links in each product
$product_count = mysqli_query($con, "SELECT COUNT(*) FROM ebook WHERE used_status = '0'");
$row_product_count = mysqli_fetch_row($product_count);
$count_product_stock = $row_product_count[0];
$stock = $count_product_stock;
$new_stock = $stock - 1;
$getValidatedCheck_ = mysqli_query($con, "SELECT txn_id, hasValidated, item_name, amount, currency, payment_date FROM payment_logs WHERE userId = '$username' ORDER BY id DESC LIMIT 1");
// Count the number of payment_logs that correspond to the logged in user
$payment_log_count = mysqli_query($con, "SELECT COUNT(*) FROM payment_logs WHERE userId = '$username'");
$row = mysqli_fetch_row($payment_log_count);
$count_payment_logs = $row[0];
$now = strtotime("-10 minutes");
// If there are no transactions for that user show the products like normal
if($count_payment_logs <= 0) {
// If there is not enough stock, show out of stock
if($stock <= 0) {
$display = "out_of_stock";
} else if($stock > 0) {
$display = "show_products";
}
} else if($count_payment_logs >= 1) {
// While loop for our successful payment_logs for each user
while ($row = mysqli_fetch_array($getValidatedCheck_)) {
$txn_id = $row['txn_id'];
$validated_check = $row['hasValidated'];
$item_name = $row['item_name'];
$item_price = $row['amount'];
$item_currency = $row['currency'];
$payment_date = $row['payment_date'];
// If they've not already seen the success message, show them it now and update their hasValidated from 0 to 1 so they don't see it again
if ($validated_check == '0') {
// If stock monitering is on, update our stock to 1 less than we had before the purchase
if($stock_monitering == '1') {
mysqli_query($con, "UPDATE products SET stock = '$new_stock' WHERE name = '$name'");
}
$product_count = mysqli_query($con, "UPDATE ebook SET used_status = '1', assigned_to = '$username', transaction_id = '$txn_id' WHERE used_status = '0'");
$display = "show_success";
} else {
if($stock <= 0) {
$display = "out_of_stock";
} else if($stock > 0) {
$display = "show_products";
}
}
}
}
switch($display) {
case "show_products":
?>
<div class="col-md-3" id="content_purchase" style="display: none;">
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title">Price: <i>$<?php echo $price; ?></i>
<div class="fRight" style="float: right;"><i><?php echo $name; ?></i> Stock
(<?php echo $stock; ?>)
</div>
</h3>
</div>
<center>
<div class="panel-body">
<img src="<?php echo $picture; ?>"
style="width: 85%; height: 100px; margin: 15px 0px 15px 0px; border-radius: 5px; border: 2px solid #ED4949;">
<form name="paypal_form" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="POST">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="<?php echo $site_config->grabSiteSettings_manual($con, 'paypal_address'); ?>">
<input type="hidden" name="item_name" value="<?php echo $name; ?>">
<input type="hidden" name="item_number" value="<?php echo $id; ?>">
<input type="hidden" name="amount" value="<?php echo $price; ?>">
<input type="hidden" name="quantity" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="custom" value="username=<?php echo $username; ?>&product= <?php echo $name; ?>">
<input type="hidden" name="notify_url" value="<?php echo $site_callback; ?>">
<input type="hidden" name="return" value="<?php echo $site_return_success; ?>">
<input type="hidden" name="cancel_return" value="<?php echo $site_return_canceled; ?>">
<button type="submit" class="btn btn-danger"
style="vertical-align : bottom; margin-bottom: 15px; display: block; width: 85%;">
<i class="fa fa-paypal"></i>aypal
</button>
<form>
</div>
</center>
</div>
</div>
';
break;
case "show_success":
?>
<div class="col-md-12" id="content_purchase" style="display: none;">
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title"><i>PAYMENT SUCCESS!</i></h3>
</div>
<div class="panel-body">
<p>You've successfully purchased <i><?php echo $item_name; ?></i> for <i><?php echo $item_price; ?> <i><?php echo $item_currency; ?></i>. We have emailed you your receipt to <?php echo $email; ?>. You can click here to download your purchased files.<br>Click here to purchase again.</p>
</div>
</div>
</div>
<?php
mysqli_query($con, "UPDATE payment_logs SET hasValidated = '1' WHERE userId = '$username' ORDER BY id DESC LIMIT 1");
break;
case "no_products":
?>
<div class="col-md-12" id="content_purchase" style="display: none;">
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><i>Currently No Products For Sale</i></h3>
</div>
<div class="panel-body">
<p>There are currently no products up for sale.</p>
</div>
</div>
</div>
<?php
break;
case "out_of_stock":
?>
<div class="col-md-3" id="content_purchase" style="display: none;">
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title">Price: <i>$<?php echo $price; ?></i>
<div class="fRight" style="float: right;"><i><?php echo $name; ?></i> Stock
(<?php echo $stock; ?>)
</div>
</h3>
</div>
<center>
<div class="panel-body">
<img src="<?php echo $picture; ?>"
style="width: 85%; height: 100px; margin: 15px 0px 15px 0px; border-radius: 5px; border: 2px solid #ED4949;">
<p style="margin: 0px 15px 0px 15px;"><i><?php echo $name; ?></i> is currently out of stock. Please come back later and try again.</p>
</div>
</center>
</div>
</div>
<?php
break;
}
}
} else {
$display = 'no_products';
}
}
You should also loop your UI box so that you can have the same number of the product in the database and the box printed.
$sql = "Select * from product";
foreach('fetch all product from DB'){
?>
<div class="col-md-3" id="content_purchase" style="display: none;">
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title">Price: <i>$<?php echo $price; ?></i>
<div class="fRight" style="float: right;"><i><?php echo $name; ?></i> Stock
(<?php echo $stock; ?>)
</div>
</h3>
</div>
<center>
<div class="panel-body">
<img src="<?php echo $picture; ?>"
style="width: 85%; height: 100px; margin: 15px 0px; border-radius: 5px; border: 2px solid #ED4949;">
<form name="paypal_form" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="POST">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="<?php echo $site_config->grabSiteSettings_manual($con, 'paypal_address'); ?>">
<input type="hidden" name="item_name" value="<?php echo $name; ?>">
<input type="hidden" name="item_number" value="<?php echo $id; ?>">
<input type="hidden" name="amount" value="<?php echo $price; ?>">
<input type="hidden" name="quantity" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="custom" value="username=<?php echo $username; ?>&product= <?php echo $name; ?>">
<input type="hidden" name="notify_url" value="<?php echo $site_callback; ?>">
<input type="hidden" name="return" value="<?php echo $site_return_success; ?>">
<input type="hidden" name="cancel_return" value="<?php echo $site_return_canceled; ?>">
<button type="submit" class="btn btn-danger"
style="vertical-align : bottom; margin-bottom: 15px; display: block; width: 85%;">
<i class="fa fa-paypal"></i>aypal
</button>
<form>
</div>
</center>
</div>
</div>
<?php
} //end loop
sorry I forgot the syntax. :D but that's the logic.