Jump to content

Variable arguments in a function?


neubie

Recommended Posts

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>

Link to comment
Share on other sites

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

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.