Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php/smarty]Problem z "pętlą"
Forum PHP.pl > Forum > PHP
zlw
Mam pewien problem. Chcę wyświetlić na stronie głównej newsy pobrane z bazy.

Tak więc kod PHP wygląda tak:

  1. <?php
  2. $result = mysql_query("SELECT news_num FROM cms_config")
  3. or die("Niepoprawne zapytanie MySQL");
  4. $news_num = mysql_fetch_array($result);
  5.  
  6. $result = mysql_query("SELECT title,author, date, news_id, body_short, body_long FROM cms_news ORD
    ER BY date DESC"
    )
  7. or die("Niepoprawne zapytanie MySQL");
  8. $number = mysql_num_rows($result);
  9.  
  10.  
  11.  
  12. if ($number > $news_num[news_num] )
  13. {
  14. $liczba_news = $news_num[news_num];
  15. }
  16. else
  17. {
  18. $liczba_news = $number;
  19. }
  20.  while ($n <> $liczba_news )
  21. {
  22.  
  23. $news = mysql_fetch_array($result);
  24. $n = $n + 1;
  25. $data = date('d.m.Y H:i', strtotime($news[date]));
  26.  
  27. $smarty->assign('news_number',$liczba_news);
  28. $smarty->assign('news_title',$news[title]);
  29. $smarty->assign('news_short',$news[body_short]);
  30. $smarty->assign('news_author',$news[author]);
  31. $smarty->assign('news_id',$news[news_id]);
  32. $smarty->assign('news_date',$data);
  33. $smarty->assign('news_long',$news[body_long]);
  34. ?>



Kod "smarków":

  1. {section name=customer loop=$news_number}
  2. <table border=0>
  3. <tr>
  4. <td width=140 id=title><b><font size=2 face=Arial>{$news_title}</font></b><hr></td>
  5. </tr>
  6. <tr>
  7. <td height=55 width=280 valign=top>{$news_short}</td></tr>
  8. <tr>
  9. <td font><b>Autor:</b> {$news_author}<br><b>Data:</b> {$news_date}</td>
  10. </tr>
  11. {if $news_long <> ""}
  12. <tr><td><a href=?news_id={$news_id}>Czytaj więcej</a></td></tr>
  13. {/if}
  14. <br><br>
  15. </font>
  16. {/section}


Efekt jest taki, że wyświetla co prawda dobrą liczbę news'ów, ale są to te same newsy.
Hacker
Po pierwsze nie $tablica[klucz(string)] tylko $tablica['klucz(string)'].
Po drugie
  1. <?php
  2. $smarty->assign('news_number',$liczba_news);
  3. $smarty->assign('news_title',$news[title]);
  4. $smarty->assign('news_short',$news[body_short]);
  5. $smarty->assign('news_author',$news[author]);
  6. $smarty->assign('news_id',$news[news_id]);
  7. $smarty->assign('news_date',$data);
  8. $smarty->assign('news_long',$news[body_long]);
  9. ?>

Czy nie zdaje Ci się, że przy każdym obrocie pętli zmieniasz wartości tych zmiennych? Niech zgadnę, zawsze wyświetla ostatniego news-a? Skoro chcesz, żeby news_title itp. zawierały tablicę danych ze wszystkich newsów to drugim argumentem assign powinna być tablica... Gotowego rozwiązania Ci nie podam. Pomyśl sam (i/lub zapytaj google)!
zlw
  1. <?php
  2. $smarty->assign('newsy',array("news_num" => $liczba_news, "news_title" => $news[title], "body_short" => $news[body_short], "author" => $news[author], "news_id" => $news[news_id], "news_date" => $data, "body_long" => $news[body_long] ));
  3. ?>


Czy powinno być to coś w tym stylu, bo wogóle nie rozumiem sadsmiley02.gif
Hacker
Wrzuć przed while
  1. <?php
  2. $smarty->assign('news_number',$liczba_news);
  3. $newstitle = array();
  4. $newsbody_short = array();
  5. $newsauthor = array();
  6. $newsid = array();
  7. $newsdate = array();
  8. $newsbody_long = array();
  9. ?>

a w while
  1. <?php
  2. $newstitle[] = $news['title'];
  3. $newsbody_short[] = $news['body_short'];
  4. $newsauthor[] = $news['author'];
  5. $newsid[] = $news['news_id'];
  6. $newsdate[] = $data;
  7. $newsbody_long[] = $news['body_long'];
  8. ?>

i po
  1. <?php
  2. $smarty->assign('news_title',$newstitle);
  3. $smarty->assign('news_short',$newsbody_short']);
  4. $smarty->assign('news_author',$newsauthor']);
  5. $smarty->assign('news_id',$newsid);
  6. $smarty->assign('news_date',$newsdate);
  7. $smarty->assign('news_long',$newsbody_long);
  8. ?>


a smarty
  1. {section name=news loop=$news_number}
  2. <table border=0>
  3. <tr>
  4. <td width=140 id=title><b><font size=2 face=Arial>{$news_title[news]}</font></b><hr></td>
  5. </tr>
  6. <tr>
  7. <td height=55 width=280 valign=top>{$news_short[news]}</td></tr>
  8. <tr>
  9. <td font><b>Autor:</b> {$news_author[news]}<br><b>Data:</b> {$news_date[news]}</td>
  10. </tr>
  11. {if $news_long[news] <> ""}
  12. <tr><td><a href=?news_id={$news_id[news]}>Czytaj więcej</a></td></tr>
  13. {/if}
  14. <br><br>
  15. </font>
  16. {/section}
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.