Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][MYSQL] Powtarzające się dane.
Forum PHP.pl > Forum > Przedszkole
Beginner
Witam,
już na wstępie zaznaczam iż jestem początkujący, przeszukałem forum, lecz nie znalazłem rozwiązania mającego zastosowanie w moim problemie.
Przejdźmy do sedna sprawy, zacząłem kleić system newsów jednakże dość szybko napotkałem trudności, mianowicie poniższy kod zamiast wyświetlania po kolei wyników, pobiera cały czas ten sam rekord. Może ktoś z Was zna rozwiązanie tego problemu?

  1. <?php
  2. $max_items = "5";
  3. $query = "SELECT * FROM news ORDER BY date DESC LIMIT $max_items";
  4. $result = mysql_query ($query);
  5.  
  6. while($row=mysql_fetch_assoc ($result)){
  7. $id = $row['id'];
  8. $title = $row['title'];
  9. $date = $row['date'];
  10. $img = $row['img'];
  11. $text = $row['text'];
  12. $author = $row['author'];
  13. }
  14. ?>


Pozdrawiam
Spirit86
Ten kod nie powinnien pobierać cały czas tego samego. Sprawdź resztę kodu, bo jak wykorzystujesz $title poza pętlą to przecież on jest przypisany tylko przy ostatnim elemencie z pętli.


  1. <?php
  2. $max_items = "5";
  3. $query = "SELECT * FROM news ORDER BY date DESC LIMIT $max_items";
  4. $result = mysql_query ($query);
  5.  
  6. while($row=mysql_fetch_assoc ($result)){
  7. $id = $row['id'];
  8. $title = $row['title'];
  9. $date = $row['date'];
  10. $img = $row['img'];
  11. $text = $row['text'];
  12. $author = $row['author'];
  13. echo '<pre>';
  14. print_r($row);
  15. echo '</pre>';
  16. }
  17. ?>


Pokaż wynik tej operacji (najprawdopodobniej będziesz miał 5 różnych tablic wylistowanych.
Beginner
Rzeczywiście, wynikiem tej operacji są dwa inne newsy.
  1. <?php
  2. (
  3.    [id] => 18
  4.    [title] => title
  5.    [date] => 2008-09-21 201230
  6.    [img] => img
  7.    [text] => text
  8.    [author] => author
  9. )
  10.  
  11. (
  12.    [id] => 16
  13.    [title] => 2222
  14.    [date] => 2008-09-21 190327
  15.    [img] => 2222
  16.    [text] => 22222
  17.    [author] => 2222
  18. )
  19. ?>


Zatem jaki popełniłem błąd iż nie wyświetla tego prawidłowo ?
Pracuję na szablonach Smarty, dorzucę kod:
  1. <?php
  2. $aproject -> assign('texts1', 'jakis tekst   '.$id.'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst');
  3. $aproject -> assign('texts2', 'jakis tekst  '.$id.'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst');
  4. ?>


W pliku .tpl
  1. {$texts1}
  2. {$texts2}

Za wszelkie pomysły z góry dzięki.
tiraeth
Ja Ci odpowiem - bo nadpisujesz zmienne. Przypisanie do obiektu smarty rób w pętli while...
Spirit86
  1. <?php
  2. $max_items = "5";
  3. $query = "SELECT * FROM news ORDER BY date DESC LIMIT $max_items";
  4. $result = mysql_query ($query);
  5.  
  6. while($row=mysql_fetch_assoc ($result)){
  7. $id = $row['id'];
  8. $title = $row['title'];
  9. $date = $row['date'];
  10. $img = $row['img'];
  11. $text = $row['text'];
  12. $author = $row['author'];
  13.  
  14. $aproject -> assign('texts1', 'jakis tekst   '.$id.'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst');
  15. $aproject -> assign('texts2', 'jakis tekst  '.$id.'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst');
  16.  
  17. }
  18. ?>


Musisz podopisywać ew. operacje na templatach, nie korzystam z Smart, więc nie wiem co tam jeszcze powinno być, chyba funkcja pharsująca.
Beginner
Niestety nadal to samo...
  1. <?php
  2. // Config //
  3. require_once('Smarty/Smarty.class.php');
  4. require ("config_db.php");
  5. connectdb();
  6. $aproject =  new Smarty;
  7. $aproject -> template_dir = "HeadTemplates/";
  8. $aproject -> compile_dir  = 'SmartyTemp/templates_c/';
  9. $aproject -> config_dir   = 'SmartyTemp/configs/';
  10. $aproject -> cache_dir    = 'SmartyTemp/cache/';
  11. // EOF Config //
  12.  
  13. $max_items = "5";
  14. $query = "SELECT * FROM news ORDER BY id DESC LIMIT $max_items";
  15. $result = mysql_query ($query);
  16.  
  17. while($row=mysql_fetch_assoc ($result))
  18. {
  19. $id = $row['id'];
  20. $title = $row['title'];
  21. $date = $row['date'];
  22. $img = $row['img'];
  23. $text = $row['text'];
  24. $author = $row['author'];
  25. $aproject -> assign('texts1', 'jakis tekst   '.$row['id'].'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst<br />');
  26. $aproject -> assign('texts2', 'jakis tekst  '.$row['id'].'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst<br />');
  27.  
  28. }
  29.  
  30.  
  31. // Config - Display  //
  32. $aproject -> display('index.tpl');
  33. require ("footer.php");
  34. // EOF Config - Display //
  35. ?>
Spirit86
jak już to:
  1. <?php
  2. // Config //
  3. require_once('Smarty/Smarty.class.php');
  4. require ("config_db.php");
  5. connectdb();
  6. $aproject =  new Smarty;
  7. $aproject -> template_dir = "HeadTemplates/";
  8. $aproject -> compile_dir  = 'SmartyTemp/templates_c/';
  9. $aproject -> config_dir   = 'SmartyTemp/configs/';
  10. $aproject -> cache_dir    = 'SmartyTemp/cache/';
  11. // EOF Config //
  12.  
  13. $max_items = "5";
  14. $query = "SELECT * FROM news ORDER BY id DESC LIMIT $max_items";
  15. $result = mysql_query ($query);
  16.  
  17. while($row=mysql_fetch_assoc ($result))
  18. {
  19. $id = $row['id'];
  20. $title = $row['title'];
  21. $date = $row['date'];
  22. $img = $row['img'];
  23. $text = $row['text'];
  24. $author = $row['author'];
  25. $aproject -> assign('texts1', 'jakis tekst   '.$row['id'].'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst<br />');
  26. $aproject -> assign('texts2', 'jakis tekst  '.$row['id'].'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst<br />');
  27. $aproject -> display('index.tpl');
  28. }
  29.  
  30.  
  31. // Config - Display  //
  32.  
  33. require ("footer.php");
  34. // EOF Config - Display //
  35. ?>
Beginner
Można powiedzieć, że działa w 50%, bowiem zawartość index.tpl wyświetla się tyle razy ile jest rekordów ( w tym przypadku 4 ), dokładniej to wygląda tak:

I strona:
  1. <?php
  2. $aproject -> assign('texts1', 'jakis tekst   '.$id.'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst<br />');
  3. $aproject -> assign('texts2', 'jakis tekst  '.$id.'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst<br />');
  4. ?>

wyświetla:
  1. jakis tekst 20.1.2008-09-21 21:57:34..333.444 jakis tekst
  2. jakis tekst 20.1.2008-09-21 21:57:34..333.444 jakis tekst


II strona:
  1. <?php
  2. $aproject -> assign('texts1', 'jakis tekst   '.$id.'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst<br />');
  3. $aproject -> assign('texts2', 'jakis tekst  '.$id.'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst<br />');
  4. ?>

wyświetla:
  1. jakis tekst 19.344.2008-09-21 21:56:13..444.444 jakis tekst
  2. jakis tekst 19.344.2008-09-21 21:56:13..444.444 jakis tekst


III strona:
  1. <?php
  2. $aproject -> assign('texts1', 'jakis tekst   '.$id.'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst<br />');
  3. $aproject -> assign('texts2', 'jakis tekst  '.$id.'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst<br />');
  4. ?>

wyświetla:
  1. jakis tekst 18.title.2008-09-21 20:12:30.img.text.author jakis tekst
  2. jakis tekst 18.title.2008-09-21 20:12:30.img.text.author jakis tekst


IV strona:
  1. <?php
  2. $aproject -> assign('texts1', 'jakis tekst   '.$id.'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst<br />');
  3. $aproject -> assign('texts2', 'jakis tekst  '.$id.'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst<br />');
  4. ?>

wyświetla:
  1. jakis tekst 16.2222.2008-09-21 19:03:27.2222.22222.2222 jakis tekst
  2. jakis tekst 16.2222.2008-09-21 19:03:27.2222.22222.2222 jakis tekst
nospor
jak czytam ten watek to nasuwa mi sie jedna mysl: prowadził ślepy głuchego winksmiley.jpg

  1. <?php
  2. $tablica = array();
  3. while($row=mysql_fetch_assoc ($result))
  4. {
  5. $id = $row['id'];
  6. $title = $row['title'];
  7. $date = $row['date'];
  8. $img = $row['img'];
  9. $text = $row['text'];
  10. $author = $row['author'];
  11. $tablica[] = 'jakis tekst   '.$row['id'].'.'.$title.'.'.$date.'.'.$img.'.'.$text.'.'.$author.'   jakis tekst<br />';
  12. }
  13. $aproject -> assign('tablica', $tablica);
  14. $aproject -> display('index.tpl');
  15. ?>

A w szablonie smartiego:
Kod
{foreach from=$tablica item="elem"}
{$elem}
{/foreach}
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.