extract the datas from a array using php - php

i searched previous sample questions but those are not helpful my question..
My task is i have several types of rooms in my hotel. when i select the rooms, after submit the form the room numbers and price details should be store into redirected page.
i use this command
here I modified my code like below
<?php while($row24 = mysql_fetch_array($result24)):;?>
<div class="item-gallery isotope-item bo-rad-10 hov-img-zoom triple">
<?php echo "<img src='images/$row24[pic]' alt='IMG-GALLERY'>"?>
<div class="overlay-item-gallery trans-0-4 flex-c-m">
<?php echo "<a class='btn-show-gallery flex-c-m fa fa-search' href='images/$row24[pic]' data-lightbox='gallery'></a>"?>&nbsp &nbsp
<?php echo "<label class='container1 txt17'>Select Room
<input type='checkbox' name = 'check_list[]' value='$row24[roomno] ,$row24[price]'>
<span class='checkmark'></span>
</label>"?>
</div>
<div class="line-item-mainmenu text-blo3 size21 flex-col-l-m">
<?php echo "<span class='txt20 m-b-3'>" .'Room No : '
.$row24['roomno'].
"</span>"?>
<?php echo "<input type='text' class=' txt16 m-b-3'>".'Price &nbsp &nbsp &nbsp : '
.$row24['price']."&nbsp &nbsp<i class='fa fa-rupee'></i>"."&nbsp &nbsp(One Night )" ?>
</div>
</div>
<?php endwhile;?>
by using this code i get he results which i expect. but my problem is how to extract price from that array

When including object/array elements in strings, you need to wrap them with {} so they will be parsed properly.
For example
<?php echo "<img src='images/$row24[pic]' alt='IMG-GALLERY'>"?>
should be
<?php echo "<img src='images/{$row24[pic]}' alt='IMG-GALLERY'>"?>
Aside from that, it does not matter what room is selected, all these form fields (minus those unchecked input boxes) are going to be submitted and you'll have a tough time trying to parse the data out.
If, at the very least, assign roomno as an associative key for each of your fields, for example:
<input type='checkbox' name = 'check_list[]' value='{$row24[roomno]}'>
<input type='text' name='price[]' value='{$row24[roomno][price]}' />
That way, at least you can iterate through the $_POST, price field(s) based on the roomno key selection(s).

Related

Populate a select box with all the numbers up to current stock

I have two questions:
1) Say if I have a database like this:
Name Stock
Prada 10
Armani 20
Gucci 25
How do I populate each select box with say 1-10; 1-20; and 1-25
This is my code:
<?php
...
while($row = mysqli_fetch_assoc($result)){
print
"<div class='item col-xs-4 col-lg-4'>"..
"<div class='form-group'>".
"<label for='sel1'>Select list:</label>".
"<select class='form-control' id='sel1'>".
"<option>$row["Stock"]</option>".
"</select>".
"</div>".
"<button id='button" class='btn btn-success add'>Add to basket</button>".
"</div>";
}
?>
<script type="text/javascript">
$(document).on("click", ".add", function(){
// HOW DO I GET THE VALUE OF THE SELECT BOX SELECTED HERE?
});
</script>
Also, how do I get the value from the selectbox?
From what you've mentioned, it seems like you want to fetch the select box options and loop through them. In that case, only the option should be within the while loop.
And, I'd suggest you to keep your code clean and write your HTML and PHP codes separate rather than printing your HTML within PHP.
<?php /* Other PHP code */ ?>
<div class='item col-xs-4 col-lg-4'>
<div class='form-group'>".
<label for='sel1'>Select list:</label>
<select name="stock" class='form-control' id='sel1'>
<?php while($row = mysqli_fetch_assoc($result)){ ?>
<option value="<?php echo "1-".$row["Stock"]; ?>">
<?php echo "1-".$row["Stock"]; ?>
</option>
<?php } ?>
</select>
</div>
<button id='button" class='btn btn-success add'>Add to basket</button>
</div>
Hope this helps!

Adding item to shopping cart PHP HTML only

I have trouble when creating code for adding an item to shopping cart. What I did is echo each individual item customers could buy, and I wanted to add an input type for how many of that specific item they want, then "add to cart" button and repeat (echo another item, another input and button etc.)
while ($zaznam = mysqli_fetch_assoc($vysledek)) {
$soubor=$zaznam["img_nazev"];
echo "<div id='zbozi_info'>";
echo "<img src='img/$soubor'>";
echo "<p class='popis'>".$zaznam["dlouhy_popis"]."</p>";
echo "<p class='cena'> Cena bez DPH: ";
echo "<span style=color:#FF0000>".$zaznam["cena"]." Kč </span> </p>";
echo "<p class='cena'> Cena s DPH: ";
echo "<span style=color:#FF0000>".$zaznam["CenaDPH"]." Kč </span> </p>";
echo "</div>";
echo "<form method='post'>
<label for='pocetk'>Počet kusů</label>
<br>
<input type='text' name='pocetk' id='pocetk' value='1'>
<br>
<input type='submit' name='pridkos' id='pridkos' value='Přidat do košíku'>
</form>";
}
This works and all, the trouble starts when I add this if to the mix, to enable that adding function
if (isset($_POST["pridkos"]) && isset($_SESSION['user'])) {
$ksk="INSERT INTO kosik
(ID_uzivatele,ID_zbozi, pocet_kusu)
VALUES ('".$_SESSION['user']."',
'".$zaznam["ID_zbozi"]."',
'".$_POST["pocetk"]."');";
$vysledek = mysqli_query($con,$ksk)
or die("Zboží nebylo přidáno do košíku");
This just dies on the mysqli_query. I am not exactly sure if to put it inside the while or outside, my suspicion is inside, but it isn't working either way
Session[user] has the user ID (ID_uzivatele), not sure what else can I clarify.
Any help is appreciated.
I think this query syntax is wrong.
Check with below one:-
$ksk="INSERT INTO kosik(ID_uzivatele,ID_zbozi, pocet_kusu)
VALUES ('$_SESSION[user]',
'$zaznam[ID_zbozi]',
'.$_POST["pocetk"]')";

php - How to make a unique $_POST to a form action?

I'm having a trouble finding a solution to this. Here is the case, assume that I have 5 books in the database, i will display them by doing a while then mysql_fetch_array assume that I create a table. This is the plot
echo"
<td>
<form action='bookext.php' method='post'>
<input type='submit' value='".$row['book_title']."' style='border: 0; background: transparent';>WHAT TO PUT HERE</form></td>";
the $row['book_title'] works fine because it must display what is the title of the book in the database. But, how can I get it's unique value which is book_id and send it to bookext.php?
PS: Sorry for my title, I can't pull the right english
First of all your current input needs a name attribute:
"<input type='submit' name="title" value='".$row['book_title']."' style='border: 0; background: transparent';>WHAT TO PUT HERE</form></td>";
Since you're using the POST method this will insure the value is available in the $_POST array, like $_POST['title'].
For the id you can add a hidden input to your form:
"<input type='hidden' name='book_id' value='".$row['book_id']."'>"
This will be available to you in $_POST['book_id'] when you submit.
Just insert hidden field to your form
<input type="hidden" name="POST_NAME" value="YOUR_VALUE_TO_IDENTIFY">
Don't use forms in tables.
<form action='bookext.php' method='post'>
<table>
<tr>
<?php foreach($books AS $book){ ?>
<td>
<button type="submit" name="bookId" value="<?php echo $book['id']; ?>">
Title of the book
</button>
</td>
<?php } ?>
</tr>
</table>
</form>
bookext.php
<?php
$id = $_POST['bookId'];

Multiple dynamic contact forms

I have built a inquiry form on my website, the idea is instead of mailing me each time a user submits a query it is added to my database which I can then go and view via my backend system
Each query will be listed one by one with a text-area contact form below it allowing me to reply to each query individually
So far I have this (sorry it's a bit messy)
foreach ($listings as $row){
$loop.= "<h3 class='text-center'>".$row['question']."</h3>";
$loop.= "<p>".$row['message']."</p>";
$loop.= "Name: <b>".$row['name']."</b>";
$loop.= "<span class='pull-right'>Email: <b>".$row['email']."</b><br></span>";
$loop.= "<div class='clearfix'></div>";
if(isset($row['website'])){ $loop.="Website: <b>".$row['website']."</b>"; }
$loop.= "<span class='pull-right'>Date: <b>".$row['date']."</b></span>";
$loop.= "<form name='submit-response' method='POST'><fieldset>";
$loop.= "<div class='form-group'> <label for='Message".$counter."'>Your Message</label> <textarea id='Message".$counter."' name='Message".$counter."' class='form-control' rows='5'></textarea> </div>";
$loop.= "<button type='submit' name='submit".$counter."' class='btn btn-default btn-block'>Reply</button>";
$loop.= "</fieldset></form>";
}
Before that is a foreach loop and the start of the oh and $counter is set to nill
What I want is for each contact form to be unique so when I click send on one of the queries it will be sent and removed so I can send another, the only issue I am having is working out how I will work out if a submit has been hit, and which submit has been hit
The code will need to workout which button has been hit and depending on which button it will then mail() to the recipient
I'm quite stuck on this one and I'm not sure of the best course of action so any advice is really appreciated
Luke
If you click a submit button inside a <form> tag, then only that form will be submitted.
You could include a hidden field with the ID of the row in it. That way you could get rid of the $counter variables altogether.
Also if you plan on just echoing out the $loop html, I wouldn't recommend storing the HTML in a PHP variable.
<?php
foreach ($listings as $row)
{
?>
<h3 class="text-center"><?php echo $row['question']; ?></h3>
<p><?php echo $row['message']; ?></p>
Name: <b><?php echo $row['name']; ?></b>
<span class="pull-right">Email: <b><?php echo $row['email']; ?></b><br></span>
<div class="clearfix"></div>
<?php
if(isset($row['website']))
{
?>
Website: <b><?php echo $row['website']; ?></b>
<?php
}
?>
<span class="pull-right">Date: <b><?php echo $row['date']; ?></b></span>
<form action="" name="submit-response" method="POST">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<fieldset>
<div class="form-group">
<label>Your Message</label>
<textarea name="Message" class="form-control" rows="5"></textarea>
</div>
<button type="submit" class="btn btn-default btn-block">Reply</button>
</fieldset>
</form>
<?php
}
?>
add a unique id to your database table, and put it in a hidden input.
Give each form a id, and possibly each submit button a unique name. This way you can easily determine which submit button was hit, or which form was submitted, and remove it or process it via javascript.
$('form').each(function() {
$(this).submit(function(event) {
event.preventDefault();
// Add AJAX code here
$(this).remove();
});
});
Of course that was pseudo-code.

Posting a variable that is unique to a row, how do I do this?

I am POSTING two things. The comment, which works ok, but the second item I need to post is the $list['id'] that is unique to this each row. How do I include this unique id, when the user clicks POST so that it can be used on the page that it is being posted to.
foreach ($posts as $key => $list){
echo " <tr valign='top'>\n";
echo " <tr>$list['id']
<div class='comment_text'>
<form method='post' action='add_comment.php'>
<textarea name='comment'</textarea>
<input class='btn' type='submit' value='Post'/>
</form>
</div>
</td>\n";
echo "</tr>\n";
}
The page I am posting to looks like this:
<?php
$commenter_user_id = $_SESSION['user_id'];
$body = substr($_POST['comment'],0,400);
$post_id=;
add_comment($commenter_user_id,$post_id,$body);
$_SESSION['message'] = "Your comment has been added!";
header("Location:/social_learning/site_pages/profile.php");
?>
You can use hidden input:
<input type="hidden" name="postName" value="<?= $list['id'] ?>" />
Then in your PHP it's available in $_POST['postName'] (in accordance to the name attribute of the hidden input)

Categories