Пример #1
0
function show_poems($id)
{
    global $db;
    require_once 'CPoem.php';
    $cPoem = new CPoem($db, $_SESSION);
    // Poems are splitted in multiple pages if there is too
    // many poems. If we do not have page number in URL, then
    // we want to show first page.
    if (isset($_GET['page'])) {
        $page = $_GET['page'];
    } else {
        $page = 1;
    }
    // Get poems. Note! This does NOT return all the poems
    // at the time. This automatically split results if there
    // is too many poems in one page.
    if ($_SESSION['id'] == $id) {
        $poems = $cPoem->getPoems($id, $page, true);
    } else {
        $poems = $cPoem->getPoems($id, $page, false);
    }
    $poemsPerPage = $cPoem->getPoemsPerPage();
    $last = $poemsPerPage * $page;
    // Count number of poems.
    if ($_SESSION['id'] == $id) {
        $numPoems = $cPoem->numPoems($id, true);
    } else {
        $numPoems = $cPoem->numPoems($id, false);
    }
    // Was there any poems in database?
    if (!is_null($poems)) {
        foreach ($poems as $cur) {
            // Create unique named DIV so we can create
            // a href what will jump directly here.
            echo '<div id="poem_' . $cur['id'] . '">';
            $cur['added'] = date('d.m.Y H:i', strtotime($cur['added']));
            $cur['poem'] = nl2br($cur['poem']);
            // If poem title is empty string, then
            // show "Nimetön runo" as a title.
            $title = stripslashes($cur['title']);
            if ($title == '') {
                $title = '<i>(Nimetön runo)</i>';
            }
            if ($cur['visible'] == '0') {
                echo '<p class="poem_header_hidden">';
            } else {
                echo '<p class="poem_header">';
            }
            echo $title;
            echo '</p>';
            if ($cur['visible'] == '0') {
                echo '<p class="poem_hidden">';
            } else {
                echo '<p class="poem">';
            }
            $cur['poem'] = str_replace('<br />', '<br>', $cur['poem']);
            echo stripslashes($cur['poem']);
            echo '<p class="poem_added">';
            echo $cur['added'];
            echo '</p>';
            // Get comments for this poem.
            $comments = $cPoem->getComments($cur['id']);
            $num = count($comments);
            // If we are browsing our own poems, then we should
            // add also links where we can remove and
            // modify those poems.
            if (isset($_SESSION['id']) && $_SESSION['id'] == $id) {
                echo '<p class="poem_actions">';
                echo '<a href="edit_poem.php?id=' . $cur['id'] . '">Muokkaa</a>';
                echo ' / ';
                echo '<a href="remove_poem.php?id=' . $cur['id'] . '">Poista</a> / ';
                echo '<a href="hide_poem.php?id=' . $cur['id'] . '&page=' . $_GET['page'] . '">';
                if ($cur['visible'] == '1') {
                    echo 'Piilota</a> / ';
                } else {
                    echo 'Näytä</a> / ';
                }
                echo '<a href="#" id="comment' . $cur['id'] . '" class="showComments" onClick="return false;">' . 'Näytä kommentit (' . $num . ')</a>';
                echo '</p>';
            } else {
                echo '<p class="poem_actions">';
                if (isset($_SESSION['username'])) {
                    echo '<a href="add_comment.php?id=' . $cur['id'] . '&amp;page=' . $page . '">Jätä kommentti</a> / ';
                }
                echo '<a href="#" id="comment' . $cur['id'] . '" class="showComments" onClick="return false;">' . 'Näytä kommentit (' . $num . ')</a>';
                echo '</p>';
            }
            // Generate unique ID so we can toggle comments
            // to visible and hidden by this unique ID.
            echo '<div class="poem_comment" id="poem_comment' . $cur['id'] . '">';
            for ($i = 0; $i < $num; $i++) {
                echo '<p>';
                echo $comments[$i]['comment'];
                echo '<br>';
                echo 'Kommentoija: <a href="poet.php?id=' . $comments[$i]['commenter_id'] . '">' . $comments[$i]['username'] . '</a><br>';
                echo $comments[$i]['date_added'];
            }
            echo '</div>';
            echo '</div>';
        }
        echo '<hr>';
        echo '<div class="change_page">';
        // Count number of poem pages.
        $numPages = ceil($numPoems / $poemsPerPage);
        echo 'Sivu ' . $page . ' / ' . $numPages . '<br>';
        // Get the correct URL where user should be forwareded
        // if he/she press "Seuraava" or "Edellinen" link.
        if (isset($_SESSION['id']) && $_SESSION['id'] == $id) {
            $url = 'ownpage.php';
        } else {
            $url = 'poet.php';
        }
        // Is "Seuraava sivu" shown?
        $prevShown = false;
        // Show link to previous poem page?
        if ($page > 1) {
            $prevPage = $page - 1;
            // Link to prev poem page
            echo '<a href="' . $url . '?page=' . $prevPage . '&amp;id=' . $id . '">' . 'Edellinen sivu</a>';
            $prevShown = true;
        }
        // Show link to next poem page?
        if ($numPoems > $last) {
            if ($prevShown) {
                echo ' / ';
            }
            $nextPage = $page + 1;
            // Link to next poem page
            echo '<a href="' . $url . '?page=' . $nextPage . '&amp;id=' . $id . '">' . 'Seuraava sivu</a>';
        }
    } else {
        echo '<p class="poem">';
        echo 'Yhtään runoa ei löytynyt.';
        echo '</p>';
    }
    echo '<br><br>';
    echo '</div>';
}
Пример #2
0
     die;
 }
 $num = $db->numRows($ret);
 if ($num > 0) {
     header('Content-Type: text/plain; charset=UTF-8');
     $ret = $db->fetchAssoc($ret);
     for ($i = 0; $i < $num; $i++) {
         echo $ret[$i]['title'] . "\n";
         echo str_pad('-', strlen($ret[$i]['title']), '-', STR_PAD_LEFT);
         echo "\n\n";
         echo $ret[$i]['poem'];
         echo "\n\n";
         // If user wanted comments too
         if ($com) {
             // Get all comments to this poem
             $comments = $cPoem->getComments($ret[$i]['id']);
             $num_comments = count($comments);
             // If comments found, show separator
             if ($num_comments > 0) {
                 echo "==========================================\n";
                 echo "   KOMMENTIT \n";
                 echo "==========================================\n";
             }
             // Show all found comments
             for ($i2 = 0; $i2 < $num_comments; $i2++) {
                 echo $comments[$i2]['comment'] . "\n";
                 echo 'Kommentoija: ' . $comments[$i2]['username'] . "\n";
                 echo $comments[$i2]['date_added'] . "\n\n";
             }
             echo "\n\n";
         }
Пример #3
0
 // If this poem is not already shown, show it.
 // Poem will occur in array as many times as there
 // is comments, so this is why there is this if-part.
 if (!in_array($value['poem_id'], $already_shown)) {
     $already_shown[] = $value['poem_id'];
     // Show poem title
     echo '<p class="poem_header">';
     echo $value['title'] . "\n";
     echo '</p>';
     // Show a poem. Stripslashes is required so we
     // do not add \ before " char etc.
     echo '<p class="poem">';
     echo stripslashes(nl2br($value['poem'])) . "\n";
     echo '</p>';
     // Get all comments to this poem
     $comments = $cPoem->getComments($value['poem_id']);
     $num = count($comments);
     // List all comments, even those what have already seen.
     // Otherwise user might think that "where is my comments what
     // I have seen already? Is those comments removed???" or something...
     echo '<div class="poem_comment_visible">';
     for ($i = 0; $i < $num; $i++) {
         echo '<p>';
         echo $comments[$i]['comment'];
         echo '<br>';
         echo 'Kommentoija: <a href="poet.php?id=' . $comments[$i]['commenter_id'] . '">' . $comments[$i]['username'] . '</a><br>';
         echo $comments[$i]['date_added'];
         // Save comment ID to array so we can later
         // mark this poem comment as shown on UPDATE-query.
         $comments_ids[] = $comments[$i]['id'];
     }