0

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: https://i.sstatic.net/pb5ww.jpg

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>';

?>
4
  • 4
    Obligatory "don't use mysql_ functions" comment. Commented Aug 10, 2013 at 22:50
  • I recommend PDO_MySQL php.net/manual/en/book.pdo.php Commented Aug 10, 2013 at 22:50
  • 1
    ids cannot be numbers in HTML. Commented Aug 10, 2013 at 22:52
  • That's not what it is about. Commented Aug 10, 2013 at 22:53

1 Answer 1

2

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>';
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ah right, thank you very much! This is just a personal project, not to be released to anyone I don't trust, so security is no matter right now. Thanks for the tip anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.