Friends, I sincerely ask for help. This question has been tormenting for several months already, I am faced with the need to solve it in various projects. So that's the point.

There is a table in the database. for example cards.

There are many fields in the table, one of them is group

There are 15 entries in the cards table, 10 of which have the value group, for example 1

What do you need:

1) Since the records are scattered in a chaotic order, first of all you need to sort by group, so that all the cards that have group 1 are in order in the array one by one.

2) As soon as we meet the first of 10 cards that have the same group (1), we display it on the screen, and then we hide the other 9 cards under the spoiler. and so on with all. If a group is assigned to the card, in which there are no more cards, then we simply output it, if we meet a group of cards, we display the first one, the rest are hidden under the spoiler.

I thought a lot and I still don’t understand whether it can be done in one cycle, or do I need to think something different?

Here is the code:

$result = mysql_query("SELECT * FROM cards ORDER BY group"); while($card = mysql_fetch_array($result)) { //далее не знаю как можно это реализовать но более наглядно покажу что хочу echo $card['name']."<br/>"; //вывели к примеру карту, у которой группа 0 //Встретили карту с группой 1 (следующие 3 карты в массиве также имеют группу 1, поэтому выводим первую попавшею, а далее вставляем все остальные которые найдем с этой же группы под спойлер. //Вот, к примеру, код спойлера: <div class="spoiler-wrapper"> <div class="spoiler folded"><a href="javascript:void(0);">Название первой карты из группы</a></div><div class="spoiler-text">Все остальные карты из группы</div></div> } 
  • The mysql extension is outdated and removed in php7. - Naumov

1 answer 1

Well, you probably don’t have to wrestle with your head, but simply output everything as it is to a campaign by putting down attributes in the form of an id group and using JS to process everything. Here is an example of a script that will hide all the same type of entries in the spoiler, leaving only the top:

 var d = $(document) , groupBlock = "" , el = "" , count ; //Обойти все записи которые подлежат группировке в спойлер , $("[data-group]").each(function(){ el = $(this); groupBlock = d.find(".container-"+el.data("group")); count = d.find("[data-group="+el.data("group")+"]").length ; //если запись уже имеет корневую группу , добавим элемент в нее , иначе создадим контэйнер if(groupBlock.length){ el.appendTo(groupBlock); }else{ //если однокоренных записей больше 1 , добовляем контэйнер if(count > 1){ el.append("<div class='block container-"+el.data("group")+"'></div>").addClass("first") } } //событие при клике на корневую группу , отобразить спойлеры }).on('click',function(){ $(this).find("div.block").stop().slideToggle(); }); 
 .block{ display:none; padding:3px; border:1px solid red; } .first{ cursor:pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div data-group='0'>Первый </div> <div data-group='0'>Первый </div> <div data-group='0'>Первый </div> <div data-group='0'>Первый </div> <div data-group='0'>Первый </div> <div data-group='1'>второй </div> <div data-group='1'>второй </div> <div data-group='1'>второй </div> <div data-group='1'>второй </div> <div data-group='3'>третий</div> <div data-group='4'>четвертый</div> <div data-group='4'>четвертый</div> <div data-group='5'>пятый</div> 

  • Sincerely grateful! - WhoIsDT
  • Sorry, but there is another question: I do not need to group maps from a specific group, for example, this is group number 0. How can I change the script? - WhoIsDT
  • Thank you, I have already done what I need, I wake up, then I thank you once again for your decision! - WhoIsDT
  • Please use)) - Redr01d
  • In that question, unfortunately, I could not clarify the essence of what I had to do. I have been suffering for a couple of hours so I can not finish it. see: - WhoIsDT