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

Recommended Answers

All 3 Replies

Let try the below code

<?php if ( $saluran == 'D') { ?>

<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 name='to_D' id='to_D' >
  <option value='' selected>-To-</option>
  <?php
     $resultSubject1 = mysql_query('SELECT * FROM status');
     while($row1 = mysql_fetch_array( $resultSubject1))
     {
          $ID1 = $row1['ID'];
          $codeStatus1 = $row1['codeStatus'];
          $statusSurat1 = $row1['statusSurat'];

  ?>
  <option value='<?php echo $codeStatus1;?>'><?php echo $statusSurat1;?></option>
  <?php } ?>
</select>
<?php } else { ?>
<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 name='to_L' id='to_L' >
  <option value='' selected>-To-</option>
  <?php
      $resultSubject1 = mysql_query('SELECT * FROM user');
      while($row1 = mysql_fetch_array( $resultSubject1))
      {
          $ID1 = $row1['ID'];
          $name1 = $row1['name'];
?>
  <option value='<?php echo $codeStatus1;?>'><?php echo $name1;?></option>
  <?php } ?>
</select>
<?php } ?>

Thank you for your answer. My problem was solved.

excuse me.
can I know why we have to make it twice?
thank you.

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.