Jump to content

PHP and MySQL table population


-Karl-

Recommended Posts

I have a table created containing the following.

 

name url members date

 

I want to populate a table like so:

 

Dates name1 name2 name3

date  membersforname1 membersforname2 membersforname3

anotherdate  membersforname1 membersforname2 membersforname3

 

So I need the dates to form each of the rows, but also, have the members in the correct places for the names.

 

Not sure if any of that made sense, if you want me to go in to more detail just ask.

Link to comment
https://forums.phpfreaks.com/topic/210915-php-and-mysql-table-population/
Share on other sites

$prevdate = NULL;
$query = mysql_query("SELECT * FROM tablename ORDER BY date ASC");
echo "<table><tr>";
while($row = mysql_fetch_array($query)) {
    if($row['date'] == $prevdate) {
        echo "<td>BLAH BLAH BLAH</td>";
    } else {
        echo "</tr><tr>";
        echo "<td>BLAH BLAH BLAH</td>";
    }
    $prevdate = $row['date'];
}
echo "</tr></table>";

 

It won't work as you expect, but you should be able to see my logic.

Anyway, I've had a little mess around

 

$prevdate = NULL;
$query = mysql_query("SELECT * FROM tracked ORDER BY date ASC");
$query2 = mysql_query("SELECT * FROM tracked GROUP BY name ASC");
echo "<table><tr>";
echo '<tr>
            <td>Date</td>';
while($row2 = mysql_fetch_assoc($query2)) {
            echo '<td>'.$row2['name'].'</td>';
}
echo '</tr>';
while($row = mysql_fetch_assoc($query)) {
    if($row['date'] == $prevdate) {
        echo '<td>'.$row['members'].'</td>';
    } else {
        echo "</tr><tr>";
        echo '<td>'.$row['date'].'</td>';
    }
    $prevdate = $row['date'];
}
echo "</tr></table>"; 

 

It's got the correct layout now, however, no members appear for name2, when there are numbers in the database. Can't figure out why.

 

EDIT: Actually, the members that come under name1, are actually the members for name2.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.