0

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?

5 Answers 5

3

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":
Sign up to request clarification or add additional context in comments.

2 Comments

The same here! It is just transfered from the left to the right.
This would only appear inline if you had CSS that caused it to render that way,
0

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

4 Comments

I tried it but it only changed the align from left to the right part of the page but all my entries are still in the same line.
I'm sorry, and here the changes :D
Thanks but we still have problem! :/
Problem solved pals! I've just added "echo "<br />";" in my while loop. Sorry for this comment but I;m not allowed to answer my own question yet! Thanks a lot for you suggestions! :D
0

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.

Comments

0

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>

Comments

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

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.