Hello, there was a problem with the logic of data output to the page. It is necessary that certain records from the database are displayed on the page in a certain "category". I tried to achieve this in the following way: I assigned each entry the ID of the category to which it belongs and if it checked if the following code came out:

<?php while ( $row = mysql_fetch_assoc($sql_response) ): $is_stock = isStock($row['stock']) ?> <h3 class="page-header"> PKVZ </h3> <?php if($row['social'] == 1){ echo showGoods($row['id'], $row['name'], $row['price'], $row['quanity'], $row['stock'], $is_stock); continue; } ?> <h3 class="page-header"> Riston </h3> <?php if($row['social'] == 2){ echo showGoods($row['id'], $row['name'], $row['price'], $row['quanity'], $row['stock'], $is_stock); continue; } ?> <h3 class="page-header"> Labby </h3> <?php if($row['social'] == 3){ echo showGoods($row['id'], $row['name'], $row['price'], $row['quanity'], $row['stock'], $is_stock); continue; } ?> <?php endwhile; ?> 

But the problem arose : HTML also gets into while (), so on the output we have, for example, with two entries:

Category No. 1 [Record from DB] Category No. 2 [Record from DB] Category No. 3 Category No. 1 Category No. 2 Category No. 3

Those. HTML is also included in the loop, and this is not necessary. In fact, the task is childish, but I cannot figure out how to do something. With foreach, too, does not come out.

Actually, the question itself: how to display data from the database so that certain data is in a certain place in HTML, given that there is more data than 1 record?

    1 answer 1

    In short, you need this:

     - категория 1 - запись - запись - категория 2 - запись - запись - запись ... 

    Do you understand correctly?

    1. if you are completely feng shui, then create a table with categories in the database (cat_id, cat_name) , and store category names in the database ... one category - one record. After that, add this category table to your data in the query (add cat_table.cat_name to the select , and join join cat_table to FROM - ct on ct.cat_id = your_table_alias.social (I correctly understood that social is the id of the category, right?

    2. sort the data in the query (add order by your_table_alias.social to your query, if sorted by id)

    3. data output ... there is something you have to do with porridge ... why do you need three identical output blocks under different conditions ??

       $current_cat = null; while ( $row = mysql_fetch_assoc($sql_response) ) { $is_stock = isStock($row['stock']); if($current_cat !== $row['social']) { echo '<h3 class="page-header">'.$row['cat_name'].'</h3>'; $current_cat = $row['social']; } echo showGoods($row['id'], $row['name'], $row['price'], $row['quanity'], $row['stock'], $is_stock); } 

    Well, if you are too lazy to start a new table (skip step 1), then

     echo '<h3 class="page-header">'.$row['cat_name'].'</h3>'; 

    change to

     switch($row['social']) { case 1: echo '<h3 class="page-header">Название категории первой</h3>'; break; case 2: echo '<h3 class="page-header">Второй категории название</h3>'; break; case 3: echo '<h3 class="page-header">Третья категория</h3>'; break; default: echo '<h3 class="page-header">Неведомая категория</h3>'; break; } 
    • Thanks for the extended answer. Yes, a little lazy, but I want to deal with the first option, because in the future it may be very useful. I hope you do not mind answering questions in the dynamics of work. Honestly, I didn’t really figure it out with the request - so far something is coming out: i.imgur.com/rpFUtx3.jpg . I want to read the syntax of such requests, do not tell me the link? Bye question: - Felix Nov.
    • on the link - everything is wrong ... in general :) - AlexandrX
    • Yes, I already understood. I want to understand the syntax, so I’m interested, so that once again you shouldn’t twitch you on silly questions. - Felix
    • one
      Well, google is here :) google.com/search?q=sql+select+inner+join+ examples - AlexandrX