Вопрос

I've been working on this for at least a month, and can't find this question elsewhere. I know I'm just missing something stupid, but...

There are 2 tables: biz and bizclass. bizclass.bizclassName holds the 250+ classifications to populate the dropdown box. Population from bizclass table and update to biz table both work, but when I try to select the current data from biz.bizClass1 , the dropdown initial value is set as the null value "Select Class". Please help. It's driving me bonkers. I'm too old for many more sleepless nights! Newbie - somewhat familiar with php and javascript but don't have a grip on ajax yet.

echo "Class1: <select name ='bizClass1' id='bizClass1'/> ";

$sql = 'SELECT bizclassName FROM bizclass ORDER BY bizclassName';
$query2 = 'SELECT `bizClass1` FROM `biz` WHERE `bizID` = "'. $search .'"'; 
$clist = mysqli_query($connection,$sql); 
$num=mysqli_num_rows($clist);
$olist = mysqli_query($connection, $query2); // select  bizClass from biz 

$bizTblRecord = mysqli_fetch_assoc($clist); // option values from bizclass table to populate the dropdown
$row2 = mysqli_fetch_assoc($olist); // fetched the bizClass from biz

if ($row2['bizClass1'] == $bizTblRecord['bizclassName']){
    printf ("<option value='%s' selected >%s</option> ", $row2['bizClass1'], $row2['bizClass1']);
} else {
    printf ("<option value=''>Select Class</option> ");
}//end if


for($numrows=1; $numrows<= $num; $numrows++)
{
    // Associative array
    $row=mysqli_fetch_assoc($clist);

    printf ("<option value='%s'>%s</option>",$row['bizclassName'],$row['bizclassName']);

} //end for

echo "</select>";

// Free result set
mysqli_free_result($clist); 
mysqli_free_result($olist);
Это было полезно?

Решение

    echo "Class1: <select name ='bizClass1' id='bizClass1'/>
                         <option value=''>Select Class</option> ";

    $sql = 'SELECT bizclassName FROM bizclass ORDER BY bizclassName';
    $clist = mysqli_query($connection,$sql); 
    $num=mysqli_num_rows($clist);



    $querySEARCH = 'SELECT `bizClass1` FROM `biz` WHERE `bizID` = "'. $search .'"'; 
    $SEARCHlist = mysqli_query($connection, $querySEARCH); // select  bizClass from biz 
    $rowSEARCH= mysqli_fetch_assoc($SEARCHlist); // fetched the bizClass from biz

    for($numrows=1; $numrows<= $num; $numrows++)
    {
        // Associative array
        $row=mysqli_fetch_assoc($clist);

        if ($rowSEARCH['bizClass1'] == $row['bizclassName']){
            printf ("<option value='%s' selected >%s</option> ", $rowSEARCH['bizClass1'], $rowSEARCH['bizClass1']);
        } else {
            printf ("<option value='%s'>%s</option>",$row['bizclassName'],$row['bizclassName']);
        }//end if

    } //end for

    echo "</select>";

    // Free result set
    mysqli_free_result($clist); 
    mysqli_free_result($SEARCHlist);

If statement have to be inside the loop to compare each option with search.

Test tables:

biz

bizID   bizClass1
1       ronaldo
2       shevshenko
3       falcao
4       zidane
5       valderrama

bizclass

bizclassName
falcao
ozil
ronaldo
messi
shevshenko
valderrama
hazard
totti
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top