Efficient Loop to Bind Parameters in PHP - php

I am trying to bind an unknown number of parameters to a prepared PHP/MySQL statement. I tried the code below. However, it only works with queries with a set number of parameters and I know that there has to be a more efficient way to do so.
switch($numparams){
case 0:
#$stmt->bind_param("");
break;
case 1:
#$stmt->bind_param($paramtypes, $param_arr0);
break;
case 2:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1);
break;
case 3:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2);
break;
case 4:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3);
break;
case 5:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4);
break;
case 6:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4,$param_arr5);
break;
case 7:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4,$param_arr5,$param_arr6);
break;
case 8:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4,$param_arr5,$param_arr6,$param_arr7);
break;
case 9:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4,$param_arr5,$param_arr6,$param_arr7,$param_arr8);
break;
case 10:
#$stmt->bind_param($paramtypes, $param_arr0,$param_arr1,$param_arr2,$param_arr3,$param_arr4,$param_arr5,$param_arr6,$param_arr7,$param_arr8,$param9);
break;
}
As you can see, it's completely inefficient to code and isn't scalable.
I tried dynamically naming variables in a for-loop based on the number of parameters, but I couldn't figure out how to then insert them into the bind_param without having a separate bind_param statement for each in a switch-case loop (as above). It might just be that there is no way to do this, but I feel there should be one. If either are the case, I appreciate any help.

I think your solution lies in call_user_func_array() array.
First you need a string similar to "ssi...." that is the types of parameters in order.
Code
$types = '';
foreach($param_arr as $param) {
$types.= substr(strtolower(gettype($param)), 0, 1);
}
call_user_func_array(array($stmt, 'bind_param'), array_merge(array($types), $param_arr)));
Reference
http://www.pontikis.net/blog/dynamically-bind_param-array-mysqli
http://php.net/manual/en/function.call-user-func-array.php
Hope this helps you through ;)

Related

PHP Switch statement comparison issues

so I have a variable called multiplier that contains a certain value depending on what the user registered with. What I am trying to write here is
If multiplier is equal to "sedentary" then give it the value of $sedmultiplier
If multiplier is equal to "lightly" then give it the value of $lightmultiplier
Im stuck on this, can't seem to figure out how this would be written.
switch ($multiplier==) {
case "sedentary":
$multiplier=$sedmultiplier;
break;
case "lightly":
$multiplier=$lightmultiplier;
break;
case "moderately":
$multiplier=$modmultiplier;
break;
case "very":
$multiplier=$verymultiplier;
break;
case "extremely":
$multiplier=$extrememultiplier;
break;
default:
multiplier==0;
}
Replace $multiplier== with $multiplier and your code should work.
Like this :
switch ($multiplier)
{
case "sedentary":
$multiplier=$sedmultiplier;
break;
case "lightly":
$multiplier=$lightmultiplier;
break;
case "moderately":
$multiplier=$modmultiplier;
break;
case "very":
$multiplier=$verymultiplier;
break;
case "extremely":
$multiplier=$extrememultiplier;
break;
default:
$multiplier==0;
}
Just like #jeroen suggested, I would set the multipliers as an array instead. This way you can reuse them and add/remove multipliers more easily.
// Create the array with name => multiplier
$multipliers = [
"sedentary" => $sedmultiplier,
"lightly" => $lightmultiplier,
"moderately" => $modmultiplier,
"very" => $verymultiplier,
"extremely" => $extrememultiplier,
// ... just add more here, if needed...
];
// Check if we have a key with the current name.
// If we do, return the value, otherwise return 0 as default.
$multiplier = array_key_exists($multiplier, $multipliers)
? $multipliers[$multiplier]
: 0;

Simpler way for multiple 'or' statements with a single variable

I have the following if statement (except the final one would be much longer with more values) that I would like to condense.
if ($row['titleId'] == '123' || $row['titleId'] == '456' || $row[){
I would imagine it would end up something like:
if ($row['titleId'] == ('123'||'456'){}
Or would I be better off like this:
$array = ('123','456')
if (in_array($row['titleId'], $array){}
You can use switch for this:
switch ($row['titleId']) {
case '123': case '456': case '789': case '314': case '271':
doSomething();
}
I'd probably still prefer to have each case on a separate line but, if your goal is to reduce the "height" of your code, you can do it as above.
In terms of shortening the code in your comment, which apparently looks like this:
switch($row['titleId']){
case '8216': case '8678': case '8705': case '8216': case '8707':
$rows[$row['titleId']]=array();
break;
case '8214':
$rows['8216'][]=$row['titleId'];
break;
case '8791':
$rows['8678'][]=$row['titleId'];
break;
case '8643':
$rows['8705'][]=$row['titleId'];
break;
case '8666':
$rows['8707'][]=$row['titleId'];
break;
}
you could opt for something like:
$xlat = array('8214'=>'8216', '8791'=>'8678', '8643'=>'8705', '8666'=>'8707');
switch($row['titleId']){
case '8216': case '8678': case '8705': case '8216': case '8707':
$rows[$row['titleId']]=array(); break;
default:
if (array_key_exists($row['titleId'],$xlat)) {
$rows[$xlat[$row['titleId']]][]=$row['titleId'];
}
}
This compresses the common cases by putting them under the control of an associative array. Basically, if the title ID is in the array as a key, its lookup value will be used to affect the correct $rows entry.

Choose random code within switch statement

I'm using a switch statement with several cases.
When one of the cases is chosen, how can I randomly choose a block of code within that case, but without using a nested switch statement?
For example:
switch ($choose_case){
case 1:
//some code
break;
case 2:
//some code
break;
case 3:
(random block of code #1)
(random block of code #2)
=randomly choose #1 or #2
break;
}
In don't know about you but i will do it like this.
switch ($choose_case){
case 1:
//some code
break;
case 2:
//some code
break;
case 3:
$var = rand(1,2);
if($var == 1)
{
//case 1
}
else
{
//case 2
}
break;
}
The best way to do that is by attributing $choose_value a random number between 1 and 2 and calling the function that contains the switch again with the new value. So, in your case:
function do_something($choose_case){
switch ($choose_case){
case 1:
//some code
break;
case 2:
//some code
break;
case 3:
$choose_case = floor(rand(1.5,2.5));
do_something($choose_case);
break;
}

Switch Case in php Multiple Case run in function?

switch(n){
case "badge01":
case "badge02":
case "badge03":
case "badge04":
case "badge05":
//dosomething
break;
}
Hi above's switch case statement, I would like to use a function to run the multiple loop to generate the case's name, so can i know how to generate with function on a switch case statement like this?
switch(n){
case badgenameloop():
//dosomething
break;
}
And is it possible to do that?
Thanks and sorry for my bad English.
According to PHP Manual
The case expression may be any expression that evaluates to a simple type, that is, integer or floating-point numbers and strings. Arrays or objects cannot be used here unless they are dereferenced to a simple type.
I'm afraid you can Not have a Loop for a case statement, hence a waste of time trying.
You can do this for example:
function badgenameloop($key)
{
switch($key){
case "badge01":
case "badge02":
case "badge03":
case "badge04":
case "badge05":
echo "badge 1";
break;
}
}
function badgename2loop($key)
{
switch($key){
case "badge_2_01":
case "badge_2_02":
case "badge_2_03":
case "badge_2_04":
case "badge_2_05":
echo "badge 2";
break;
}
}
$key = "badge_2_01";
switch($key){
case badgenameloop($key): break;
case badgename2loop($key): break;
}

Multiple Separate Switch Statements

I have multiple switch statements in on one of the pages in order to pass different variables to the URL, as well as different case. I need these different switch statements because I need the different variables.
However, when I put a "default" in one of the switch statements, that default applies to every other switch statement and so when I use the variable of another switch statement in the URL, the default case of that other switch statement will appear on screen, along with the case of this switch statement.
All of my switch statements have one or more cases and I really cannot figure out how to get around this. Please may somebody help me?
Thanks in advance,
Calum.
This might be way off, but I think you need something like this:
if (isset($_POST['myvar'])) {
switch ($_POST['myvar'] {
case 1:
....
break;
default:
....
break;
}
} else if (isset($_POST['myvar2'])) {
switch ($_POST['myvar2'] {
case 1:
....
break;
default:
....
break;
}
}
Does that make sense?
make sure that you have a "break;" statement at the end of each of your cases, and that the default case is the last one. like this:
switch ($var) {
case 1: // do stuff 1;
break;
case 3: // do stuff 2;
break;
// ...
default: // do default stuff
}

Categories