• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

Tabelle nach bestimmten Durchlauf neue Spalte

stenz

Mitglied
Hallo,
da ich mich zurzeit viel mit Datenbanken beschäftige, habe ich eine neue Frage. :) Hoffentlich belästige ich euch nicht all zu sehr damit!! :)
Auf jeden Fall, habe ich eine Tabelle erstellt, deren Inhalt sich mit Daten aus der DB füllt.
Das Ganze schaut dann so aus.
Imageshack - abcr.png
Mit folgendem Code.
PHP:
<html>
    <head>
        <title>Hello World</title>
    </head>
    
    <body>
        <?php
            mysql_connect("","root","");
            mysql_select_db("Shop");
            
            $res = mysql_query("select * from `artikel`");
                        
            echo "<table border>";
                echo "<tr>";
                    while ($ds = mysql_fetch_assoc($res))
                    {
                        echo "<td><img src='".$ds["bild"]."' /></td>";
                        echo "<td>".$ds["name"]."</td>";
                        if ($ds["sonderpreis"] > 0)
                            echo "<td>".$ds["sonderpreis"]."</td>";
                        else
                            echo "<td>".$ds["preis"]."</td>";
                    }
                echo "</tr>";
            echo "</table>";
        ?>
    </body>
</html>
Glaube, das man diesmal nicht braucht wie die DB aufgebaut ist.
Meine Frage lautet, wie kann ich es so programmieren, das nach dem zweiten Durchlauf der While-Schleife eine neue Zeile erzeugt wird. D.h. das nicht 4 Artikel nebeneinander sein sollen, sondern nur zwei.

Gruß Stenz
 
PHP:
<?php
//[...]
$res = mysql_query("SELECT bild, name, sonderpreis, preis FROM artikel");
$count = mysql_num_rows($res);

// anzahl reihen
$cols = 2;
?>

<?php for ($i = 0; $i < $count; $i++): ?>
<tr>
    <?php for ($j = 0; $j < $cols; $j++, $i += $cols) ?>

        <?php if ($ds = mysql_fetch_assoc($res): ?>

            <td><img src="<?php print $ds["bild"]; ?>" /></td>
            <td><?php print $ds["name"]; ?></td>

            <td><?php
            if ($ds["sonderpreis"] > 0):
                 print $ds["sonderpreis"];
            else:
                print $ds["preis"]
            endif; ?></td>

        <?php endif; ?>

    <?php endfor; ?>
</tr>
<?php endfor; ?>
Nicht getestet.
 
Ich glaube, das Beispiel haut leider nicht hin.

Ansatz mit Modulo:

PHP:
<?php

$data = array(
    array('bild' => 'a.jpg', 'name' => 'A', 'sonderpreis' => 10, 'preis' =>  5),
    array('bild' => 'b.jpg', 'name' => 'B', 'sonderpreis' =>  0, 'preis' => 10),
    array('bild' => 'c.jpg', 'name' => 'C', 'sonderpreis' =>  0, 'preis' => 15),
    array('bild' => 'd.jpg', 'name' => 'D', 'sonderpreis' =>  0, 'preis' => 20),
    array('bild' => 'e.jpg', 'name' => 'E', 'sonderpreis' =>  5, 'preis' => 25),
    array('bild' => 'f.jpg', 'name' => 'F', 'sonderpreis' => 15, 'preis' => 30),
    array('bild' => 'g.jpg', 'name' => 'G', 'sonderpreis' =>  0, 'preis' => 35)
);

$count = count($data);

// Bzw. entsprechende MySQL-Befehle

// anzahl reihen
$cols = 4;

?>

<table border="1">

<tr>

<?php for ($i = 0; $i < $count; $i++): ?>    

    <?php
    $ds = $data[$i];
    // Bzw. $ds = mysql_fetch_assoc($res);
    ?>

    <td><img src="<?php print $ds["bild"]; ?>" /></td>
    <td><?php print $ds["name"]; ?></td>

    <td><?php
    if ($ds["sonderpreis"] > 0):
         print $ds["sonderpreis"];
    else:
        print $ds["preis"];
    endif; ?></td>

    <?php if (($i + 1) % $cols === 0 && $i < $count - 1): ?>

        </tr><tr>

    <?php endif; ?>    

<?php endfor; ?>

<?php
// Letzte Zeile mit korrekter Anzahl Zellen auffüllen
echo str_repeat('<td>-</td><td>-</td><td>-</td>', ($cols - $count % $cols) % $cols);
?>

</tr>

</table>
 
Ich hab's mal entsprechend angepasst:
PHP:
<?php
//[...]
$data = array(
    array('bild' => 'a.jpg', 'name' => 'A', 'sonderpreis' => 10, 'preis' =>  5),
    array('bild' => 'b.jpg', 'name' => 'B', 'sonderpreis' =>  0, 'preis' => 10),
    array('bild' => 'c.jpg', 'name' => 'C', 'sonderpreis' =>  0, 'preis' => 15),
    array('bild' => 'd.jpg', 'name' => 'D', 'sonderpreis' =>  0, 'preis' => 20),
    array('bild' => 'e.jpg', 'name' => 'E', 'sonderpreis' =>  5, 'preis' => 25),
    array('bild' => 'f.jpg', 'name' => 'F', 'sonderpreis' => 15, 'preis' => 30),
    array('bild' => 'g.jpg', 'name' => 'G', 'sonderpreis' =>  0, 'preis' => 35)
); 
$count = count($data);

// anzahl reihen
$cols = 2;
?>

<?php for ($i = 0; $i < $count; $i++): ?>
<tr>
    <?php for ($j = 0; $j < $cols; $j++, $i++): ?>

        <?php if (isset($data[$i])):
           $ds = $data[$i];
        ?>

            <td><img src="<?php print $ds["bild"]; ?>" /></td>
            <td><?php print $ds["name"]; ?></td>

            <td><?php
            if ($ds["sonderpreis"] > 0):
                 print $ds["sonderpreis"];
            else:
                print $ds["preis"];
            endif; ?></td>
	<?php else: ?>
	    <td>-<td>
            <td>-</td>
            <td>-</td>
        <?php endif; ?>

    <?php endfor; ?>
</tr>
<?php endfor; ?>
 
In der äußeren Schleife darfst du $i glaube ich nicht noch zusätzlich hochzählen, sonst werden Datensätze übersprungen. while ($i < $count) statt der for-Schleife.

Ansonsten ist's schon einfacher zu lesen als das Modulo-Gebastel.
 
Stimmt, das $i++ ist lässt das Script nach jedem vierten Datensatz einen aus. Wenn ich nicht für alles einen Unittest schreibe, fällt mir sowas vor Faulheit nicht mal mehr auf.
 
Zurück
Oben