PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 11, 2022

[FIXED] Populating drop down list in the PHP variable

 January 11, 2022     html, php     No comments   

Issue

    while ($row = mysqli_fetch_array($result)){
      $move = '
        <select type = text name="mover">
         <option>select something</option>
          '".while ($dbrow = mysqli_fetch_array($result)){
              echo "<option value='".$dbrow['deptname']."'>".$dbrow['deptname']."</option>";}."'
        </select>';

    echo '<tr><td>'.$row['name'].'</td><td>'.$move.'</td></tr>';}

I am trying to display dropdown list inside the table. However whenever I apply while(...) codes, it displays an error.

shouldn't HTML + '".PHP."' + HTML be the correct way?


Solution

Generate the select menu before entering the main loop that builds the table and then rewind the recordset so that the main loop can begin

<?php
    $html='<select name="mover">
        <option selected disabled hidden>Please select something';
        
    while( $row = mysqli_fetch_array( $result ) ){
        $html.=sprintf('<option>%s',$row['deptname'] );
    }
    
    $html.='</select>';
    $result->data_seek(0);
?>

Then build the table.

<table>
<?php
    while( $row = mysqli_fetch_array( $result ) ){
        printf('<tr><td>%1$s</td><td>%2$s</td></tr>',$row['deptname'],$html);
    }
?>
</table>


Answered By - Professor Abronsius
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing