I want to retrieve all mysql table data using this script:
<?php
//WHERE id_post = '$id_post'
$sq = mysql_query("SELECT * FROM article_comments ") or die(mysql_error());;
connect();
while($row = mysql_fetch_assoc($sq)){
$result[$row["parent_id"]][] = $row["id"];
}
$rowIsOpened = false; //Indicates whether a row is currently opened.
//I'm using $rowIsOpened because the row immediately after the rowspanned cell shouldn't be closed.
echo <<<HTML
<table style="border-color: black;border-style: solid;">
<thead>
<tr>
<th>Parent</th>
<th>Children</th>
</tr>
</thead>
<tbody>
HTML;
//Echo a bunch of HTML before actually looping
foreach ($result as $parent => $children) {
echo "<tr style='border-color: black;border-style: solid;vertical-align:top;'>";
echo "<td rowspan=";
echo count($children); //Span over <how many children are> rows
echo " style='border-color: black;border-style: solid;' >$parent</td>";
$rowIsOpened = true; //Row is opened
foreach ($children as $child) {
if (!$rowIsOpened) {
echo "<tr>";
} //Only open a row if row is not opened
echo "<td style='border-color: black;border-style: solid;'>$child</td>";
echo "</tr>";
$rowIsOpened = false; //Row is now closed. Ready for next iteration.
}
}
//Close the table tags etc.
echo <<<HTML
</tbody>
</table>
HTML;
?>
This script creates an output table like that
Parent Children
0 1
3
4
5
1 1
2
My problem is when i try to add the other data from the mysql table: comment, name, date etc.
I have to replace these numbers with:
<div class="cmt-cnt">
<img src="<?php echo $grav_url; ?>" />
<div class="thecom">
<h5><?php echo $name; ?></h5><span data-utime="1371248446" class="com-dt"><?php echo $date; ?></span>
<br/>
<p>
<?php echo $comment; ?>
</p>
</div>
</div><!-- end "cmt-cnt" -->
I tried this but it does not work and i didn't got much about arrays.... A little explaining would help me understand the concept better :)
$id_post = "1";
$name = array();
$email = array();
$comment = array();
$date = array();
//WHERE id_post = '$id_post'
$sq = mysql_query("SELECT * FROM article_comments WHERE id_post = '$id_post' ") or die(mysql_error());;
connect();
while($row = mysql_fetch_assoc($sq)){
$result[$row["parent_id"]][] = $row["id"];
$comment[$row['id']] = $row['comment'];
// Get gravatar Image
// https://fr.gravatar.com/site/implement/images/php/
$default = "mm";
$size = 35;
// $grav_url = "http://www.gravatar.com/avatar/".md5(strtolower(trim($email)))."?d=".$default."&s=".$size;
}
$rowIsOpened = false; //Indicates whether a row is currently opened.
//I'm using $rowIsOpened because the row immediately after the rowspanned cell shouldn't be closed.
echo <<<HTML
<table style="border-color: black;border-style: solid;">
<thead>
<tr>
<th>Parent</th>
<th>Children</th>
</tr>
</thead>
<tbody>
HTML;
//Echo a bunch of HTML before actually looping
foreach ($result as $parent => $children) {
echo "<tr style='border-color: black;border-style: solid;vertical-align: top;'>";
echo "<td rowspan=";
echo count($children); //Span over <how many children are> rows
echo " style='border-color: black;border-style: solid;width:400px;' >$parent
<div class='cmt-cnt'>
<img src='".$grav_url."' />
<div class='thecom'>
<h5>".$name."</h5><span data-utime='1371248446' class='com-dt'>".$date."</span>
<br/>
<p>
".$comment."
</p>
</div>
</div>
</td>";
$rowIsOpened = true; //Row is opened
foreach ($children as $child) {
if (!$rowIsOpened) {
echo "<tr>";
} //Only open a row if row is not opened
echo "<td style='border-color: black;border-style: solid;width:400px;'>$child</td>";
echo "</tr>";
$rowIsOpened = false; //Row is now closed. Ready for next iteration.
}
}
//Close the table tags etc.
echo <<<HTML
</tbody>
</table>
HTML;
?>