0

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>
1
  • 2
    That code shouldn't even run. The while-loop will run forever.. Commented Sep 29, 2010 at 10:00

2 Answers 2

1
<?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;
?>
3
  • 1
    The condition should be: while($row = mysq_fetch_array($set)) whith $set= mysql_query(.... Commented Sep 29, 2010 at 10:04
  • 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 Commented Sep 29, 2010 at 12:41
  • 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. Commented Sep 29, 2010 at 13:24
-1

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;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.