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 } ?>
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
}
)
?>