Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP]Tworzenie drzewek
snerf
post
Post #1





Grupa: Zarejestrowani
Postów: 74
Pomógł: 3
Dołączył: 30.03.2014

Ostrzeżenie: (0%)
-----


Witam,

Mam kod na tworzenie drzewka np komentarzy itp:
  1. function drzewoKategorii($pdo, $prefix_mysql, $rodzic='0', $poziom = '0')
  2. {
  3. global $tablica;
  4. GLOBAL $id_news;
  5. static $tablica;
  6. global $lp;
  7. static $lp;
  8. $stmt = $pdo -> prepare('SELECT * FROM `'.$prefix_mysql.comments.'` WHERE comment = :id_newsa AND qoute_id = :id_news ORDER BY time ASC');
  9. $stmt -> bindValue(':id_newsa', 'news_'.$id_news, PDO::PARAM_INT);
  10. $stmt -> bindValue(':id_news', $rodzic, PDO::PARAM_INT);
  11. $stmt -> execute();
  12. while($wiersz = $stmt -> fetch())
  13. {
  14. $lp++;
  15. $tablica[$lp] = array(id=>$wiersz['id'], content=>htmlentities($wiersz['content']), poziom=>$poziom, datetime=> $wiersz['time'],author=>$wiersz['author']);
  16. drzewoKategorii($pdo,$prefix_mysql, $wiersz['id'], $poziom+1);
  17. }
  18. return $tablica;
  19. }



Czy uważacie że przy większej bazie danych ten kod by się nie spisał? Bo działa w pętli?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 6)
nospor
post
Post #2





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Tu nie masz petli a rekurencje, czyli jeszcze gorzej.
Tak, ten kod jest kiepski. Przy np. 100 zaglebieniach masz 100 zapytan tylko po to by wyswietlic komentarze dla newsa. Strasznie nieoptymalne.
To powinno byc realizowane tylko jednym zapytaniem. Poczytaj o jakis strukturach drzewiastych np. drzewka IP
Go to the top of the page
+Quote Post
snerf
post
Post #3





Grupa: Zarejestrowani
Postów: 74
Pomógł: 3
Dołączył: 30.03.2014

Ostrzeżenie: (0%)
-----


A coś takiego?:

  1. function buildTree(array $elements, $parentId = 0) {
  2. $branch = array();
  3.  
  4. foreach ($elements as $element) {
  5. if ($element['qoute_id'] == $parentId) {
  6. $children = buildTree($elements, $element['id']);
  7. if ($children) {
  8. $element['children'] = $children;
  9. }
  10. $branch[] = $element;
  11. }
  12. }
  13.  
  14. return $branch;
  15. }
  16.  
  17. $stmt = $pdo -> prepare('SELECT * FROM `'.$prefix_mysql.comments.'` WHERE comment = :id_newsa ORDER BY time ASC');
  18. $stmt -> bindValue(':id_newsa', 'news_'.$id_news, PDO::PARAM_INT);
  19. $stmt -> execute();
  20. $wiersz = $stmt ->fetchAll(PDO::FETCH_ASSOC);
  21. $tree = buildTree($wiersz);



W systemie szablonów wyświetlam w następujący sposób:

  1. {% for comment in file.comments %}
  2. <div style="width:870px;">
  3. 1:<b><a href="profil-{{comment.author|get_username}}.html">{{comment.author|get_username}}</a></b> - {{comment.datetime|date("H:i d-m-Y")}}<br>
  4. {{comment.content}}<br>
  5. <a href="news-{{file.get_actual}}-{{file.get_actual3}}-{{comment.id}}.html#answer"><i><font size="2">[odpowiedz]</font></i></a>
  6. </div>
  7. {% for comment2 in comment.children %}
  8. <div style="width:800px;margin-left:70px;">
  9. 2:<b><a href="profil-{{comment2.author|get_username}}.html">{{comment2.author|get_username}}</a></b> - {{comment2.datetime|date("H:i d-m-Y")}}<br>
  10. {{comment2.content}}<br>
  11. <a href="news-{{file.get_actual}}-{{file.get_actual3}}-{{comment2.id}}.html#answer"><i><font size="2">[odpowiedz]</font></i></a>
  12. </div>
  13.  
  14. {% for comment3 in comment2.children %}
  15. <div style="width:730px;margin-left:140px;">
  16. 3:<b><a href="profil-{{comment3.author|get_username}}.html">{{comment3.author|get_username}}</a></b> - {{comment3.datetime|date("H:i d-m-Y")}}<br>
  17. {{comment3.content}}<br>
  18. <a href="news-{{file.get_actual}}-{{file.get_actual3}}-{{comment3.id}}.html#answer"><i><font size="2">[odpowiedz]</font></i></a>
  19. </div>
  20.  
  21. {% for comment4 in comment3.children %}
  22. <div style="width:730px;margin-left:140px;">
  23. 4:<b><a href="profil-{{comment4.author|get_username}}.html">{{comment4.author|get_username}}</a></b> - {{comment4.datetime|date("H:i d-m-Y")}}<br>
  24. {{comment4.content}}<br>
  25. <a href="news-{{file.get_actual}}-{{file.get_actual3}}-{{comment4.id}}.html#answer"><i><font size="2">[odpowiedz]</font></i></a>
  26. </div>
  27. {% endfor %} {% endfor %} {% endfor %}
  28. <a name="last_comment"></a>
  29. {% endfor %}
Go to the top of the page
+Quote Post
nospor
post
Post #4





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Wywaliles zapytanie z rekurencji, czyli masz o niebo lepiej (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
snerf
post
Post #5





Grupa: Zarejestrowani
Postów: 74
Pomógł: 3
Dołączył: 30.03.2014

Ostrzeżenie: (0%)
-----


@nospor
Niby tak ale gryzie mnie wyświetlanie tego w szablonach.

Chciałbym zrobić to jak najładniej bo wychodzą teraz 4 pętle.
Wiem jak użyć jednej ale to mi przysporzy kolejnego problemu, postaram się go rozwiązać sam (IMG:style_emoticons/default/smile.gif)


Dobra dzięki za info o rekurencji i pozdrawiam (IMG:style_emoticons/default/smile.gif)




@edit
Niemogę sobie poradzić z tym problemem a za dużo godzin spędziłem nad tym więc napisze.

Chciałbym zminimalizować kod w szablonach{twig} do wyświetlania np komentarzy z wyżej wymienionej funkcji tworzących drzewka:

  1. {% for comment1 in file.comments %}
  2. <div style="width:870px;">
  3. <b><a href="profil-{{comment1.author|get_username}}.html">{{comment1.author|get_username}}</a></b> - {{comment1.datetime|date("H:i d-m-Y")}}<br>
  4. {{comment1.content}}<br>
  5. <a href="news-{{file.get_actual}}-{{file.get_actual3}}-{{comment1.id}}.html#answer"><i><font size="2">[odpowiedz]</font></i></a>
  6. </div>
  7. {% for comment2 in comment1.children %}
  8. <div style="width:800px;margin-left:70px;">
  9. <b><a href="profil-{{comment2.author|get_username}}.html">{{comment2.author|get_username}}</a></b> - {{comment2.datetime|date("H:i d-m-Y")}}<br>
  10. {{comment2.content}}<br>
  11. <a href="news-{{file.get_actual}}-{{file.get_actual3}}-{{comment2.id}}.html#answer"><i><font size="2">[odpowiedz]</font></i></a>
  12. </div>
  13. {% for comment3 in comment2.children %}
  14. <div style="width:730px;margin-left:140px;">
  15. <b><a href="profil-{{comment3.author|get_username}}.html">{{comment3.author|get_username}}</a></b> - {{comment3.datetime|date("H:i d-m-Y")}}<br>
  16. {{comment3.content}}<br>
  17. <a href="news-{{file.get_actual}}-{{file.get_actual3}}-{{comment3.id}}.html#answer"><i><font size="2">[odpowiedz]</font></i></a>
  18. </div>
  19.  
  20. {% endfor %}
  21. {% endfor %}
  22. {% endfor %}


Wiem że kod brzydki, za każdym razem gdy chce zejść w dół musze tworzyć nową pętle (IMG:style_emoticons/default/thumbsdownsmileyanim.gif)

Ten post edytował snerf 31.03.2014, 14:04:46
Go to the top of the page
+Quote Post
nospor
post
Post #6





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Do wyswietlania tez musisz uzyc rekurencji.

Nie znam twig'a wiec nie wiem czy mozna w nim robic rekurencje. Osobiscie polecalbym ci przygotowac kod HTML w php i wynik przekazac do twig. Wowczas w php bez problemu zrobisz rekurencje do wyswietlania
Go to the top of the page
+Quote Post
snerf
post
Post #7





Grupa: Zarejestrowani
Postów: 74
Pomógł: 3
Dołączył: 30.03.2014

Ostrzeżenie: (0%)
-----


Bodajże rozwiązanie rekurencji w twigu:

  1. {% macro recursiveCategory(category) %}
  2. <li>
  3. <h4><a href="{{ path(category.route, category.routeParams) }}">{{ category }}</a></h4>
  4.  
  5. {% if category.children|length %}
  6. <ul>
  7. {% for child in category.children %}
  8. {{ _self.recursiveCategory(child) }}
  9. {% endfor %}
  10. </ul>
  11. {% endif %}
  12. </li>
  13. {% endmacro %}
  14.  
  15. {% if categories %}
  16. <div id="categories">
  17. <ul>
  18. {% for category in categories %}
  19. {{ _self.recursiveCategory(category) }}
  20. {% endfor %}
  21. </ul>
  22. </div>
  23. {% endif %}


Popróbuję chociaż za bardzo nwm jak się zabrać (IMG:style_emoticons/default/smile.gif)

Pozdrawiam!
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 2.10.2025 - 18:28