Re: Globals or Super Global Variables To Be Reused from For Loops
| From: | Jim Lucas | Date: | Thu, 06 Nov 2008 16:11:48 +0000 |
| Subject: | Re: Globals or Super Global Variables To Be Reused from For Loops | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-283002@lists.php.net to get a copy of this message | ||
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: phpster@gmail.com
>> To: ajwei@alumni.iu.edu
>> 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=hmtag1&loc=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