Jump to content

++$variable not working in { }


bambinou1980

Recommended Posts

Hello,

 

I am having a strange problem, I have one line of code that shows a variable this way:

 

   <input type="radio" name="<?php echo "action".++$number3; ?>" value="<?php echo $price1;?>" id="<?php ++$id_1; ?>" /> €<?php echo $price1;?>

 

As you can see I have added increment of 1 to 2 variables.

 

Below it I would like to echo another line as:

 
      <input type=\"radio\" name=\"action{++$number3}\" value=\"{++$price2}\" id=\"{$id_2}\" /> € {++$price2}

 

I was expecting this to work in php but it is causing a page error "Blank page" each time I add the ++ on a variable inside the echo statement.

 

Could someone explain to me why this is not allowed in PHP please? What are the other possibilities?

 

Thank you,

 

Ben

Link to comment
Share on other sites

PHP cannot do any operation on a variable within a string, it can only output the value of the variable. You must do the operation before using the variable in the string or use concatenation to perform the operation (like your first code snippet).

  <input type=\"radio\" name=\"action".(++$number3)."\" value=\"".(++$price2)."\" id=\"{$id_2}\" /> € ".(++$price2)."

NOTE: Why is $price2 being incremented twice? This will result in two completely different (price) values

$price2 = 2.50;
echo "  <input type=\"radio\" name=\"action".(++$number3)."\" value=\"".(++$price2)."\" id=\"{$id_2}\" /> € ".(++$price2) . "";

// output
  <input type="radio" name="action2" value="3.5" id="1" /> € 4.5
                                             ^                ^
                                             |                |
                                             +--- different --+
                                                   values

Also you should not name fields like action1, action2, action3 etc. You should use square brackets in the name as demonstrated in your other topic by cyberRobot/Barand

Edited by Ch0cu3r
Link to comment
Share on other sites

Also you should not name fields like action1, action2, action3 etc. You should use square brackets in the name as demonstrated in your other topic by cyberRobot/Barand

 

 

not only that, all the radio buttons in the same group must have the same name so that the browser will cause them to operate as a group of radio buttons. giving each one a different name makes them completely separate radio buttons.

 

in one of your previous threads, i suggested storing the price values separately (something you had asked about doing in an even earlier design based question), so that when you retrieve the product/price information, you could just loop to produce a radio button for each stored price. and in that earlier design thread both Barand and i (mostly him) gave you detailed help about how your data should be organized. a good data design, results in less code and less work for you, not more code and work. if you had stored the pricing based on the user type, you wouldn't even need these radio buttons.

 

based on what you are trying to output, all you should need to do is write a JOINed query for the products you want to display and the price for those products based on the 'type' of the selected user, loop over the rows that the query matches, display the product name, display that user's price for the product, and output a form input for entering the quantity for each product. instead you have threads using select/option menus that contain one value to display the product name, hard-coded logic to display a radio button for each non-null price, questions about dynamically adding form fields and using javascript/ajax. you are making this harder than it is.

Edited by mac_gyver
Link to comment
Share on other sites

Yes for the radio buttons it is deliverate.

 

Imagine form fields with 3 possible choices per line, as I did it above each inpout field will have 1 choice as the action will be identical to all 3 radio buttons. This is why I increase ++$number3; the first action then add it as $number3 in the next 2 radio buttons so the action name id increases of 1 each time but is also cpied to the next radio buttons. as:

 

 

                         <?php                         //Query all the prices                         $query_field2 = "SELECT * FROM products ORDER BY name desc";                        $result_field2 = mysqli_query($connection, $query_field2) or die (mysqli_error());                        $number1 = 0;                        $number2 = 0;                        $number3 = 0;                        $id_1 = 0;                        $id_2 = 1;                        $id_3 = 2;                         while($row_field2 = mysqli_fetch_array($result_field2)){                        $name = htmlspecialchars($row_field2['name']);                        $price1 = htmlspecialchars($row_field2['price1']);                        $price2 = htmlspecialchars($row_field2['price2']);                        $price3 = htmlspecialchars($row_field2['price3']);                        ?>       <!--Price 1-->                        <label class="btn btn-default">                        <input type="radio" name="<?php echo "action".++$number3; ?>" value="<?php echo $price1;?>" id="<?php ++$id_1; ?>" /> €<?php echo $price1;?>                        </label>                         <!--Price 1-->                         <!--Price 2-->                        <?php                        //Do not show second price if not set                       if(($price2) != null) {                        echo "<label class=\"btn btn-default\">                        <input type=\"radio\" name=\"action{$number3}\" value=\"{$price2}\" id=\"{$id_2}\" /> € {$price2}                        </label>";                        } ?>                        <!--Price 2-->                     <!--Price 3-->                     <?php                    //Do not show third price if not set                    if(($price3) != null) {                    echo "<label class=\"btn btn-default\">                    <input type=\"radio\" name=\"action{$number3}\" value=\"{$price3}\"  id=\"{$id_3}\"/> € {$price3}                     </label>";} ?>                        <!--Price 3--> 
 

 

Yes I know about your solutions but I did find Barand solutions a little bit too hard to implement for me, I am still a bit confused about relational database. So I thought about doing it this way:

 

I created a form where I can add  product name and 3 prices and add as manay product names as I want.

In the form above, I loop through this and have 1 input field per product which shows the 3 prices for each product name.

When this "add an order" form is filled up, the output is recorded is a new table called "orders".

Now if I want to retrieve the orders per customer, I can simply use a mysql short mysql query.

The only problem with this way of doing this is that I might now know how much of each product I have sold each month.....unless I send the output data of the form to a second table each time I pass a command, but if the command is deleted, the values in that new table will stay there.

 

Where I did not understand Barand and yourself is to why use relational database in this case if I can still show all the orders into a table without it, if you could explain this a little further it would be great please. As All I want is 1 admin that can add an order or list all the orders for each customer(which can be done without relational database), then have the orders of the day shown to only 1 other user(semi admin). The customers themselves will not access the database as it is internal to a small factory.

 

 

So instead I thought about doing it in an easier way by increase the first variables of ++ for price one and repeating it for price 2 and 3 for each line(which works) when I have this type of code:

 <input type="radio" name="<?php echo "action".++$number3; ?>" value="<?php echo $price1;?>" id="<?php ++$id_1; ?>" /> €<?php echo $price1;?>

 

But all I wanted to try is why I cannot add my ++ in an echo(Remember this is just as a trial as I am trying to understand more of php):

 

echo "<label class=\"btn btn-default\">                        <input type=\"radio\" name=\"action{++$number}\" value=\"{$price2}\" id=\"{$id_2}\" /> € {$price2}                        </label>";
 

 

So quickly, in one case ++$variable works but in the other case {++$variable} does not work, I find it weird.....as the second solution should work also no?

Edited by bambinou1980
Link to comment
Share on other sites

Sorry here is the updated code:

 

                         <?php                         //Query all the prices                         $query_field2 = "SELECT * FROM products ORDER BY name desc";                        $result_field2 = mysqli_query($connection, $query_field2) or die (mysqli_error());                        $number1 = 0;                        $number2 = 0;                        $number3 = 0;                        $id_1 = 0;                        $id_2 = 1;                        $id_3 = 2;                         while($row_field2 = mysqli_fetch_array($result_field2)){                        $name = htmlspecialchars($row_field2['name']);                        $price1 = htmlspecialchars($row_field2['price1']);                        $price2 = htmlspecialchars($row_field2['price2']);                        $price3 = htmlspecialchars($row_field2['price3']);                        ?>                        <div class="form-inline well">                        <label for="product1_id">Choose Product <?php echo ++$number1; ?></label>                        <div class="form-group">                        <input name="<?php echo "product".++$number2; ?>" type="text" class="form-control" value="<?php echo $name; ?>" disabled>                          <!--Price 1-->                        <label class="btn btn-default">                        <input type="radio" name="<?php echo "action".++$number3; ?>" value="<?php echo $price1;?>" id="<?php ++$id_1; ?>" /> €<?php echo $price1;?>                        </label>                         <!--Price 1-->                         <!--Price 2-->                         <?php                         //Do not show second price if not set                        if(($price2) != null) {                        echo "<label class=\"btn btn-default\">                        <input type=\"radio\" name=\"action{$number3}\" value=\"{$price2}\" id=\"{$id_2}\" /> € {$price2}                        </label>";                         } ?>                        <!--Price 2-->                         <!--Price 3-->                         <?php                        //Do not show third price if not set                        if(($price3) != null) {                        echo "<label class=\"btn btn-default\">                        <input type=\"radio\" name=\"action{$number3}\" value=\"{$price3}\"  id=\"{$id_3}\"/> € {$price3}                        </label>";                        } ?>                        <!--Price 3--> 
 

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.