Suspicius
Suspicius

Reputation: 41

Dynamic Checkbox in php/html

I've written these lines of code in order to crate my dynamic(fetch data from a database) checkbox.

**<?php 

   db_connect();

   $sql = mysql_query('SELECT title  FROM products WHERE cname LIKE "Coffee"') or die(mysql_error());     

   while($row = mysql_fetch_array($sql, MYSQL_BOTH)){
       echo "<input type='checkbox' name='products[]' value='".$row['title']."'>"
        .$row['title'];
   }

?>**

The problem is that the results come in the same line in my webpage, but I'd like them to be appeared as a list so as to create an order-list /menu of me cafeteria. How can I correct this?

Upvotes: 0

Views: 12727

Answers (5)

Bitopan Das
Bitopan Das

Reputation: 1

<?php 

   db_connect();

   $sql = mysql_query('SELECT title  FROM products WHERE cname LIKE "Coffee"') or die(mysql_error());     

   while($row = mysql_fetch_array($sql, MYSQL_BOTH)){
       echo "<input type='checkbox' name='products[]' value='".$row['title']."'>"
        .$row['title']."<br/>";
   }

?>

Upvotes: 0

nathanfirth
nathanfirth

Reputation: 27

You could put them in to a unordered list (UL):

<ul>
<?php 
db_connect();

$sql = mysql_query('SELECT title  FROM products WHERE cname LIKE "Coffee"') or die(mysql_error());

while($row = mysql_fetch_array($sql, MYSQL_BOTH)){
echo "<li><input type='checkbox' name='products[]' value='".$row['title']."'>"
    .$row['title']."</li>";}

?>
</ul>

Upvotes: 0

jedwards
jedwards

Reputation: 30210

Well, the easiest solution would be to add line breaks (<br />) after each row.

You could also use a table. For example, something like:

<?php 
    echo '<table>';
    while($row = mysql_fetch_array($sql, MYSQL_BOTH)){
        echo "<tr><td>";
        echo "<input type='checkbox' name='products[]' value='".$row['title']."'>";
        echo $row['title'];
        echho "</td></tr>";
   }
   echo '</table>'
?>

But there are better alternatives that involve divs and CSS.

Upvotes: 0

j08691
j08691

Reputation: 207901

Change:

while($row = mysql_fetch_array($sql, MYSQL_BOTH)){
    echo "<input type='checkbox' name='products[]' value='".$row['title']."'>"
     .$row['title'];
}

to

echo "<ul>\n":
while($row = mysql_fetch_array($sql, MYSQL_BOTH)){
    echo "<li><input type='checkbox' name='products[]' value='".$row['title']."'>"
     .$row['title']."</li>";
}
echo "</ul>\n":

Upvotes: 3

frank_ivan
frank_ivan

Reputation: 123

try with this:

<?php
db_connect();
$sql = mysql_query('SELECT title  FROM products WHERE cname LIKE "Coffee"') or     die(mysql_error());
echo "<ul>";
while($row = mysql_fetch_array($sql, MYSQL_BOTH)){

echo "<li> <input type='checkbox' name='products[]' value='".$row['title']."'></li>"
    .$row['title'];}//end while

echo"</ul>";

Upvotes: 0

Related Questions