neubie Posted June 6, 2011 Share Posted June 6, 2011 Hello I have a bunch of tables with similar structure in my mySQL db. I want to query each of those tables and output the results to dropdown lists in my php page. I can do it simply copying the code for each dropdownlist, but as there will be 18 of these, I thought it would be wiser to write a function for it. I thought I'd use variable variables, but it turns out these can't be used in functions (and are considered a bad practice anyway). Can You tell, what would be the correct way to handle this? Here's my code, which didn't work: function comboesitus($query, $tabel, $ID, $result, $rida, $strSQL, $nimi, $objQuery, $option, $objResuut, $roll) { ${$query} = " SELECT $tabel.$ID FROM kontaktid, $tabel WHERE $tabel.$ID = kontaktid.$ID AND kontaktid.kliendikood = $kli_kood "; ${$result} = mysql_query(${$query}); ${$rida} = mysql_fetch_array(${$result}) or die(mysql_error()); ${$strSQL} = "SELECT * FROM $tabel ORDER BY $nimi"; ${$objQuery} = mysql_query(${$strSQL}); ${$option}=""; while(${$objResuut}=mysql_fetch_array(${$objQuery})) { ${$option} .= "<option value=". ${$objResuut}["$ID"]; if (${$rida}['$ID']==${$objResuut}['$ID']) {${$option}.= " selected='selected'";} ${$option} .= ">" . ${$objResuut}['nimi'] . "</option>"; } echo "<p><label for ='lbl_".$tabel."'>"; echo $roll; echo "<select name='sel_".$tabel."'>"; echo ${$option}; echo "</select>"; echo "</p>"; } comboesitus("query1","buyer","buyer_ID","result1","rida1","strSQL1","nimi1", "objQuery1","option1","objResuut1","Buyer"); And this works: <?php $query1 = " SELECT buyer.buyer_ID FROM kontaktid, buyer WHERE buyer.buyer_ID = kontaktid.buyer_ID AND kontaktid.kliendikood = $kli_kood "; $result1 = mysql_query($query1); $rida1 = mysql_fetch_array($result1); $strSQL1 = "SELECT * FROM buyer ORDER BY nimi1"; $objQuery1 = mysql_query($strSQL1); $option1=""; while($objResuut1=mysql_fetch_array($objQuery1)) { $option1 .= "<option value=". $objResuut1["buyer_ID"]; if ($rida1['buyer_ID']==$objResuut1['buyer_ID']) {$option1.= " selected='selected'";} $option1 .= ">" . $objResuut1['nimi1'] . "</option>"; } ?> <p><label for ="lbl_buyer"> Buyer:</label> <select name="sel_buyer"> <?php echo $option1; ?> </select> </p> Quote Link to comment https://forums.phpfreaks.com/topic/238551-variable-arguments-in-a-function/ Share on other sites More sharing options...
KevinM1 Posted June 6, 2011 Share Posted June 6, 2011 Please put all code in either tags or tags. I took the liberty of doing it for you above. Quote Link to comment https://forums.phpfreaks.com/topic/238551-variable-arguments-in-a-function/#findComment-1225845 Share on other sites More sharing options...
WebStyles Posted June 6, 2011 Share Posted June 6, 2011 I use something like this to build all my dynamic combos: <?php function buildCombo($table,$values,$displayText,$selectedValue,$comboName){ $conn = conn('databaseName'); $output = '<select name="'.$comboName.'" size="1">'; $q = mysql_query("select $values,$displayText from $table order by $displayText",$conn); while($r = mysql_fetch_assoc($q)){ $output .= '<option value="'.$r[$values].'"'; if($selectedValue==$r[$displayText]) $output .= ' selected'; // you can use $values or $displayText to check for selected $output .= '>'.$r[$displayText].'</option>'; } @mysql_close($conn); $output .= "</select>"; return $output; } // to show the comboBox use: echo buildCombo('tableName','fieldWithValues','fieldWithText','selectedValue','nameForSELECTelement'); // fieldWithValues can be the same as fieldWithText if needed ?> (p.s. I just typed all that in as an example, there maybe typo's) hope it helps Quote Link to comment https://forums.phpfreaks.com/topic/238551-variable-arguments-in-a-function/#findComment-1225908 Share on other sites More sharing options...
trq Posted June 7, 2011 Share Posted June 7, 2011 Your code makes little sense. What exactly do you hope to achieve using variable variables? Quote Link to comment https://forums.phpfreaks.com/topic/238551-variable-arguments-in-a-function/#findComment-1226377 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.