Jump to content

Joining together values of an array into a single string


cparekh

Recommended Posts

Hi,

 

this is probably an easy one but my mind is mush so I thought I'd ask here...hopefully someone can point me in the right direction.. :)

 

I have a simple array and I want to join all the values into a single string. I'm writing a dynamic conditional statement for a sql query; The array is created based on certain criteria being met and then a single condition being created to insert into the sql query.

 

What I have so far:

 

$querybuild[] = "condition1";

$querybuild[] = "condition2";

$querybuild[] = "condition3";

etc..

 

//add the OR operator between array members

for($i = 0, $size = sizeof($querybuild); $i < $size - 1; ++$i)

{

    $querybuild[$i] = $querybuild[$i]. " OR ";

}

and now I'd like to join whatever the array holds into a single string to use in the sql query thus:

 

$sql = "SELECT * FROM table WHERE ".$querybuild." ORDER BY id DESC";

 

where $querybuild would read "condition1 OR condition2 OR condition3".

 

Hope that makes sense :)

 

Thanks in advance for any help.

 

C.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.