Grow your CSS skills. Land your dream job.

article preview that takes you to the actual article page.

  • # November 1, 2013 at 8:59 pm

    as i mentioned before i am new to PHP and MYSQL. This is a question that i cant seem to find the answer for on google(maybe because i don’t know how to ask it). So i’m going to ask my question as clear as i can.

    I’m working on a blog and this blog has a page that shows the preview of the many posts and a “read more” button that takes them to the page were the full article is written. Thats something that i don’t know how to do dynamically. When i add a new post to my blog, how do i present the preview on the home page, and have the user click the “read more” button that takes them to the actual article? i want to do this automatically every time i post a new article.

    PS. I know how to retrieve data from MYSQL so you can skip that. Maybe pointing me to the right tutorial can be helpful. Thank you.

    # November 2, 2013 at 5:27 pm

    Depends what you use. Did you code that all from scratch yourself, or are you using a specific platform such as WordPress or something…?

    # November 2, 2013 at 9:28 pm

    I coded it myself using a few tutorials from youtube. Basically what i will be adding as the actual article to the database is this.

    <!--content-->
                <div class="content_aligner">
                    <?php
                        $sql = mysql_query("SELECT * FROM articles ORDER BY id DESC");
                        while($row = mysql_fetch_array($sql)) {
                            $title = $row['title'];
                            $content = $row['content'];
                            $written_by = $row['written_by'];
                            $written_on = $row['written_on'];
                            $category = $row['category'];
                    ?>
                    <div class="content_wrapper shadow">
                        <div class="content_title shadow"><h1><?php echo $title;?></h1></div>
                        <div class="content_text_aligner">
                            <p><?php echo nl2br($content);?></p>
                            <span class="article_details"><?php echo "Written By: $written_by";?></span>
                            <span class="article_details"><?php echo "Written On: $written_on";?></span>
                            <span class="article_details"><?php echo "Category: $category";?></span>
                        </div>
                    </div>
                    <?php
                    }
                    ?>
                </div>
    

    And so since that will be the full article, i want to show a preview on the home page and a “read more” button that automatically takes them to the article page. But i dont know how to do that dynamically.

    # November 3, 2013 at 4:29 am

    How do you mean dynamically?

    Change the “read more” button into being an actual hyperlink and it will automatically take them to the article page.

    If you’re meaning how to dynamically have previews, go with writing a function that limits the number of words in the string, and use that with as the excerpt, unless you want to handcraft the excerpt and store that in the database too.

    # November 3, 2013 at 10:07 am

    I dynamically created the article previews in the index page. Its the actual article but with it only showing 500 characters. Now all i need is when the user clicks the “read more” button, it takes them to the full article. I managed to get the id of the article whose “read more” button was clicked. So in the url it has this “article.php?id=3″. Thats the id of the article preview whose “read more” button wa clicked. Now i want to display the full article when the read more” button is clicked.

    # November 3, 2013 at 12:57 pm

    Why can’t you use a normal <a href="article.php?id=3">Read more</a> element?

    # November 3, 2013 at 2:52 pm

    I am. I got a little further since i posted this and when i click the “read more” button it takes me to the article.php?id=3 page with the id depending on what “read more” button i clicked form the preview. now in the “article.php” page i added the code bellow to get the full article to show.

    <?php
        include "php/db_connect.php";
    
        if(!isset($_GET['id'])) {
            header('Location: index.php');
            exit();
        } else {
            $id = $_GET['id'];
        }
        if(!is_numeric($id)) {
            header('Location: index.php');
        }
        $connect = "SELECT title, body FROM articles WHERE id='$id";
        $query = $connect->query($connect);
        echo $query->num_rows;
    ?>
    

    But in the article.php?id i get this error

    Fatal error: Call to a member function query() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/Poverty To Profits/article.php on line 14

    Im not sure how to fix this though in the code above.

    # November 3, 2013 at 3:19 pm

    Idiot me forgot a little something. Problem fixed.

    WHERE id='$id";

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".