2017-07-25 4 views
0

Ich versuche, Daten als Checkbox aus der Datenbank aufrufen und wenn Zeile = 5 Checkbox dann geht es nächste Zeile,While-Schleife Eingang Checkbox

Ich habe versucht, dies mit:

while($row = $result->fetch_assoc()) { 

        $x=0; 
        while ($x < 5) { 

      echo "<input type='checkbox' name='food[]' value ='". $row['price']."'>"; 
      echo $row['name'] . "&nbsp"; 

        $x++; 

         } 
        } 

Aber die Dinge ist, ist das Ergebnis wie folgt wurde ....

the result of the code below

Wenn Sie Jungs wissen das, was falsch ist bitte helfen!

das ist mein HTML, PHP-Code:

 <!DOCTYPE html> 
    <html> 
    <head> 
     <title></title> 
    </head> 
    <body> 


    <form action="another_sample.php" method="POST"> 

    <?php 
      include "connection.php"; 

      $sql = "SELECT m.type, m.name, m.price, mt.name as 'type' FROM table_menu m LEFT JOIN table_menu_type mt ON m.type = mt.id WHERE m.type LIKE '%1%' "; 
      $result = $con->query($sql); 

      if ($result->num_rows > 0) { 
       // output data of each row 

       while($row = $result->fetch_assoc()) { 

          $x=0; 
          while ($x < 5) { 

        echo "<input type='checkbox' name='food[]' value ='". $row['price']."'>"; 
        echo $row['name'] . "&nbsp"; 

          $x++; 

           } 
          } 
      } 

      else { 
       echo "0 results"; 
      } 

      $con->close(); 

      ?> 
    </form> 

    </body> 
    </html> 

Antwort

1
echo ='<ul class="checkbox"> 
    <li><input type='checkbox' name='food[]' value ='". $row['price']."'></li> 
</ul>'; 

CSS:

ul.checkbox { 
    margin: 0; 
    padding: 0; 
    margin-left: 20px; 
    list-style: none; 
} 

ul.checkbox li input { 
    margin-right: .25em; 
} 

ul.checkbox li { 
    border: 1px transparent solid; 
    display:inline-block; 
    width:12em; 
} 

ul.checkbox li label { 
    margin-left: ; 
} 
ul.checkbox li:hover, 
ul.checkbox li.focus { 
    background-color: lightyellow; 
    border: 1px gray solid; 
    width: 12em; 
} 
0

unten Code Versuchen:

$x = 1; 
while($row = $result->fetch_assoc()) { 

     echo "<input type='checkbox' name='food[]' value ='". $row['price']."'>"; 
     echo $row['name'] . "&nbsp"; 

     $x++; 
     if($x > 5){ 
      echo "<br/>"; 
      $x = 1; 
     } 
} 
+0

Dank! es funktioniert aber ich das $ i = 1; ist ein Tippfehler, sollte es x richtig sein? es funktioniert, wenn ich das gemacht habe. Vielen Dank! –

+0

Ja richtig, siehe aktualisierte Antwort. –

+0

Wir freuen uns, Ihnen zu helfen und begrüßen zu Stack Overflow. Wenn diese Antwort oder eine andere Lösung Ihr Problem gelöst hat, markieren Sie es als akzeptiert, –

0

es versuchen:

while ($row = $result->fetch_assoc()) { 
    $x=0; 
    while ($x < 5) { 
     echo "<input type='checkbox' name='food[]' value ='". $row['price']."'>"; 
     echo $row['name'] . "&nbsp"; 

     $x++; 
    } 

    echo nl2br ("\n"); 
} 
0
<!DOCTYPE html> 
    <html> 
    <head> 
     <title></title> 
    </head> 
    <body> 


    <form action="another_sample.php" method="POST"> 

    <?php 


      include "connection.php"; 

      $sql = "SELECT m.type, m.name, m.price, mt.name as 'type' FROM table_menu m LEFT JOIN table_menu_type mt ON m.type = mt.id WHERE m.type LIKE '%1%' "; 
      $result = $con->query($sql); 

      if ($result->num_rows > 0) { 
       // output data of each row 
       $x = 1; 
       while($row = $result->fetch_assoc()) { 



        echo "<input type='checkbox' name='food[]' value ='". $row['price']."'>"; 
        echo $row['name'] . "&nbsp"; 
         $x++; 
          if($x > 5){ 
           echo "<br/>"; 
           $x = 1; 
           }         
         } 
      } 

      else { 
       echo "0 results"; 
      } 

      $con->close(); 

      ?> 
    </form> 

    </body> 
    </html>