0

I have a table with data mobile(id, name, size);

(1, "Asha 301", "240 x 400");
(2, "Asha 302", "480 x 800");
(3, "Asha 303", "240 x 400");
(4, "Asha 304", "480 x 800");
(5, "Asha 305", "240 x 400");
(6, "Asha 306", "240 x 400");
<?php
    $query = "SELECT id, name, size
              FROM `mobile`
              GROUP BY size Order by display_size ";
    $results = mysql_fetch_array($query);
   foreach ($results as $result) {
        ?>
        <li>
           <?php echo $result['size']; ?>
           <ul>
               <li><?php echo $result['name']; ?></li>
           </ul>
        </li>
<?php
}
?>

And result is:

240 x 400
   Asha 301
400 x 800
   Asha 302

result only get 1 row, how to get all rows ?

240 x 400
   Asha 301
   Asha 303
   Asha 305
   Asha 306
400 x 800
   Asha 302
   Asha 304

How to fix it ?

1

2 Answers 2

1

mysql_fetch_object returns an object, so iterating through it in a foreach does nothing.

Instead, you should use mysql_fetch_array, which returns an array of the results,

Sign up to request clarification or add additional context in comments.

Comments

1
 $query = "SELECT `id`, `name`, `size` FROM `mobile` Order by `size` ";
 $results = mysql_query($query);
 while ($result=mysql_fetch_array($results)) {
  ?>
    <li>
       <?php echo $result['size']; ?>
       <ul>
           <li><?php echo $result['name']; ?></li>
       </ul>
    </li>

1 Comment

This unexplained snippet clearly does not satisfy the brief of needing the group text once followed by all children of the group.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.