Jump to content

Why Does my For Loop eliminate the decimals in a Variable ?


vincej

Recommended Posts

Hi - To me, this doesn't make sense.  My Order form uploads product weights in an array . If I echo out those weights they are fine ie 25.25

 

However, when I try to update the DB for some reason, the for loop strips out all but the first digit, so 25.25 becomes '2' or indeed if I had entered 9999999 it just '9' is entered in to the DB.

 

I have tried using number_format to pre-process the decimals, - but I get an error saying you can not use it in a write format.

 

Many Thanks to anyone who can help with this  !!

 

 

PS - I have abbreviated the below code for clarity:

 

$weight = $_POST['weight']; 

        for ($i = 0; $i < $numloops ; $i++)

      {
       $sql ="
UPDATE `confirmedorder` 
SET orderid = '$orderid[$i]', weight='$weight[$i]',status='finished', ordervalue='$ordervalue' 
WHERE customerid = $customerid
";
$this->db->query($sql); 
	}



Link to comment
Share on other sites

You're sending it to the DB as a string. Remove the single quotes around the variable.

 

Edit: Or, your weight is not an array of numbers but a string, so you're getting each digit separately.

print_r($weight); before the loop, what do you get?

 

Link to comment
Share on other sites

Thanks jesirose ,  Nope unfortunately it made no difference. Here is the whole code unabbreviated.

 

You will notice that I have a couple of tests.  Test One where there is no [$i] echos out with the full decimals. Test two with [$i] strips the decimals.

 

 



    function finishedorder(){  
$prodname = $_POST['prodname'];
$prodid =$_POST['prodid'];
$quantity = $_POST['quantity'];
$pricelb = $_POST['pricelb']; 
$customerid = $_POST['customerid']; 
$price = $_POST['price']; 
$weight = $_POST['weight']; 
$customerid = $_POST['customerid']; 
$orderid = $_POST['orderid']; 

$numloops = count($_POST['prodid']);

for ($i = 0; $i < $numloops ; $i++) {

$pricelb = $pricelb[$i]; 
$quantity = $quantity[$i];
$weight = $weight[$i]; 

echo "weight test One  ". $weight ."<br/>";               //TESTS 
echo "weight Test Two  " . $weight[$i] . "<br/>";
echo "ordervalue ". $ordervalue = $pricelb * $weight . "<br/>";


$sql ="
UPDATE `confirmedorder` 
SET orderid = '$orderid[$i]', weight=$weight[$i],status='finished', ordervalue='$ordervalue' 
WHERE customerid = $customerid
";
$this->db->query($sql); 
	}
}



Link to comment
Share on other sites

$weight = $weight[$i]; 

...

$sql ="
UPDATE `confirmedorder` 
SET orderid = '$orderid[$i]', weight=$weight[$i],status='finished', ordervalue='$ordervalue' 
WHERE customerid = $customerid
";

 

This explains your error.

 

You didn't answer my question but the problem is that you are taking your array and getting the value, then treating that value like an array.

Link to comment
Share on other sites

To elaborate, PHP allows you to use array notation in strings to get a specific character, starting at 0.

 

<?php

$string = 'abcde';

echo $string[2]; // outputs 'c'

echo $string[4]; // outputs 'e'

?>

 

http://php.net/manual/en/language.types.string.php

String access and modification by character

 

Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose. The functions substr() and substr_replace() can be used when you want to extract or replace more than 1 character.

 

Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose.

Link to comment
Share on other sites

jesirose Thank you for your help ! sorry - I didn't see your question.  It works !!

 

the problem is that you are taking your array and getting the value, then treating that value like an array.

 

So I removed the array brackets off  $weight from the Update statement - I case of Male Onset Blindness !

 

Many Thanks

 

 

 

Thanks  xyph  - I didn't know that !  But in my case all seems to be working well - it is grabbing the whole string .... I wonder why ?

Link to comment
Share on other sites

But in my case all seems to be working well - it is grabbing the whole string .... I wonder why ?

No, it's not...that's the entire thing you're complaining about, and you just said that when you removed the brackets, it works. Before it was converting your number to a string and getting one character of it, which is what he described. Now, it's not...

Link to comment
Share on other sites

It was always a string. Using square brackets on an integer or float returns nothing.

 

You should also change your column back to decimal data type. It's more accurate when dealing with currencies.

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.