Simple Problem...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Simple Problem...

Post by Joe »

I have an order form which uses checkboxes to identify if the product was selected or not. However my code does not seem to work:

Code: Select all

$sql = "SELECT * FROM dbproducts";
$result = mysql_query($sql) or die(mysql_error());

while (true)
{
 $row = mysql_fetch_assoc($result);
 if ($row == false) break;
 $val = $row['product'];
 $val1 = $_POST[$val];

 if ($val1 > '0')
 {
  print("<input type='hidden' name='$val' value='$val1'>");
 }
}
The checkbox value is set to 0 on each product by default and the script I wrote tests whether the checkbox value is more than 0. Instead of displaying the product, it displays nothing therefore the value is not passing 0.

Any help appreciated :D
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

1 - What is the form like? (<form action...>...<input...>...</form>)

2 - What does print_r($_POST); return?

3 - Please give us an example of $row['product'] at one iteration.

Thanks,
Scorphus.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Simple Problem...

Post by timvw »

Joe wrote:

Code: Select all

$sql = "SELECT * FROM dbproducts";
$result = mysql_query($sql) or die(mysql_error());
I presume you have a valid mysql connection. (Generally, acquired via mysql_connect and mysql_select_db)
Joe wrote:

Code: Select all

while (true)
Why do you repeat this loop forever?

I think you want while ($row = mysql_fetch_assoc($result))
Joe wrote:

Code: Select all

if ($val1 > '0')
Are you sure you don't want $val1 > 0 ?
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

It is not repeating forever, just look at this line:

Code: Select all

<?php
//...
 if ($row == false) break;
//...
?>
Of course this isn't the best way since the compiler tests both the while and the if conditions instead of just the while one, and it is time consuming. Ok, you must show him this, but don't make he think he's doing all wrong.

-- Scorphus
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Imho, for the sake of clarity, it is wrong. And Tanenbaum agrees with me ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

agreed, using an infinite loop set up is kinda dangerous.. and a bit wasteful of cpu in this case ;), well in most cases :D
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Fine, now he knows what is wrong, or _not_right_ following good-programming concepts.

-- Scorphus
Post Reply