Re: whats wrong in this program.
| From: | babu | Date: | Wed, 14 Sep 2005 11:36:58 +0000 |
| Subject: | Re: whats wrong in this program. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-222380@lists.php.net to get a copy of this message | ||
Hi,
I tried to use the final array values in a insert statement, but the values are not inserted.
the code is
foreach ($final as $subnum){
$res = $db->query("INSERT INTO
substrate_protocoll(substrate_type,substrate_num,operator,location,solvent,ultrasonic,duration,cdate,ctime,comment)
VALUES('$substrate_type1',$subnum,'$operator','$location','$solvent',$uv,$duration,'$cdate','$sctime','$comment')");
if(!$res){
echo "insert failed";
}
}
the values of array ($subnum)are not inserted , can you tell me where the problem is.
Jordan Miller <jmil@hymiegladstone.com> wrote:
I think I finally understand what you are trying to do. I don't see
any reason why you need to use the token functions, and I would
recommend using array functions instead (also, it is exceedingly easy
to sort the elements of an array... see the end).
I believe this will do what you are trying to do:
//Tokenizer for Babu
$str = '10,12,14-18';
$commas = explode(',', $str); // $commas will be an array of three
items in this case
// Final Values will go into the $final array
$final = array();
foreach ($commas as $value) {
// If one of the $commas elements contains a dash, we need to
get the range between them!
if (strstr($value, '-')) {
// Explode based on the dash. This code assumes there will
only be a single dash
$rangeValues = explode('-', $value);
foreach (range($rangeValues[0], $rangeValues[1]) as $number) {
$final[] = $number;
}
} else {
// If $value does not contain a dash, add it directly to the
$final array
$final[] = $value;
}
}
echo "All your values in the range $str are ".implode(' ', $final);
// Prints "All your values in the range 10,12,14-18 are 10 12 14 15
16 17 18"
In your last email, you had some of the values given out of order:
1. 20,21-24
2. 21-24,20
3. 10,20,21-24,25,26,30
To make sure the $final values are always ascending, just do this at
the end:
sort($final);
Done!!
Jordan
On Sep 13, 2005, at 7:16 PM, babu wrote:
> $str=10,12,14-18;
>
> $tok = strtok($str, ',');
> while ($tok !== false) {
> $toks[] = $tok;
> $tok = strtok(',');
> }
>
> foreach ($toks as $token){
> if (strpos($token,'-')){
> stringtokenize($token);
> }else{
> $finaltokens[]= $token;
> }
> }
>
> function stringtokenize($nstr){
> $ntok1= strtok($nstr,'-');
> $ntok2=strtok('-');
> for($i=$ntok1;$i<=$ntok2;$i++){
> $finaltokens[]= $i;
> }
> }
>
> foreach ($finaltokens as $ftoken){
> echo $ftoken;
> echo "
";
> }
>
> the ouput prints only 10,12 but not 14,15,16,17,18. where is the
> problem.
>
>
>
> ---------------------------------
> To help you stay safe and secure online, we've developed the all
> new Yahoo! Security Centre.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
---------------------------------
How much free photo storage do you get? Store your holiday snaps for FREE with Yahoo! Photos. Get
Yahoo! Photos