Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP][MYSQL] Powtarzające się dane.
-Beginner-
post
Post #1





Goście







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
Go to the top of the page
+Quote Post
Spirit86
post
Post #2





Grupa: Zarejestrowani
Postów: 607
Pomógł: 23
Dołączył: 8.09.2004
Skąd: Wrocław

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


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.
Go to the top of the page
+Quote Post
-Beginner-
post
Post #3





Goście







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.
Go to the top of the page
+Quote Post
tiraeth
post
Post #4





Grupa: Przyjaciele php.pl
Postów: 1 789
Pomógł: 41
Dołączył: 30.10.2003
Skąd: Wrocław

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


Ja Ci odpowiem - bo nadpisujesz zmienne. Przypisanie do obiektu smarty rób w pętli while...
Go to the top of the page
+Quote Post
Spirit86
post
Post #5





Grupa: Zarejestrowani
Postów: 607
Pomógł: 23
Dołączył: 8.09.2004
Skąd: Wrocław

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


  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.
Go to the top of the page
+Quote Post
-Beginner-
post
Post #6





Goście







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. ?>
Go to the top of the page
+Quote Post
Spirit86
post
Post #7





Grupa: Zarejestrowani
Postów: 607
Pomógł: 23
Dołączył: 8.09.2004
Skąd: Wrocław

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


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. ?>
Go to the top of the page
+Quote Post
-Beginner-
post
Post #8





Goście







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
Go to the top of the page
+Quote Post
nospor
post
Post #9





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




jak czytam ten watek to nasuwa mi sie jedna mysl: prowadził ślepy głuchego (IMG:http://forum.php.pl/style_emoticons/default/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}
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: 23.08.2025 - 18:30