Reputation: 101
Let say that we are going to add some array to a multidimensional array. I can select the number of arrays which should be added to the main array but what about 99 array? Here is my code that I can change $howmany to add the number of arrays that I need (up to 4 arrays):
function DB_SINGLE_EXE($query, array $array_par, $howmany = 0 ,$column1 = "null", $column2 = "null", $column3 = "null", $column4 = "null"){
global $host;
global $dbname;
global $username;
global $password;
global $db;
try {
$stmt = $db->prepare($query);
$stmt->execute($array_par);
}catch(PDOException $e)
{
echo "ERROR: " . $query . "<br>" . $e->getMessage();
}
if ($howmany > 0){
$rowF = $stmt->fetchAll();
if($rowF){
$hell = count($rowF);
$stack = array(array("true" , $hell));
foreach ($rowF as $row) {
switch ($howmany) {
case 1:
array_push($stack, array($row[$column1]));
break;
case 2:
array_push($stack, array($row[$column1], $row[$column2]));
break;
case 3:
array_push($stack, array($row[$column1], $row[$column2], $row[$column3]));
break;
case 4:
array_push($stack, array($row[$column1], $row[$column2], $row[$column3], $row[$column4]));
break;
}
}
}else{
$stack = array(array("false" , "0"));
}
return $stack;
}
}
How I call the function:
$temp_query = "SELECT * FROM users WHERE status=:one";
$temp_array = array(":one" => "OK");
$results =DB_SINGLE_EXE($temp_query, $temp_array, 4, "user", "pw", "a_succ", "m_succ");
if ($results[0][0]="true"){
$username = $results[1][0];
$password = $results[1][1];
$suc = $results[1][2];
$m_suc = $results[1][3];
// Here I want to get more results but in function I just limited to 4 outputs. It might be 99 or even more and I don't want to use "select case" for 99 cases!
}
Upvotes: 0
Views: 1651
Reputation: 256
I had a hard time interpreting what you're trying to do, so this may or may not work since the provided code isn't actually functional for comparison. It looks like you're needing to take certain columns out of a SELECT * result set, or something like that. Assuming so, it would be better to just modify your query to only get what you need, then use a basic fetchAll() approach.
However, assuming you can't do that for some reason, see if this works for you. I initialized a $rowF variable here with something like I am expecting it to be when your code runs. If that's incorrect, this may be way off, but here it is:
function DB_SINGLE_EXE(array $columnsToPush) {
$rowF = array(array('col1' => 'col1val', 'col2' => 'col2val', 'cola' => 'colaval', 'colb' => 'colbval'));
$stack = array(array('foo', 'bar'));
foreach ($rowF as $row) {
$toBePushed = array();
foreach ($columnsToPush as $columnName) {
$toBePushed[] = $row[$columnName];
}
$stack[] = $toBePushed;
}
return $stack;
}
DB_SINGLE_EXE(array('col1', 'cola'));
Some sanity checking to make sure the column names exist in the row is in order, among other hardening, of course.
After seeing the full function, while I have concerns about the design, I think you could simplify things greatly with:
function DB_SINGLE_EXE($query, array $array_par){
global $host;
global $dbname;
global $username;
global $password;
global $db;
try {
$stmt = $db->prepare($query);
$stmt->execute($array_par);
} catch (PDOException $e) {
echo "ERROR: " . $query . "<br>" . $e->getMessage();
// should probably return here, or do something to halt further execution
}
return $stmt->fetchAll();
}
...
$results = DB_SINGLE_EXE($temp_query, $temp_array);
if (!empty($results)) {
// wrap in foreach if appropriate
$username = $results[0]['user'];
$password = $results[0]['pw'];
$suc = $results[0]['a_succ'];
$m_suc = $results[0]['m_succ'];
}
Upvotes: 2
Reputation: 14593
If you are using PHP 5.6 you may use ellipses (...) syntax. If not jump to the end. Example from php.net:
<?php
function sum(...$numbers) {
$acc = 0;
foreach ($numbers as $n) {
$acc += $n;
}
return $acc;
}
echo sum(1, 2, 3, 4);
?>
Then your function will become two loops inside each other.
EDIT: To make things simpler, here is your transformed code
function DB_SINGLE_EXE(...$columns){
$stack = array(array("foo" , "bar"));
foreach ($rowF as $row) {
$arr=array();
foreach($columns as $col) {
$arr[]=$row[col];
}
array_push($stack, $arr);
}
return $stack;
}
DB_SINGLE_EXE("id", "name", "age");
You can do the same without using ellipses, simply pass an array of columns:
function DB_SINGLE_EXE($columns){
$stack = array(array("foo" , "bar"));
foreach ($rowF as $row) {
$arr=array();
foreach($columns as $col) {
$arr[]=$row[col];
}
array_push($stack, $arr);
}
return $stack;
}
DB_SINGLE_EXE(array("id", "name", "age"));
Not as pretty but still works.
Upvotes: 3
Reputation: 8595
Passing in 99 parameters to function seems like an overhead. Why can't your pass in the columns as array. This will allow you to pass in any number of column keys (even more than 99) ;)
function DB_SINGLE_EXE( $rowF, $columns ){
$columns = array_flip( $columns );
$stack = array( array( "foo" , "bar" ) );
foreach ($rowF as $row) {
array_push( $stack, array_intersect_key( $row, $columns ) );
}
return $stack;
}
$result = DB_SINGLE_EXE(array(
array(
'col1' => 'aaa1',
'col2' => 'bbb1'
),
array(
'col2' => 'bbb2',
'col3' => 'ccc2'
),
array(
'col1' => 'aaa3',
'col2' => 'bbb3',
'col3' => 'ccc3'
),
array(
'col1' => 'aaa3',
'col5' => 'eee3',
'col6' => 'fff3'
)
), array('col1', 'col2', 'col3'));
Upvotes: 1