Jump to content

[SOLVED] Need help with some php loop coding


jlgray48

Recommended Posts

I have created a database named PHP012 and a table within the database named Inventorytable.

The inventorytable has 4 fields named ItemID ItemName Itemprice ItemQuantity.

I have created an html form with 3 checkboxes named Item1, Item2, Item3 and values of 1, 2, 3.

The Html form passes the checkboxes that are checked to a php file named order.php.

The php file is to take the passed data and update the Itemquantity if the box was checked.

I am having problems creating my loops. That may not be needed just let me know if that is the case.

What I'm trying to do in the code below is start for loop from 1 to 3 to check all 3 possible passed values.

If the value is null or doesn't exist then it want enter the while loop for processing. I don't know the code for null or empty so I put not empty in the code.

 

<?php

 

$Item1 = $_REQUEST[item 1];

$Item2 = $_REQUEST[item 2];

$Item3 = $_REQUEST[item 3];

 

$conn=mysql_connect("127.0.0.1", "odbc", "") ;

mysql_select_db("php012",$conn);

 

For ($i = 1; $i <=3 ; $i+=1) {

 

    While $Item.$i (not empty) {

 

    Update inventorytable set Itemquantity = 'Itemquantity -1' where ItemID = $Item.$i ;

}

 

 

 

Link to comment
Share on other sites

As in the other thread:

<?php
$Item1 = $_REQUEST[item 1];
$Item2 = $_REQUEST[item 2];
$Item3 = $_REQUEST[item 3];

$conn=mysql_connect("127.0.0.1", "odbc", "") ;
mysql_select_db("php012",$conn);

For ($i = 1; $i <=3 ; $i+=1) {

     While $Item.$i (not empty) {

     Update inventorytable set Itemquantity = 'Itemquantity -1' where ItemID = $Item.$i ;
}
?>

 

Try:

<?php
$Item1 = $_REQUEST[item 1];
$Item2 = $_REQUEST[item 2];
$Item3 = $_REQUEST[item 3];

$conn=mysql_connect("127.0.0.1", "odbc", "") ;
mysql_select_db("php012",$conn);

For ($i = 1; $i <=3 ; $i++)
{
     While ($Item.$i == FALSE)
     {
          Update inventorytable set Itemquantity = 'Itemquantity -1' where ItemID = $Item.$i ;
     }
}
?>

Link to comment
Share on other sites

Ok, I can see how I've confused you. I have passed from an html file ITEM1, ITEM2 and ITEM3 and stored them in $ITEM1 $ITEM2 and $ITEM3. I am creating a loop to see if they are empty if they aren't empy I want the sql query to run. My problem is withing the $item.$i I'm tryint to combine the 2 to create the variable above. I need to check $Item[$i] and I can't figure it out. I may have confused you more.

 

 

 

 

Link to comment
Share on other sites

How about something like this:

 

<?php

$conn = mysql_connect("127.0.0.1", "odbc", "") or die("ERROR: Cannot connect to database");
mysql_select_db("php012",$conn) or die("ERROR: Cannot select database");

for ($i = 1; $i <=3 ; $i++){
$item = $_REQUEST['Item ' . $i];
echo "$item (just checking)<br><br>";  // Remove this line later, just to check the variables coming through
     
 if(!empty($item))
     {
          mysql_query("UPDATE inventorytable SET Itemquantity = Itemquantity - 1 WHERE ItemID = '$item'") or die(mysql_error());
     }
}
?>

 

The only thing im not really sure of there is the syntax of the UPDATE query where its subtracting

Link to comment
Share on other sites

Ok, I can see how I've confused you. I have passed from an html file ITEM1, ITEM2 and ITEM3 and stored them in $ITEM1 $ITEM2 and $ITEM3. I am creating a loop to see if they are empty if they aren't empy I want the sql query to run. My problem is withing the $item.$i I'm tryint to combine the 2 to create the variable above. I need to check $Item[$i] and I can't figure it out. I may have confused you more.

 

No, I think I understand. I still want you to try the following:

<?php
$Item1 = $_REQUEST[item 1];
$Item2 = $_REQUEST[item 2];
$Item3 = $_REQUEST[item 3];

for ($i = 1; $i <= 3 ; $i++)
{
    echo $Item.$i . "<br />";
} 
?>

 

I would like to see what your results are. Based on those results, I may be able to help you more.

Link to comment
Share on other sites

Oh, OK, then try this:

<?php
for ($i = 1; $i <= 3 ; $i++)
{
    $Item = $_REQUEST['Item'.$i];
    echo $Item."<br />";
}
?>

 

If the above worked, try:

<?php
for ($i = 1; $i <= 3 ; $i++)
{
    $Item = $_REQUEST['Item'.$i];

    if ($Item == FALSE)
    {
        $conn=mysql_connect("127.0.0.1", "odbc", "") ;
        mysql_select_db("php012",$conn);
        
        Update inventorytable set Itemquantity = 'Itemquantity -1' where ItemID = $Item;
    }
}
?>

Link to comment
Share on other sites

Yeah, 1 is the same as saying TRUE or isset($variable) or the list goes on...

 

If the last script worked backwards, use this:

<?php
for ($i = 1; $i <= 3 ; $i++)
{
    $Item = $_REQUEST['Item'.$i];

    if ($Item == TRUE)
    {
        $conn=mysql_connect("127.0.0.1", "odbc", "") ;
        mysql_select_db("php012",$conn);
        
        Update inventorytable set Itemquantity = 'Itemquantity -1' where ItemID = $Item;
    }
}
?>

Link to comment
Share on other sites

If you're checking for a TRUE statement, you can omit the == TRUE as it is assumed within PHP if statements.

 

if ($someVar == TRUE)

Is the same as saying

if ($someVar)

True!

 

jlgray48 recently made a different thread asking how to say not empty.

 

And I think he is finally realizing that we weren't joking about how many ways to say that in php!

 

Not Empty equals:

if ($variable)

if (isset($variable))

if ($variable == 1)

if ($variable != 2)

if ($variable == TRUE)

if ($variable != FALSE)

if (!empty($variable))

 

Should I continue!? lol

Link to comment
Share on other sites

OK, it is done. Boy did I make this way too hard.

 

THANKS A MILLION FOR EVERYONES HELP.

 

<?php

 

$conn=mysql_connect("127.0.0.1", "odbc", "") ;

mysql_select_db("php012",$conn);

 

for ($i = 1; $i <=3 ; $i++)

{

    $item = $_REQUEST['Item'.$i];

    If ($item == true) {

    echo "$item";

    $sql = "Update inventorytable set Itemquantity = Itemquantity -1 where ItemID = $item;";

    mysql_query($sql,$conn);

}

}

 

?>

Link to comment
Share on other sites

Yes I would :)  Of course you will have to adapt the code below for your own database structure, but it should give you an idea...  Im also assuming short tags are enabled....

 

Basically, you just extract the option values and names using the while() function

 

<select name="selectMenu">
<? 	// db connection info goes here
$r = mysql_query("SELECT * FROM tablename WHERE somefield = '$somevalue'") or die(mysql_error());
	while($row = mysql_fetch_array($r)){
		$field_id = $row['field_id'];
		$field_name = $row['field_name'];  ?>
<option value="<?=$field_id?>"><?=$field_name?>
<?		}  ?>
</select>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.