select all from database of the selected checkbox - php

The code given below which display all the products in the home page.. and each product has a checkbox.. I wanted to select the checkbox of few products, and if I click on submit, the next page doesn't view the selected checkbox results
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 15");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
$colindex = 1;
$totcols = 3;
$rowclosed = false;
//$totrows=ceil($count/$totcols);
$dynamicList .= "<dl id='Searchresult'> <form action='selected.php' method='POST'> <table width=\"50%\" border=\"0\" cellspacing=\"0\" align=\"center\" cellpadding=\"3\">";
while ($row = mysql_fetch_array($sql)) {
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
if ($colindex == 1) {
$dynamicList .= "<tr>";
$rowclosed = false;
}
$dynamicList .= '<td width="142" valign="top" align="center">
<div id="products">
<div class="product">
<img style=" 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="100" height="100" border="0" /><div class="pr-info"><h4>' . $product_name . '</h4>
<input type="checkbox" name="check[]" value="<?php .$id. ?>"/>
</div>
</div></td>';
}
}
The Code below is where i used to retrieve the data from database of selected checkbox
foreach ($_POST['check'] as $k => $check) {
$where[ ] = $k . " = '" . mysql_real_escape_string($check) . "'";
}
include "storescripts/connect_to_mysql.php";
$sql = mysql_query("Select * from products where " . implode(' AND ', $where)); // Connect to the MySQL database
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while ($row = mysql_fetch_array($sql)) {
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList .= '<td width="142" valign="top" align="center">
<div id="products">
<div class="product">
<a href="printer.php?id=' . $id . '">
<img style=" 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="200" height="200" border="0" />
</a>
<div class="pr-info"><h4>' . $product_name . '</h4><p>' . $details . ' </p>
<span class="pr-price"><span>Rs.</span><sup>' . $price . '</sup></span>
</div>
</div></td>';
}
} else {
echo "That item does not exist.";
exit( );
}

Try changing the beginning of your second code to this:
include "storescripts/connect_to_mysql.php";
$sql = mysql_query('SELECT * FROM products WHERE id IN ('.implode(', ', array_values($_POST['check'])).')');
// rest of your code, beginning at $productCount = ...
But, how it was said before, you should start using PDO!

I think you final query and code should look this way...please change your code with this...and post the results:
foreach($_POST['check'] as $k){
$where[]= $k;}
var_dump($where); //it show you something, show me the var_dump result ???
include "storescripts/connect_to_mysql.php";
$query = "Select * from products where id IN(".implode(",",$where).")";
echo $query; //this line too....show me the result
$sql = mysql_query($query);
// Connect to the MySQL database
$productCount = mysql_num_rows($sql);
//the rest of your code ;)
-----------------------EDITED--------------------------------
Your problem is in this line:
<input type="checkbox" name="check[]" value="<?php .$id. ?>"/>
That line is wrong, make this change and see how it's work:
<input type="checkbox" name="check[]" value="<?php echo $id;?>"/>
Saludos ;)

Related

How can you get the PHP code to render 4x4 grid layout for my products?

//Also how can I achieve a 4x4 grid view of my products without using a table? thanks in advance.
<?php
include"storescripts/connect_to_mysql.php";
$sql=mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 15");
$columncount = 0;
$dynamicList = '<table align="center" width="100%" border="0" cellpadding="6"><tr>';
while($row=mysql_fetch_array($sql)){
$id=$row["id"];
$product_image = $row["product_image"];
$product_name = $row["product_name"];
$price=$row["price"];
$date_added = strftime("&b &d, &y", strtotime($row["date_added"]));
$dynamicList .= '<td width="135"><a href="product.php?id=' . $id . '">
<img src="inventory_images' . $id . '.jpg" alt="" width="129" height="169" border="0">
</a>
</td>
<td width="593" valign="bottom"></br>' . $product_name . '<br>
£' . $price . '<br>
View Product Details</td>';
//The missing table tag is causes my footer to go up above the product list. thats why I have left it out
if($columncount == 3)
{
$dynamicList .= '</tr>';
$columncount = 0;
}
else
{
$columncount++;
}
}
mysql_close();
?>
This question is more about HTML and CSS than about PHP. But a grid without using a HTML table you could do like this:
echo '<div class="items">' , PHP_EOL , PHP_EOL;
$itemCount = 0;
while($itemCount < 16){
$rowOpeningHtml = null;
$rowClosingHtml = null;
if($itemCount%4 === 0) {
$rowOpeningHtml = '<div class="item-row">' . PHP_EOL;
if($itemCount > 0)
$rowClosingHtml = '</div>' . PHP_EOL . PHP_EOL;
}
echo $rowClosingHtml , $rowOpeningHtml , "\t" , '<div class="item">item' , ($itemCount + 1) , '</div>' , PHP_EOL;
++$itemCount;
}
echo '</div>' , PHP_EOL , PHP_EOL , '</div>';
And style it with some fancy CSS.

Put status on item

I would like to put status on item. When the quantity is 0 it should come out Sold Out else Available. But I'm getting a parse error on line 16. Why is that?
<?php
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
$dynamicList = "";
$status = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$quantity = $row["quantity"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$status = if($quantity == 0) echo "Sold Out";
else echo "Available";
$dynamicList .= '<table width="100%" border="0" cellspacing="1" cellpadding="6">
<tr>
<td width="17%" valign="top"><img style="border:#666 0px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="77" height="102" border="1" /></td>
<td width="83%" valign="top">
' . $product_name . '<br />
$' . $price . '<br />
Quantity =' .$quantity . '
<br /> Status = ' . $status . ' <br />
</td>
</tr>
</table>';
}
} else {
$dynamicList = "We have no products listed in our store yet";
}
mysql_close();
?>
You have syntax errors in your code : you can't assign value to the if statement while echoing something in it.Try the following :
$status = $quantity == 0 ? "Sold Out" : "Available";
echo $status;
This is completely wrong syntax-wise:
$status = if($quantity == 0) echo "Sold Out";
else echo "Available";
Try this instead:
if ($quantity == 0)
$status = "Sold Out";
else
$status = "Available";
echo $status;

How to set echo mysql_fetch_array to horizontal? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This code load from mysql data based on varchar 'group' to table and put picture in each of them. It is working but result is vertical album but I want horizontal.
<?php
include "../script/connect_to_mysql.php";
if (isset($_GET['a'])) {
$ge = $_GET['a'];
$tisk = "";
$sql = mysql_query("SELECT * FROM lch WHERE `group` = '$ge' ");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$name = $row["name"];
$tisk .='<table width="120" height="120" ><br>
<tr><br>
<td height="118" colspan="2"><br><img src="../lch/ikon/' . $id . '.jpg" alt=' . $name . ' width="100" height="100" /><br>;</td>
</table>';<br>
}<br>
}<br>
echo "$tisk";<br>
}<br>
?>
LOL I can post my own answer. I found solution what working for me. Here I am posting it. Anyway thanks for help :-P
include "../script/connect_to_mysql.php";
if (isset($_GET['a'])) {
$ge = $_GET['a'];
$tisk = "";
$sql = mysql_query("SELECT * FROM lch WHERE `group` = '$ge' ");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
$tisk = '<table width="120" height="120"><tr>';
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$name = $row["name"];
$tisk .='<td height="118" colspan="2"><img src="../lch/ikon/' . $id . '.jpg" alt="$name" width="100" height="100" /></td>';
}
echo '<table width="120" height="120">
<tdbody>
<tr>
' .$tisk. '
</tr>
</tbody>
</table>
';
}
}
I sort-of fixed your post, SO certainly does let you post tables within code. (I left in what might be syntax errors)
Assuming that the <br>s are an artifact of your posting messy code in the editor, you simply need to move the <tr></tr> outside the loop.
if ($productCount > 0) {
$tisk = '<table><tr>';
while(row = mysql_fetch_array($sql)){
$id = $row["id"];
$name = $row["name"];
$tisk .='<td><img src="../lch/ikon/' . $id . '.jpg" alt=' . $name . ' width="100" height="100" /></td>';
}
$tisk = '</tr></table>';
echo $tisk;
}
This will produce a table with all the cells in the same row, instead of a bunch of tables with all one column each.
If you then want a certain number per row, you'll need to use modulo:
if ($productCount > 0) {
$cellCount = 0;
$tisk = '<table><tr>';
while(row = mysql_fetch_array($sql)){
$cellCount++;
if($cellCount%5==0){
$tisk .= '</tr><tr>';
}
$id = $row["id"];
$name = $row["name"];
$tisk .='<td><img src="../lch/ikon/' . $id . '.jpg" alt=' . $name . ' width="100" height="100" /></td>';
}
$tisk = '</tr></table>';
echo $tisk;
}

Style with css in php

I'm new to this so please be gentle
I want to style for example $product_name, $author, $details.
I guess I could style the echos but in this case the only echo is
<?php
// Connect to the MySQL database
include "storescripts/connect_mysql.php";
// This block grabs the whole list for viewing
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY id DESC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$author = $row["author"];
$details = $row["details"];
$link = $row["link"];
$isbn = $row["isbn"];
$isbn13 = $row["isbn13"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList .= '<table width="580" border="0" cellspacing="0">
<tr>
<td width="120"><img src="/inventory_images/' . $id . '.jpg" width="120" height="184" border="0" /></td>
<td width="456"><p>' . $product_name . '<p><br />
<p>' . $author . '</span><br />
<p>' . $details . '<br />
visa </td>
</tr>
</table>';
}
} else {
$dynamicList = "Database empty.";
}
mysql_close();
?>
Have you tried adding some CSS classes to your table cells?
$dynamicList .= '
<table width="580" border="0" cellspacing="0">
<tr>
<td width="120"><img src="/inventory_images/' . $id . '.jpg" width="120" height="184" border="0" /></td>
<td width="456"><p class="product_name">' . $product_name . '<p><br />
<p class="author">' . $author . '</span><br />
<p class="detail">' . $details . '<br />
visa </td>
</tr>
</table>';
In the head add some styles:
<style>
.product_name{color:red;}
.author{color:green;}
.detail{color:blue;}
</style>
Why not just add CSS rules to your stylesheet?
table tr td p {
// some styles
}
You can add a class with styles to the <p> elements that contain each value
<td width="456"><p class="product-name">' . $product_name . '</p>...
PHP is only a preprocssor and coughs up the code as the page loads and before any of the css of HTML kicks in so you can style it as normal
You can use style="" and class="" parameters on your <p> paragraph tags as well on the table cells <td>. You can also try to add for example styled <span> or <div> tags to your variables I suppose.
For example:
$product_name = "<span class='yourstyleclass'>$row['product_name']</span>";

creating a new array every 10 items added

I have the following code that shows the items added in the producs table in the database and i want to make it every 10 items creating a new array so that it won't be a long list of items.
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="10%" valign="top"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpeg" alt="' . $product_name . '" width="93" height="102" border="1" /></td>
<td width="90%" valign="top">' . $product_name . '<br />
€' . $price . '<br />
<a class="provoli" href="product.php?id=' . $id . '">Προβολή Λεπτομεριών</a></td>
</tr>
</table>';
Your code-style could use some work, as it is not very pretty, but, to make this work, you could try putting your list in an array.
This requires two steps, the first is keeping record of an index. The second is adding the values to your new array.
$index = 0;
while(<your condition>) {
$index++;
<more code>
$list[((int)$index/10)] .= <string>
}
A better way is to implement this when printing the array with all your items. But I don't know how you're implementing this further.

Categories