I have a piece of code which executes a calendar. On this calendar certain names have to appear. However, on each date appears only one name, while in the database some dates hold up to 20 names.
This image should hopefully clarify things: http://imgur.com/a31pj0s
In this image you can see each date, which on some of them one name. As stated before, most of these contain many more names.
Now, I understand why this does not work, as it executes only once, but how can I get it to actually work? Please help me out :)
Here is also a link to the same piece of code, except easier to read: http://codepad.org/tVSzHDTx
<?php
$j = 2;
$i = 1;
//query the database
$query = mysql_query("SELECT * FROM months");
//fetch the results / convert results into an array
echo '<div class="accordion" id="calendar">';
while($rows = mysql_fetch_array($query)) {
$month = $rows['month'];
$id = $rows['id'];
echo '
<div class="accordion-group">
<div class="accordion-heading" id="month_'.$id.'">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#calendar" href="#monthsid'.$id.'">
'.$month.'
</a>
</div>
<div id="monthsid'.$id.'" class="accordion-body collapse">
<div class="accordion-inner">';
$n = $rows['num_of_days'];
echo '<ul class="list-group" id="'.$id.'" style="margin-bottom: 0;">';
for ($i = 1; $i <= $n; $i++) {
if ($j == 7) {
echo '<li class="list-group-item" id="'.$i.'" style="margin-bottom: 5px;">';
} else {
echo '<li class="list-group-item" id="'.$i.'">';
}
echo '<a data-toggle="modal" href="#myModal" class="add_member" id="'.$month.' '.$i.'">+</a>
<span class="badge">'.$i.'</span>';
if ($i == date("j")) {
echo '<div class="day_segment current_day">';
} else {
echo '<div class="day_segment">';
}
if ($j == 1) {
echo '<i style="color: #aaa;">'.$i.' |</i> Monday';
$j++;
} else if ($j == 2) {
echo '<i style="color: #aaa;">'.$i.' |</i> Tuesday';
$j++;
} else if ($j == 3) {
echo '<i style="color: #aaa;">'.$i.' |</i> Wednesday';
$j++;
} else if ($j == 4) {
echo '<i style="color: #aaa;">'.$i.' |</i> Thursday';
$j++;
} else if ($j == 5) {
echo '<i style="color: #aaa;">'.$i.' |</i> Friday';
$j++;
} else if ($j == 6) {
echo '<i style="color: #aaa;">'.$i.' |</i> Saturday';
$j++;
} else if ($j == 7) {
echo '<i style="color: #aaa;">'.$i.' |</i> Sunday';
$j = 1;
}
echo '</div>';
$posts_query = mysql_query("SELECT * FROM posts WHERE day=$i ");
while ($rows_items = mysql_fetch_array($posts_query)) {
$entry_player = $rows_items['author'];
$entry_comment = $rows_items['comment'];
$entry_day = $rows_items['day'];
$entry_month = $rows_items['month'];
}
for ($k = 1; $k <= 1; $k++) { /* I tried using another for loop, did not work */
if ($id == $entry_month && $i == $entry_day) {
echo '<span class="label label-success" data-toggle="tooltip" rel="tooltip" title="'.$entry_comment.'">'.$entry_player.'</span>';
} else {
echo '<span class="label"></span>';
}
}
echo '</li>';
}
echo '
</ul>
</div>
</div>
</div>
';
} /* End While Loop */
echo '</div></div>';
?>
As mentioned in the comments, you should not use the mysql_ functions as they are deprecated and not secure. Use MySQLi or PDO instead.
To answer your question, the code block below is the reason why it only displays one entry per day. In the while loop, you overwrite all the four variables each time, thus only the last one will be displayed in your for loop. The for loop is not necessary.
while ($rows_items = mysql_fetch_array($posts_query)) {
$entry_player = $rows_items['author'];
$entry_comment = $rows_items['comment'];
$entry_day = $rows_items['day'];
$entry_month = $rows_items['month'];
}
for ($k = 1; $k <= 1; $k++) { /* I tried using another for loop, did not work */
if ($id == $entry_month && $i == $entry_day) {
echo '<span class="label label-success" data-toggle="tooltip" rel="tooltip" title="'.$entry_comment.'">'.$entry_player.'</span>';
} else {
echo '<span class="label"></span>';
}
}
Try changing your code to something like this:
while ($rows_items = mysql_fetch_array($posts_query)) {
$entry_player = $rows_items['author'];
$entry_comment = $rows_items['comment'];
$entry_day = $rows_items['day'];
$entry_month = $rows_items['month'];
if ($id == $entry_month && $i == $entry_day) {
echo '<span class="label label-success" data-toggle="tooltip" rel="tooltip" title="'.$entry_comment.'">'.$entry_player.'</span>';
} else {
echo '<span class="label"></span>';
}
}