alle einträge aus einer Tabelle in eine Variable, wie?

Die Scriptsprache PHP für die Gestaltung von dynamischen Websites.

alle einträge aus einer Tabelle in eine Variable, wie?

Beitragvon ragman » Di 06 Apr, 2004 07:08

Hallo,

ich habe ein PHP/MySql-Problem:

Ein Formular, das per Mail verschickt wird, soll unter anderem auch eine Liste, aller in einer Tabelle (artikel) befindlichen Einträge beinhalten.

Gedacht hatte ich mir das so:
Code: Alles auswählen
$result = mysql_query("select * from artikel");
      $n = mysql_num_rows($result);
      for($i=0;$i<$n;$i++)
         {
         $o = mysql_fetch_object($result);
         echo "<tr>
            <td align='right'>&nbsp;</td>
            <td>$o->artnr</td>
                  <td>$o->description</td>
                <td>$o->quantity</td>
                <td>$o->price</td>
                 <td>$o->total</td>
            </tr>";


Das funktioniert auch, aber statt der Ausgabe mittels echo möchte ich das als Variable haben, die ich an passender Stelle im Inhalt des Mails ausgeben kann.

Wenn ich das so schreibe, bekomme ich aber immer nur einen Artikel:
Code: Alles auswählen
$result = mysql_query("select * from artikel");
      $n = mysql_num_rows($result);
      for($i=0;$i<$n;$i++)
         {
         $o = mysql_fetch_object($result);
         $artikel = "<tr>
            <td align='right'>&nbsp;</td>
            <td>$o->artnr</td>
                  <td>$o->description</td>
                <td>$o->quantity</td>
                <td>$o->price</td>
                 <td>$o->total</td>
            </tr>";


So, und nun stehe ich irgendwie an :D
[/code]
ragman
Board-Mitglied
Board-Mitglied
 
Beiträge: 244
Registriert: Mo 23 Jun, 2003 18:31
Wohnort: Villach, Österreich

Beitragvon martin » Di 06 Apr, 2004 08:33

ich mach das immer mit arrays und nicht mit objekten.
anhand deines codes könnte das z.b. so aussehen:

[php]
$result = mysql_query("select * from artikel");
while ($array = mysql_fetch_array($result, MYSQL_BOTH))
{
$artikel = "<tr>
<td align='right'>&nbsp;</td>
<td>$array['artnr']</td>
<td>$array['description']</td>
<td>$array['quantity']</td>
<td>$array['price']</td>
<td>$$array['total']</td>
</tr>";
echo $artikel;
}
[/php]

hth
martin
martin
Moderator
Moderator
 
Beiträge: 1577
Registriert: Mo 23 Jun, 2003 16:56
Wohnort: Kremsmünster

Beitragvon ragman » Di 06 Apr, 2004 10:00

So, habs jetzt mittels array probiert, leider noch immer dasselbe Problem - der erste Eintrag wird ausgegeben, der Rest nicht.

Code: Alles auswählen
$result = mysql_query("SELECT * FROM artikel");
      if (!$result) {
               echo ("<p>Fehler bei Ausführung der Abfrage: " . mysql_error() . "</p>");
               exit();
               }
      while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
            $artikel = ("<tr><td>&nbsp;</td><td>"
                     . $row["artnr"] . "</td><td>"
                     . $row["description"] . "</td><td>"
                     . $row["quantity"] . "</td><td>"
                     . $row["price"] . "</td><td>"
                     . $row["total"] . "</td></tr>");
            }


Nur zur Sicherheit - in der DB sind zwei Einträge drin:

Code: Alles auswählen
mysql> select * from artikel;
+----+-------+-------------+----------+-------+-------+
| id | artnr | description | quantity | price | total |
+----+-------+-------------+----------+-------+-------+
| 10 | 2     | artikel2    |        2 | 2     | 4     |
|  9 | 1     | artikel1    |        1 | 1     | 1     |
+----+-------+-------------+----------+-------+-------+
2 rows in set (0.00 sec)


Hmmmmm??????? Was ist denn nun noch?

Die Ausgabe für $artikel sieht so aus:
Code: Alles auswählen
<html>
      <head>
      <title>Order Form</title>
      </head>
      <body>

<table><tr><td>&nbsp;</td><td>1</td><td>artikel1</td><td>1</td><td>1</td><td>1</td></tr></table>Order Form wurde versendet.

</body></html>



Nachtrag: Wenn ich das testweise mit echo mache, funktioniert die Ausgabe
Code: Alles auswählen
$result = mysql_query("SELECT * FROM artikel");
      if (!$result) {
               echo ("<p>Fehler bei Ausführung der Abfrage: " . mysql_error() . "</p>");
               exit();
               }
      while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
            echo "<table>";
            echo ("<tr><td>&nbsp;</td><td>"
                     . $row["artnr"] . "</td><td>"
                     . $row["description"] . "</td><td>"
                     . $row["quantity"] . "</td><td>"
                     . $row["price"] . "</td><td>"
                     . $row["total"] . "</td></tr>");
            }
            echo "</table>";


Die Ausgabe sieht dann korrekterweise so aus:
Code: Alles auswählen

<html>
      <head>
      <title>Order Form</title>
      </head>
      <body>

<table><tr><td>&nbsp;</td><td>2</td><td>artikel2</td><td>2</td><td>2</td><td>4</td></tr><table><tr><td>&nbsp;</td><td>1</td><td>artikel1</td><td>1</td><td>1</td><td>1</td></tr></table><br />

Order Form wurde versendet.
</body></html>




Ok, den Html-Teil der Tabelle muß ich noch ausbessern, aber so sollte es funktionieren. Das Problem ist, daß das nicht an dieser Stelle mittels echo erfolgen soll, sondern eben als $artikel innerhalbe einer anderen Variablen, nämlich $inhalt, die dann im Mail ausgegeben wird.
ragman
Board-Mitglied
Board-Mitglied
 
Beiträge: 244
Registriert: Mo 23 Jun, 2003 18:31
Wohnort: Villach, Österreich

Beitragvon ragman » Di 06 Apr, 2004 14:37

So, mittlerweile klappts:

Code: Alles auswählen
$tab = '';
      //Alle eingetragenen Artikel ausgeben
      $result = mysql_query("SELECT * FROM artikel");
      if (!$result) {
               echo ("<p>Fehler bei Ausführung der Abfrage: " . mysql_error() . "</p>");
               exit();
               }
      while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
            $tab .= ("<tr><td>&nbsp;</td><td>"
                     . $row["artnr"] . "</td><td>"
                     . $row["description"] . "</td><td>"
                     . $row["quantity"] . "</td><td>"
                     . $row["price"] . "</td><td>"
                     . $row["total"] . "</td></tr>");
            }
      $artikel = $tab;
ragman
Board-Mitglied
Board-Mitglied
 
Beiträge: 244
Registriert: Mo 23 Jun, 2003 18:31
Wohnort: Villach, Österreich


Zurück zu PHP

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 27 Gäste