0

I know this question has been answered similar to the previous asked question. I have tried the solutions but it seems that no matter what way I have tried I still can't get this to work. I am not sure is my coding structure is wrong somewhere.

Currently it is displaying the result as:

Cafes (Category)
Starbucks

Cafes (Category)
CoffeeBean

But I wanted it to show:

Cafes (Category)
Starbucks
CoffeeBean

Below is my code:

<?php
    $levelArray=array('L1','B1','L2','B2');
        foreach ($levelArray as $i=>$level) {
             $data='';
            $img = "img/".$levelArray[$i];
           $result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='$levelArray[$i]'");
           while($row = mysqli_fetch_array($result)){
            $data .= '<h1>'.$row['categories'].'</h1>
                        <ul class="shop_listing clearfix">
                            <li class="float_left">'.$row['name'].'</li>
                            <li class="float_right">'.$row['unit_number'].'</li>
                        </ul>';
        }
        echo '<div class="swiper-slide">
            <img src="'.$img.'" alt="" />
                <div class="content_container">'.$data.'</div>
          </div>'

        }

     ?>

Any help will be appreciated. Thanks in advance.

3
  • 1
    Well, you want to group stuff by category. To do that, introduce a processing step between query and display. In general going directly from query results to HTML is inadequate for anything except tutorials. Commented Oct 30, 2013 at 9:51
  • In addition: $levelArray[$i] inside the foreach() is unnecessary, since you are extracting the values in $level, just use $level Commented Oct 30, 2013 at 9:58
  • Thanks Royal for pointing that out. Have amended that. =) Commented Oct 30, 2013 at 10:25

2 Answers 2

1
$previousVal = "";
while($row = mysqli_fetch_array($result)){
if($previousVal != $row['categories']){
     $data .= '<h1>'.$row['categories'].'</h1>
     $previousVal = $row['categories'];
}
     $data .= '<ul class="shop_listing clearfix">
               <li class="float_left">'.$row['name'].'</li>
               <li class="float_right">'.$row['unit_number'].'</li>
               </ul>';
}

try this one.

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

1 Comment

Hi Ran, I have tested it and it worked somehow. I tried to add in 1 more record into the table, it display the latest input data as separated from the previous one. Example, I displayed 2 shop name from Cafe category followed by 1 shop name from Restaurant category. When I input new data into Cafe category, it will display after the Restaurant category instead of showing together with the Cafe category. Hope you get what I mean.
0

Well, every time you echo the name of a shop, the category is also echoed, because you're printing the $row['categories'] and $row['name'] at the same time.

To fix this, you probably have to separate it, probably by adding a processing step as mentioned by Jon that groups all the shops in the same category together, and echoes the categories once only.

$allCategories = array(); $data='';

while($row = mysqli_fetch_array($result)){
    if(!array_search($row['categories'],$allCategories)){
        array_push($allCategories,$row['categories']);
        $data .= "<h1>{$row['categories']}</h1>";
    }
    $data.= '<ul class="shop_listing clearfix">
                 <li class="float_left">'.$row['name'].'</li>
                 <li class="float_right">'.$row['unit_number'].'</li>
             </ul>';

        }

3 Comments

If answering, then show a code example, this is nothing much than Jon's comment :)
Thanks huey for the help =) It turns out the same issue that I am facing using Ran's solution. =S Meaning it will display the new data in new line instead of displaying together with the same category whenever there is new data input. Example: (Cafes, Starbucks, Coffeebean) (Restaurant, Swensen) [When adding in new data] -> it displays (Cafes, TCC) behind the Restaurant instead of (Cafes, Starbucks, Coffeebean, TCC)
Try changing your mySQL query to sort the rows by category. That should resolve it. So it's something like: SELECT * FROM floor_directory WHERE level='$level' ORDER BY categories

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.