PHP Forum

PHP Forum (http://www.selfphp.de/forum/index.php)
-   PHP Grundlagen (http://www.selfphp.de/forum/forumdisplay.php?f=12)
-   -   Tabelle: Eine Variable?, 2 Zeilen (http://www.selfphp.de/forum/showthread.php?t=24515)

Sin84 17.12.2011 21:43:28

Tabelle: Eine Variable?, 2 Zeilen
 
Ich hoffe, ich bin hier richtig.

Und ich hoffe, dass ich mein Problem verständlich schildern kann.

Mehrere Images möchte ich in einer Tabelle in 2 Spalten anzeigen. Die Images befinden sich bisher in einer Zeile.
Da ich recht wenig Ahnung von PHP und MySQL habe, sitze ich schon seit gestern an dem scheiß Problem und komme nicht weiter.

Hier erstmal Code:

PHP-Code:

if ($z1_artikel!='0') {
print (
"<table>");
$sql2_topartikel "SELECT * FROM topartikel ORDER by top_id";
$res2_topartikel mysql_query($sql2_topartikel);
while(
$row mysql_fetch_assoc($res2_topartikel)) {
$top_id $row['top_id']; $top_artid $row['top_artid'];
$sql2_artikel "SELECT * FROM artikel WHERE art_id = '$top_artid'";
$res2_artikel mysql_query($sql2_artikel);      
while(
$row mysql_fetch_assoc($res2_artikel)) {
$art_id $row['art_id']; $art_kat $row['art_kat']; $art_nr $row['art_nr']; $art_titel $row['art_titel']; $art_img $row['art_img']; $art_imgw $row['art_imgw']; $art_imgh $row['art_imgh']; $art_datei $row['art_datei']; $art_size $row['art_size']; $art_demo $row['art_demo']; $art_descr $row['art_descr']; $art_preis1 $row['art_preis1']; $art_preis2 $row['art_preis2']; $art_preis3 $row['art_preis3']; $art_preis4 $row['art_preis4']; $art_liz1 $row['art_liz1']; $art_liz2 $row['art_liz2']; $art_liz3 $row['art_liz3']; $art_liz4 $row['art_liz4'];
if (
$art_img=='') {$image="img/noimage150.png"$breite=150$hoehe=120;}
else {
$image="artikel/".$art_img;
if (
$art_imgw>=$art_imgh) {
    
$breite "320";    $brprozent = ((110 $breite) / $art_imgw);
    
$hoehe = (($art_imgh $brprozent) / 75);    $hoehe = (ceil ($hoehe));
}
else {    
$hoehe "120";    $brprozent = ((100 $hoehe) / $art_imgh);
    
$breite = (($art_imgw $brprozent) / 100);    $breite = (ceil ($breite));
}
}
print (
"<td width=340>
<table width=340 border=0 cellpadding=0 cellspacing=0>
<tr><td colspan=2 height=130 style='border:1px solid #CCCCCC;' align=center><a href='details.php?session=$session&art=$art_id&kat=$kat&next=$next&page=$page&back=index'><img src='$image' width='$breite' height='$hoehe' border='0'></a></td></tr>
<tr><td id='space' height=2>&nbsp;</td></tr>
<tr><td><a href='details.php?session=$session&art=$art_id&kat=$kat&next=$next&page=$page&back=index'><img src='img/bt-detailsind.png' width='164' height='23' border='0'></a></td></tr>
</table>
</td>"
);
if (
$y!=3) {
print (
"<td width=9 id='space'>&nbsp;</td>");
}
}
$y++;
}
print (
"</tr>");



Wahrscheinlich sind gerade mal die ersten 8 Zeilen relevant, aber vorsichtshalber habe ich die komplette if Abfrage gepostet.

Mithilfe des Codes werden 3 Images aufgerufen, die nebeneinander angezeigt werden. Ich werde die Zahl der Images auf 4 erweitern und möchte diese dann in 2 Reihen nebeneinander anzeigen lassen. Also erste Reihe 2 Bilder. Zweite Reihe 2 Bilder.
Wenn ich echo "<tr>"; im Code einfüge, erreiche ich zumindest schon mal, dass alle Images untereinander gezeigt werden. Aber ganz so will ich das ja nicht. :)

PHP-Code:

if ($z1_artikel!='0') {
print (
"<table>");
$sql2_topartikel "SELECT * FROM topartikel ORDER by top_id";
$res2_topartikel mysql_query($sql2_topartikel);
while(
$row mysql_fetch_assoc($res2_topartikel)) {
$top_id $row['top_id']; $top_artid $row['top_artid'];
$sql2_artikel "SELECT * FROM artikel WHERE art_id = '$top_artid'";
$res2_artikel mysql_query($sql2_artikel);

echo 
"<tr>";

while(
$row mysql_fetch_assoc($res2_artikel)) { 


Hilfe. Bitte!

meikel (†) 17.12.2011 23:37:01

AW: Tabelle: Eine Variable?, 2 Zeilen
 
Zitat:

Zitat von Sin84 (Beitrag 142611)
Ich hoffe, ich bin hier richtig.

Hier ist MySQL. Du suchst PHP + HTML.

Zitat:

Mithilfe des Codes werden 3 Images aufgerufen, die nebeneinander angezeigt werden. Ich werde die Zahl der Images auf 4 erweitern und möchte diese dann in 2 Reihen nebeneinander anzeigen lassen. Also erste Reihe 2 Bilder. Zweite Reihe 2 Bilder.
Wenn ich echo "<tr>"; im Code einfüge, erreiche ich zumindest schon mal, dass alle Images untereinander gezeigt werden. Aber ganz so will ich das ja nicht. :)
So soll es wahrscheinlich aussehen:
Code:

<table>
<tr>
  <td>Spalte 1</td>
  <td>Spalte 2</td>
</tr>
<tr>
...
</tr>
</table>

Du benötigst also einen Spaltenzähler, der nach 2 Spalten das einfügt:
Code:

</tr><tr>
Läßt sich mit dem Modulo Operator ermitteln:
PHP-Code:

$cols 2# Anzahl der Spalten
$d = new mysqli(hostuserpassdbname);

$r $d->query('SELECT .... ');

$f 0# Feldzähler

echo '<table><tr>';
while(
$row $r->fetch_assoc()){
  
$f++;
  if (! 
$f $cols)
    echo 
'</tr><tr>';
  echo 
'<td>';
  echo 
mach_was($row); # Ausgabe des img Tags
  
echo '</td>';
}
echo 
'</tr></table>'

Die Zeile:
PHP-Code:

  if (! $f $cols

fragt ab, ob es bei $f / $cols einen Rest gibt. Falls nicht, wird eine neue Zeile erzeugt.

Sin84 18.12.2011 12:28:10

AW: Tabelle: Eine Variable?, 2 Zeilen
 
Hi. Danke für deine Mühe, aber das funktioniert leider gar nicht. Gebe ich deinen Code unverändert ein, (abgesehen davon, dass ich host,user,pass,dbname mit meinen Daten ersetze) bekomme ich die Fehlermeldung:

Fatal error: Call to a member function fetch_assoc() on a non-object in bla bla

Die Kombi und alle anderen

PHP-Code:


$r 
$d->query("SELECT * FROM topartikel ORDER by top_id");

while(
$row $r->fetch_assoc($res2_topartikel)){ 

geben folgende Fehlermeldung aus:

Warning: mysqli_result::fetch_assoc() expects exactly 0 parameters, 1 given in bla bla
Warning: mysqli_result::fetch_assoc() expects exactly 0 parameters, 1 given in bla bla
Warning: mysqli_result::fetch_assoc() expects exactly 0 parameters, 1 given in bla bla


Hilfe? :(

meikel (†) 18.12.2011 13:20:04

AW: Tabelle: Eine Variable?, 2 Zeilen
 
Zitat:

Zitat von Sin84 (Beitrag 142613)
Hi. Danke für deine Mühe, aber das funktioniert leider gar nicht.

Das funktioniert deshalb nicht, weil Deine Veränderungen falsch sind.

PHP-Code:

$r $d->query("SELECT * FROM topartikel ORDER by top_id");
while(
$row $r->fetch_assoc($res2_topartikel)){ 

$r ist das Resultobjekt der Abfrage. Die Methode fetch_assoc der Resultklasse benötigt keinen Parameter.

Zitat:

Fatal error: Call to a member function fetch_assoc() on a non-object in bla bla
Diese Fehlermeldung kommt genau dann, wenn die Abfrage fehlerhaft ist. Die Query Methode der MySQLi Instanz liefert nur dann das gewünschte Objekt, wenn der MySQL Server die Abfrage fehlerfrei durchführen konnte.
Zitat:

Return Values

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a MySQLi_Result object. For other successful queries mysqli_query() will return TRUE.
Solche Fehler fängt man wie folgt ab:
PHP-Code:

if (false !== ($r $d->query("SELECT * FROM topartikel ORDER by top_id"))){
  while(
$row $r->fetch_assoc($res2_topartikel)){
  
  }
}else{
  
printf("Errormessage: %s<br />"$d->error);



Sin84 19.12.2011 16:32:21

AW: Tabelle: Eine Variable?, 2 Zeilen
 
Also eine Fehlermeldung wird jetzt nicht mehr angezeigt. Je nachdem wo ich deinen Code einfüge, werden die Wörter "Array Array" entweder nebeneinander oder untereinander angezeigt.

Ich sehe wohl den Wald vor lauter Bäumen nicht. Auf jeden Fall funktioniert es nicht.
An welche Stelle muss ich denn den Code einfügen? Muss ich irgendwas ersetzen?

Was muss ich denn jetzt genau machen? *verzweifel*

PHP-Code:

if ($z1_artikel!='0') {
print (
"<td>");
$sql2_topartikel "SELECT * FROM topartikel ORDER by top_id";
$res2_topartikel mysql_query($sql2_topartikel);


$cols 2# Anzahl der Spalten
$d = new mysqli(hostuserpassdbname);

$r $d->query("SELECT * FROM topartikel ORDER by top_id");

$f 0# Feldzähler

echo '<table><tr>';
while(
$row $r->fetch_assoc()){
  
$f++;
  if (! 
$f $cols)
    echo 
'</tr><tr>';
  echo 
'<td>';
  echo (
$row); # Ausgabe des img Tags
  
echo '</td>';
}
echo 
'</tr></table>';  

if (
false !== ($r $d->query("SELECT * FROM topartikel ORDER by top_id"))){ 
  while(
$row $r->fetch_assoc()){ 
   
  } 
}else{ 
  
printf("Errormessage: %s<br />"$d->error); 
}  


while(
$row mysql_fetch_assoc($res2_topartikel)) { 


meikel (†) 19.12.2011 18:01:54

AW: Tabelle: Eine Variable?, 2 Zeilen
 
Zitat:

Zitat von Sin84 (Beitrag 142627)
Also eine Fehlermeldung wird jetzt nicht mehr angezeigt. Je nachdem wo ich deinen Code einfüge, werden die Wörter "Array Array" entweder nebeneinander oder untereinander angezeigt.

Ich sehe wohl den Wald vor lauter Bäumen nicht. Auf jeden Fall funktioniert es nicht.

Das funktioniert deshalb nicht, weil es falsch ist, was Du machst.

Ich lösche alles mal weg, was nichts mit der Tabellendarstellung zu tun hat.
PHP-Code:

$cols 2# Anzahl der Spalten
$d = new mysqli(hostuserpassdbname);

$r $d->query("SELECT * FROM topartikel ORDER by top_id");

$f 0# Feldzähler

echo '<table><tr>';
while(
$row $r->fetch_assoc()){
  
$f++;
  if (! 
$f $cols)
    echo 
'</tr><tr>';
  echo 
'<td>';
  echo (
$row); # Ausgabe des img Tags  <-- FALSCH
  
echo '</td>';
}
echo 
'</tr></table>'

Das ist falsch:
Zitat:

echo ($row); # <-- FALSCH
$row ist ein assoziatives Array und kein String. Wie die einsame Spalte heißt, die Du ausgeben willst, weiß ich nicht, weil in Deiner SQL Abfrage sinnfreierweise ein * steht.

Sin84 19.12.2011 20:18:36

AW: Tabelle: Eine Variable?, 2 Zeilen
 
Danke. Jetzt werden immerhin schon mal die Images angezeigt!

Es geht eigentlich um 2 Tabellen, in denen Images untergebracht sind. Ich habe bisher immer nur den Code der ersten gepostet.

Leider werden die Images immer noch falsch angezeigt.

Ich wollte die Images ja so anordnen:

Image1 Image2
Image3 Image4

Leider werden sie in Tabelle 1 jetzt untereinander angeordnet:

Image1
Image2
Image3
Image4

In Tabelle 2 wird in jeder Reihe das Image 4 Mal angezeigt. also:

Image1 Image1 Image1 Image1
Image2 Image2 Image2 Image2
Image3 Image3 Image3 Image3
Image4 Image4 Image4 Image4


Hier nochmal Code:

PHP-Code:

if ($z1_artikel!='0') {
print (
"<td>");
$sql2_topartikel "SELECT * FROM topartikel ORDER by top_id";
$res2_topartikel mysql_query($sql2_topartikel);
while(
$row mysql_fetch_assoc($res2_topartikel)) {
$top_id $row['top_id']; $top_artid $row['top_artid'];
$sql2_artikel "SELECT * FROM artikel WHERE art_id = '$top_artid'";
$res2_artikel mysql_query($sql2_artikel);
while(
$row mysql_fetch_assoc($res2_artikel)) {
$art_id $row['art_id']; $art_kat $row['art_kat']; $art_nr $row['art_nr']; $art_titel $row['art_titel']; $art_img $row['art_img']; $art_imgw $row['art_imgw']; $art_imgh $row['art_imgh']; $art_datei $row['art_datei']; $art_size $row['art_size']; $art_demo $row['art_demo']; $art_descr $row['art_descr']; $art_preis1 $row['art_preis1']; $art_preis2 $row['art_preis2']; $art_preis3 $row['art_preis3']; $art_preis4 $row['art_preis4']; $art_liz1 $row['art_liz1']; $art_liz2 $row['art_liz2']; $art_liz3 $row['art_liz3']; $art_liz4 $row['art_liz4'];
if (
$art_img=='') {$image="img/noimage150.png"$breite=150$hoehe=120;}
else {
$image="artikel/".$art_img;
if (
$art_imgw>=$art_imgh) {
    
$breite "331";    $brprozent = ((110 $breite) / $art_imgw);
    
$hoehe = (($art_imgh $brprozent) / 75);    $hoehe = (ceil ($hoehe));
}
else {    
$hoehe "120";    $brprozent = ((100 $hoehe) / $art_imgh);
    
$breite = (($art_imgw $brprozent) / 100);    $breite = (ceil ($breite));
}
}


$cols 2# Anzahl der Spalten
$d = new mysqli(hostuserpassdbname);

$r $d->query("SELECT * FROM artikel WHERE art_id = '$top_artid'");

$f 0# Feldzähler

echo '<table><tr>';
while(
$row $r->fetch_assoc()){
  
$f++;
  if (! 
$f $cols)
    echo 
'</tr><tr>';
  echo 
'<td>';
  echo (
"<td width=351>
<table width=351 border=0 cellpadding=0 cellspacing=0>
<tr><td colspan=2 height=130 style='border:1px solid #CCCCCC;' align=center><a href='details.php?session=$session&art=$art_id&kat=$kat&next=$next&page=$page&back=index'><img src='$image' width='$breite' height='$hoehe' border='0'></a></td></tr>
<tr><td id='space' height=2>&nbsp;</td></tr>
<tr><td><a href='details.php?session=$session&art=$art_id&kat=$kat&next=$next&page=$page&back=index'><img src='img/bt-detailsind.png' width='164' height='23' border='0'></a></td></tr>
</table>
</td>"
); # Ausgabe des img Tags  <-- FALSCH #Tabelle 1
  
echo '</td>';
}
echo 
'</tr></table>'

if (
false !== ($r $d->query("SELECT * FROM artikel WHERE art_id = '$top_artid'"))){ 
  while(
$row $r->fetch_assoc()){ 
   
  } 
}else{ 
  
printf("Errormessage: %s<br />"$d->error); 
}  


if (
$y!=3) {
print (
"<td width=9 id='space'>&nbsp;</td>");
}
}
$y++;
}
print (
"</tr>");

}
else {
print (
"<tr><td>Wir haben leider noch keine Artikel im Angebot!</td></tr>");
}
print (
"</table>
</td></tr>
</table>
</td></tr>

<tr><td id='space' height=10>&nbsp;</td></tr>
<tr><td id='tabrighthead'>Ganz neu im Sortiment</td></tr>
<tr><td id='space' height=10>&nbsp;</td></tr>
<tr><td>
<table id='tabiright' align=center border=0 cellpadding=0 cellspacing=0>
<tr><td id='conth100' valign=top>
<table id='content' align=center border=0 cellpadding=0 cellspacing=0>"
);
$sql1_artikel "SELECT * FROM artikel";
$res1_artikel mysql_query($sql1_artikel);
$z1_artikel mysql_num_rows($res1_artikel);
$x=1;
if (
$z1_artikel!='0') {
print (
"<td>");
$sql2_artikel "SELECT * FROM artikel ORDER by art_id DESC limit 0,4";
$res2_artikel mysql_query($sql2_artikel);
while(
$row mysql_fetch_assoc($res2_artikel)) {
$art_id $row['art_id']; $art_kat $row['art_kat']; $art_nr $row['art_nr']; $art_titel $row['art_titel']; $art_img $row['art_img']; $art_imgw $row['art_imgw']; $art_imgh $row['art_imgh']; $art_datei $row['art_datei']; $art_size $row['art_size']; $art_demo $row['art_demo']; $art_descr $row['art_descr']; $art_preis1 $row['art_preis1']; $art_preis2 $row['art_preis2']; $art_preis3 $row['art_preis3']; $art_preis4 $row['art_preis4']; $art_liz1 $row['art_liz1']; $art_liz2 $row['art_liz2']; $art_liz3 $row['art_liz3']; $art_liz4 $row['art_liz4'];
if (
$art_img=='') {$image="img/noimage150.png"$breite=150$hoehe=120;}
else {
$image="artikel/".$art_img;
if (
$art_imgw>=$art_imgh) {
    
$breite "331";    $brprozent = ((110 $breite) / $art_imgw);
    
$hoehe = (($art_imgh $brprozent) / 75);    $hoehe = (ceil ($hoehe));
}
else {    
$hoehe "120";    $brprozent = ((100 $hoehe) / $art_imgh);
    
$breite = (($art_imgw $brprozent) / 100);    $breite = (ceil ($breite));
}
}


$cols 2# Anzahl der Spalten
$d = new mysqli(hostuserpassdbname);

$r $d->query("SELECT * FROM artikel ORDER by art_id DESC limit 0,4");

$f 0# Feldzähler

echo '<table><tr>';
while(
$row $r->fetch_assoc()){
  
$f++;
  if (! 
$f $cols)
    echo 
'</tr><tr>';
  echo 
'<td>';
  echo (
"<td width=351>
<table width=351 border=0 cellpadding=0 cellspacing=0>
<tr><td height=130 style='border:1px solid #CCCCCC;' align=center><a href='details.php?session=$session&art=$art_id&kat=$kat&next=$next&page=$page&back=index'><img src='$image' width='$breite' height='$hoehe' border='0'></a></td></tr>
<tr><td id='space' height=2>&nbsp;</td></tr>
<tr><td><a href='details.php?session=$session&art=$art_id&kat=$kat&next=$next&page=$page&back=index'><img src='img/bt-detailsind.png' width='164' height='23' border='0'></a></td></tr>
</table>
</td>"
); # Ausgabe des img Tags  <-- FALSCH #Tabelle2
  
echo '</td>';
}
echo 
'</tr></table>'

if (
false !== ($r $d->query("SELECT * FROM artikel ORDER by art_id DESC limit 0,4"))){ 
  while(
$row $r->fetch_assoc()){ 
   
  } 
}else{ 
  
printf("Errormessage: %s<br />"$d->error); 
}  


if (
$x!=3) { 

Ich hoffe du wirst aus dem ganzen Code schlau und bist nochmal so nett, mir zu helfen.

meikel (†) 20.12.2011 03:03:09

AW: Tabelle: Eine Variable?, 2 Zeilen
 
Zitat:

Zitat von Sin84 (Beitrag 142632)
Ich hoffe du wirst aus dem ganzen Code schlau

Nö. Is mir zu umständlich.

http://fastcgi.php.developer.vc/tabellenAlgorithmus/
1. zwei kleine Fehlerchen dort korrigiert (hier kann ich das ja nicht)
2. Quelltext wird angezeigt.

Sin84 20.12.2011 19:28:07

AW: Tabelle: Eine Variable?, 2 Zeilen
 
Hi meikel,

klappt leider immer noch nicht. :(

Jetzt wird in der ersten Tabelle die Images so angezeigt:

Image1 Image2
Image3 Image4

und dann wieder:

Image1 Image2
Image3 Image4

und wieder:

Image1 Image2
Image3 Image4


In der zweiten Tabelle:

Image1 Image1
Image2 Image2
Image3 Image3
Image4 Image4

Ich habe den Code jetzt mal hochgeladen und kommentiert. Wenn du also noch Bock hast?
http://nopaste.php-q.net/134142


Gruß
Alex


Alle Zeitangaben in WEZ +2. Es ist jetzt 07:20:58 Uhr.

Powered by vBulletin® Version 3.8.3 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.