PHP 1 item conversion to PHP loop - php

So I am trying to learn php, I have a simple 1 item order form, However I have 4 items in the data base I need to show on this order form. In javascript I would just write a loop for an array, however the whole php query form a table from a database has got me flustered and I am not sure how to loop the results to show more then the 1 item from the query and the still allow for ordering all on 1 page. Originaly I had it list a table so you would click on the item and it would redirect to a page for each item so each on can be ordered.
<?php
include_once 'helpers/helper.php';
include_once 'db_function.php';
session_start();
$show_query = "SELECT * FROM `meal_info` ORDER BY meal_time DESC LIMIT 4;";
$show_result = db_query($connect, $show_query);
if($show_result) {
$show_result_row = db_fetch_array($show_result);
}
?>
<?php include_once 'layout/header.php'; ?>
<?php
if(#$_SESSION['order_submit_msg']) {
echo flash_message('order_submit_msg');
unset($_SESSION['order_submit_msg']);
}
?>
<div>
<form action="make_deal.php" method="post">
<div>
<h1><?php echo $show_result_row['meal_name'];?></h1>
</div>
<hr>
<div>
<img class="meal-img" id="meal-img" src="<?php echo $show_result_row['meal_image'];?>" alt="">
</div>
<br>
<div>
<h2>Feature: <?php echo $show_result_row['meal_content'];?></h2>
<h3>Price: <?php echo $show_result_row['meal_price'];?></h3>
</div>
<input type="hidden" name="meal-id" value="<?php echo $show_result_row['meal_id'];?>">
<input type="hidden" name="meal-price" value="<?php echo $show_result_row['meal_price'];?>">
<br>
<div>
<legend>Name</legend>
<input type="text" name="order-username" placeholder="Full Name" required>
</div>
<br>
<div>
<legend>Phone</legend>
<input type="text" name="order-phone" placeholder="555-555-5555" required>
</div>
<br>
<div>
<legend>Address</legend>
<input type="text" name="order-address" placeholder="555 N. Main Street" required>
</div>
<br>
<div>
<legend>Quantity (Dozens)</legend>
<input type="text" name="order-count" placeholder="12" required>
</div>
<hr>
<div>
<button class="btn btn-warning btn-lg btn-block" type="submit">Submit Order</button>
</div>
</form>
</div>
<?php include_once 'layout/footer.php'; ?>

You're trying to display an array. You need to loop through the array.
<?php foreach ($show_result_row as $result) { ?>
<div>
<h1><?php echo $result['meal_name'];?></h1>
</div>
<hr>
<div>
<img class="meal-img" id="meal-img" src="<?php echo $result['meal_image'];?>" alt="">
</div>
<?php } ?>
Something like that.

Related

Bootstrap 4 Image product didn't line up properly

I'm tryin to make a simple online shopping page for my PHP training.
But, I'm kinda stuck in the HTML part where using bootstrap didn't make
something more simpler.
The result of the code makes my product line up vertically, and what I want to with it is to make it line up horizontally.
Code :
<div class="container">
<?php
$connect = mysqli_connect('localhost', 'root', '', 'cart');
$query = 'SELECT * FROM products ORDER by id ASC';
$result = mysqli_query($connect, $query);
if ($result):
if (mysqli_num_rows($result)>0):
while ($product = mysqli_fetch_assoc($result)):
//print_r($product);
?>
<div class="col-sm-4">
<form class="" action="welcome.php?action=add&id=<?php echo $product['id'] ?>" method="post">
<div class="products">
<img src="<?php echo $product['image']; ?>" class="img-fluid text-center" />
<h4 class="text-info"><?php echo $product['name']; ?></h4>
<h4>$ <?php echo $product['price']; ?></h4>
<input type="text" name="quantity" class="form-control" value="1" />
<input type="hidden" name="name" value="<?php echo $product['name'] ?>" />
<input type="hidden" name="price" value="<?php echo $product['price']; ?>" />
<button type="submit" name="add_to_cart" class="btn btn-outline-primary">Add to Cart</button>
</div>
</form>
</div>
<?php
endwhile;
endif;
endif;
?>
For bootstrap grid system to work , the tags must be in following order:
<div class="container">
<div class="row">
<div class="col-md-6">
One of two columns
</div>
<div class="col-md-6">
One of two columns
</div>
</div>
</div>

Why does the value of the hidden input filed gives me the same result alwayes?

First I'm beginner in php, second I'm trying to trace the value of the hidden input which is " the ID of the image in table products ", but whenever I click the delete button of any image, it always gives me the last id of the last image in my products table , and when I changed the input into text it prints the correct id but if I used it with POST it will not work .
Here is the codes :
<?php
if(isset($_POST['delete'])){
$dataBase = mysql_connect("localhost","root","");
mysql_select_db('HouseOfCake');
$PID = $_POST['PID'];
echo $PID ;
}
?>
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<form method="POST" action="Delete.php">
<div class="container">
<?php
$dataBase = mysqli_connect("localhost","root","" , "HouseOfCake");
?>
<div class="row text-center">
<?php
$r=mysqli_query($dataBase,"SELECT*FROM Products");
while($Products=mysqli_fetch_array($r, MYSQLI_ASSOC)){
?>
<div class="col-lg-3 col-md-6 mb-4">
<div class="card" name= <?php $Products['CakeID']; ?>>
<image src = <?php echo
'data:image/jpg;base64,'.base64_encode($Products['Image']).'' ; ?> />
<div class="card-body">
<h4 class="card-title"> <?php echo $Products['Price']; ?> SR. </h4>
<p class="card-text"> Details.</p>
</div>
<div class="card-footer">
<input type="text" value= "<?php echo $Products['CakeID'] ?>" name="PID" >
<input type = "submit" name="delete" value=" Delete Item."
style="width:250px" >
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</form>
</body>
</html>
To clean up what you are doing, so its easier to understand, you have this in a nutshell:
<form>
while {
<inputs>
}
</form>
What this will do is submit ALL those inputs to PHP. And since each one of the inputs in the while have the same name, PHP will only use the 'last one received'.
You want to have your flow like this:
while {
<form>
<inputs>
</form>
}
That way each form only submits the inputs defined inside of it (not all of them on the page).
So to recap with your code, you would want to have multiple forms on the page as such:
<?php while($Products=mysqli_fetch_array($r, MYSQLI_ASSOC)){ ?>
<form method="POST" action="Delete.php">
<input type="text" value="<?php echo $Products['CakeID'];?>" name="PID">
<input type="submit" name="delete" value="Delete Item." style="width:250px">
</form>
<?php }?>

Search function with php PDO and mysql

I'm Working on a Webshop for a School Project. The website is done and works as intended. Except for the Search function, which works but does something weird.
This is the Code from my Search Page
<?php
$stmt = $auth_user->runQuery("SELECT * FROM customer WHERE id=:id");
$stmt->execute(array(":id"=>$user_id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
if(isset($_POST['btn-offer']))
{
try
{
$auth_user->redirect('offer.php');
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
?>
<form method="post" class="form-signin">
<div class="form-group">
<input type="text" class="form-control" name="search" placeholder="Enter an Item"/> <br />
<button type="Search" name="btn-search">
<i class="glyphicon glyphicon-open-file"></i> Search
</button>
<hr />
</div>
<?php
if(isset($error)){
foreach($error as $error){
?>
<div class="alert alert-danger">
<i class="glyphicon glyphicon-warning-sign"></i> <?php echo $error; ?>
</div>
<?php
}
}
else if(isset($_GET['search'])){
//$search_term = $_SESSION['search_term'];
$stmt = $user_req->runQuery("SELECT * FROM requests WHERE item LIKE :search");
$stmt->bindValue(":search","%".$_SESSION['search_term']."%");
$stmt->execute();
while($userReq=$stmt->fetch(PDO::FETCH_ASSOC)){
?><h6> Request ID </h6><?php echo($userReq['id']); ?> <br /><br /> <?php
?><h6> Requested Item </h6><?php echo($userReq['Item']); ?> <br /><br /> <?php
?><h6> Requested Price </h6><?php echo($userReq['price']); ?> <br /><br /> <?php
?><h6> Requested Quantity </h6><?php echo($userReq['quantity']); ?> <br /><br /> <?php
?><h6> Subject </h6><?php echo($userReq['subject']); ?> <br /><br /> <?php
?><h6> Description </h6><?php echo($userReq['descr']);
?>
<br />
<form action="offer.php" method="post" class="form-signin">
<input type="hidden" class="form-control" name="offer_id" value="<?php echo htmlspecialchars($userReq['id']); ?>"/> <br />
<input type="hidden" class="form-control" name="offer_item" value="<?php echo htmlspecialchars($userReq['Item']); ?>"/> <br />
<button type="Search" name="btn-offer">
<i class="glyphicon glyphicon-open-file"></i> Create Offer
</button>
</form>
<form action="all_offers.php" method="post" class="form-signin">
<input type="hidden" class="form-control" name="offer_id" value="<?php echo htmlspecialchars($userReq['id']); ?>"/> <br />
<button type="Search" name="btn-show">
<i class="glyphicon glyphicon-open-file"></i> Show Offers
</button>
</form>
<hr />
<?php
}
?>
<?php
}
?>
<br />
</form>
It displays everything as desired (all entries containing the search input).
But if i click on Create Offer, the hidden Input Types gets passed to my Search function just once. It wont update it with the new hidden variables if i click on another create offer button.
Offer.php file
<?php
if(isset($_POST['offer_id']))
{
$_SESSION['off_rid'] = $_POST['offer_id'];
$_SESSION['offer_item'] = $_POST['offer_item'];
}
?>
<h2 class="form-signin-heading">Create Offer for Request ID <?php echo htmlspecialchars($_SESSION['off_rid']); ?></h2><hr />
<form method="post" class="form-signin">
<input type="hidden" class="form-control" name="off_rid" value="<?php echo htmlspecialchars($_SESSION['off_rid']); ?>"/> <br />
<div class="form-group">
<label>Requested Item</label>
<input type="text" class="form-control" name="off_item" placeholder="<?php echo htmlspecialchars($_SESSION['offer_item']); ?>" value="<?php if(isset($error)){echo htmlspecialchars($_SESSION['offer_item']);}?>" readonly/>
</div>
<div class="form-group">
<input type="number" class="form-control" name="off_price" placeholder="Enter a Price" value="<?php if(isset($error)){echo $off_price;}?>" />
</div>
<div class="form-group">
<input type="number" class="form-control" name="off_quant" placeholder="Enter the Quantity" value="<?php if(isset($error)){echo $off_quant;}?>" />
</div>
<hr />
<div class="form-group">
<button type="submit" name="btn-createoffer">
<i class="glyphicon glyphicon-open-file"></i> Post Offer
</button>
</div>
<?php
if(isset($error)){
foreach($error as $error){
?>
<div class="alert alert-danger">
<i class="glyphicon glyphicon-warning-sign"></i> <?php echo $error; ?>
</div>
<?php
}
}
else if(isset($_GET['posted'])){
?>
<div class="alert alert-info">
<i class="glyphicon glyphicon-log-in"></i> Successfully submitted the Offer!
</div>
<?php
}
?>
<br />
</form>
If i search for every entry containing "ni" i get all the entries, the hidden Inputtypes are also correct.
If i click on create offer it should write the id and the item on the redirected Page but it only works once. if i go back and search another item it still displays the info from the first item i created an offer.
Can anybody point out what i'm doing wrong. I'm stuck on this Problem since a few days and just cant figure it out.
Thanks in advance

Passing variables from view to controller (MVC)

In my view i have a foreach statement which grabs all the nessesary data from the database which displays correctly.
Here is the view:
<?php
?>
<div class="cmt-container" >
<?php
foreach($results as $row){
$user = $row->user;
$comment = $row->comment;
$date = $row->date;
$name = $row->name;
$joke = $row->joke;
$joke_id = $row->joke_id;
// Get gravatar Image
// https://fr.gravatar.com/site/implement/images/php/
$default = "mm";
$size = 35;
$grav_url = "http://www.gravatar.com/avatar/"."?d=".$default."&s=".$size;
?>
<div class="cmt-cnt">
<img src="<?php echo $grav_url; ?>" />
<div class="thecom">
<h5><?php echo $user; ?></h5><span data-utime="1371248446" class="com-dt"><?php echo $date; ?></span>
<br/>
<p>
<?php echo $comment; ?>
</p>
</div>
</div><!-- end "cmt-cnt" -->
<?php
}
?>
<?php
echo form_open('comments/insertComment');
?>
<div class="new-com-bt">
<span>Write a comment ...</span>
</div>
<div class="new-com-cnt">
<input type="text" id="name-com" name="name-com" value="" placeholder="Name is optional" />
<textarea class="the-new-com" id="the-new-com" name="the-new-com" placeholder="Write your comment here..."></textarea>
<input type="hidden" name="joke_id">
<input class="bt-add-com" type="submit" value="Post comment">
<div class="bt-cancel-com">Cancel</div>
</div>
<div class="clear"></div>
</div><!-- end of comments container "cmt-container" -->
<?php
echo form_close();
?>
My question is, how can i pass the $joke_id variable to the insertComment function in my comments controller.
I have put the input field as hidden on the joke_id field because i want to assign the ID of a joke to a comment, so the joke will have unique comments.
If the comments are on the same page as the joke, you can just take the $joke_id and put it in the hidden input:
<input type="hidden" name="joke_id" value="<?php echo $joke_id; ?>">
And when you add a comment (I assume you handle the form datas in the insertComment() function), you can access the joke id by $_POST['joke_id'].
(yes, that's not really secure but if your user can comment any joke, you just have to check that a joke with the id equal to $_POST['joke_id'] exists in the DB and if so, you just insert the comment)
Is that what you wanted?
try this..
<?php echo form_open('comments/insertComment'); ?>
<div class="new-com-bt">
<span>Write a comment ...</span>
</div>
<div class="new-com-cnt">
<input type="text" id="name-com" name="name-com" value="" placeholder="Name is optional" />
<textarea class="the-new-com" id="the-new-com" name="the-new-com" placeholder="Write your comment here..."></textarea>
<input type="hidden" name="jokeid" value="<?= $joke_id; ?>">
<input class="bt-add-com" type="submit" value="Post comment">
<div class="bt-cancel-com">Cancel</div>
</div>
<div class="clear"></div>
</div><!-- end of comments container "cmt-container" -->
<?php echo form_close();?>
and in your insert comment controller
public function __construct() {
//codes here
$joke_id = $this->input->post('jokeid');
}

Comments on the page wont show up

Hello i have a problem i cant see the comments on the page.
I dont get any error on the page so i am stuck on this moment
can somebody help me??
This is the php code:
<div id="container">
<?php include('includes/menu.php');?>
<div id="post">
<?php
$row = $query->fetch_object();
echo "<h2>".$row->title."</h1>";
echo "<p>".$row->body."</p>";
?>
</div>
<hr />
<div id="add-comments">
<form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"?>" method="post">
<div>
<label>Email Adres</label><input type="text" name="email" />
</div>
<div>
<label>Naam</label><input type="text" name="name" />
</div>
<div>
<label>Commentaar</label><textarea name="comment"></textarea>
</div>
<input type="hidden" name="post_id" value="<?php echo $id?>" />
<input type="submit" name="submit" value="Toevoegen"/>
</form>
</div>
<hr />
<div id="comments">
<?php
$query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()):
?>
<div>
<h5><?php echo $row->name?></h5>
<blockquote><?php echo $row->comment?></blockquote>
<?php endwhile;?>
</div>
</div>
</div>
and the rest of the page is:
<div id="container">
<?php include('includes/menu.php');?>
<div id="post">
<?php
$row = $query->fetch_object();
echo "<h2>".$row->title."</h1>";
echo "<p>".$row->body."</p>";
?>
</div>
<hr />
<div id="add-comments">
<form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"?>" method="post">
<div>
<label>Email Adres</label><input type="text" name="email" />
</div>
<div>
<label>Naam</label><input type="text" name="name" />
</div>
<div>
<label>Commentaar</label><textarea name="comment"></textarea>
</div>
<input type="hidden" name="post_id" value="<?php echo $id?>" />
<input type="submit" name="submit" value="Toevoegen"/>
</form>
</div>
<hr />
<div id="comments">
<?php
$query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()):
?>
<div>
<h5><?php echo $row->name?></h5>
<blockquote><?php echo $row->comment?></blockquote>
<?php endwhile;?>
</div>
</div>
</div>
Hope somebody see the problem.
try like this for printing comments..
<div id="comments">
<?php
$query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()){ //opening while block
?>
<div>
<h5><?php echo $row->name;?></h5>
<blockquote><?php echo $row->comment;?></blockquote>
<?php }// ending while block ?>
</div>
</div>
<div id="comments">
<?PHP
$query = $db->query("SELECT name, comment FROM comments WHERE post_id='$id' ORDER BY comment_id DESC");
while($row = $query->fetch_object()) {
?>
<div>
<h5><?= $row->name ?></h5>
<blockquote><?= $row->comment ?></blockquote>
</div>
<?PHP } ?>
</div>
If that does not work, i would suggest dumping out the SQL query and making sure it is working.

Categories