Page 1 of 1

PHP Order Forms From While Loop .. I Need Help

Posted: Tue Nov 15, 2011 7:52 am
by ronnimallouk
Hi All. Im making a website for a customer and have tried everything to get this working the way he wants it. Ive got a while loop getting products for sale from a sql database. i have a textbox in each product bar. what he needs is the text box used for entering quantity and only one button on the bottom of the page that takes you to a review page with a list of what items have a quantity of 1 or more, To eventually be sent to 3 emails.

I need help doing this at the moment i have textboxes that dont do nothing.
Heres the code and a current screenshot of the page please please help ive been 2 weeks trying to figure this out.

Code: Select all

<?php
$qty = 0
?> 

<?php 
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>

<!-- ONE --------------------------------------------------------------------------------------------------->
<?php 
include "storescripts/connect_to_mysql.php"; 
$list = "";
$sql = mysql_query("SELECT * FROM food ORDER BY id ASC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
	while($row = mysql_fetch_array($sql)){ 
                                     $id = $row["id"];
		     $product_code = $row["product_code"];
		     $description = $row["description"];
		     $units_per_product = $row["units_per_product"];
		     $product_price = $row["product_price"];
		     $unit_price = $row["unit_price"];
		     $points = $row["points"];
		     $subcategory = $row["subcategory"];
		     $list .= '<body topmargin="0" leftmargin="0">

<div align="center">
	<table cellpadding="0" cellspacing="0" border="0" bordercolor="#C0C0C0" width="768" height="45">
		<!-- MSTableType="layout" -->
		<tr>
			<td valign="middle" width="88" style="border-left-style: solid; border-left-width: 1px; border-top-style: solid; border-top-width: 1px; border-bottom-style: solid; border-bottom-width: 1px; border-right-style:none; border-right-width:medium">
			<font face="Verdana" size="1">&nbsp; ' .$product_code. '</font></td>
			<td valign="middle" width="245" style="border-left-style: none; border-left-width: medium; border-top-style: solid; border-top-width: 1px; border-bottom-style: solid; border-bottom-width: 1px; border-right-style:none; border-right-width:medium">
			<font face="Verdana" size="2">' .$description. '</font></td>
			<td valign="middle" width="118" style="border-left-style: none; border-left-width: medium; border-top-style: solid; border-top-width: 1px; border-bottom-style: solid; border-bottom-width: 1px; border-right-style:none; border-right-width:medium">
			<p align="center"><font face="Verdana" style="font-size: 8.5pt">$' .$units_per_product. '</font></td>
			<td valign="middle" width="79" style="border-left-style:none; border-left-width:medium; border-right-style:none; border-right-width:medium; border-top-style:solid; border-top-width:1px; border-bottom-style:solid; border-bottom-width:1px">
			<p align="left"><font face="Verdana" style="font-size: 8.5pt">&nbsp; $' .$unit_price. '			</font></td>
			<td valign="middle" width="93" style="border-top-style: solid; border-top-width: 1px; border-bottom-style: solid; border-bottom-width: 1px; border-left-style:none; border-left-width:medium; border-right-style:none; border-right-width:medium">
			<p align="left"><font face="Verdana" style="font-size: 8.5pt">&nbsp; $' .$product_price. '</font></td>
			<td width="56" style="border-left-style:none; border-left-width:medium; border-right-style:none; border-right-width:medium; border-top-style:solid; border-top-width:1px; border-bottom-style:solid; border-bottom-width:1px" valign="middle">
			<p align="right">&nbsp;&nbsp;&nbsp;<font face="Verdana" size="1">Qty:&nbsp;&nbsp;&nbsp; </font></td>
			<td height="45" width="87" style="border-left-style: none; border-left-width: medium; border-right-style: solid; border-right-width: 1px; border-top-style: solid; border-top-width: 1px; border-bottom-style: solid; border-bottom-width: 1px">
			

<form><p align="center">
	<input name="' .$id. '" size="1" value="' .$qty. '" style="float: left">
	</form></p>	</td>
			</tr>
	</table>
	</div>

<div align="center">
	<table cellpadding="0" cellspacing="0" width="779" height="3">
				<!-- MSTableType="layout" -->
				<tr>
	<td width="779" height="3"></td>
				</tr>
			</table>

	</div>';
	
    }
	
	
} else {
	$list = "";
}

mysql_close();
?>

Screenshot:
http://www.malloukcompany.com.au/1.png

Image

Re: PHP Order Forms From While Loop .. I Need Help

Posted: Tue Nov 15, 2011 8:22 am
by Celauran
It looks like you've got one form per row, no action, method, or submit buttons for your form, and no form processing code. I'd start by placing the table inside the form rather than having many forms inside the table. Post the form and loop through the resulting $_POST array to find out which items have been ordered and in which quantities.

Re: PHP Order Forms From While Loop .. I Need Help

Posted: Tue Nov 15, 2011 8:44 am
by ronnimallouk
Celauran wrote:It looks like you've got one form per row, no action, method, or submit buttons for your form, and no form processing code. I'd start by placing the table inside the form rather than having many forms inside the table. Post the form and loop through the resulting $_POST array to find out which items have been ordered and in which quantities.
Can you explain to me where exactly do i put the form tags becuase ive tried to put the table inside the form tags and kept gettin problems.
Can you please explain in a little bit more detail on how to do it?

Re: PHP Order Forms From While Loop .. I Need Help

Posted: Tue Nov 15, 2011 9:04 am
by Celauran
Quick and untested, but this should set you on the right path.

Code: Select all

<?php

// If the form has been posted, process it.
if (!empty($_POST))
{
    foreach ($_POST as $k => $v)
    {
        echo "Item: $k<br />";
        echo "Quantity: $v<br /><br />";
    }
}
// Otherwise, display the order form
else
{
    $query = "SELECT product_code, description, units_per_product, unit_price, product_price
              FROM food
              ORDER BY id ASC";
    $products = mysql_query($sql);
}

?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Debug</title>
    </head>
    <body>
        <?php if (isset($products) && $products !== FALSE): ?>
        <form id="frmPurchase" action="" method="post">
            <table>
                <tr>
                    <th>Code</th>
                    <th>Description</th>
                    <th>Units Per</th>
                    <th>Unit Price</th>
                    <th>Product Price</th>
                    <th>Qty</th>
                </tr>
                <?php while ($row = mysql_fetch_assoc($products)): ?>
                <tr>
                    <td><?php echo $row['product_id'];        ?></td>
                    <td><?php echo $row['description'];       ?></td>
                    <td><?php echo $row['units_per_product']; ?></td>
                    <td><?php echo $row['unit_price'];        ?></td>
                    <td><?php echo $row['product_price'];     ?></td>
                    <td>
                        <input type="text" name="<?php echo $row['product_id']; ?>" />
                    </td>
                </tr>
                <?php endwhile; ?>
                <tr>
                    <td colspan="6">
                        <input type="submit" value="Place Order" />
                    </td>
                </tr>
            </table>
        </form>
        <?php endif; ?>
    </body>
</html>

Re: PHP Order Forms From While Loop .. I Need Help

Posted: Tue Nov 15, 2011 9:23 am
by ronnimallouk
Celauran wrote:Quick and untested, but this should set you on the right path.

Code: Select all

<?php

// If the form has been posted, process it.
if (!empty($_POST))
{
    foreach ($_POST as $k => $v)
    {
        echo "Item: $k<br />";
        echo "Quantity: $v<br /><br />";
    }
}
// Otherwise, display the order form
else
{
    $query = "SELECT product_code, description, units_per_product, unit_price, product_price
              FROM food
              ORDER BY id ASC";
    $products = mysql_query($sql);
}

?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Debug</title>
    </head>
    <body>
        <?php if (isset($products) && $products !== FALSE): ?>
        <form id="frmPurchase" action="" method="post">
            <table>
                <tr>
                    <th>Code</th>
                    <th>Description</th>
                    <th>Units Per</th>
                    <th>Unit Price</th>
                    <th>Product Price</th>
                    <th>Qty</th>
                </tr>
                <?php while ($row = mysql_fetch_assoc($products)): ?>
                <tr>
                    <td><?php echo $row['product_id'];        ?></td>
                    <td><?php echo $row['description'];       ?></td>
                    <td><?php echo $row['units_per_product']; ?></td>
                    <td><?php echo $row['unit_price'];        ?></td>
                    <td><?php echo $row['product_price'];     ?></td>
                    <td>
                        <input type="text" name="<?php echo $row['product_id']; ?>" />
                    </td>
                </tr>
                <?php endwhile; ?>
                <tr>
                    <td colspan="6">
                        <input type="submit" value="Place Order" />
                    </td>
                </tr>
            </table>
        </form>
        <?php endif; ?>
    </body>
</html>
Thanks a million for your time buddy i really appreciate it, ill go through it and try it. ill let you know how i go.

Re: PHP Order Forms From While Loop .. I Need Help

Posted: Tue Nov 15, 2011 9:24 am
by Celauran
Just noticed a fairly obvious typo.

Code: Select all

 $products = mysql_query($sql);
should be:

Code: Select all

 $products = mysql_query($query);

Re: PHP Order Forms From While Loop .. I Need Help

Posted: Wed Nov 16, 2011 9:18 am
by ronnimallouk
Celauran wrote:Just noticed a fairly obvious typo.

Code: Select all

 $products = mysql_query($sql);
should be:

Code: Select all

 $products = mysql_query($query);
Thanks yeah i did notice that.

& thanks it worked, what im trying to do is have only the products with a qty of 1 or more show up when submitted also i want to have more information of each product come up when submitted. i notice that you have a key of the id and value of the qty. i cant seem to figure out how to have other information showing such as product price and total price.

this is what comes up when i submit the form.
Image

i want each items other info to show up too and total price depending on qty.

& if its not too much trouble can you explain how the code you show me works?

Im sorry i know im asking for too much but no one else seems to be helping out but you. So thanks again i look forward to your reply.

Re: PHP Order Forms From While Loop .. I Need Help

Posted: Wed Nov 16, 2011 9:30 am
by Celauran
Something like this?

Code: Select all

<?php

// If the form has been posted, process it.
if (!empty($_POST))
{
    foreach ($_POST as $k => $v)
    {
        if ($v > 0)
        {
            //  This is pretty messy, but it prevents having to query the database again
            $details = explode('|', $k);
            echo "Item: {$details[0]}<br />";
            echo "Description: {$details[1]}<br />";
            echo "Price: {$details[2]}<br />";
            echo "Quantity: {$v}<br /><br />";
        }
    }
}
// Otherwise, display the order form
else
{
    $query = "SELECT product_code, description, units_per_product, unit_price, product_price
              FROM food
              ORDER BY id ASC";
    $products = mysql_query($sql);
}

?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Debug</title>
    </head>
    <body>
        <?php if (isset($products) && $products !== FALSE): ?>
        <form id="frmPurchase" action="" method="post">
            <table>
                <tr>
                    <th>Code</th>
                    <th>Description</th>
                    <th>Units Per</th>
                    <th>Unit Price</th>
                    <th>Product Price</th>
                    <th>Qty</th>
                </tr>
                <?php while ($row = mysql_fetch_assoc($products)): ?>
                <tr>
                    <td><?php echo $row['product_id'];        ?></td>
                    <td><?php echo $row['description'];       ?></td>
                    <td><?php echo $row['units_per_product']; ?></td>
                    <td><?php echo $row['unit_price'];        ?></td>
                    <td><?php echo $row['product_price'];     ?></td>
                    <td>
                        <input type="text" name="<?php echo "{$row['product_id']}|{$row['description']}|{$row['product_price']}"; ?>" />
                    </td>
                </tr>
                <?php endwhile; ?>
                <tr>
                    <td colspan="6">
                        <input type="submit" value="Place Order" />
                    </td>
                </tr>
            </table>
        </form>
        <?php endif; ?>
    </body>
</html>
I'll leave the styling up to you.

Re: PHP Order Forms From While Loop .. I Need Help

Posted: Wed Nov 16, 2011 9:34 am
by ronnimallouk
Mate u r a legend, thanks for replying so quickly. Ill test this and let you know how i go.

Re: PHP Order Forms From While Loop .. I Need Help

Posted: Wed Nov 16, 2011 9:34 am
by ronnimallouk
Mate u r a legend, thanks for replying so quickly. Ill test this and let you know how i go.