0

I am building a blog. I have the text of the post in a MySql database. Now I have a file for each post. I show here a simplified case. It works well but I ask if this can be improved:
1- Is it possible to have just one file for all the posts (in this example post.php) and make all the content dynamic. Hot to do that in this specific example?.
2- In that case, how would be the link from index.php to post.php?

In index.php I have the title and a intro of all the posts. I have a link to post complete:

<?php $result = mysqli_query($con,"SELECT * FROM blog") or die(mysqli_error());
while($row = mysqli_fetch_array($result))  { ?>

    <a href="post.php?id=<?php echo $row["id"]; ?>">
    <div>More</div>
    </a>

<?php } ?>

In post.php I have the post complete:

<?php 
$result = mysqli_query($con, "SELECT * FROM blog
                              WHERE id='".$_GET["id"]."'  ") 
                              or die(mysqli_error());
while($row = mysqli_fetch_array($result))  { 
?>
<div class="text"><?php echo $row['text_post']; ?> </div>
<?php } ?>
Nrc
  • 9,577
  • 17
  • 67
  • 114

1 Answers1

0

You dont need more the index.php for a whole site: First of all I would advise about separating html and code but... In your own example you could:

<? 
if (!isset($_GET["id"])){ /* show initial content */

    php $result = mysqli_query($con,"SELECT * FROM blog") or die(mysqli_error());
    while($row = mysqli_fetch_array($result))  { ?>

        <a href="post.php?id=<?php echo $row["id"]?>">
        <div>More</div>
        </a>
<?php } 

}else{ // show other stuff

    $result = mysqli_query($con, "SELECT * FROM blog
                          WHERE id='".$_GET["id"]."'  ") 
                          or die(mysqli_error());
    while($row = mysqli_fetch_array($result))  { 
        ?>
        <div class="text"><?php echo $row['text_post'] ?> </div>
        <?php 
    } 
)

?>

Julio Soares
  • 1,200
  • 8
  • 11
  • You unify index.php and post.php, ok. But what I ask and I do not know how to do is how to have just one post.php for all the different posts of different texts and how to make the link to that file – Nrc Sep 25 '15 at 14:17
  • I think I don't understand what you mean... just set the action of all forms to the same post.php and you will have them all in the same place – Julio Soares Sep 25 '15 at 14:20
  • hum... looking again... you know you are not generating any POST righ? you are using an "a" tag only as a generic example and is not real code right?... (?). – Julio Soares Sep 25 '15 at 14:24
  • Sorry perhaps I do not explain well. I mean that now I have a link to first-post.php, second-post.php... because I need the name of the article in the url for Google. How to link to a single file but have the name of the article different in each different post? – Nrc Sep 25 '15 at 14:25
  • hum... I guess we have a long way ahead... You need to read about .htaccess, rewriting, slugs... – Julio Soares Sep 25 '15 at 14:28
  • Yes I know .htaccess and regex to redirect. How can I use it in this case? – Nrc Sep 25 '15 at 14:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90630/discussion-between-nrc-and-julio-soares). – Nrc Sep 25 '15 at 14:30