gathering all info from database - php

I created a database for my wifes jewelry site. When i tried to gather the information from the database for the product page I only was able to get the last item I had put inside the database. I originally got the code from a tutorial and had to work on it in order to get any items at all. Basically I need to access all the product but I only get one. Can someone show me what I am missing?
The code to bring up the items:
$id = '';
if( isset( $_GET['id'])) {
$id = $_GET['id'];
}
$sql = "SELECT * FROM products WHERE category = 'Accessories'";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $item_number = $row["item_number"];
$price = $row["price"];
$desc = $row["description"];
$category = $row["category"];
}
}
This is the code for the table:
<table width="100%" border="2" cellspacing="0" cellpadding="15">
<tr>
<td width="19%" valign="top"><img src="pictures/inventory/<?php echo $pid; ?>.jpg" width="142" height="188" alt="<?php echo $item_number; ?>" /><br />
View Full Size Image
</td>
<td width="81%" valign="top">
<h3 class="Item"><?php echo $item_number; ?></h3>
<p>
<?php echo "$".$price; ?>
<br />
<br />
<?php echo $desc; ?>
<br />
</p>
<form id="form1" name="form1" method="post" action="cart.php">
<input type="hidden" name="pid" id="pid" value="<?php echo $pid; ?>" />
<input class="button" type="submit" name="button" id="button" value="Add to Shopping Cart" />
</form>
</td>
</tr>
</table>

Basic Solution:
try to assign the data to arrays instead of variables like that:
while($row = $result->fetch_assoc()) {
$item_number[] = $row["item_number"];
$price[] = $row["price"];
$desc[] = $row["description"];
$category[] = $row["category"];
}
and then use a for to display your HTML table like that:
<?php
for ($i=0; $i <sizeof($item_number) ; $i++) {
?>
<table width="100%" border="2" cellspacing="0" cellpadding="15">
<tr>
<td width="19%" valign="top"><img src="pictures/inventory/<?php echo $item_number[$i]; ?>.jpg" width="142" height="188" alt="<?php echo $item_number[$i]; ?>" /><br />
View Full Size Image</td>
<td width="81%" valign="top"><h3 class="Item"><?php echo $item_number[$i]; ?></h3>
<p><?php echo "$".$price[$i]; ?><br />
<br />
<?php echo $desc[$i]; ?>
<br />
</p>
<form id="form1" name="form1" method="post" action="cart.php">
<input type="hidden" name="pid" id="pid" value="<?php echo $item_number[$i]; ?>" />
<input class="button" type="submit" name="button" id="button" value="Add to Shopping Cart" />
</form>
</td>
</tr>
</table>
<?php
}
?>
I replaced your $pid with $item_number[$i].

Assuming you are trying to show all items. You could use a function to generate the html for all the items and outside the function declare a variable containing all the html. You can then echo that anywhere in the page.
<?php
function generateTableHtml() {
$tableHtml = '<table width="100%" border="2" cellspacing="0" cellpadding="15">';
while($row = $result->fetch_assoc()) {
$tableHtml .= '<tr>';
$tableHtml .= '<td width="19%" valign="top">';
$tableHtml .= '<img src="pictures/inventory/' . $row['item_number'] . '.jpg" width="142" height="188" alt="' . $row['item_number'] . '" /><br />';
$tableHtml .= 'View Full Size Image</td>';
$tableHtml .= '<td width="81%" valign="top"><h3 class="Item">' . $row['item_number'] . '</h3>';
$tableHtml .= '<p>' . $row['price'] . '<br/><br/>';
$tableHtml .= $row['description'];
$tableHtml .= '<br />';
$tableHtml .= '</p>';
$tableHtml .= '<form id="form1" name="form1" method="post" action="cart.php">';
$tableHtml .= '<input type="hidden" name="pid" id="pid" value="'. $pid . '" />'
$tableHtml .= '<input class="button" type="submit" name="button" id="button" value="Add to Shopping Cart" />';
$tableHtml .= '</form>';
$tableHtml .= '</td>';
$tableHtml .= '</tr>';
}
$tableHtml .= '<table>';
return $tableHtml;
}
$tableHtml = generateTableHtml();
?>
<!-- the rest of your html -->
<?php echo $tableHtml; ?>

Related

HTML PHP check box

How can I style my code so that the check box can be aligned beside the table.
All of the check boxes are appearing above the table, but not beside it.
Consider the following snippet of code as well as the picture attached at the end.
<html>
<head>
<title> Furni checker </title>
</head>
<body>
<h1>Furniture Lookup / Furniture Checker</h1> <br />
<br /><p>This tool is used for removal or check furniture on a user account.</p> <br />
<h2> Instructions : </h2> <br />
<p> Lookup User ID and Lookup for the Furniture! </p>
<?php
require_once "global.php";
echo '<h1>Furniture Lookup / Furniture Checker</h1>
<br /><p>This tool is used for removal or check furniture on a user account.</p>';
echo '<br />
<form method="post">
Username:
<input type="text" name="user">
<input type="submit" value="Lookup">
</form>';
if (isset($_POST['user'])) {
$user = filter($_POST['user']);
$getUser = dbquery("SELECT * FROM users WHERE users.username = '" . $user . "' ");
while($show1 = mysql_fetch_array($getUser)) {
echo ''. $show1['id']. '';
}
}
?>
<br />
<?php
echo '
<form method="post">
ID:
<input type="text" name="id">
<input type="submit" value="Lookup">
</form>';
if (isset($_POST['id'])) {
$id = filter($_POST['id']);
$getFurniture = dbquery("SELECT public_name FROM furniture JOIN items ON items.base_item = furniture.id WHERE items.user_id = '" . $id . "' ORDER BY public_name ASC ");
echo '<table border=1 cellpadding=1 width="10%">';
while($show = mysql_fetch_array($getFurniture)) {
//echo ''. $show['public_name'].'' ;
echo '<tr class="t_row">';
echo '<input type="checkbox"> <td>' .$show['public_name']. '</td>';
echo '</tr>';
}
echo '</input>';
echo '</table>';
}
?>
Image:
Try this:
<?php
if (isset($_POST['submit']))
{
$id = filter($_POST['id']);
$getFurniture = dbquery("SELECT public_name FROM furniture JOIN items ON items.base_item = furniture.id WHERE items.user_id = '" . $id . "' ORDER BY public_name ASC ");
echo '<table border=1 cellpadding=1 width="10%">';
while($show = mysql_fetch_array($getFurniture)){
//echo ''. $show['public_name'].'' ;
echo '<tr class="t_row">';
echo '<td>' .$show['public_name']. '</td><td><input type="checkbox" /> </td>';
echo '</tr>';
}
echo '</table>';
}
?>
Let me know if it's not working.

Save Checkbox submit if check or unchecked with arrays

http://i.stack.imgur.com/TyC6U.png
I have something like this and when a person checked a checkbox and then click the button Save, it would save the checked box and the next time the person see this table it would appear the table with all the checkboxs that the person checked or unchecked last time. How can I do that?
The months are in one array, check my code:
<?php
$results = mysql_query("SELECT * FROM utilizadores");
$month = array("January","February","March","April","May","June","July","August","September","October","November","December");
echo '<form id="form" method="post" action="">';
echo '<table border="1" align="center">';
echo '<tr class="d0">';
echo '<td align="center" height="30" width="400"><strong>Nome</strong></td>';
for($i=0;$i<=11;$i++)
{
echo '<td align="center" width="80"><strong>'.#$month[$i].'</strong></td>';
}
echo '</tr>';
$rowColors = Array('#ffffff','#ffffcc'); $c= 0;
while($row = mysql_fetch_array($results)) {
echo '<tr style="background-color:'.$rowColors[$c++ % count($rowColors)].';">';
echo '<td align="center" height="30" width="400">' . $row['nome'] . "</td>";
for($i=0;$i<=11;$i++) {
echo '<td align="center" width="80"><input type="checkbox" name="asd" /></td>';
}
}
echo "</tr>";
echo "</table>";
echo "<br>";
echo '<input name="submit" type="submit" value="Save" />';
echo '</form>';
?>
<br />
</body>
</html>

Create checkboxes in php that correspond to MySQL data

I have a php script that pulls information out from my database.
$result = mysqli_query($con,"SELECT * FROM menuitem WHERE id='$id' LIMIT 1");
if (!$result) {
printf("Error: %s\n", mysqli_error($con));// Displays the error that mysql will generate if syntax is not correct.
exit();
}
//DYNAMIC PHP PULLING IN THE DATA AND SPITTING OUT THE RESULTS
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$description = $row['description'];
$picturepath = $row['picturepath'];
$name = $row['name'];
$price = $row['price'];
$keywords = $row['keywords'];
$dynamiclist = '<table align="center" width="60%" border="0" cellspacing="5" cellpadding="8">
<tr height="150"></tr>
<tr>
<td width="30% valign="top" align="center"><img style="border: #66cc33 5px solid;" src=" ' .$picturepath . '" height="200" width="200" border="1"/></td>
<td width="70%" valign="top" align="left"> <br />' . $name . ' <br /><br />$' . $price . '<br /><br /><font>Description:</font><br /> ' . $description . ' <br /><br /><br />
<form id="form1" name="form1" method="post" action="cart.php">
<input type="hidden" name="keywords" id="keywords" value=" '. $keywords .'"/>
<input type="hidden" name="pid" id="pid" value=" ' . $name . ' - $' . $price . ' "/>
<input type="submit" name="button" id="button" value="Add to Order"/>
</form>
</td>
</tr>
<tr align="center"><td>View Your Order</td></tr>
</table>';
echo $dynamiclist;
}
mysqli_close($con); //close the db connection
?>
And then I have another page where I want checkboxes created for each keyword listed. I'm not sure if I have my db colum set up correctly, but I have a column set up with multiple keywords: first, second, fifty, none, and more.
I've tried creating this, but it only displays actual "checkboxes" with no text next to any of them.
<?php
require("database.php"); //connect to the database
if(isset($_POST['keywords'])){
$pid = $_POST['keywords'];
}
$result = mysqli_query($con,"SELECT * FROM menuitem ");
if (!$result) {
printf("Error: %s\n", mysqli_error($con));// Displays the error that mysql will generate if syntax is not correct.
exit();
}
//DYNAMIC PHP PULLING IN THE DATA AND SPITTING OUT THE RESULTS
$option = '';
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$description = $row['description'];
$picturepath = $row['picturepath'];
$name = $row['name'];
$price = $row['price'];
$keywords = $row['keywords'];
$dynamiclist ="<input type='checkbox' name='checkbox' value='". $keywords ."'>";
echo $dynamiclist;
}
The goal is to have a user select an item from the first page, and on the second page, list several checkbox options for that specific item. Any help would be greatly appreciated.
If you want text next to the checkbox, you have to put it in the HTML. And if the column contains a comma-separated list, you need to split it up.
foreach(explode(',', $keywords) as $keyword) {
$keyword = trim($keyword);
echo "<input type='checkbox' name='checkbox' value='$keyword'>$keyword ";
}
Try this:
<?php
$keywords = explode(',', $keywords);
if(!empty($keywords))
{
foreach($keywords as $keyword) {
$keyword = trim($keyword);
echo "<input type='checkbox' name='checkbox' value='$keyword'>$keyword ";
}
}
?>
Thanks!

Saving data from a checkbox using PHP and MySQL

Here is my code:
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$description = $row['description'];
$picturepath = $row['picturepath'];
$name = $row['name'];
$price = $row['price'];
$keywords = $row['keywords'];
$dynamiclist = '<table align="center" width="60%" border="0" cellspacing="5" cellpadding="8">
<tr height="20"></tr>
<tr>
<td width="70%" valign="top" align="left"> <br />' . $name . ' <br /><br />$' . $price . '<br /><br />
<form id="form1" name="form1" method="post" action="cart.php">
<input type="hidden" name="pid" id="pid" value=" ' . $name . ' - $' . $price . ' - "checkbox"/>';
echo $dynamiclist;
foreach(explode(',', $keywords) as $keyword) {
$keyword = trim($keyword);
echo "<input type='checkbox' name='checkbox' value='$keyword'>$keyword <br /><br />";
}
echo '<input type="submit" name="button" id="button" value="Add to Order"/> </form> </td></tr></table>';
}
mysqli_close($con); //close the db connection
The code pulls in an item using the items id, and then creates checkboxes and keywords for that item. A user is to click on the checkbox options they want and then submit the form which sends the information to the shopping cart. The problem is that when the form is submitted, only the name and price of the item is sent over, but not the checkbox options that were clicked. Can you help me identify why? Side note. In my database I have a column "keywords" where multiple keywords are stored. // medium, well, rare, milk, apple juice, fries. Also, on the cart.php the I have:
if(isset($_POST['pid'])){
$pid = $_POST['pid'];
which posts the data from the script using the checkboxes. I'm just stuck and can't get any further.
You should have different name for your checkbox:
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$description = $row['description'];
$picturepath = $row['picturepath'];
$name = $row['name'];
$price = $row['price'];
$keywords = $row['keywords'];
$dynamiclist = '<table align="center" width="60%" border="0" cellspacing="5" cellpadding="8">
<tr height="20"></tr>
<tr>
<td width="70%" valign="top" align="left"> <br />' . $name . ' <br /><br />$' . $price . '<br /><br />
<form id="form1" name="form1" method="post" action="cart.php">
<input type="hidden" name="pid" id="pid" value=" ' . $name . ' - $' . $price . ' - "checkbox"/>';
echo $dynamiclist;
$i=0;
foreach(explode(',', $keywords) as $keyword) {
$keyword = trim($keyword);
$chkname = "checkbox{$i}";
$i = $i+1;
echo "<input type='checkbox' name='$chkname' value='$keyword'>$keyword <br /><br />";
}
echo '<input type="submit" name="button" id="button" value="Add to Order"/> </form> </td></tr></table>';
}
mysqli_close($con); //close the db connection
You need to get checkbox data in the cart.php like this:
if($_REQUEST['checkbox0']) echo $_REQUEST['checkbox0'];
if($_REQUEST['checkbox1']) echo $_REQUEST['checkbox1'];
//so on foreach checkbox
Your checkboxes need to have different names, otherwise the values are overwritten:
<input type="checkbox" name="keyword1" value="keyword1">Keyword1
<input type="checkbox" name="keyword2" value="keyword2">Keyword2
<input type="checkbox" name="keyword3" value="keyword3">Keyword3
You then access them in PHP like so:
if ( isset($_GET['keyword1']) ) {
}
if ( isset($_GET['keyword2']) ) {
}
if ( isset($_GET['keyword3']) ) {
}
A more logical way to manage the data is to pass the checkbox values as an array:
<input type="checkbox" name="keyword[]" value="keyword1">Keyword1
<input type="checkbox" name="keyword[]" value="keyword2">Keyword2
<input type="checkbox" name="keyword[]" value="keyword3">Keyword3
It then becomes much easier to work with the data in PHP;
if ( isset($_GET['keyword']) && is_array($_GET['keyword']) ) {
foreach ( $_GET['keyword'] as $v ) {
}
}

Why am I getting forms like this?

I'm trying to create insert, update and delete relate to shopping, all of them are working fine except update page.
Please see sample code of list of product from index.php page ...
<?php
$sql = "SELECT product_id, product_name, product_category, product_retail_price, product_price, product_detail FROM product";
$result_db = $db->query($sql) or die('Error perform query!');
?>
<table border="1">
<tr>
<th>product name</th>
<th>product category</th>
<th>product retail price</th>
<th>price</th>
<th>detail</th>
<th>Update</th>
<th>Delete</th>
<th>Insert</th>
</tr>
<?php
while ($r = $result_db->fetch_object()) {
$update = "update_form.php?product_id={$r->product_id}&
product_name={$r->product_name}&
product_category=$r->product_category&
product_retail_price={$r->product_retail_price}&
product_price={$r->product_price}&
product_detail={$r->product_detail}";
$delete = "delete.php?product_id={$r->product_id}";
$insert = "insert.php";
echo '<tr>';
echo '<td>' . $r->product_id . '</td>';
echo '<td>' . $r->product_name . '</td>';
echo '<td>' . $r->product_category . '</td>';
echo '<td>' . $r->product_retail_price . '</td>';
echo '<td>' . $r->product_price . '</td>';
echo '<td>' . $r->product_detail . '</td>';
echo "<td><a href='{$update}'>Update</a></td>";
echo "<td><a href='{$delete}'>Delete</a></td>";
echo "<td><a href='{$insert}'>Insert</a></td>";
echo '</tr>';
}
$db->close();
?>
</table>
</body>
As you can see above code where it said
while ($r = $result_db->fetch_object()) {
$update = ......
This sending the data of relate to product using "product_id" sending sending to the updateform.php page ... that updateform.php page is the code showing
<body>
<form action="update.php" method="post">
<input type="hidden" value="<?= $_GET['product_id'] ?>" name="product_id"/>
product name: <input type="text" name="product_name" value="<?= $_GET['product_name'] ?>">
product category: <input type="text" name="product_category" value="<?= $_GET['product_category'] ?>">
product retail price: <input type="text" name="product_retail_price" value="<?= $_GET['product_retail_price'] ?>">
product price: <input type="text" name="product_price" value="<?= $_GET['product_price'] ?>">
product detail: <input type="text" name="product_detail" value="<?= $_GET['product_detail'] ?>">
<input type="submit" name="submit">
</form>
</body>
</html>
When I run the code, the updateform.php showing the text field with <?= $_GET['xxxxxx'] ?>"
Why am I getting this result?
Is it this code?
$update = "update_form.php?product_id={$r->product_id}&
product_name={$r->product_name}&
product_category=$r->product_category&
product_retail_price={$r->product_retail_price}&
product_price={$r->product_price}&
product_detail={$r->product_detail}";
Is it the right code to get right information from database using "product_id" number? Is there better code to write than this?
Try using :
<?php echo $_GET['xxxxxx']; ?>
and if it works, it means that in your php configuration the use of short opening tags is restricted. meaning no

Categories