PHP redirect loop issue - php

When I submit my category search form, I want it to run the php code in the DisplayMealsCont.php and then go back to the main.php with the updated session variables. But when I try to put in a Header("Location: ../View/Main.php"); in the DisplayMealsCont.php it gets stuck in a redirect loop.
main.php
<?php
require_once("../Controller/DisplayMealsCont.php");
session_start();
$category = "All";
if(isset($_SESSION['table'])){
$table = $_SESSION['table'];
}
if(isset($_SESSION['Category'])){
$category = $_SESSION['Category'];
if($category == "All"){
$category = " ";
}
}
?>
<?php
if (is_array($table)) {
foreach($table as $item) {
?>
<td align="centre"> <?= $item['Meal_Name'] ?> </td>
<td align="centre"> <?= $item['Description'] ?> </td>
<td align="centre"> <?= $item['Image'] ?> </td>
<td align="centre"> <?= $item['Category'] ?> </td>
<td align="centre"> <?= $item['Quantity'] ?> </td>
<td align="centre"> £ <?= $item['Price'] ?> </td>
<tr>
<?php
}
}
?>
</table>
<form method="post" action="../Controller/DisplayMealsCont.php">
Category: <br />
<select name="Category">
<option value="All">All</option>
<option value="Main">Main</option>
<option value="Drink">Drinks</option>
</select> <br />
<input type="submit" name="categorysubmit" value="Search" />
</form>
DisplayMealModel.php
<?php
require_once("../Model/config.php");
class DisplayMeals
{
public $dbConnection;
public function __construct(){
$db= new database();
$connect=$db->getdatabase();
$this->dbConnection = $connect;
}
public function MainMeals($category){
$_SESSION['test'] = $category;
$stmt = $this->dbConnection->prepare("SELECT * from meal WHERE Category LIKE '$category%'");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>
DisplayMealCont.php
<?php
require("../Model/DisplayMealModel.php");
$newMeal = new DisplayMeals();
if(!isset($category)){
$category = "main";
}
if(isset($_SESSION['Category'])){
$category = $_SESSION['Category'];
}
$table = $newMeal-> MainMeals($category);
$_SESSION['table'] = $table;
?>

I can't see why a header() in there causes a redirection loop, but have you tried adding a die(); after header()?
It's a good practice, and in case it works it may shed light into what is causing the issue. Other than that, it would help if you posted the code with the looping header in it so we can see.

Related

Display content on the basis of filter value

I have created simple page in php which fetch data from database and display on web page.
index.php
<?php
include_once 'dbop.php';
$objUser = new Users();
$users = $objUser->get_users();
?>
<!DOCTYPE html>
<html lang="en">
.
.
<form action="index.php" method="POST">
<select name="role" id="role">
<option value="Admin"><?php echo 'Admin' ?></option>
<option value="SysAdmin"><?php echo 'SysAdmin' ?></option>
<option value="User"><?php echo 'User' ?></option>
</select>
<table>
<thead>
<tr>
<th>Name</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<?php
for($i = 0; $i < sizeof($users); $i++) {
?>
<tr>
<td><?php echo ($users[$i]["name"]); ?></td>
<td><?php echo ($users[$i]["role"]); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
.
.
</html>
dbop.php
<?php
.
.
public function get_users() {
$sql = "select name, role from users";
$data = $this->dbObj->ExecuteQuery($sql);
$data_all = array();
if ($data != null && mysqli_num_rows($data) > 0) {
while($fetch_data = mysqli_fetch_assoc($data)) {
$arr = array();
$arr["name] = $fetch_data['name'];
$arr["role"] = $fetch_data['role'];
$data_all[] = $arr;
}
}
return $data_all;
}
?>
I want to display the user name and role from database on the basis of selected role (dropdownlist).
like if Admin value selected from dropdownlist then only Admin records need to display on web page.
You can use a different methods, this is a simplified method :
<?php
include_once 'dbop.php';
$selected ="";
if ($_SERVER['REQUEST_METHOD'] === "POST") {
if ( !isset( $_POST['Filter'] ) ) {
$selected = $_POST['role'];
}}
$objUser = new Users();
$users = $objUser->get_users();
?>
<!DOCTYPE html>
<html lang="en">
.
.
<form action="#" method="POST">
<select name="role" id="role">
<option value="Admin"><?php echo 'Admin' ?></option>
<option value="SysAdmin"><?php echo 'SysAdmin' ?></option>
<option value="User"><?php echo 'User' ?></option>
</select>
<input type="submit" name="submit" value="Filter" >
</form>
<table>
<thead>
<tr>
<th>Name</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<?php
for($i = 0; $i < sizeof($users); $i++) {
if ($selected !=""){
if (($users[$i]["role"])== $selected) {
?>
<tr>
<td><?php echo ($users[$i]["name"]); ?></td>
<td><?php echo ($users[$i]["role"]); ?></td>
</tr>
<?php } } }?>
</tbody>
</table>
.
.
</html>
Hope this helps.

Foreach loop are displaying same rows multiple times

I have a problem with my first attempt at making a foreach loop.
My problem is, that I'm only trying to call out two rows to be displayed, which kinda works, although they are being displayed as many times as there are different rows in my table.
My code looks like this:
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$assoc_query = mysql_fetch_assoc($result);
<?php foreach ($assoc_query as $value) { ?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
It's being displayed like so on the page:
/picture/ DAK
/picture/ DAK
/picture/ DAK
/picture/ DAK
Hope you can help me:)
<?php
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$counter=0;
while($row= mysql_fetch_assoc($result))
{
$assoc_query[$counter] = $row;
$counter++;
}
foreach ($assoc_query as $value) { ?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $value['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $value['name']; ?> </div>
</td>
</tr>
<?php } ?>
Instead of foreach you will want a while. What you are doing here is looping the elements of array $assoc_query.
A while loop will get the next result set to be used.
while($assoc_query = mysql_fetch_assoc($result)){
//Do your thing
}
Also please consider updating from mysql_ to mysqli_ or PDO. What you are using is depreciated and there is added security with the newer ones.
this can't be done with foreach loop unless you gonna show 1 user's information only using limit Clause or Where Clause otherwise that it's useless
instead you need a while loop like this
<?php while ($assoc_query = mysql_fetch_assoc($result)) { ?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
You should do it like this:
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
while ($assoc_query = mysql_fetch_assoc($result)) {
?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
That should work. But as said before you shouldn't code like this. Use mysqli or PDO like nerdlyist says.
#Nerdlyist
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$assoc_query = mysql_fetch_assoc($result);
<?php while ($assoc_query = mysql_fetch_assoc($result)) {
?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>

Display only specific array elements in a foreach loop

I have a page that contains an ordering form, on this form it lists the vendor information and then each of the products for the vendor underneath and in front of the product is an input field that allows the user to input the quantity of each product that they want.
Upon submitting the information goes to a confirmation page where I need to be able to show the order information. On the form on the order page, I have a hidden field that contains the vendor id. and the vendor id is put once for each vendor. What I need to be able to do is not only echo out the quantity but also echo out the vendor id specific for each order. My code is below. The first block is the order page and then the block below that will be the confirm page.
As it stands right now underneath every quantity it displays all the vendor ids as opposed to just the one I need.
<?php defined('C5_EXECUTE') or die("Access Denied.");?>
<div class="ccm-ui">
<?php
$db= Loader::db(); //This loads the database helper.
Loader::model('user'); //This loads the user Model.
$user = new User();
$userInfo = UserInfo::getByID($user->getUserID()); //This gets the user info for the current user.
$userCostCenter = $userInfo->getAttribute('cost_center'); //This sets a variable equal to the attribute Cost Center for the current user.
//The if statement below checks if the user is an admin and then displays the info accordingly.
if ($userCostCenter === "Admin") {
?>
<form name="SelectCostCenter" action="/adminorder" method="POST">
<select name="CostCenter">
<option value="unitedilluminating">United Illumination</option>
<option value="clp">CL&P</option>
</select>
<input type="submit" value="Continue">
<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
<?php
} elseif ($userCostCenter === "United Illuminating") {
?>
<form name="OrderForm" action="/confirm" method="POST">
<?php
$query = 'SELECT * FROM Vendors WHERE costCenterID = 1';
$productQuery = 'SELECT * FROM Products WHERE costCenterID = 1';
$results = $db->getAll($query);
$productResults = $db->getAll($productQuery);?>
<table class="table">
<thead>
<tr>
<th>Quantity/Product</th>
<th>Category</th>
<th>Vendor</th>
<th>Address</th>
</tr>
<?php
foreach ($results as $vendor) {
?>
<tr class="category">
<td></td>
<td><?php echo $vendor['Category']; ?></td>
<td><?php echo $vendor['Vendor']; ?></td>
<td><?php echo $vendor['Address']; ?></td>
</tr>
<?php foreach ($productResults as $product) { ?>
<tr class="product">
<td colspan="4"><span class="name"><input type="text" name="quantities[]" size="1" /><?php echo $product['Product'];?></span></td>
</tr>
<?php } ?>
<td><input type="hidden" name="vendor[]" value="<?php echo $vendor['vendorID']; ?>"/></td>
<?php
}?>
</table>
<input type="submit" value="Checkout"<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
</div><?php
}
else {
?>
<form name="OrderForm" action="/confirm" method="POST">
<?php $query = 'SELECT * FROM Vendors Where costCenterID = 2';
$productquery = 'SELECT * FROM Products WHERE costCenterID = 2';
$results = $db->getAll($query);
$productresults = $db->getAll($productquery);?>
<table class="table">
<thead>
<tr>
<th>Quantity/Product</th>
<th>Category</th>
<th>Vendor</th>
<th>Address</th>
</tr>
<?php
foreach ($results as $vendor) {
?>
<tr class="category">
<td></td>
<td><?php echo $vendor['Category'];?></td>
<td><?php echo $vendor['Vendor'];?> </td>
<td><?php echo $vendor['Address'];?></td>
</tr>
<?php
foreach ($productresults as $product){
?>
<tr class="product">
<td colspan="4"><span class="name"><input type="text" name="quantities[<?php echo $vendor['vendorID']; ?>]" size="1" /><?php echo $product['Product'];?></span></td>
<td><input type="hidden" name="vendor[]" value="<?php echo $vendor['vendorID']; ?>"/></td>
</tr>
<?php
}
?>
<?php
}?>
</table>
<input type="submit" value="Checkout"<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
</div><?php
}
?>
This is the confirm page below.
<?php defined('C5_EXECUTE') or die("Access Denied.");
$db= Loader::db();
$quantity = $_POST['quantities'];
$vendor = $_POST['vendor'];
$minimumorder = 25;
foreach($quantity as $num){
if ($num >= $minimumorder){
echo "$num";
echo "</br>";
foreach($vendor as $vendors){
echo "$vendors";
echo "</br>";
}
}
}
?>
I appreciate any help anyone can give. This has had me stumped for a few days actually.
you might want to rearrange your array, and do something like:
$i = 0;
foreach ($productresults as $product) {
echo '<input name="product['.$i.'][quantity]" />';
echo '<input name="product['.$i.'][vendor_id]" value="'.$vendor['vendorID'].'" type="hidden" />';
++$i;
}
The resulting array in $_POST would have the quantities & their vendor separated into their own arrays.
In your code $vendor['vendorID'] seems the key of your $_POST['quantities'] so in your confirm page you could use:
foreach($quantity as $vendorid=>$num){
if ($num >= $minimumorder){
echo "$num";
echo "</br>";
echo "$vendorid";
}
}

Table won't populate with data

I've been new to programming and I been working with phpmyadmin on localhost. I am making a simple table on a webpage to display data. The problem is that everytime I load the page it only displays the table and not table will load up. Here is my code:
<?php
require('../model/database.php');
require('../model/product_db.php');
$products = get_products();
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'under_construction';
}
// Display the product list
include('view-productList.php');
?>
This is the view-productList.php:
<?php include '../view/header.php'; ?>
<div id="main">
<h1>Product List</h1>
<div id="content">
<!-- display a table of products -->
<h2><?php echo $name; ?></h2>
<table>
<tr>
<th>Code</th>
<th>Name</th>
<th class="right">Version</th>
<th> </th>
</tr>
<?php foreach ($products as $product) : ?>
<tr>
<td><?php echo $product['productCode']; ?></td>
<td><?php echo $product['name']; ?></td>
<td class="right"><?php echo $product['version']; ?></td>
<td><form action="." method="post">
<input type="hidden" name="action"
value="delete_product" />
<input type="hidden" name="product_id"
value="<?php echo $product['productID']; ?>" />
<input type="hidden" name="category_id"
value="<?php echo $product['categoryID']; ?>" />
<input type="submit" value="Delete" />
</form></td>
</tr>
<?php endforeach; ?>
</table>
<p>Add Product</p>
</div>
</div>
<?php include '../view/footer.php'; ?>
Query Page:
<?php
function get_products() {
global $db;
$query = 'SELECT * FROM products
ORDER BY productID';
$products = $db->query($query);
return $products;
}
function get_products_by_category($category_id) {
global $db;
$query = "SELECT * FROM products
WHERE products.categoryID = '$category_id'
ORDER BY productID";
$products = $db->query($query);
return $products;
}
function get_product($product_id) {
global $db;
$query = "SELECT * FROM products
WHERE productID = '$product_id'";
$product = $db->query($query);
$product = $product->fetch();
return $product;
}
function delete_product($product_id) {
global $db;
$query = "DELETE FROM products
WHERE productID = '$product_id'";
$db->exec($query);
}
product_db.php should not be commented out for one - assuming that is the file that holds the "Query Page:" contents.
$products = get_products();
should come immediately after the include.
and your for loop needs the fetch result and not just the product resource:
<?php foreach ($products->fetch() as $product) : ?>
assuming fetch() is relevant to this type of resource since we can't see your db class.

Join 2 tables by merk_id

I'm trying to combine to tables. I got 11 different brands and I got wheels that belong to a brand. You can select a brand by its name with an option selectbox and when submut I want alle the wheels with that brand.
This is the class I'm using:
<?php
include_once "connect.class.php";
class merken extends connect
{
private $merkenlijst;
public function getMerken($database, $id = NULL)
{
$sql = "SELECT * FROM ".$database."_merken";
if(!empty($id))
{
$sql .= " WHERE merk_code=:id";
}
else
{
$sql .= " ORDER BY merk_naam ASC";
}
try
{
$stmt = $this->db->prepare($sql);
if(!empty($id)){ $stmt->bindParam(":id", $id, PDO::PARAM_STR); }
$stmt->execute();
$this->merkenlijst = $stmt->fetchAll(PDO::FETCH_OBJ);
$stmt->closeCursor();
return $this->merkenlijst;
}
catch (Exception $e)
{
die ( $e->getMessage() );
}
}
public function __construct($dbo = NULL)
{
parent::__construct($dbo);
}
}
?>
This is how I echo the info from my database:
<div class="bandwielkolom">
<form action="index.php?lang=nl&p=<?php echo $_GET['p']; ?>#keuze" method="post">
<table class="bandentabel">
<tr>
<th colspan="2">Zoek op merk<a name="wiel"></a></th>
</tr>
<tr>
<td>Merk:</td>
<td>
<select name="wiel_merk">
<option value="0">- Merk -</option>
<?php
$merken = $merkclass->getMerken($website);
foreach($merken as $merk)
{
echo "\t\t\t\t\t\t\t\t\t\t\t<option value=\"".$merk->merk_code."\"";
if(isset($_GET['search']) && $_GET['search'] == "wiel" && isset($_GET['merk']) && $_GET['merk'] == $merk->merk_code || isset($_POST['wiel_submit']) && $_POST['wiel_merk'] == $merk->merk_code) { echo " selected=\"selected\""; }
echo ">".$merk->merk_naam."</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="wiel_submit" value="Zoek" /></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
</form>
</div>
<div class="clearboth"></div>
<br />
<?php
if(isset($_POST['wiel_submit']) && $_POST['wiel_submit'] == "Zoek" || isset($_GET['merk']))
{
$merk = NULL;
if(isset($_POST['wiel_submit']) && $_POST['wiel_submit'] == "Zoek")
{
$merk = $_POST['wiel_merk'];
}
$merken = $merkclass->getMerken($website, $merk);
foreach($merken as $merk)
{
?>
<img src="http://www.etyre.net/preview/bnet/logos/<?php echo str_replace(".png", "_150.png", $merk->merk_logo); ?>" width="150" class="logo" alt="<?php echo $merk->merk_naam; ?>"/>
<div id="merken">
<li><span class="title"><?php echo $merk->wiel_info; ?></span>
<a href="http://www.inter-tyre.nl/inter-tyre/images/w3/<?php echo $merk->wiel_foto; ?>" class="preview" title="Fotonummer: <?php echo $merk->wiel_foto; ?>">
<img src="http://www.inter-tyre.nl/inter-tyre/images/w2/<?php echo $merk->wiel_foto; ?>" alt="Fotonummer: <?php echo $merk->wiel_foto; ?>" class="wheelImg"/>
</a>
<div class="clearboth"></div>
</div>
<?php
}
?>
<?php
}
?>
How can I join this two tables together so than when I select a brand it does show all the wheels that has that brand? And no double brands in my select option?
wielen table
merken table
Can somebody help me out?
Thanks
Your question is a little unclear, and it appears to me that you are too lazy to just go and read the documentation for JOIN.
Anyway, something along the following lines should work:
SELECT w.*,m.* FROM wielen w
LEFT JOIN merken m ON w.merk_id = m.id
WHERE m.id = $your_current_merk;
This gives you a list of all wheels, together with their associated brand information.
If you want to do it the other way around and you don't want to have your brands reoccurring, with your current setup, you would want to do a GROUP_CONCAT, like in this question.

Categories