tags:

views:

40

answers:

2

Hi i'm trying to process the mysql_fetch_array query below and simplify the code so only 1 query is ran for both sets, is that possible

<select name=[set1]>    
<?php
$set1 = mysql_fetch_array(mysql_query("SELECT `Locale` FROM `language` WHERE `Setting` = '1' ORDER BY FormatSet"));
while($row = $set1){
    echo "<option value=\"$set1\">$set1</option>\n";
}
?>
                </select>
                <select name=[set2]>    
<?php
$set2 = mysql_fetch_array(mysql_query("SELECT `Locale` FROM `language` WHERE `Setting` = '2' ORDER BY FormatSet"));
while($row = $set2){
    echo "<option value=\"$set2\">$set2</option>\n";
}
?>
</select>
A: 

If I understand your question you want to perform both queries at once and half your code.

I'd suggest changing your query to get all the records in one go, and use a flag to create a new select for each new setting, this should work and can easily have more settings added:

$set = mysql_fetch_assoc(mysql_query("SELECT `Locale` FROM `language` WHERE `Setting` = '1' OR 'Setting' = '2' ORDER BY Setting, FormatSet"));

$setting = 0;
$close = false;

foreach ($set as $row)
{
    if ($row['Setting'] > $setting)
    {
        $setting = $row['Setting'];
        echo "<select name=[set{$setting}]>";
        // Set flag to close select
        $close = true;
    }

    echo "<option value=\"{$row['Locale']}\">{$row['Locale']}</option>\n";

    if ($close)
    {
        echo '</select>';
        $close = false;
    }
}
John
+1  A: 
<?php 
$textSet1 = '<select name=[set1]>';
$textSet2 = '<select name=[set2]>';
$set = mysql_query("SELECT `Locale`, `Setting` FROM `language` WHERE `Setting` in ('1','2') ORDER BY FormatSet");
while($row = mysql_fetch_array($set)){
    if ($row['Setting'] == '1')
         $textSet1 .= '<option value="'.$row['Locale'].'">'.$row['Locale'].'</option>';
    else
         $textSet2 .= '<option value="'.$row['Locale'].'">'.$row['Locale'].'</option>';
}
$textSet1 .= '</select>';
$textSet2 .= '</select>';

echo $textSet1;
echo $textSet2;
?>
Parkyprg
The condition should be: `while($row = mysq_fetch_array($set))` whith `$set= mysql_query(...`.
Felix Kling
I forgot to mention that both part dropdown boxes must show. would the could above allow that? I'm not able to test yet but I'm noticing the condition statement
acctman
Yes, both of them will show. The condition is to put each result in the corresponding select. At the end there are the 2 echo lines.
Parkyprg