Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End article preview that takes you to the actual article page.

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #154995
    Anonymous
    Inactive

    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.

    #155040
    Senff
    Participant

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

    #155049
    Anonymous
    Inactive

    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.

    #155080
    Anonymous
    Inactive

    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.

    #155098
    Anonymous
    Inactive

    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.

    #155099
    Anonymous
    Inactive

    Idiot me forgot a little something. Problem fixed.

    WHERE id='$id";

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.