Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [MySQL][PHP]Wyświetlanie rekordów z tablicy
Humcio
post 27.11.2014, 22:27:19
Post #1





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 18.09.2014

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


Cześć
Mam mały problem(kolejny).
Pobieram dane z db do tablicy:

  1.  
  2. // Wyświetlanie albumów
  3. require ('common.php');
  4.  
  5. //Pobierz dane z tabeli
  6. $query ="SELECT gallery_cat_id, gallery_cat_name FROM gallery_catalogs";
  7. try{
  8. $stmt=$db->prepare($query);
  9. $stmt->execute();
  10. }
  11. catch (PDOException $ex)
  12. {
  13. die('Nie nawiązano połączenia: ' . $ex->getMessage());
  14. }
  15.  
  16. $album=array();
  17. while( $row = $stmt->fetch(PDO::FETCH_OBJ) )
  18. {
  19. $album[$row->gallery_cat_id] = $row;
  20. }
  21.  
  22. $stmt->closeCursor();
  23.  
  24. echo '<pre>',print_r($album),'</pre>';


Rekordy zostają zapisane

  1. Array
  2. (
  3. [8] => stdClass Object
  4. (
  5. [gallery_cat_id] => 8
  6. [gallery_cat_name] => Album_Matura_2014
  7. )
  8.  
  9. [9] => stdClass Object
  10. (
  11. [gallery_cat_id] => 9
  12. [gallery_cat_name] => Testowy_album
  13. )
  14.  
  15. )


I teraz moje pytanie, jak wyświetlić wszystkie elementy tablicy, ale po prostu jako:

Album_Matura_2014
Testowy_album
Go to the top of the page
+Quote Post
kamilo818
post 27.11.2014, 22:43:49
Post #2





Grupa: Zarejestrowani
Postów: 250
Pomógł: 11
Dołączył: 20.01.2014

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


np w petli
  1. for($i=0;$i<=count($album);$i++){
  2. echo $album[$i];
  3. }


Ten post edytował kamilo818 27.11.2014, 22:45:11
Go to the top of the page
+Quote Post
Humcio
post 27.11.2014, 22:56:16
Post #3





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 18.09.2014

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


Próbowałem czegoś podobnego (męczę się z tym od prawie dwóch godzin), ale nie działa, dostaje komunikat:
  1. Undefined offset: 0

dla linijki
  1. echo $album[$i];

Go to the top of the page
+Quote Post
kamilo818
post 27.11.2014, 22:57:40
Post #4





Grupa: Zarejestrowani
Postów: 250
Pomógł: 11
Dołączył: 20.01.2014

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


co ci wyswietla print_r($album)?
Go to the top of the page
+Quote Post
Humcio
post 27.11.2014, 22:59:33
Post #5





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 18.09.2014

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


print_r($album) wyswietla:

  1. Array
  2. (
  3. [8] => stdClass Object
  4. (
  5. [gallery_cat_id] => 8
  6. [gallery_cat_name] => Album_Matura_2014
  7. )
  8.  
  9. [9] => stdClass Object
  10. (
  11. [gallery_cat_id] => 9
  12. [gallery_cat_name] => Testowy_album
  13. )
  14.  
  15. )
  16. 1


Ten post edytował Humcio 27.11.2014, 23:00:03
Go to the top of the page
+Quote Post
kamilo818
post 27.11.2014, 23:02:24
Post #6





Grupa: Zarejestrowani
Postów: 250
Pomógł: 11
Dołączył: 20.01.2014

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


No to Undefined offset: 0 bo jak widzisz masz wartosci 8 i 9.

Zrob
  1. for($i=8;$i<=9;$i++){
  2. echo $album[$i];
  3. }
Go to the top of the page
+Quote Post
Humcio
post 27.11.2014, 23:17:45
Post #7





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 18.09.2014

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


Pomogło, teraz tylko musze znaleźć sposób na zapisanie objektu do stringa:

  1. Catchable fatal error: Object of class stdClass could not be converted to string


Chyba, że jest lepszy sposób na zapis danych z db do tablicy niż

  1. $album=array();
  2. while( $row = $stmt->fetch(PDO::FETCH_OBJ) )
  3. {
  4. $album[$row->gallery_cat_id] = $row;
  5. }


Ten post edytował Humcio 27.11.2014, 23:30:25
Go to the top of the page
+Quote Post
Turson
post 28.11.2014, 08:02:16
Post #8





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


fetchAll
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 13.07.2025 - 06:18