php - Creating a Dynamic Array and returning array from with-in a function -
i'm trying write function selects contents database. problem have lack of understanding of arrays , returning variables functions.
i've written function seem going round in circles how build array , return function.php.
main php file. (labels , names have been changed protect innocent.)
<?php //include '../sb_mysqli_connect.php';//connect database include 'functions.php' ; //include functions list $username = 'foo'; $password = 'bar'; $fields = array( array('username', $username), array('password', $password)); //prep's array multiple statements sbpolldb ("users",$fields, null, 1, null); //function sbpolldb($sbbeta, $sbbecon, $sbbeorder, $sbbelimit, $sbbegroup) echo $tablex['row_name_y']; ?>
the function.php file
<?php function sbpolldb($sbbeta, $sbbecon, $sbbeorder, $sbbelimit, $sbbegroup){ //sbbeta = table name, sbbecon = condition, sbbeval = value, sbbesort = sort value, sbbelim - include '../sb_mysqli_connect.php';//connect database if (empty($sbbeorder)) { $sbbeotemp=""; } else { $sbbeotemp=" order ".$sbbeorder; } //check if order null if (empty($sbbelimit)){ $sbbeltemp=""; } else { $sbbeltemp=" limit ".$sbbelimit; } //check if there's limit set if (empty($sbbegroup)){ $sbbegtemp=""; } else {//$ssbegtemp=' group '.$sbbegroup; $count = sizeof($sbbegroup); $sbbegtemp = " group "; //loop create conditons ($i = 0; $i < $count; $i++) { $value = $sbbegroup[$i]; $sbbegtemp = $sbbegtemp.$value; if ($i < ($count -1 )){ $sbbegtemp = $sbbegtemp.' , '; } }; } if (empty($sbbecon)){ $sbbectemp=''; } else { $count = sizeof($sbbecon); $sbbectemp = 'where '; //loop create conditons ($i = 0; $i < $count; $i++) { $value = $sbbecon[$i]; $sbbectemp = $sbbectemp.$value[0]." ="."'".$value{1}."'"; // ' code apostraphe if ($i < ($count -1 )){ $sbbectemp = $sbbectemp.' , '; } }; } $sbbesql = "select * ".$sbbeta.' ' .$sbbectemp .$sbbegtemp .$sbbeotemp .$sbbeltemp; //" code speach marks mysql_select_db("database1"); $result = mysql_query($sbbesql, $sbbedbc) or die($sbbesql."<br/><br/>".mysql_error());; $temp = mysql_fetch_array($result, mysql_assoc); // build array here dynamically $sbbeta content variable name. // how return array name no longer $temp? } ?>
the construction of sql works, believe dynamic variables created using $$label
, declares new variable content of old one. apply arrays well? i'm building web application doing lot of database queries , wanted shorten repetition of code.
you can return array in function.php(by adding return statement in last line as):
return $temp;
so retrieve returned value in calling function,
$tablex=sbpolldb ("users",$fields, null, 1, null); echo $tablex['row_name'];
Comments
Post a Comment