Checking string contents

Hi, I`m trying to write code that will check the contents of a
variable ($distance) to see if it contains the value 'ft'. If it is
true, I need it to divide the valiable $max_depth by 3.28; then write
the variable into a MYSQL database. I have tried several different
ways, but it seems that only the original (not divided by 3.28) value
keeps getting put into the database. I`m using the line below to do
the checking:

if(strcmp($distance,"ft")==0) $max_depth=$max_depth/3.28;


Does it matter where I put this line? I have it just before the
INSERT command.
James_sgp [ Di, 19 Juni 2007 08:21 ] [ ID #1742054 ]

Re: Checking string contents

..oO(James_sgp)

>Hi, I`m trying to write code that will check the contents of a
>variable ($distance) to see if it contains the value 'ft'. If it is
>true, I need it to divide the valiable $max_depth by 3.28; then write
>the variable into a MYSQL database. I have tried several different
>ways, but it seems that only the original (not divided by 3.28) value
>keeps getting put into the database. I`m using the line below to do
>the checking:
>
>if(strcmp($distance,"ft")==0) $max_depth=$max_depth/3.28;

This only checks if the value of $distance is the string 'ft':

if ($distance == 'ft') {...}

Obviously this is not enough if $distance contains a real value,
measured in foot, like '1000ft'. Try strpos() instead or a regular
expression.

Micha
Michael Fesser [ Di, 19 Juni 2007 15:24 ] [ ID #1742060 ]

Re: Checking string contents

On Jun 19, 9:24 pm, Michael Fesser <neti... [at] gmx.de> wrote:
> .oO(James_sgp)
>
> >Hi, I`m trying to write code that will check the contents of a
> >variable ($distance) to see if it contains the value 'ft'. If it is
> >true, I need it to divide the valiable $max_depth by 3.28; then write
> >the variable into a MYSQL database. I have tried several different
> >ways, but it seems that only the original (not divided by 3.28) value
> >keeps getting put into the database. I`m using the line below to do
> >the checking:
>
> >if(strcmp($distance,"ft")==0) $max_depth=$max_depth/3.28;
>
> This only checks if the value of $distance is the string 'ft':
>
> if ($distance == 'ft') {...}
>
> Obviously this is not enough if $distance contains a real value,
> measured in foot, like '1000ft'. Try strpos() instead or a regular
> expression.
>
> Micha

Micha,

Ok, my problem doesn`t seem to be my if statement. As I can display
the result and it is correct! The problem is that the variable
$max_depth isn`t updated when it is written into the database, hence
the original value is always there.
James_sgp [ Mi, 20 Juni 2007 04:24 ] [ ID #1744033 ]

Re: Checking string contents

Message-ID: <1182306258.201378.288000 [at] j4g2000prf.googlegroups.com> from
James_sgp contained the following:

>The problem is that the variable
>$max_depth isn`t updated when it is written into the database, hence
>the original value is always there.

Then you have to do the debugging all of us do. If you have
something like
$sql= "INSERT... ";

Then add
echo $sql;
to see whether the variable is making it to the insert statement



--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Geoff Berrow [ Mi, 20 Juni 2007 08:58 ] [ ID #1744036 ]

Re: Checking string contents

On Jun 20, 2:58 pm, Geoff Berrow <blthe... [at] ckdog.co.uk> wrote:
> Message-ID: <1182306258.201378.288000 [at] j4g2000prf.googlegroups.com> from
> James_sgp contained the following:
>
> >The problem is that the variable
> >$max_depth isn`t updated when it is written into the database, hence
> >the original value is always there.
>
> Then you have to do the debugging all of us do. If you have
> something like
> $sql= "INSERT... ";
>
> Then add
> echo $sql;
> to see whether the variable is making it to the insert statement
>
> --
> Geoff Berrow 0110001001101100010000000110
> 001101101011011001000110111101100111001011
> 100110001101101111001011100111010101101011

I have...
The SQL query contains the old value, while is i 'echo' the variable
before making the SQL staement it contains the new number!
James_sgp [ Mi, 20 Juni 2007 09:53 ] [ ID #1744037 ]

Re: Checking string contents

Message-ID: <1182325982.532226.144310 [at] i13g2000prf.googlegroups.com> from
James_sgp contained the following:

>On Jun 20, 2:58 pm, Geoff Berrow <blthe... [at] ckdog.co.uk> wrote:
>> Message-ID: <1182306258.201378.288000 [at] j4g2000prf.googlegroups.com> from
>> James_sgp contained the following:
>>
>> >The problem is that the variable
>> >$max_depth isn`t updated when it is written into the database, hence
>> >the original value is always there.
>>
>> Then you have to do the debugging all of us do. If you have
>> something like
>> $sql= "INSERT... ";
>>
>> Then add
>> echo $sql;
>> to see whether the variable is making it to the insert statement

>I have...
>The SQL query contains the old value, while is i 'echo' the variable
>before making the SQL staement it contains the new number!

so you've done

if(strcmp($distance,"ft")==0) $max_depth=$max_depth/3.28;
echo $max_depth;
$sql= "INSERT... $max_depth ... ";
echo $sql;

and $max_depth is different in the INSERT statement?


--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Geoff Berrow [ Mi, 20 Juni 2007 10:22 ] [ ID #1744038 ]

Re: Checking string contents

On Jun 20, 4:22 pm, Geoff Berrow <blthe... [at] ckdog.co.uk> wrote:
> Message-ID: <1182325982.532226.144310 [at] i13g2000prf.googlegroups.com> fromJames_sgpcontained the following:
>
>
>
>
>
> >On Jun 20, 2:58 pm, Geoff Berrow <blthe... [at] ckdog.co.uk> wrote:
> >> Message-ID: <1182306258.201378.288... [at] j4g2000prf.googlegroups.com> from
> >>James_sgpcontained the following:
>
> >> >The problem is that the variable
> >> >$max_depth isn`t updated when it is written into the database, hence
> >> >the original value is always there.
>
> >> Then you have to do the debugging all of us do. If you have
> >> something like
> >> $sql= "INSERT... ";
>
> >> Then add
> >> echo $sql;
> >> to see whether the variable is making it to the insert statement
> >I have...
> >The SQL query contains the old value, while is i 'echo' the variable
> >before making the SQL staement it contains the new number!
>
> so you've done
>
> if(strcmp($distance,"ft")==0) $max_depth=$max_depth/3.28;
> echo $max_depth;
> $sql= "INSERT... $max_depth ... ";
> echo $sql;
>
> and $max_depth is different in the INSERT statement?
>
> --
> Geoff Berrow 0110001001101100010000000110
> 001101101011011001000110111101100111001011
> 100110001101101111001011100111010101101011- Hide quoted text -
>
> - Show quoted text -

Geoff,
Yes you are correct...it is still the original value (ie.it seems to
not update after doing the sum!
James_sgp [ Mi, 20 Juni 2007 15:33 ] [ ID #1744043 ]

Re: Checking string contents

Message-ID: <1182346415.457377.221980 [at] a26g2000pre.googlegroups.com> from
James_sgp contained the following:

>> so you've done
>>
>> if(strcmp($distance,"ft")==0) $max_depth=$max_depth/3.28;
>> echo $max_depth;
>> $sql= "INSERT... $max_depth ... ";
>> echo $sql;
>>
>> and $max_depth is different in the INSERT statement?

>Geoff,
> Yes you are correct...it is still the original value (ie.it seems to
>not update after doing the sum!

If the first echo shows the result of the calculation, then the second
must also.

Strip the code down to the smallest snippet that still shows the
problem. If you still haven't found the answer, post it here.
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Geoff Berrow [ Mi, 20 Juni 2007 16:49 ] [ ID #1744047 ]
PHP » alt.php » Checking string contents

Vorheriges Thema: open_basedir: allowed path not recognized
Nächstes Thema: Routine for solving linear n-dimensinal equations