Weekly Menu Rotation - php

So basically i have a Project where i have to make our Menu Order Website better, for that i want to Include a Weekly rotating menu where the Users can order, i have the whole website done including the Admin panels yet i cant figure out how to create a rotating website, for now i have all Menus included in the Main Website i tried it with Wordpress code, which i then realised isnt supported in basic php... the only code i find is from 2014 and i dont have a clue where to include it in my code as im a Newbie :) What would be cool is like a small dropdown button where you can show the next weeks menu too
Main Site section to order menus
<!-- fOOD sEARCH Section Starts Here -->
<section class="food-search text-center">
<div class="container">
<form action="<?php echo SITEURL; ?>food-search.php" method="POST">
<input type="search" name="search" placeholder="Search for Food.." required>
<input type="submit" name="submit" value="Search" class="btn btn-primary">
</form>
</div>
</section>
<!-- fOOD sEARCH Section Ends Here -->
<!-- fOOD MEnu Section Starts Here -->
<section class="food-menu">
<div class="container">
<h2 class="text-center">Mama Bringts Menüs</h2>
<?php
//Zeigt Menüs an die auf Active gesetzt sind
$sql ="SELECT * FROM tbl_food WHERE active='Yes'";
//Query ausführen
$res = mysqli_query($conn, $sql);
//Rows zählen
$count= mysqli_num_rows($res);
//Schauen ob die Menüs verfügbar (Active) sind
if($count>0)
{
//Verfügbar
while($row=mysqli_fetch_assoc($res))
{
//Werte auslesen
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$price = $row['price'];
$image_name =$row['image_name'];
?>
<div class="food-menu-box">
<div class="food-menu-img">
<?php
//Bild verfügbar oder nicht
if($image_name=="")
{
//Bild nicht verfügbar
echo "<div class='error'>Das Bild ist nicht verfügbar</div>";
}
else
{
//Bild verfügbar
?>
<img src="<?php echo SITEURL; ?>images/food/<?php echo $image_name; ?>" alt="Chicke Hawain Pizza" class="img-responsive img-curve">
<?php
}
?>
</div>
<div class="food-menu-desc">
<h4><?php echo $title; ?></h4>
<p class="food-price"><?php echo $price; ?></p>
<p class="food-detail">
<?php echo $description; ?>
</p>
<br>
Jetzt Bestellen
</div>
</div>
<?php
}
}
else
{
//Nicht verfügbar
echo "<div class='error'>Das Menü wurde nicht gefunden!</div>";
}
?>
<div class="clearfix"></div>
</div>
</section>
<!-- fOOD Menu Section Ends Here -->
<?php include('partials-front/footer.php')?>
Admin Site to Manage the Menus
<!-- Main Content Section -->
<div class="main-content">
<div class="wrapper">
<h1>Administrator Verwaltung</h1>
<br /><br /><br />
<?php
if(isset($_SESSION['add']))
{
echo $_SESSION['add']; //Zeigt Session Nachricht an
unset($_SESSION['add']); //Entfernt Session Nachricht
}
if(isset($_SESSION['delete']))
{
Echo $_SESSION['delete'];
unset($_SESSION['delete']);
}
if(isset($_SESSION['update']))
{
echo $_SESSION['update'];
unset($_SESSION['update']);
}
if(isset($_SESSION['user-not-found']))
{
echo $_SESSION['user-not-found'];
unset($_SESSION['user-not-found']);
}
if(isset($_SESSION['pwd-not-match']))
{
echo $_SESSION['pwd-not-match'];
unset($_SESSION['pwd-not-match']);
}
if(isset($_SESSION['change-pwd']))
{
echo $_SESSION['change-pwd'];
unset($_SESSION['change-pwd']);
}
?>
<br><br><br>
<!-- Button to add Admin -->
Add Admin
<br/><br/><br/>
<table class="tbl-full">
<tr>
<th>S.N.</th>
<th>Full Name</th>
<th>Username</th>
<th>Actions</th>
</tr>
<?php
//Fügt alle Admins zum Table hinzu
$sql = "SELECT * FROM tbl_admin";
//Führt die Query aus
$res = mysqli_query($conn, $sql);
//Schaut nach ob die Query ausgeführt wurde oder nicht
if($res==TRUE)
{
// Zählt die Zeilen in der Datenbank um zu schauen ob wir Daten in der Bank haben oder nicht
$count = mysqli_num_rows($res); // Holt sich alle Zeilen der Datenbank
$sn=1; //Created die Variablen und Weisst die den Admins zu
//Checkt die Anzahl von Zeilen
if($count>0)
{
//Haben Daten in Datenbank
while($rows=mysqli_fetch_assoc($res))
{
//Benutzen While loop um die Daten in der Datenbank zu bekommen
//Der Loop lauft so lang wir Daten haben
//Individuelle Daten ziehen
$id=$rows['id'];
$full_name=$rows['full_name'];
$username=$rows['username'];
//Values im Table anzeigen
?>
<tr>
<td><?php echo $sn++; ?></td>
<td><?php echo $full_name; ?></td>
<td><?php echo $username; ?></td>
<td>
Change Password
<a href="<?php echo SITEURL; ?>admin/update-admin.php?id=<?php echo $id; ?>" class="btn-secondary" >Update Admin</a>
<a href="<?php echo SITEURL; ?>admin/delete-admin.php?id=<?php echo $id; ?>" class="btn-danger" >Delete Admin</a>
</td>
</tr>
<?php
}
}
else
{
//Wir haben keine Daten in der Datenbank
}
}
?>
</table>
<!-- Button Ende -->
</div>

Has been fixed, used this code for Sorting and switching through Menus
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name='kwlist'>
<option value="">--- KW wählen ---</option>
<?php
$sql = "SELECT KW, Year from tbl_food ORDER BY `tbl_food`.`Year`, `tbl_food`.`KW` ASC LIMIT 3;";
$res = mysqli_query($conn, $sql);
$count= mysqli_num_rows($res);
if($count>0)
{
while($row=mysqli_fetch_assoc($res))
{
$KW = $row['KW'];
$year = $row['Year'];
echo '<option value="'.$KW.'/'.$year.'">'.$KW.' / '.$year.'</option>';
}
}
?>
</select>
<input type="submit" name="Submit" value="Auswählen" />
</form>
</body>
</html>
<?php
//Sortiert Menüs nach KW max 1 Woche vor und 1 Woche rückblickend
if(isset($_POST['kwlist']) && $_POST['kwlist'] != "")
{
$kwyear = explode("/", $_POST['kwlist']);
//echo $kwyear[0]; // KW
//echo $kwyear[1]; // Jahr
$sql = "SELECT * FROM tbl_food WHERE `KW` =$kwyear[0] AND `Year` =$kwyear[1]";
}
else
{
$actual_kw = date('W');
$actual_year = date('Y');
$sql = "SELECT * FROM tbl_food WHERE `KW` = $actual_kw AND `Year` = $actual_year";
}

Related

DIV Card misplacing in another row

I am trying to get my div card to stay right next to my other card board and also dont get strached out. The first one works perfectly, but I dont know how to place the next one right next to the first one...
$sql = "SELECT * FROM beitrag WHERE beitrag_id=97 OR beitrag_id=98";
$res = mysqli_query($db, $sql) or die(mysqli_error());
if(mysqli_num_rows($res) > 0) {
$beitrag = "
<div class='pagecontainer'>
<div class='container'>
<div class='jumbotron p- p-md-5 text-white rounded bg-dark'>
<div class='col-md-6 px-0'>
<h1 class='display-4 font-italic'>Student, Azubi, FSJler, Schüler...?</h1>
<p class='lead my-3'>Tauschen Sie sich noch heute mit tausenden Gleichgesinnten über Ihre finanzielle Planung aus!</p>
<p class='lead mb-0'><a href='beitraege.php' class='text-white font-weight-bold'>Ab zu den Beitraegen!</a></p>
</div>
</div>
<div class='row mb-2'>";
while ($row = mysqli_fetch_assoc($res)) {
$beitrag_id = $row['beitrag_id'];
$titel = $row['titel'];
$miete = $row['miete'];
$einkauf = $row['einkauf'];
$freizeit = $row['freizeit'];
$sonstiges = $row['sonstiges'];
$g_kosten = $row['g_kosten'];
$p_text = $row['p_text'];
$beitrag .="
<div class='col-md-6'>
<div class='card' style=''>
<div class='card-body'>
<h4 class='card-title'>$titel</h4>
<p class='card-text'>$p_text</p>
<a href='siehe_beitrag.php?pid=$beitrag_id' class='btn btn-primary'>Mehr erfahren</a>
</div>
</div>
";
$beitrag .="
</div>
</div>
</div>
</div>
";
}
echo $beitrag;
}
Try with this code istead of yours.
$sql = "SELECT * FROM beitrag WHERE beitrag_id=97 OR beitrag_id=98";
$res = mysqli_query($db, $sql) or die(mysqli_error());
if(mysqli_num_rows($res) > 0) {
$beitrag = "
<div class='pagecontainer'>
<div class='container'>
<div class='jumbotron p- p-md-5 text-white rounded bg-dark'>
<div class='col-md-6 px-0'>
<h1 class='display-4 font-italic'>Student, Azubi, FSJler, Schüler...?</h1>
<p class='lead my-3'>Tauschen Sie sich noch heute mit tausenden Gleichgesinnten über Ihre finanzielle Planung aus!</p>
<p class='lead mb-0'><a href='beitraege.php' class='text-white font-weight-bold'>Ab zu den Beitraegen!</a></p>
</div>
</div>
<div class='row mb-2'>";
while ($row = mysqli_fetch_assoc($res)) {
$beitrag_id = $row['beitrag_id'];
$titel = $row['titel'];
$miete = $row['miete'];
$einkauf = $row['einkauf'];
$freizeit = $row['freizeit'];
$sonstiges = $row['sonstiges'];
$g_kosten = $row['g_kosten'];
$p_text = $row['p_text'];
$beitrag .="
<div class='col-md-6'>
<div class='card' style=''>
<div class='card-body'>
<h4 class='card-title'>$titel</h4>
<p class='card-text'>$p_text</p>
<a href='siehe_beitrag.php?pid=$beitrag_id' class='btn btn-primary'>Mehr erfahren</a>
</div>
</div>
</div>
";
//you fault here. You should have the loop on this card only.
}
//you fault here. one </div> goes down bottom.
$beitrag .="
</div>
</div>
</div>
";
echo $beitrag;
}

PHP code not working correctly. Everything works except the qty of the first product in cart

Like written in the title my code works okay except for the quantity for the first product when there are multiple products, where you can remove by clicking update but cant change the quantity.
<?php
include("functions/functions.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.12/css/all.css" integrity="sha384-G0fIWCsCzJIMAVNQPfjH08cyYaUtMwjJwqiRKxxE/rx96Uroj1BtIQ6MLJuheaO9" crossorigin="anonymous">
<link type="text/css" rel="stylesheet" href="styles/style.css" />
<title>Prodavnica+</title>
</head>
<body>
<div id="header" class="cf">
<img src="images/logo.png" />
<div id="navbar">
<ul>
<li>
Home
</li>
<li>
Products
</li>
<li>
My Account
</li>
<li>
Sign Up
</li>
<li>
Shopping Card
</li>
<li>
Contact Us
</li>
</ul>
</div> <!-- END navbar -->
<div id="search">
<form method="get" action="results.php" enctype="multipart/form-data">
<input type="text" name="search_query" placeholder="Search Product" />
<input type="submit" name="search_button" value="Search" />
</form>
</div>
</div> <!-- END header -->
<?php cart(); ?>
<div id="shop-bar">
<p>
Total items: <?php totalItems() ?>
</p>
<p>
Total price: <?php totalPrice()?>
</p>
<i class="fas fa-shopping-cart"> | </i>
<span> Welcome Guest! </span>
</div> <!-- END shop-bar -->
<div id="container">
<div id="main">
<div id="product-box-cart">
<form action="" method="post" enctype="multipart/form-data">
<table>
<thead>
<th>Product</th>
<th>Quantity</th>
<th>Remove</th>
<th>Price</th>
</thead>
<tbody>
<?php
$total = 0;
global $con;
$ip = getIp();
$run_price = mysqli_query($con,"SELECT * FROM cart WHERE ip_add = '$ip'");
while($row_pro_price = mysqli_fetch_array($run_price)) {
$pro_id = $row_pro_price['p_id'];
$pro_qty = $row_pro_price['qty'];
$run_pro_price2 = mysqli_query($con,"SELECT * FROM products WHERE product_id = '$pro_id'") or die(mysqli_error($con));
while($row_pro_price2 = mysqli_fetch_array($run_pro_price2)) {
$pro_price = array($row_pro_price2['product_price']);
$pro_title = $row_pro_price2['product_title'];
$product_image = $row_pro_price2['product_image'];
$single_price = $row_pro_price2['product_price'];
$pro_price_values = array_sum($pro_price);
$total += $pro_price_values;
?>
<tr>
<td>
<h2><?php echo $pro_title ?></h2>
<img src="admin/product_images/<?php echo $product_image;?>">
</td>
<td>
<input type="text" name="qty[]" value = "<?php echo $pro_qty;?>">
</td>
<td>
<input type="checkbox" name="remove[]" value="<?php echo $pro_id ?>" />
</td>
<td>
<?php echo "$" . $single_price; ?>
</td>
</tr>
</tbody>
<?php }} ?>
</table>
<p>
<b> Total Value: </b> <?php echo "$" . $total;?>
</p>
<div id="check-buttons">
<input type="submit" name="update_cart" value="Update Cart" />
<input type="submit" name="continue" value="Continue Shopping" />
<input type="button" value="Checkout" />
</div>
</form>
<?php
//pressing update cart ->
if(isset($_POST['update_cart'])) {
//removing the products
if(isset($_POST['remove'])) {
foreach($_POST['remove'] as $remove_id) {
$delete_product = "DELETE FROM cart WHERE p_id = '$remove_id' AND ip_add = '$ip'";
$run_delete = mysqli_query($con,$delete_product);
if($run_delete) {
echo "<script>window.open('cart.php','_self')</script>";
}
}
}
//quantity of product
if(isset($_POST['qty'])) {
foreach($_POST['qty'] as $qty_id) {
$update_qty = "UPDATE cart SET qty = '$qty_id' WHERE p_id = '$pro_id' AND ip_add = '$ip'";
$run_qty = mysqli_query($con, $update_qty) or die(mysqli_error($con));
}
}
}
elseif(isset($_POST['continue'])) {
echo "<script>window.open('index.php','_self')</script>";
}
?>
</div> <!-- END product box -->
</div> <!-- END main -->
<div id="side">
<div id="side-category">
<h2>Categories</h2>
<hr />
<table id="mw">
<tr>
<?php
getBrands();
?>
</tr>
</table>
<ul>
<?php
getCats();
?>
</ul>
</div><!-- END side-category-->
</div> <!-- END side -->
</div> <!--END container -->
<div id="footer">
<p>© 2018 by </p>
</div> <!-- END footer -->
</body>
</html>
This is my cart.php page where the code executes. The $pro_id variable is from GET method for adding to cart. Therefore the $pro_id = $_GET[add_cart] which is in index.php. I will also include the index.php file bellow so if needed, one can take a look.
Quick answer would mean a lot. Thanks
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.12/css/all.css" integrity="sha384-G0fIWCsCzJIMAVNQPfjH08cyYaUtMwjJwqiRKxxE/rx96Uroj1BtIQ6MLJuheaO9" crossorigin="anonymous">
<link type="text/css" rel="stylesheet" href="styles/style.css" />
<title>Prodavnica+</title>
</head>
<body>
<div id="header" class="cf">
<img src="images/logo.png" />
<div id="navbar">
<ul>
<li>
Home
</li>
<li>
Products
</li>
<li>
My Account
</li>
<li>
Sign Up
</li>
<li>
Shopping Card
</li>
<li>
Contact Us
</li>
</ul>
</div> <!-- END navbar -->
<div id="search">
<form method="get" action="results.php" enctype="multipart/form-data">
<input type="text" name="search_query" placeholder="Search Product" />
<input type="submit" name="search_button" value="Search" />
</form>
</div>
</div> <!-- END header -->
<?php cart(); ?>
<div id="shop-bar">
<p>
Total items: <?php totalItems() ?>
</p>
<p>
Total price: <?php totalPrice()?>
</p>
<i class="fas fa-shopping-cart"> | </i>
<span> Welcome Guest! </span>
</div> <!-- END shop-bar -->
<div id="container">
<div id="main">
<div id="product-box">
<?php
if(isset($_GET['cat'])) {
getCatPro();
}
elseif(isset($_GET['brand'])) {
getBrandPro();
}
else {
getPro();
}
?>
</div> <!-- END product box -->
</div> <!-- END main -->
<div id="side">
<div id="side-category">
<h2>Categories</h2>
<hr />
<table id="mw">
<tr>
<?php
getBrands();
?>
</tr>
</table>
<ul>
<?php
getCats();
?>
</ul>
</div><!-- END side-category-->
</div> <!-- END side -->
</div> <!--END container -->
<div id="footer">
<p>© 2018 by Djordje Stamenkovic</p>
</div> <!-- END footer -->
</body>
</html>
functions.php
<?php
//database connection
$con = mysqli_connect("localhost","root","","eprodavnica");
// function for getting ip address of the client
/*In this PHP function, first attempt is to get the direct IP address of client’s machine, if not available then try for forwarded for IP address using HTTP_X_FORWARDED_FOR. And if this is also not available, then finally get the IP address using REMOTE_ADDR.*/
function getIp()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
//adding a single product to cart
function cart() {
if(isset($_GET['add_cart'])) {
global $con;
$ip = getIp();
$pro_id = $_GET['add_cart'];
$run_check = mysqli_query($con,"SELECT * FROM cart WHERE ip_add = '$ip' AND p_id = '$pro_id'") or die(mysqli_error($con));
if(mysqli_num_rows($run_check)>0) {
echo "";
} else {
$run_pro = mysqli_query($con, "INSERT INTO cart (p_id, ip_add, qty) values ('$pro_id','$ip',1)") or die(mysqli_error($con));
echo "<script>window.open('index.php','_self')</script>";
}
}
}
//getting the total added items
function totalItems() {
if(isset($_GET['add_cart'])) {
global $con;
$ip = getIp();
$run_items = mysqli_query($con, "SELECT * FROM cart WHERE ip_add='$ip'");
$count_items = mysqli_num_rows($run_items);
} else {
global $con;
$ip = getIp();
$run_items = mysqli_query($con, "SELECT * FROM cart WHERE ip_add='$ip'") or die(mysqli_error($con));
$count_items = mysqli_num_rows($run_items);
}
echo $count_items;
}
//getting the total Price of items in the cart
function totalPrice() {
$total = 0;
global $con;
$ip = getIp();
$run_price = mysqli_query($con,"SELECT * FROM cart WHERE ip_add = '$ip'");
while($row_pro_price = mysqli_fetch_array($run_price)) {
$pro_id = $row_pro_price['p_id'];
$run_pro_price2 = mysqli_query($con,"SELECT * FROM products WHERE product_id = '$pro_id'");
while($row_pro_price2 = mysqli_fetch_array($run_pro_price2)) {
$pro_price = array($row_pro_price2['product_price']);
$pro_price_values = array_sum($pro_price);
$total += $pro_price_values;
}
}
echo "$" . $total;
}
//getting the categories
function getCats() {
global $con;
$run_cats = mysqli_query($con,"SELECT * FROM categories");
while($row_cats = mysqli_fetch_array($run_cats)) {
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_title'];
echo "<li>
<a href='index.php?cat=$cat_id'>$cat_title</a>
</li>";
}
}
//getting the brands
function getBrands() {
global $con;
$run_brands = mysqli_query($con,"SELECT * FROM brands");
while($row_brands = mysqli_fetch_array($run_brands)) {
$brand_id = $row_brands['brand_id'];
$brand_title = $row_brands['brand_title'];
echo "<td>
<a href='index.php?brand=$brand_id'>$brand_title</a>
</td>";
}
}
//getting products on main page
function getPro() {
if(!isset($_GET['cat'])){
if(!isset($_GET['brands'])) {
global $con;
$run_pro = mysqli_query($con,"SELECT * FROM products ORDER BY RAND() LIMIT 0,9");
while($row_pro = mysqli_fetch_array($run_pro)) {
$pro_id = $row_pro['product_id'];
$pro_cat = $row_pro['product_cat'];
$pro_brand = $row_pro['product_brand'];
$pro_title = $row_pro['product_title'];
$pro_price = $row_pro['product_price'];
$pro_image = $row_pro['product_image'];
echo "
<div class='single-product cf'>
<h4><a href='#'>$pro_title</a></h4>
<a href='details.php?pro_id=$pro_id'><img src='admin/product_images/$pro_image' /></a>
<p>
Price: $ $pro_price
</p>
<a href='index.php?add_cart=$pro_id'><button>Add To Cart</button></a>
</div>
";
}
}
}
}
//getting the category products
function getCatPro() {
if(isset($_GET['cat'])){
$cat_id = $_GET['cat'];
global $con;
$run_cat_pro = mysqli_query($con,"SELECT * FROM products WHERE product_cat = '$cat_id'");
$count_cats = mysqli_num_rows($run_cat_pro);
if($count_cats == 0) {
echo "<div class='no-cat'>
<h1> We're sorry! There are currently no products with that category. :(</h1>
</div>";
} else {
while($row_cat_pro = mysqli_fetch_array($run_cat_pro)) {
$pro_id = $row_cat_pro['product_id'];
$pro_cat = $row_cat_pro['product_cat'];
$pro_brand = $row_cat_pro['product_brand'];
$pro_title = $row_cat_pro['product_title'];
$pro_price = $row_cat_pro['product_price'];
$pro_image = $row_cat_pro['product_image'];
echo "
<div class='single-product cf'>
<h4><a href='#'>$pro_title</a></h4>
<a href='details.php?pro_id=$pro_id'><img src='admin/product_images/$pro_image' /></a>
<p>
Price: $ $pro_price
</p>
<a href='index.php?add_cart=$pro_id'><button>Add To Cart</button></a>
</div>
";
}
}
}
}
//getting the brand products (in this case sex)
function getBrandPro() {
if(isset($_GET['brand'])){
$brand_id = $_GET['brand'];
global $con;
$run_brand_pro = mysqli_query($con,"SELECT * FROM products WHERE product_brand = '$brand_id'") or die(mysqli_error($con));
$count_brands = mysqli_num_rows($run_brand_pro);
if($count_brands == 0) {
echo "<div class='no-cat'>
<h1> We're sorry! There are currently no products for that sex. :(</h1>
</div>";
} else {
while($row_brand_pro = mysqli_fetch_array($run_brand_pro)) {
$pro_id = $row_brand_pro['product_id'];
$pro_cat = $row_brand_pro['product_cat'];
$pro_brand = $row_brand_pro['product_brand'];
$pro_title = $row_brand_pro['product_title'];
$pro_price = $row_brand_pro['product_price'];
$pro_image = $row_brand_pro['product_image'];
echo "
<div class='single-product cf'>
<h4><a href='#'>$pro_title</a></h4>
<a href='details.php?pro_id=$pro_id'><img src='admin/product_images/$pro_image' /></a>
<p>
Price: $ $pro_price
</p>
<a href='index.php?add_cart=$pro_id'><button>Add To Cart</button></a>
</div>
";
}
}
}
}
NOTE: It works fine when one product is in the cart.

undefined variable when i use mysqli

I am new in mysqli. I was using mysql and converting it in mysqli. When the data in my database are empty the error showed up and said " Undefined variable: candidate_name_5" but in mysql when the data in mydatabase is empty it still works. I dont know whats the problem. Its working fine in mysql but many errors in mysqli. Please help me what is the problem
<?php
require('connection.php');
$connect = mysqli_connect('localhost','root','','smart_id_user');
// retrieving candidate(s) results based on position
if (isset($_POST['monthReport'])){
/*
$resulta = mysql_query("SELECT * FROM tbCandidates where candidate_name='Luis Nani'");
while($row1 = mysql_fetch_array($resulta))
{
$candidate_1=$row1['candidate_cvotes'];
}
*/
$monthReport = $_POST['monthReport'];
$results = mysqli_query($connect,"SELECT * FROM voting_tbCandidates where candidate_position='$monthReport'");
$row1 = mysqli_fetch_array($results); // for the first candidate
$row2 = mysqli_fetch_array($results); // for the second candidate
$row3 = mysqli_fetch_array($results);
$row4 = mysqli_fetch_array($results);
$row5 = mysqli_fetch_array($results);
$row6 = mysqli_fetch_array($results);
$row7 = mysqli_fetch_array($results);
$row8 = mysqli_fetch_array($results);
$row9 = mysqli_fetch_array($results);
$row10 = mysqli_fetch_array($results);
$row11 = mysqli_fetch_array($results);
$row12 = mysqli_fetch_array($results);
if ($row1){
$candidate_name_1=$row1['candidate_name']; // first candidate name
$candidate_1=$row1['candidate_cvotes']; // first candidate votes
}
if ($row2){
$candidate_name_2=$row2['candidate_name']; // second candidate name
$candidate_2=$row2['candidate_cvotes']; // second candidate votes
}
if ($row3){
$candidate_name_3=$row3['candidate_name'];
$candidate_3=$row3['candidates_cvotes'];
}
if($row4){
$candidate_name_4=$row4['candidate_name'];
$candidate_4=$row4['candidate_cvotes'];
}
if($row5){
$candidate_name_5=$row5['candidate_name'];
$candidate_5=$row5['candidate_cvotes'];
}
if($row6){
$candidate_name_6=$row6['candidate_name'];
$candidate_6=$row6['candidate_cvotes'];
}
if($row7){
$candidate_name_7=$row7['candidate_name'];
$candidate_7=$row7['candidate_cvotes'];
}
if($row8){
$candidate_name_8=$row8['candidate_name'];
$candidate_8=$row8['candidate_cvotes'];
}
if($row9){
$candidate_name_9=$row9['candidate_name'];
$candidate_9=$row9['candidate_cvotes'];
}
}
else
// do nothing
?>
<?php
// retrieving positions sql query
$positions=mysqli_query($connect,"SELECT * FROM voting_tbPositions")
or die("There are no records to display\n" . mysqli_error());
?>
<?php
session_start();
//If your session isn't valid, it returns you to the login screen for
protection//
if(empty($_SESSION['user_id'])){
}
?>
<?php if(isset($_POST['monthReport']))
{$totalvotes=$candidate_1+$candidate_2+$candidate_3+$candidate_4;} ?>
<html><head>
<div id="mySidenav" class="sidenav">
×
HOME
<a><hr></a>
VOTE
<a><hr></a>
POLL RESULTS
<a><hr></a>
LOGOUT
</div>
<div id="main">
<span style="font-size:30px;cursor:pointer" onclick="openNav()">☰
</span>
<script language="JavaScript" src="js/admin.js">
</script>
</head><body bgcolor="tan">
<center>
<font color = "white" size="7"> <div id="hihi">ONLINE VOTING SYSTEM</div></font>
<br><br>
<br>
<br></center>
<div id="page">
<div id="header">
<h1>POLL RESULTS </h1>
</div>
<div id="container">
<table width="420" align="center">
<form method="POST" action="refresh.php">
<tr>
<td>Choose Position</td>
<td><SELECT name="monthReport" id="monthReport" onchange="form.submit()">
<OPTION VALUE="select">select
< ?php
//loop through all table rows
while ($row=mysqli_fetch_array($positions)){
echo "<OPTION VALUE=$row[position_name]>$row[position_name]";
}
?>
</SELECT></td>
<script type="text/javascript">
document.getElementById('month').value = "<?php echo $_POST['monthReport'];?>";
</script>
<?php
echo '<div id="gitna">';
echo $monthReport;
echo '</div>';
?>
<?php if(isset($_POST['monthReport'])){echo $candidate_name_1;} ?>:<br>
<img src="images/candidate-1.gif"
width='<?php if(isset($_POST['monthReport'])){ if ($candidate_2 ||
$candidate_1 || $candidate_3 || $candidate_4 || $candidate_5!= 0){echo(100*round($candidate_1/($candidate_2+$candidate_1+$candidate_3+$candidate_4+$candidate_5),5));}} ?>'
height='20'>
<?php if(isset($_POST['monthReport'])){ if ($candidate_2 ||
$candidate_1 || $candidate_3 ||$candidate_4 || $candidate_5!= 0)
{echo(100*round($candidate_1/($candidate_2+$candidate_1+$candidate_3+$candidate_4+$candidate_5),5));}} ?>% of <?php if(isset($_POST['monthReport'])){echo $totalvotes;} ?> total votes
<br> votes <?php if(isset($_POST['monthReport'])){ echo $candidate_1;} ?>
<br>
<br>
<?php if(isset($_POST['monthReport'])){ echo $candidate_name_2;} ?>:<br>
<img src="images/candidate-2.gif"
width='<?php if(isset($_POST['monthReport'])){ if ($candidate_2 || $candidate_1 || $candidate_3 || $candidate_4 || $candidate_5!= 0){echo(100*round($candidate_2/($candidate_2+$candidate_1+$candidate_3+$candidate_4+$candidate_5),5));}} ?>'
height='20'>
<?php if(isset($_POST['monthReport'])){if ($candidate_5 || $candidate_4 || $candidate_3 || $candidate_2 || $candidate_1!=0){echo(100*round($candidate_5/($candidate_5+$candidate_4+$candidate_3+$candidate_2+$candidate_1),5));}} ?>% of <?php if(isset($_POST['monthReport'])){echo $totalvotes;} ?> total votes
<br>votes <?php if(isset($_POST['monthReport'])){echo $candidate_5;} ?>
<br>
<br>
<br>
</div>
</body></html>
i just add
error_reporting(1);
and everything works fine now

PHP - Foreach via Certain active Sessions

I'm currently working on a super simple Online shop idea with a shopping cart. At the end i want to print out a table with the products you ordered. Currently using a foreach but i have no idea how to solve this. I tried to use sessions as a way to give the loop an idea how many different products are ordered. But it seems like the $_SESSION way will take all the current active sessions. And using a simple variable f.e. $piano will make it print 2 rows ( using 2 piano's in my shop, tried to solve it with a if (session active) $piano1 = active. But it seems the foreach statement doesn't give a whoop about that and will print 2 rows anyways.
Sorry for the long block of text. Here's my page. Again apologies. I just started php.
Variable names are dutch but that shouldn't really matter for you guys i think.
Starting from line 103.
Thanks in advance!
<div class="logincontainer"><!-- Php Session Script Actief? -->
<?php
session_start();
if(isset($_SESSION['naam'])) :
echo "<div class='content_login'>";
echo "Hallo " . $_SESSION['naam'] . ". Welkom bij de Pianoshop.</br></br>";?>
<form method='post' action='uitlog.php'>
<input type='submit' name='loguit' Value='Loguit!'></form><br />
<form action='winkelmand.php' class="left">
<input type='image' src='images/winkelwagen-knop.png'/>
</form><br />
<form method='post' name='emptycart' action='emptycart.php' class="right">
<input type="submit" id="submitpic" name="leegwinkelmand" value="">
<?php
if(isset($_SESSION['winkelmand'])) {
echo $_SESSION['aantalproducten'] . " Item(s) - €" . $_SESSION['totaalprijs'] . ",-";
} else {
echo "Jouw winkelwagen is leeg.";
}?>
</form>
</div>
<?php else :?>
<div class='content_login'>
<form method='post' action='checklogin.php'>
<p><input type='text' name='gebruikersnaam' required='required' value='' placeholder='Gebruikersnaam'></p>
<p><input type='password' name='password' required='required' value='' placeholder='Wachtwoord'></p>
<font color="red"><p class='submit'>
<input type='submit' name='login' value='Login'>
<?php if(isset($_SESSION['logged_in'])) :?>
Verkeerd wachtwoord.
<?php session_destroy();
endif; ?>
</p></font>
<p>Nog niet geregistreerd? Doe dat hier!.</p>
</form>
</div>
<?php endif; ?></div>
<div id="site">
<div id="menubar">
<div id="logo">
<img src="images/pianotoetsen.png" >
</div>
<div id="menu_items">
<ul id="menu">
<li>Home</li>
<li>Toetsinstrumenten</li>
<li>Jouw account</li>
<li class="current">Winkelmand</li>
<li>Contact</li>
</ul>
</div></div>
<div id="site_content">
<div class="sidebar_container">
<div class="sidebar">
<h2>Sale!</h2>
<div id="thumbnail"><img src="images/piano1.jpg"></div>
<p>Yamaha CLP-575 voor maar €2599,- !</p>
<div id="thumbnail"><img src="images/piano2.jpg"></div>
<p>Ritmuller 120SL €4999,- !</p>
</div>
<div class="sidebar">
<h2>Laatste Updates</h2>
<h3>Juni 2015</h3>
<p>Site in constructie.</p>
</div>
<div class="sidebar">
<h3>Wij zijn op Facebook</h3>
<p>Klik hier.</p>
</div>
</div>
<div id="content">
<div id="wallpaperbanner">
<img src="images/banner.jpg">
</div>
<div class="content_item">
<h1>Winkelmand</h1>
<?php
$user = 'root';
$pass = '';
$db = 'online shop';
$conn = mysql_connect('localhost', $user, $pass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
if(isset($_SESSION['winkelmand'])) {
echo "Deze producten staan in je winkelwagen</br></br>";
if(isset($_SESSION['totaalprijs2']) == 0) {
// Do nothing?
} else {
mysql_select_db($db);
$query = mysql_query("SELECT * FROM product WHERE productnummer='2'");
$productgegevens = mysql_fetch_row($query);
$piano["piano2"] = "ritmuller";
$pianoarray[1] = $productgegevens['1'];
$pianoarray[2] = $productgegevens['2'];
$pianoarray[3] = $productgegevens['4'];
$pianoarray[5] = $productgegevens['3'];
$pianoarray[4] = $_SESSION['aantal_prod2'];
}
if(isset($_SESSION['totaalprijs1']))
{
mysql_select_db($db);
$query = mysql_query("SELECT * FROM product WHERE productnummer='1'");
$productgegevens = mysql_fetch_row($query);
$piano["piano1"] = "yamaha";
$pianoarray[4] = $_SESSION['aantal_prod1'];
$pianoarray[1] = $productgegevens['1'];
$pianoarray[2] = $productgegevens['2'];
$pianoarray[3] = $productgegevens['4'];
$pianoarray[5] = $productgegevens['3'];
$pianoarray[4] = $_SESSION['aantal_prod2'];
}
echo "<br />
<table width='80%' >
<thead>
<tr><th>Productnaam</th><th>Merk</th>
<th>Voorraad</th><th>Aantal</th><th>Prijs</th>
</tr>
</thead>
<tbody>";
foreach($piano as $key => $value) {
echo $key . "</br>" . $value . "<br />";
$row = "<tr>";
for ($x=1; $x<=sizeof($pianoarray); $x++){
$row = $row . "<td>" . $pianoarray[$x] . "</td>";
}
$row = $row . "</tr>";
echo $row;
}
echo "<tr><td></td><td></td><td></td><td></td><td>" . '€' . $_SESSION['totaalprijs'] . ',-' . "</td></tr></tbody></table>";
}
else {
echo "Jouw winkelwagen is leeg. <br />" . "Klik <a href='toetsinstrumenten.php'>Hier</a> om wat items toe te voegen.";
}?>
</div>
</div>
</div>
</div>
Create an array variable in the $_SESSION array and do a foreach loop on that
$_SESSION['cart']['piano1'] = 'piano1';
$_SESSION['cart']['piano2'] = 'piano2';
$cart = $_SESSION['cart'];
foreach ($cart as $key => $item) {
//do something with $item or $key
}

Why data will post on the same "window"

I am trying to want to post data on many "windows", not just one.
To help explain what I am doing, here are some examples.
Screenshot 1
Next, I want to post news on many windows like this:
Screenshot 2
HTML:
<div id="news">
<div id="newsWindow">
<div id="newsTitle">
<?php
session_start();
$uchwyt=mysql_connect('=====','=====','=====')
or die('Nieudane polaczenie z baza danych...');
mysql_select_db('a6039035_news')
or die('Nie udalo sie wybrac bazy danych...');
$link=mysql_query("SELECT * FROM news ORDER BY id desc LIMIT 4");
while($wiersz=mysql_fetch_array($link))
{
echo $wiersz['tytul'];
echo "<br>\n";
}
mysql_close($uchwyt);
?>
</div>
<div id="newsInfo">
<div class="time">
<?php
session_start();
$uchwyt=mysql_connect('=====','=====','=====')
or die('Nieudane polaczenie z baza danych...');
mysql_select_db('a6039035_news')
or die('Nie udalo sie wybrac bazy danych...');
$link=mysql_query("SELECT * FROM news ORDER BY id desc LIMIT 4");
while($wiersz=mysql_fetch_array($link))
{
echo $wiersz['godzina'];
}
mysql_close($uchwyt);
?>
</div>
<div class="date">
<?php
session_start();
$uchwyt=mysql_connect('=====','=====','=====')
or die('Nieudane polaczenie z baza danych...');
mysql_select_db('a6039035_news')
or die('Nie udalo sie wybrac bazy danych...');
$link=mysql_query("SELECT * FROM news ORDER BY id desc LIMIT 4");
while($wiersz=mysql_fetch_array($link))
{
echo $wiersz['data'];
}
mysql_close($uchwyt);
?>
</div>
</div>
<div id="newsContent">
<p>
<?php
session_start();
$uchwyt=mysql_connect('=====','=====','====')
or die('Nieudane polaczenie z baza danych...');
mysql_select_db('a6039035_news')
or die('Nie udalo sie wybrac bazy danych...');
$link=mysql_query("SELECT * FROM news ORDER BY id desc LIMIT 4");
while($wiersz=mysql_fetch_array($link))
{
echo $wiersz['tresc'];
}
mysql_close($uchwyt);
?>
</p>
<div id="newsMore">
Czytaj więcej...
</div>
</div>
You are doing a query and loop in each news <div>. You need to do the query before, and then loop over each news <div> and returned data
<?php
$uchwyt=mysql_connect('=====','=====','=====')
or die('Nieudane polaczenie z baza danych...');
mysql_select_db('a6039035_news')
or die('Nie udalo sie wybrac bazy danych...');
$link=mysql_query("SELECT * FROM news ORDER BY id desc LIMIT 4");
?>
<div id="news">
<?php
while($wiersz=mysql_fetch_array($link))
{
?>
<div class="newsWindow">
<div class="newsTitle">
<?php
echo $wiersz['tytul'];
echo "<br>\n";
?>
</div>
<div class="newsInfo">
<div class="time">
<?php
echo $wiersz['godzina'];
?>
</div>
<div class="date">
<?php
echo $wiersz['data'];
?>
</div>
</div>
<div id="newsContent">
<p>
<?php
echo $wiersz['tresc'];
?>
</p>
<div id="newsMore">
Czytaj więcej...
</div>
</div>
<?php
}
?>

Categories