![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Programmer
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4
![]() |
I can't get my head round why this isnt working????
<?php
header("Cache-control: private"); //IE 6 Fix
error_reporting(E_ALL);
include 'connect.php';
$query = "SELECT * FROM product";
$result = mysql_query($query);
$num = mysql_numrows($result);
mysql_close();
$text = array("$num");
?>
<form action="cbox.php" method="get" name="myForm">
<input name="ButtonWidget" type="submit" value="Submit"><br/>
<?
$i=0;
while ($i < $num)
{
$productID=mysql_result($result,$i,"productID");
$prodName=mysql_result($result,$i,"prodName");
$text[$i] = "$productID";
echo $text[$i];
?>
Checkbox <?php echo $prodName;?> <input name="CheckBox<?php echo $productID;?>" type="checkbox" value="<?php echo $productID;?>"><br/>
</form>
</body>
</html>
<?
$i++;
}
echo "Boxes checked: ";
for ($i = 0; $i < $num; $i++)
{
if (!empty ($_GET ["CheckBox$productID"]))
{
echo $productID;
}//if
}
echo "<br/>";
?> |
|
|
|
|
|
#22 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Look, you're getting a lot of help here. The least you could do is say WHAT isn't working or WHAT your code is doing or not doing that isn't up to your expectations. If you don't know as much about what the contents of your variables look like as you'd like to know, USE ECHO OR PRINT_R. Gather some information. Present it to those you're asking for help. Information is key to debugging. Put some effort on YOUR side, here. Of course, you're frustrated. Get over it and try to help your helpers.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#23 |
|
Programmer
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4
![]() |
Sorz, your right.
Ok, so my array is working correctly as echo $text[$i]; Its my for loop and something to do with the GET method. When i check the boxes and hit submit nothing happens. My code is <?php
header("Cache-control: private"); //IE 6 Fix
error_reporting(E_ALL);
include 'connect.php';
$query = "SELECT * FROM product";
$result = mysql_query($query);
$num = mysql_numrows($result);
mysql_close();
$text = array("$num");
?>
<form action="cbox.php" method="get" name="myForm">
<input name="ButtonWidget" type="submit" value="Submit"><br/>
<?
$i=0;
while ($i < $num)
{
$productID=mysql_result($result,$i,"productID");
$prodName=mysql_result($result,$i,"prodName");
$text[$i] = "$productID";
echo $text[$i];
?>
Checkbox <?php echo $prodName;?> <input name="CheckBox<?php echo $productID;?>" type="checkbox" value="<?php echo $productID;?>"><br/>
</form>
</body>
</html>
<?
$i++;
}
echo "Boxes checked: ";
for ($i = 0; $i < $num; $i++)
{
if (!empty ($_GET ["CheckBox$productID"]))
{
echo $productID;
}//if
}
echo "<br/>";
?>The page im working on is www.lingan.co.uk/cbox.php |
|
|
|
|
|
#24 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
View your source and look at the HTML. Probably your jaw will drop. I'd also suggest you process the 'get' array at the top of the code, before you dink with the page and checkbox contents again, but I don't really know what you want to do with the information.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#25 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I don't know exactly what you are trying to accomplish, and of course I don't have your database. I have taken the liberty of making a small database containing product id and product name. It contains three items. When the page is first accessed, it is in "Catalog" mode and presents all three items with a checkbox for each. When you select items and click on "Select" the request is tendered again with the selected items being sent to the server via the "Get" method. The server responds and resends the page in "Cart" mode, showing only those items which were selected. It removes the "Select button and adds a second form with a "Catalog" button that submits via the "Post" method. This means that on submissions of the "Post" type, the "Get" array will be empty, and the page will perform as from the beginning. You may see this here. The code is:
[php] <?php require_once ("functions.php"); $secondRequest = count ($_GET); // Open the database demoConnect (); $queryProducts = "SELECT ProductID, ProductName FROM Demonstration ORDER BY ProductID"; $catalog = mysql_query ($queryProducts) or die (mysql_error()."product query"); if ($secondRequest) $pageType = 'Cart'; else $pageType = 'Catalog'; $title = '<p style="text-align: center; font-weight: bold; font-size: larger; padding: 5px;">'; $title .= $pageType; $title .= '</p>'; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <style type="text/css"> td { border: 1px solid #000000; padding: 10px; } .products { border: 1px solid #000000; } .head { font-weight: bold; text-align: center; } .num { text-align: right; } .name { text-align: left; } .box { text-align: center; } </style> <title>Checkbox Check</title> </head> <body> <?php echo $title;?> <form action="cbox.php" method="get" name="productSelect"> <table class="products" align="center" width="40%"> <tr> <td class="head">Product ID</td> <td class="head">Product Name</td> <td class="head">Select</td> </tr> <?php $count = 0; $catalog = mysql_query ($queryProducts) or die (mysql_error()."product query"); while ($rowData = mysql_fetch_array ($catalog)) { $box = 'CB'.$count; if (!empty ($_GET [$box])) $checked = 'checked'; else $checked = ''; if (!$secondRequest || $checked) { echo '<tr>'; echo '<td class="num">'.$rowData ['ProductID'].'</td>'; echo '<td class="name">'.$rowData ['ProductName'].'</td>'; echo '<td class="box"><input name="CB'.$count.'" type="checkbox" '; echo $checked.'></td>'; echo '</tr>'; } $count++; } ?> </table> <?php if (!$secondRequest) echo <<< qqqq <div align="center" style="padding: 20px;"> <input name="pSelect" type="submit" value="Select"> </div> </form> qqqq; else echo <<< qqqq </form> <form action="cbox.php" method="post" name="showCatalog"> <div align="center" style="padding: 20px;"> <input name="pReturn" type="submit" value="Catalog"> </div> </form> qqqq; ?> </body> </html> [/php] Perhaps this will give some ideas on the various ways to accomplish these kinds of interactions.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#26 |
|
Programmer
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4
![]() |
Thanks Dawei.
Thats helped a lot! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|