This code gets all values from a table and for each row it shows its details inside a alert div and i can click a "order ready button" for that single product.
What I need to do is put in a single div all the products that are from the same order, and for that I'm thinking about using all the rows that have the same date value and when this value changes create a new div.
<?php
$result = mysqli_query($mysqli, "SELECT * FROM kitchen");
while ($row = mysqli_fetch_array($result)) {
$table = $row['table'];
$customer = $row['customer'];
$product = $row['product_name'];
$code = $row['product_code'];
$size = $row['size'];
$id = $row['id'];
$date = $row['date'];
// It would have to open here in each first distinct $date
echo '<div class="alert alert-info" role="alert" id="'.$code.'">';
echo '<h4>'.'Table '.$table.'</h4>';
echo '<h4>'.'Name: '.$name.'</h4>';
// Repeat this for each equal $date value
if($code=="A01"||$code=="A02"||$code=="A03"||$code=="A04"){
echo '<h4>'.$code.' - '.$product.' ('.$size.')'.'</h4>';
}
else{
echo '<h4>'.$code.' - '.$product.'</h4>';
}
// Close here before each next distinct $date
echo '<form action="actionkitchen.php" method="post">';
echo "<button class='btn btn-lg btn-primary btn-block' name='data' value='$data' type='submit'>Order Ready</button>";
echo '</form>';
echo '</div>';
}
?>
This is what I ended up with, not the most elegant solution but it's working.
<?php
$result = mysqli_query($mysqli, "SELECT * FROM kitchen");
while ($row = mysqli_fetch_array($result)) {
$table[] = $row['table'];
$name[] = $row['name'];
$product[] = $row['product_name'];
$code[] = $row['product_code'];
$size[] = $row['size'];
$date[] = $row['date'];
}
$count = array_count_values($date);
$y = 0;
foreach ($count as $item){
for($i=0;$i<$item;$i++){
if($i==0){
echo '<div class="alert alert-info">';
echo '<h4>'.'Table '.$table[$y].'</h4>';
echo '<h4>'.'Name: '.$name[$y].'</h4>';
}
if($code[$y]=="A01"||$code[$y]=="A02"||$code[$y]=="A03"||$code[$y]=="A04"){
echo '<h4>'.$code[$y].' - '.$product[$y].' ('.$size[$y].')'.'</h4>';
}
else{
echo '<h4>'.$code[$y].' - '.$product[$y].'</h4>';
}
if($i==$item-1){
echo '<form action="actionkitchen.php" method="post">';
echo "<button class='btn btn-lg btn-primary btn-block' name='data' value='$data[$y]' type='submit'>Order Ready</button>";
echo '</form>';
echo '</div>';
}
$y++;
}
}
?>
To set your products in the same order, I would group them by the key in an array. For our purposes, we'll use a multidimensional array so that we can add our products within the unique key (using "date" in the example). Below you will see me set the array, fetch the rows from the database (sorting by our group key so that we have some consistency on the front end) and begin placing them in their unique groups. When pushing a product into the date array, I am using array_merge() in combination of in_array() and a ternary operator to set the "product string" within the HTML.
<?php
/* Fetch/Set Kitchen */
$kitchen = array();
$sql = "SELECT * FROM `kitchen` ORDER BY `date`";
$query = mysqli_query($mysqli, $sql);
while($row = mysqli_fetch_array($query)) {
$kitchen[$row['date']][] = array_merge($row, array(
'product_string' => (in_array($row['product_code'], array('A01', 'A02', 'A03', 'A04')) !== FALSE)
? $row['product_code'] . ' - ' . $row['product_name'] . ' (' . $row['size'] . ')'
: $row['product_code'] . ' - ' . $row['product_name']
));
}
?>
To keep our HTML tidy and readable apart from our PHP, you'll see that I've chosen to use an alternative syntax for the control structures. This helps by using tab indentations from having put any awkwardly placed curly brackets in our code.
<?php foreach($kitchen as $date => $items): ?>
<div class="alert alert-info" role="alert" id="<?php echo $date; ?>">
<?php foreach($items as $item): ?>
<h4>Table <?php echo $item['table']; ?></h4>
<h4>Name: <?php echo $item['customer']; ?></h4>
<h4><?php echo $item['product_string']; ?></h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="<?php echo $item['data']; ?>" type="submit">Order Ready</button>
</form>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
The above reference code will output HTML similar to:
<div class="alert alert-info" role="alert" id="2016-10-21">
<h4>Table Table 1</h4>
<h4>Name: Name 1</h4>
<h4>XXX1 - Product 1 (XXX)</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX1" type="submit">Order Ready</button>
</form>
<h4>Table Table 2</h4>
<h4>Name: Name 2</h4>
<h4>XXX2 - Product 2</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX2" type="submit">Order Ready</button>
</form>
<h4>Table Table 3</h4>
<h4>Name: Name 3</h4>
<h4>XXX3 - Product 3 (XXX)</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX3" type="submit">Order Ready</button>
</form>
</div>
<div class="alert alert-info" role="alert" id="2016-10-27">
<h4>Table Table 4</h4>
<h4>Name: Name 4</h4>
<h4>XXX4 - Product 4</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX4" type="submit">Order Ready</button>
</form>
<h4>Table Table 5</h4>
<h4>Name: Name 5</h4>
<h4>XXX5 - Product 5 (XXX)</h4>
<form action="actionkitchen.php" method="POST">
<button class="btn btn-lg btn-primary btn-block" name="data" value="XXX5" type="submit">Order Ready</button>
</form>
</div>
<div class="alert alert-info" role="alert" id="2016-11-06">
...etc.
Related
I want to view all posts, all comments for each post at once. While I have no problems with displaying posts, displaying comments is already a problem. One post can have multiple comments, so I have no idea how to create a query to display this. I tried to use LEFT JOIN but it didn't help. I include a table schema below the code to make my problem easier to understand.
<?php foreach ($tweet->userData as $user)
{ ?>
<div class='col-xl-8' id='posty'>
<div class='row' id='time'>
<div class='btn-block d-flex justify-content-between'>
<div class='imie'>
<?php echo $user['autor'] ?>
</div>
<div class='czas'>
<?php echo $user['date_to_add'] ?>
</div>
</div>
</div>
<?php echo $user['comment'] ?>
<form action='' method='post' id="myForm">
<div class='row'>
<div class='col-12 col-xl-12 d-flex justify-content-between' id='icon'>
<button class='button2' name='dodaj_like' style="background-color: <?php if($user['like_color']==1){echo '#00FA9A';}else{echo 'black';} ?>" ><i class='fas fa-heart' ></i><input type='hidden' name='like' value="<?php echo $user['id']?>" /><span id="font"><?php echo $user['likes']?></span></button>
<button class='button2' name='dodaj_dislike' style="background-color: <?php if($user['dislike_color']==1){echo '#00FA9A';}else{echo 'black';} ?>"><i class='fas fa-heart-broken'></i><input type='hidden' name='dislike' value="<?php echo $user['id']?>"/><span id="font"><?php echo $user['dislikes']?></span></button>
<button class='button2' name='dodaj_comment' id="com" ><i class='far fa-comment-dots'></i><input type='hidden' name='comment' value="<?php echo $user['id']?>"/><span id="font">Comment</span></button>
<button class='button2' name='dodaj_share' style="background-color: <?php if($user['share_color']==1){echo '#00FA9A';}else{echo 'black';} ?>" ><i class='far fa-share-square' ></i><input type='hidden' name='share' value="<?php echo $user['id']?>"/><span id="font"><?php echo $user['shares'] ?></span></button>
</div>
</div>
</form>
<div class="row d-flex">
<div class="col-xl-12 bg-success ">
<form method="post">
<textarea id="form103" class="md-textarea form-control" rows="5" placeholder="Co słychać?" name="komentarz"></textarea>
<div>
<?php echo $aabbcc ?? '' ?>
</div>
<div class="button">
<button class="btn btn-danger mt-2" name='dodaj_comment'><input type='hidden' name='com' value="<?php echo $user['id']?>"/>Publikuj</button>
</div>
</form>
</div>
</div>
</div>
<?php } ?>
$id = $_GET['id'];
$session = $_SESSION['id'];
$sql = $this->database->connect()->prepare("SELECT post.id, CONCAT(first_name,' ', last_name) AS author, post.comment, post.date_to_add, post_comment.comment, post_comment.date_to_add FROM user JOIN post ON user.id = post.user_id LEFT JOIN post_comment ON post.user_id=post_comment.post_id where post.user_id = :user_id order by post.id DESC");
$sql->bindParam(':user_id',$id, PDO::PARAM_INT);
$sql->bindParam(':id',$session, PDO::PARAM_INT);
$sql->execute();
if($sql->rowCount())
{
$this->userData = [];
while ($row = $sql->fetch())
{
$this->userData[] = $row;
}
}
}
You can get the Comment form database for specific Post using post_id by selecting Comment Table:
$query="select * form post_comment where post_id=". $post_id;
here is the PHP Implementation :
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql_post = "SELECT * FROM post";
$result = $conn->query($sql_post );
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<br> post id: ". $row["id"]. " - <br> Post: ". $row["post"]<br>";
$comment_query=$conn->query("select * form post_comment where post_id=". $row["id"]);
if ($comment_query->num_rows > 0) {
// output data of each row
while($comment= $result->fetch_assoc()) {
echo " <p>Comment ".comment['comment']." </p>";
}
}
}
} else {
echo "0 results";
}
$conn->close();
How to delete related data one by one in my PHP?
functions.php
$conn = mysqli_connect("localhost:3305","root","1234","dj"); //connect database
function getRealIpUser(){
switch(true){
case(!empty($_SERVER['HTTP_X_REAL_IP'])) : return $_SERVER['HTTP_X_REAL_IP'];
case(!empty($_SERVER['HTTP_CLIENT_IP'])) : return $_SERVER['HTTP_CLIENT_IP'];
case(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) : return $_SERVER['HTTP_X_FORWARDED_FOR'];
default : return $_SERVER['REMOTE_ADDR'];
}
}
cart.php
<include ("functions.php");>
<div class="shopping-cart">
<h6>My Cart</h6>
<hr>
<?php
$ip_add = getRealIpUser(); //getRealIpuser from functions.php
$select_cart = "select * from cart where ip_add='$ip_add'";
$run_cart = mysqli_query($conn,$select_cart);
while($row_cart = mysqli_fetch_array($run_cart)){
$pro_id = $row_cart['p_id'];
$pro_size = $row_cart['size'];
$pro_quantity = $row_cart['quantity'];
?>
<form action="cart.php" method="post" class="cart-items">
<div class="border rounded">
<div class="row bg-white">
<div class="col-md-6">
<button type="submit" class="btn btn-warning">Save for Later</button>
<button type="submit" id="<?php echo $pro_id;?>" class="btn btn-danger mx-2" name="remove">Remove</button>
</div>
</div>
</div>
</form>
<?php }?>
</div>
database:
loading page:
my problem is, I want to delete related data one by one. But my
ability limited, so how to write the PHP code with MySQL.
Here are my wrong codes:
<?php
global $conn;
if(isset($_POST['remove'])){
$delete_product = "delete from cart where p_id='$pro_id'";
$run_delete = mysqli_query($conn,$delete_product);
if($run_delete){
echo "<script>window.open('cart.php','_self')</script>";
}
}
?>
you Form has to be like this with a hidden parameter, that can be identified by its name.
In this case productid
<form action="cart.php" method="post" class="cart-items">
<div class="border rounded">
<div class="row bg-white">
<div class="col-md-6">
<input type="hidden" name="productid" value=<?php echo $pro_id;?>>
<button type="submit" class="btn btn-warning">Save for Later</button>
<button type="submit" id="<?php echo $pro_id;?>" class="btn btn-danger mx-2" name="remove">Remove</button>
</div>
</div>
</div>
</form>
In you cart.php You then uset he hidden productid to delete
I changed you vulnerable code to Procedural style preprared statement.
<?php
global $conn;
if(isset($_POST['productid'])){
if ($stmt = mysqli_prepare($conn, "delete from cart where p_id=?")) {
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "s", $_POST['productid']);
mysqli_stmt_execute($stmt);
if(mysqli_affected_rows($conn) > 0 ){
echo "<script>window.open('cart.php','_self')</script>";
}else{
echo mysqli_error($connection);
}
}
}
?>
You should read urgently how to prevent sqlinjection
And you can also take a look at the Object oriented style of pho encoding
I have a form which can submited.and have list,that listed all submited form details.
I tried it in different ways.I want to fill the form with the corresponding details when I clicked the edit button.
Here is my php file
<div class="row">
<div class="col-sm-9">
<b>Leader Name : </b><?php echo($row["lead_name"]); ?><br>
<b>Phone Number : </b><?php echo($row["phone_number"]); ?><br>
<b>Email : </b><?php echo($row["email"]); ?><br>
<b>Part Created Time : </b><?php echo($row["create_date_and_time"]); ?>
<br>
</div>
<div class="col-sm-3 ">
<form role="form" action='index.php' method='POST'>
<input type='hidden' name='party_id' value='<?php echo($row["party_id"]); ?> '>
<input type="submit" class="btn btn-sm btn-success btn-block" id="edit" name="edit" value="Edit" style="font-size: 12px; padding: 3px;">
<input type="submit" class="btn btn-sm btn-danger btn-block" id="delete" name="delete" value="Delete" style="font-size: 12px; padding: 3px;">
</form>
<?php
if (isset($_POST['delete'])) {
print("<script> alert('delete'); </script>");
$party_id = isset($_POST['party_id']) ? $_POST['party_id'] : "";
$queryDelete = "DELETE FROM party_details WHERE party_id='$party_id'";
if ($conn->query($queryDelete)) {
$_SESSION['party'] = "";
$_SESSION['trips'] = [];
print("<script>
alert('Party removed');
window.location.href='../tripCreate';
</script>");
} else {
print("<script>alert('Error when remove ! ');</script>");
}
$_POST = array();
}
if (isset($_POST['edit'])) {
$party_id1 = isset($_POST['party_id']) ? $_POST['party_id'] : "";
$query1 = "SELECT * FROM party_details WHERE party_id='$party_id1'";
$result1 = $conn->query($query1);
$row1 = $result1->fetch_assoc();
}
?>
</div>
first of all, you should specify not only result you want to achieve, but also what kind of problem you are facing.
is it php error, or information not being displayed in resulted page?
one thing i spotted is that you got $row1 = $result1->fetch_assoc(); but in form you echo $row[] (instead of $row1[]), which i dont see being created anywhere.
also, did you try var_dump($row) in php and check its content (or $row1...)?
Actually i want to develop a application from where user can set attendance for student. so the html form for attendance will come from my db query. and it's coming too but the prob is that how can i insert that form information to my db . actually i searched lot but i didn't get any result for this as perfect as i want i mean please can anyone help me . thanks in advance
<form action="attendance.php" method="post">
<?php include '../database-config.php';
foreach($dbh->query("SELECT * FROM student WHERE active_class='VII'") as $row){
echo "<div>
<label>".htmlentities($row['student_id'])."</label>
<input type='radio' name='atten".htmlentities($row['student_id'])."' checked='checked'>Present
<input type='radio' name='atten".htmlentities($row['student_id'])."'>Absent
</div></br>";
}
?>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
<button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>
<form action="attendance.php" method="post">
<?php include '../database-config.php';
$result = mysql_query("SELECT * FROM student WHERE active_class='VII'");
foreach($result as $row)
{
?>
<div>
<label><?php echo $row['student_id']?></label>
<input type="radio" name="attend" value="present" checked>Present
<input type="radio" name="attend" value="absent">Absent
</div>
</br>
<?php
}
?>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
<button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>
so in php you can get value like this
<?php
$attend = $_POST['attend'];
echo $attend;
?>
So in $attend it contain value(value="present") of radio button.
it may be present or either absent
damn getting tired xD
this should work though but you have to add the column attendency to the database table by yourself cheers
<form action="" method="post">
<?php
include '../database-config.php';
if(isset($_POST['attendency']) && isset($_POST['id']))
{
$id_to_update = $_POST['id'];
$status = $_POST['attendency'];
$ar = array('p','a');
$attend = !empty($status) && in_array($status,$ar) ? $status : 'p';
//you have to create a column named attendency for this to work
$sql = "INSERT INTO student(attendency) VALUES ('$attend ') WHERE user_id = '$id_to_update '";
$dbh->query($sql);
}
foreach($dbh->query("SELECT * FROM student WHERE active_class='VII'") as $row)
{
if($row['attendency'] == 'p')
{
$p = 'checked="checked"';
$a = '';
} else {
$a = 'checked="checked"'
$p = '';
} ?>
<div>
<input type="hidden" name="id" value="<?=$row['student_id']?>">
<label><?=$row['student_id']?></label>
<input type='radio' name='attendency' <?=$p?>>Present
<input type='radio' name='attendency' <?=$a?>>Absent
</div></br>
<?php } ?>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
<button type="reset" class="btn btn-danger btn-lg">Reset</button>
</form>
So I have the ability to submit a form which allows me to set which category my post belongs to which works great - yet if I want to edit the categories it belongs to the update is not changing the join table? I also am looking on how to show which categories are checked when you go in to update the document and pull down which categories the post belongs to.
Here is the form to update:
<?php
require_once '../../db_con.php';
if(!empty($_GET['doc_id'])){
$doc = intval($_GET['doc_id']);
try{
$results = $dbh->prepare('SELECT * FROM doc_list WHERE doc_id = ?');
$results->bindParam(1, $doc);
$results->execute();
} catch(Exception $e) {
echo $e->getMessage();
die();
}
$doc = $results->fetch(PDO::FETCH_ASSOC);
if($doc == FALSE){
echo '<div class="container">';
echo "<img src='../img/404.jpg' style='margin: 40px auto; display: block;' />";
echo "<h1 style='margin: 40px auto; display: block; text-align: center;' />Oh Crumbs! You upset the bubba!</h1>";
echo 'Get me outta here!';
echo'</div>';
die();
}
}
?>
<div class="container">
<br>
<i class="fa fa-angle-double-left"></i> Previous page
<br>
<h3 class="subTitle">
<i class="fa fa-pencil"></i></span> Edit Document
</h3>
<?php
if(isset($doc)){
?>
<form action="actions/update_doc.php" method="POST" id="rtf" name="">
<input type="hidden" value="<?php echo $doc['doc_id'] ?>" name="doc_id" />
<input type="text" value="<?php echo $doc['doc_title'] ?>" name="doc_title" required />
<br />
<?php
try{
// Selecting entire row from cat_list table
$results = $dbh->query("SELECT cat_id, cat_title FROM cat_list");
}catch(Exception $e) {
echo $e->getMessage();
die();
}
$category = $results->fetchAll(PDO::FETCH_ASSOC);
?>
<br>
<label><input type="checkbox" name="" class="selectall"/> Select all</label>
<div id="checkboxlist" >
<?php
foreach($category as $cat){
?>
<input type="checkbox" value="<?php echo $cat["cat_id"]; ?>" name="cat_no[]" id="box1"> <?php echo $cat["cat_title"]; ?></a><br>
<?php
}
?>
</div>
<br><br>
<button class="postEditBtn" type="button" onclick="ibold()" title="Bold Text"><i class="fa fa-bold"></i></button>
<button class="postEditBtn" type="button" onclick="iitalic()" title="Italic Text"><i class="fa fa-italic"></i></button>
<button class="postEditBtn" type="button" onclick="iunderline()" title="Underline Text"><i class="fa fa-underline"></i></button>
<button class="postEditBtn" type="button" onclick="ifontName()" title="Font Family"><i class="fa fa-font"></i></button>
<button class="postEditBtn" type="button" onclick="ifontsize()" title="Font Size"><i class="fa fa-text-height"></i></button>
<button class="postEditBtn" type="button" onclick="ifontcolor()" title="Font Colour"><i class="fa fa-eraser"></i></button>
<button class="postEditBtn" type="button" onclick="ihiliteColor()" title="Highlight Text"><i class="fa fa-magic"></i></button>
<button class="postEditBtn" type="button" onclick="ilink()" title="Add/Edit Link"><i class="fa fa-link"></i></button>
<button class="postEditBtn" type="button" onclick="iunlink()" title="Remove Link"><i class="fa fa-chain-broken"></i></button>
<button class="postEditBtn" type="button" onclick="ijustifyLeft()" title="Text align-left"><i class="fa fa-align-left"></i></button>
<button class="postEditBtn" type="button" onclick="ijustifyCenter()" title="Text align-center"><i class="fa fa-align-center"></i></button>
<button class="postEditBtn" type="button" onclick="ijustifyRight()" title="Text align-right"><i class="fa fa-align-right"></i></button>
<button class="postEditBtn" type="button" onClick="iUnorderedList()" title="Unordered List"><i class="fa fa-list-ul"></i></button>
<button class="postEditBtn" type="button" onClick="iOrderedList()" title="Ordered List"><i class="fa fa-list-ol"></i></button>
<button class="postEditBtnUndo" type="button" onClick="iUndo()" title="Undo last change"><i class="fa fa-rotate-left"></i></button>
<button class="postEditBtnRedo" type="button" onClick="iRedo()" title="Redo last change"><i class="fa fa-rotate-right"></i></button>
<br><br>
<textarea name="doc_content" id="doc_content" placeholder="Document Content" style="display: none;"></textarea>
<iframe name="editor" id="editor" style="width:100%; height: 600px;"></iframe>
<br />
<input onclick="formsubmit()" type="submit" value="Update Document" name="submit"/>
</form>
Here is my update script:
<?php
/******************************************************************
** ACTION SCRIPT TO UPDATE THE DOCUMENT AFTER CHANGES ARE MADE **
******************************************************************/
if(isset($_POST["submit"])){
include_once'../../config.php';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=dashboardr",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "UPDATE doc_list SET doc_title = :doc_title, doc_content = :doc_content, doc_updated=CURRENT_TIMESTAMP WHERE doc_id = :doc_id";
$query = $dbh->prepare($sql);
$query->execute(array(":doc_title"=>$_POST["doc_title"],":doc_content"=>$_POST["doc_content"], ":doc_id"=> $_POST["doc_id"]));
if ($query) {
header ('Location: ../list_doc.php?success=2');
}
/***********************************************************************
** INSERTS THE ARRAY DEPENDING ON WHICH WAS CHECKED WITHIN THE FORM **
***********************************************************************/
$sql = "UPDATE `cat_doc_link_table`(`link_cat_id`, `link_doc_id`) VALUES";
$values = "";
$params = [];
foreach($_POST["cat_no"] as $cat)
{
$values.= "(?, ?), ";
$params[] = $cat; // correct here
$params[] = $docId;
}
$values = substr($values, 0, -2);
$sql.= $values;
$query = $dbh->prepare($sql);
$query->execute($params);
if ($dbh->query($sql)) {
}else{}
$dbh = null;
}catch(PDOException $e)
{
header ('Location: ../list_doc.php?success=2');
}
}
?>
So I have two three tables, one called doc_list (for the posts) cat_list which handles the categories and a joint able which takes the id from both the doc_list and cat_list which marries up the two tables.
As mentioned above, in the update doc form (the first script) a way to pull in and tick the checkboxes from which category they are associated too. the main issue I have is updating the tables when you go in and want to update.