[PHP] Globals or Super Global Variables To Be Reused from For Loops

2008-11-06 Thread Alice Wei

Hi, 

I have a snippet of code as shown in the following:

$message2=47406|Detroit;
$stringChunk2= explode(|, $message2);

 if ($message2!=) {
   $count_chunk2= count($stringChunk2);
   $count_chunk_2= $count_chunk2-1;
}

   for ($j=0; $j$count_chunk2; $j++) {

  $string3= $stringChunk2[$j];   
  if ($j $count_chunk_2) {
 $string2=  OR ;
 $string3=$string3.$string2;
   }
 else {
 //Don't do anything
   }
   echo $string3;
  }

The code itself works, but I would like to reuse $string3 variable somewhere 
else where I can build SQL statements from it. 
I tried using 

global $string3; 
echo $string3;

after the final curly brace, but I only get Detroit and not 47406 OR Detroit. 
Can anyone please suggest me what I should be using here?

Thanks for your help.

Alice

_
All-in-one security and maintenance for your PC.  Get a free 90-day trial!
http://www.windowsonecare.com/purchase/trial.aspx?sc_cid=wl_wlmail

Re: [PHP] Globals or Super Global Variables To Be Reused from For Loops

2008-11-06 Thread Bastien Koert
[snip]
[/snip]

Alice,

The big problem here is that you are resetting the $string3 variable in the
loop

  for ($j=0; $j$count_chunk2; $j++) {

 $string3= $stringChunk2[$j];//  -- resetting
the value
 if ($j $count_chunk_2) {
$string2=  OR ;
$string3=$string3.$string2;
  }
else {
//Don't do anything
  }
  echo $string3;
 }


I am not sure of your goal since you have not stated it, but it certainly
should be easier to just replace the PIPE  with the OR

$message = str_replace(|,  OR , $message);



-- 

Bastien

Cat, the other other white meat


RE: [PHP] Globals or Super Global Variables To Be Reused from For Loops

2008-11-06 Thread Alice Wei

Hi, 


  Sorry, I cannot use that because I am supposed to turn the string into 
something that looks like regions.name LIKE '%47406' OR regions.name LIKE 
'%Detroit', which I had to fix $string3 variable to   

  $string3=regions.name LIKE '% . $stringChunk2[$j] . ';

  The goal is that the $message variable would be only a $_POST['message'] 
variable so that the where clause can be generated dynamically. 
  The code I have above is part of my where clause in the full SQL statement I 
intend to
construct, which means I have to reuse this $string3 variable somewhere
else.
  
This is what my global declaration looks like: 

  if ($j $count_chunk_2) {

 $string2=  OR ;
 $string3=$string3.$string2;
 global $string3;
   }
 else {
 //Don't do anything
   }
   echo $string3;
  }
echo $string3;

   The last $string3 echo only gives me regions.name
LIKE '%Detroit' according to the current construct and not regions.name
LIKE '%47406' OR regions.name LIKE '%Detroit'. Is there something else I should 
do to have it give me the same output as the echo $string3 as I have had after 
the second to last curly brace? 

Thanks again for your help.

Alice


 Date: Thu, 6 Nov 2008 10:22:44 -0500
 From: [EMAIL PROTECTED]
 To: [EMAIL PROTECTED]
 CC: php-general@lists.php.net
 Subject: Re: [PHP] Globals or Super Global Variables To Be Reused from For 
 Loops
 
 [snip]
 [/snip]
 
 Alice,
 
 The big problem here is that you are resetting the $string3 variable in the
 loop
 
   for ($j=0; $j$count_chunk2; $j++) {
 
  $string3= $stringChunk2[$j];//  -- resetting
 the value
  if ($j $count_chunk_2) {
 $string2=  OR ;
 $string3=$string3.$string2;
   }
 else {
 //Don't do anything
   }
   echo $string3;
  }
 
 
 I am not sure of your goal since you have not stated it, but it certainly
 should be easier to just replace the PIPE  with the OR
 
 $message = str_replace(|,  OR , $message);
 
 
 
 -- 
 
 Bastien
 
 Cat, the other other white meat

_
Express yourself with gadgets on Windows Live Spaces
http://discoverspaces.live.com?source=hmtag1loc=us

Re: [PHP] Globals or Super Global Variables To Be Reused from For Loops

2008-11-06 Thread Jim Lucas
Alice Wei wrote:
 Hi, 
 
 
   Sorry, I cannot use that because I am supposed to turn the string into 
 something that looks like

Sounds like we are doing someones school work again.

 regions.name LIKE '%47406' OR regions.name LIKE '%Detroit', which I had to 
 fix $string3 variable to   
 
   $string3=regions.name LIKE '% . $stringChunk2[$j] . ';
 
   The goal is that the $message variable would be only a $_POST['message'] 
 variable so that the where clause can be generated dynamically. 
   The code I have above is part of my where clause in the full SQL statement 
 I intend to
 construct, which means I have to reuse this $string3 variable somewhere
 else.
   
 This is what my global declaration looks like: 
 
   if ($j $count_chunk_2) {
 
  $string2=  OR ;
  $string3=$string3.$string2;
  global $string3;
}
  else {
  //Don't do anything
}
echo $string3;
   }
 echo $string3;

against my better judgment, I believe this is what you are looking for.

# Get your starting string
$pieces = 47406|Detroit;

# Break it into the parts you are looking to use
$parts  = explode(|, $pieces);

# Empty temporary array
$tmpHolder = array();

# Loop through parts list
foreach ( $parts AS $part ) {

# Create a single statement and stuff it in your tmp array
$tmpHolder[] = regions.name LIKE '%{$part}';
}

# Join all the parts together, placing an OR between each element.
$string3 = join(' OR ', $tmpHolder);


 
The last $string3 echo only gives me regions.name
 LIKE '%Detroit' according to the current construct and not regions.name
 LIKE '%47406' OR regions.name LIKE '%Detroit'. Is there something else I 
 should do to have it give me the same output as the echo $string3 as I have 
 had after the second to last curly brace? 
 
 Thanks again for your help.
 
 Alice
 
 
 Date: Thu, 6 Nov 2008 10:22:44 -0500
 From: [EMAIL PROTECTED]
 To: [EMAIL PROTECTED]
 CC: php-general@lists.php.net
 Subject: Re: [PHP] Globals or Super Global Variables To Be Reused from For 
 Loops

 [snip]
 [/snip]

 Alice,

 The big problem here is that you are resetting the $string3 variable in the
 loop

   for ($j=0; $j$count_chunk2; $j++) {

  $string3= $stringChunk2[$j];//  -- resetting
 the value
  if ($j $count_chunk_2) {
 $string2=  OR ;
 $string3=$string3.$string2;
   }
 else {
 //Don't do anything
   }
   echo $string3;
  }


 I am not sure of your goal since you have not stated it, but it certainly
 should be easier to just replace the PIPE  with the OR

 $message = str_replace(|,  OR , $message);



 -- 

 Bastien

 Cat, the other other white meat
 
 _
 Express yourself with gadgets on Windows Live Spaces
 http://discoverspaces.live.com?source=hmtag1loc=us


-- 
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Globals or Super Global Variables To Be Reused from For Loops

2008-11-06 Thread Alice Wei

Hi, 

  Thanks for your tip, and I am surprised that this could be done so easily. 

Alice

 Date: Thu, 6 Nov 2008 08:11:48 -0800
 From: [EMAIL PROTECTED]
 To: [EMAIL PROTECTED]
 CC: [EMAIL PROTECTED]; php-general@lists.php.net
 Subject: Re: [PHP] Globals or Super Global Variables To Be Reused from For 
 Loops
 
 Alice Wei wrote:
  Hi, 
  
  
Sorry, I cannot use that because I am supposed to turn the string into 
  something that looks like
 
 Sounds like we are doing someones school work again.
 
  regions.name LIKE '%47406' OR regions.name LIKE '%Detroit', which I had to 
  fix $string3 variable to   
  
$string3=regions.name LIKE '% . $stringChunk2[$j] . ';
  
The goal is that the $message variable would be only a $_POST['message'] 
  variable so that the where clause can be generated dynamically. 
The code I have above is part of my where clause in the full SQL 
  statement I intend to
  construct, which means I have to reuse this $string3 variable somewhere
  else.

  This is what my global declaration looks like: 
  
if ($j $count_chunk_2) {
  
   $string2=  OR ;
   $string3=$string3.$string2;
   global $string3;
 }
   else {
   //Don't do anything
 }
 echo $string3;
}
  echo $string3;
 
 against my better judgment, I believe this is what you are looking for.
 
 # Get your starting string
 $pieces = 47406|Detroit;
 
 # Break it into the parts you are looking to use
 $parts  = explode(|, $pieces);
 
 # Empty temporary array
 $tmpHolder = array();
 
 # Loop through parts list
 foreach ( $parts AS $part ) {
 
   # Create a single statement and stuff it in your tmp array
   $tmpHolder[] = regions.name LIKE '%{$part}';
 }
 
 # Join all the parts together, placing an OR between each element.
 $string3 = join(' OR ', $tmpHolder);
 
 
  
 The last $string3 echo only gives me regions.name
  LIKE '%Detroit' according to the current construct and not regions.name
  LIKE '%47406' OR regions.name LIKE '%Detroit'. Is there something else I 
  should do to have it give me the same output as the echo $string3 as I have 
  had after the second to last curly brace? 
  
  Thanks again for your help.
  
  Alice
  
  
  Date: Thu, 6 Nov 2008 10:22:44 -0500
  From: [EMAIL PROTECTED]
  To: [EMAIL PROTECTED]
  CC: php-general@lists.php.net
  Subject: Re: [PHP] Globals or Super Global Variables To Be Reused from For 
  Loops
 
  [snip]
  [/snip]
 
  Alice,
 
  The big problem here is that you are resetting the $string3 variable in the
  loop
 
for ($j=0; $j$count_chunk2; $j++) {
 
   $string3= $stringChunk2[$j];//  -- 
  resetting
  the value
   if ($j $count_chunk_2) {
  $string2=  OR ;
  $string3=$string3.$string2;
}
  else {
  //Don't do anything
}
echo $string3;
   }
 
 
  I am not sure of your goal since you have not stated it, but it certainly
  should be easier to just replace the PIPE  with the OR
 
  $message = str_replace(|,  OR , $message);
 
 
 
  -- 
 
  Bastien
 
  Cat, the other other white meat
  
  _
  Express yourself with gadgets on Windows Live Spaces
  http://discoverspaces.live.com?source=hmtag1loc=us
 
 
 -- 
 Jim Lucas
 
Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them.
 
 Twelfth Night, Act II, Scene V
 by William Shakespeare
 
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 

_
Express yourself with gadgets on Windows Live Spaces
http://discoverspaces.live.com?source=hmtag1loc=us