Hello, I have a problem with my list down menu. My list down menu will display data from MySQL. It working very well. But, there are problems when I add if else statement. I want to display the list down menu based on the one field from MySQL. For example :

I named the field as 'saluran'. This field has two value which are 'D' and 'L'.

Based on this field, the list down menu will display. If 'saluran' is D, then the list down menu will display from 'status' table. If 'saluran' is L, then the list down menu will display data from 'user' table.

I tried change the code many times, but still there are problems when I add if else statement. Please help me asap. Thank you in advanced.

Here my code without if else statement:

<select name="from" id="from" >
            <option value="" selected>-From-</option>
             <?php
                        $resultSubject = mysql_query("SELECT * FROM status");
                         while($row = mysql_fetch_array( $resultSubject))
                         {
                            $ID = $row['ID'];
                            $codeStatus = $row['codeStatus'];
                            $statusSurat = $row['statusSurat'];

                       ?>
                        <option value="<?php echo $codeStatus;?>"><?php echo $statusSurat;?></option>
                  <?php } ?>
              </select>
          </select>


              <select name="to" id="to" >
            <option value="" selected>-To-</option>
            <?php
                        $resultSubject = mysql_query("SELECT * FROM status");
                         while($row = mysql_fetch_array( $resultSubject))
                         {
                            $ID = $row['ID'];
                            $codeStatus = $row['codeStatus'];
                            $statusSurat = $row['statusSurat'];

                       ?>
                        <option value="<?php echo $codeStatus;?>"><?php echo $statusSurat;?></option>
                  <?php } ?>
              </select>
          </select>

Here my code with if else statement :

<?php

    if ( $saluran == 'D')
        {
            echo "<select name='from_D' id='from_D' >
            <option value='' selected>-From-</option>
             <?php
                        $resultSubject = mysql_query('SELECT * FROM status');
                         while($row = mysql_fetch_array( $resultSubject))
                         {
                            $ID = $row['ID'];
                            $codeStatus = $row['codeStatus'];
                            $statusSurat = $row['statusSurat'];

                       ?>
                        <option value='<?php echo $codeStatus;?>'><?php echo $statusSurat;?></option>
                  <?php } ?>
              </select>
          </select>


              <select name='to_D' id='to_D' >
            <option value='' selected>-To-</option>
            <?php
                        $resultSubject = mysql_query('SELECT * FROM status');
                         while($row = mysql_fetch_array( $resultSubject))
                         {
                            $ID = $row['ID'];
                            $codeStatus = $row['codeStatus'];
                            $statusSurat = $row['statusSurat'];

                       ?>
                        <option value='<?php echo $codeStatus;?>'><?php echo $statusSurat;?></option>
                  <?php } ?>
              </select>
          </select>";

        }
        else
        {
            echo "<select name='from_L' id='from_L' >
            <option value='' selected>-From-</option>
             <?php
                        $resultSubject = mysql_query('SELECT * FROM user');
                         while($row = mysql_fetch_array( $resultSubject))
                         {
                            $ID = $row['ID'];
                            $name = $row['name'];


                       ?>
                        <option value='<?php echo $ID;?>'><?php echo $name;?></option>
                  <?php } ?>
              </select>
          </select>


              <select name='to_L' id='to_L' >
            <option value='' selected>-To-</option>
            <?php
                        $resultSubject = mysql_query('SELECT * FROM user');
                         while($row = mysql_fetch_array( $resultSubject))
                         {
                            $ID = $row['ID'];
                            $name = $row['name'];


                       ?>
                        <option value='<?php echo $codeStatus;?>'><?php echo $name;?></option>
                  <?php } ?>
              </select>
          </select>";    
        }
        ?>

What do you mean by problem?
Tell us what exaclty is not working or what is happneing?
How you set value in variable $saluran

Thank you for your reply. The problem is there is error at this line

$ID = $row['ID'];

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

This how I set variable $saluran

<?php
        include("dbase.php");

    $ID = $_GET['ID'];
    $query = "SELECT * FROM pendaftaransurat WHERE ID ='".$ID."'";  
    $result = mysql_query($query, $conn) or die ("Could not execute query");
    $row = mysql_fetch_array($result, MYSQL_BOTH);

        $ID = $row["ID"];
        $noTrack = $row["noTrack"];
        $noRujukan = $row["noRujukan"];
        $saluran = $row["saluran"];


        @mysql_free_result($result);
    ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.