Jump to content

sprintf-like for variable-only purposes without printing


neuroxik

Recommended Posts

Hey everyone,

 

I've come around this problem quite a few times now, so I'm looking for a cleaner way to do what I always resort to. Here's my scenario:

 

The following line is fairly straightforward:

 

$sql_where = " WHERE SUBSTRING(name,1,1) = '".$c."' ";

 

But sometimes I need to explode on some values, rising a need for multiple instances of the above, as in MYSQL's WHERE bla = 1 || WHERE bla = 2 || WHERE bla = 3

 

Of course, I could always write it out manually, like this:

foreach($c as $v) {
   $sql_str .=  "WHERE SUBSTRING(name,1,1) = '".$v."' ";
}

 

But I want my code to be more modular. The only solution I've been using so far is something like this:

$where_start = "WHERE SUBSTRING(name,1,1) = '";
$where_end = "' ";
foreach($c as $v) {
   $sql_str .= $where_start . $v . $where_end;
}

 

I find this technique a bit ridiculous and I was wondering if there was a similar method as say when you want to sprintf like this:

 

$v = 'Bla bla %s bla';
sprintf($str, $v);

 

So my question is basically this: is there any way to use a sprintf-like function that will concatenate variables inside variables?

 

Thanks for any suggestions or input.

Link to comment
Share on other sites

with a foreach... which would defeat your purpose.

sprintf follows a format, so that would be more typing and not scalable.

 

i'm surely open to a better way but using foreach to build the query is the best way i've found yet...

 

Yah, I've been stuck on that method too

Link to comment
Share on other sites

Since your example isn't valid mysql, I'm not sure what it is you are actually trying to produce, but one way of putting together a query with multiple repeating terms is to implode an array with the right 'glue' to produce a term that you then put into the final query statement.

 

Do you have an actual example of an array of values and what query you want to produce from those values?

Link to comment
Share on other sites

Since your example isn't valid mysql, I'm not sure what it is you are actually trying to produce, but one way of putting together a query with multiple repeating terms is to implode an array with the right 'glue' to produce a term that you then put into the final query statement.

 

Do you have an actual example of an array of values and what query you want to produce from those values?

 

Maybe I haven't been clear about what it is I was (my bad), but the imploding part for the mysql is done later and I found it irrelevant to my question. My question isn't about mysql, I just happened to use it as an example.

 

Here's my code in context as you asked:

$sl = 'e';

function sqlWhereMultipleChrs($c=null) {
$default = " WHERE SUBSTRING(name,1,1) = '".$c."' ";
$w_start = ' WHERE SUBSTRING(name,1,1) = \'';
$w_end = '\' ';
if(is_null($c)) return $default;
$alternatives = array(
	'a'	=> array('a','à','á','â','ä','ã'),
	'b' => array('b'),
	'c' => array('c'),
	'd' => array('d'),
	'e' => array('e','è','é','ê','ë'),	//
	'f' => array('f'),
	'g' => array('g'),
	'h' => array('h'),
	'i' => array('i','ì','í','î','ï'), //
	'j' => array('j'),
	'k' => array('k'),
	'l' => array('l'),
	'm' => array('m'),
	'n' => array('n','ñ'),
	'o' => array('o','ò','ó','ô','ö','õ'), //
	'p' => array('p'),
	'q' => array('q'),
	'r' => array('r'),
	's' => array('s'),
	't' => array('t'),
	'u' => array('u','ù','ú','û','ü'), //
	'v' => array('v'),
	'w' => array('w'),
	'x' => array('x'),
	'y' => array('y','ý'),
	'z'	=> array('z'),
);
$possibs = $alternatives[$c];
$cnt = count($possibs);
if($cnt == 1) $sql = $w_start.$possibs[0].$w_end;
elseif($cnt > 1) {
	$sql = array();
	foreach($alternatives[$c] as $v) {
		$sql[] = $w_start.$v.$w_end;
	}
	$sql = implode(" || ",$sql);
}
else 	return $default;

return $sql;
}

$sqlwhere = sqlWhereMultipleChrs($sl);
// outputs : WHERE SUBSTRING(name,1,1) = 'e' || WHERE SUBSTRING(name,1,1) = 'è' || WHERE SUBSTRING(name,1,1) = 'é' || WHERE SUBSTRING(name,1,1) = 'ê' || WHERE SUBSTRING(name,1,1) = 'ë' 

Link to comment
Share on other sites

Maybe I haven't been clear about what it is I was (my bad), but the imploding part for the mysql is done later and I found it irrelevant to my question.

 

The point was that your query is invalid, both in the example and your code.  A query can only have a single WHERE clause, not several joined together using ||.  Also, since your using an OR condition, you can just use the IN clause in your query to make things easier.

 

ex:

WHERE  SUBSTRING(name,1,1) IN ('a', 'b', 'c', ...)

 

The value you put in the IN clause can be generated very easily via imploding an array of multiple possible values.  You would just find your array of alternatives in the list and implode that and stick it into the in clause.

 

 

Link to comment
Share on other sites

Maybe I haven't been clear about what it is I was (my bad), but the imploding part for the mysql is done later and I found it irrelevant to my question.

 

The point was that your query is invalid, both in the example and your code.  A query can only have a single WHERE clause, not several joined together using ||.  Also, since your using an OR condition, you can just use the IN clause in your query to make things easier.

 

ex:

WHERE  SUBSTRING(name,1,1) IN ('a', 'b', 'c', ...)

 

The value you put in the IN clause can be generated very easily via imploding an array of multiple possible values.  You would just find your array of alternatives in the list and implode that and stick it into the in clause.

 

About the multiple WHERE's, I wasn't sure anymore and upon execution I got an error, so removed the WHERE and just put it in the SQL before including my variable, I just couldn't edit the post above after that. But again, that's not my point, I should have avoided MySQL in my question altogether. I don't care whether the MySQL works or not, I just used it quickly to give a case scenario.

 

My question is: is there any way to use a sprintf-like function that will concatenate variables inside variables? in the way I've described in my first post, a bit à la sprintf.

 

But on another note: thanks for the IN trick, will spare me some time one day :)

Link to comment
Share on other sites

Your question doesn't really make much sense to me.  Why not just use sprintf exactly like you did in your 'something like this' example.

 

$where = "WHERE SUBSTRING(name,1,1) = '%s' ";
foreach($c as $v) {
   $sql = sprintf($where, $v);
}

 

Going back to your full mysql example:

$conditions = array();
$where = "SUBSTRING(name,1,1)='%s' ";
foreach($alternatives[$c] as $v) {
$conditions[] = sprintf($where, $v);
}

$sql = 'blah blah WHERE '.implode(' OR ', $conditions);

 

Link to comment
Share on other sites

Why not just use sprintf exactly like you did in your 'something like this' example.

 

I'll take the next guess

 

Hmm... I'm kind of ashamed. I'm answering to you both to thank you for your time and effort.

 

Kicken: exactly, why didn't I use sprintf, which does exactly what I was aiming for. I do not know WHY my head interpreted sprintf as the equivalent of printf (which actually prints the data and doesn't assign it to a variable, as you most probably know), so what I was looking for was a function LIKE printf but that you can use inside variables, but it happens that my example was the solution. I'm quite confused as to why this even processed in my mind.

 

So my problem is solved, thanks to you all.

 

p.s.: I'm not experienced on this board, so do I "mark as solved" when a thread is solved or do I leave time for others (like you guys) to reply?

Link to comment
Share on other sites

p.s.: I'm not experienced on this board, so do I "mark as solved" when a thread is solved or do I leave time for others (like you guys) to reply?

 

Once you consider it to be solved you can mark it as solved.  Others can still make replies if they have something else to add.  I went ahead and marked it solved for you.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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