Php döngü yardıma ihtiyacım var?

3 Cevap

Bu sql tablo

id    id_user  id_userfor          description      date    
 1       231          119          a to b           2010-02-05 17:42:47     
 2       231          231          myself           2010-02-05 17:36:28     
 3       231          119          from x to b      2010-02-05 17:36:29 

Böyle bir şey görünüyor çıkış sonucu seçmek için bir yol bulamadı:

user 119 

    a to b            2010-02-05 17:42:47
    from x to b       2010-02-05 17:36:29
user 231

    myself            2010-02-05 17:36:28 

3 Cevap

Sen id_userfor alanı ile sorgulayabilirsiniz döngü Sonra id_userfor bunu baskı yeni bir değer karşılaştığınız tüm rekor ve her şey sınıflandırılmaktadır.

Böyle bir şey:

$SQL    = 'select * from ... order by `id_userfor`';
$Result      = ...;
$PrevUserFor = '';
while($Row = ...) {
    const $UserFor = $Row['id_userfor'];
    if ($PrevUserFor != $UserFor) {
        // Here is where you print the user ID
        echo "user $UserFor<br>";
        $PrevUserFor != $UserFor;
    }

    $Description = $Row['description'];
    $Date        = $Row['date'];
    echo('&nbsp;$Description&nbsp;$Date<br>');
}

Umarım bu yardımcı olur.

Kullanabilirsiniz

select id_userfor, description, date from table order by id_userfor;

o zaman her farklı id_userfor için tüm date sütun veri yazdırabilirsiniz

$old = '';
while($row = mysql_fetch_array($result)) {

    if($old != $row['id_userfor']) {

        $old = $row['id_userfor'];
        echo "$row['id_userfor']<br />";
    }
    echo $row['description'] . " " . $row['date'] . "<br />";
}

Codaddict biraz daha okunabilir sorgulamak:

SELECT `id_userfor`, `description`, `date` FROM `table` ORDER BY `id_userfor`

Ve sonra, döngü içinde, böyle bir şey kullanacağız:

$sql = "SELECT `id_userfor`, `description`, `date` FROM `table` ORDER BY `id_userfor`";
$query = mysql_query($sql);

$user = null; // Empty user

while($data = mysql_fetch_assoc($query)) {
    if ($data['id_userfor'] != $user) {
        // Display the user only when it changes
        echo '<H1>'. $data['id_userfor'] .'</H1>';
        $user = $data['id_userfor'];
    }

    // Display the rest of the fields here :)
}