0
I’m creating a news portal in PHP. How can I automatically create a new page (with header and footer) for every post I do?
I’m trying to do this with fopen / fclose, but I don’t know how to make it automatic. For now it’s like this:
<?php
$myFile = "novo_artigo.php";
$fh = fopen($myFile, 'w') or die("error");
$stringData = '
include "header.php";
$query = "SELECT titulo_artigo, subtitulo_artigo, conteudo_artigo FROM tb_artigo WHERE id_tb_artigo = 14";
$conexao = mysqli_connect('127.0.0.1:3307', 'root', '', 'artigo') or die("erro conexao");
$resultado = mysqli_query($conexao, $query);
if (mysqli_num_rows($resultado) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($resultado)) {
echo "<div class='titulodiv'><h1 class='titulo'>" . $row["titulo_artigo"]. "</h1></div><div class='titulodiv'><h3 class='titulo'>". $row["subtitulo_artigo"]. "</h3></div><div class='textodiv'><p class='texto'>" . $row["conteudo_artigo"]. "</p></div><br>";
}
} else {
echo "0 results";
}
include "footer.php";
';
fwrite($fh, $stringData);
fclose($fh);
?>
Friend, in fact you will create a dynamic page, where you search only the variable data...
– MagicHat
How do I do it @Magichat?
– mysql123
You create a news page with the layout you want, and feed all the data that will be variable with a database query, like this you have there.
– MagicHat
...For example, in your script you first query the Database by the news id, with the result of the query you feed the variables, as it is there, nor need the while, if the id is really unique. You know a little php, know how it works or are starting now?
– MagicHat
to start now
– mysql123
Good html, or are you starting? Look, I’m asking to check the best way to help...
– MagicHat
html and css I know
– mysql123
Brother I recommend you to search right here:
client-side, server-side, url, http, htaccess,dados variáveis, include, CRUD e os que aparecerem no meio destes
. Understand that if I gave you an answer I would have to adapt to the old web model, where php would not be being used as it should be, and this way I could be more disruptive than helping. That said, I recommend checking the way your portal is being built. Roughly speaking, you would need a (back-end only) file structure, something like this:index.php, noticias.php e noticiax.php
...– MagicHat
... Where
https://seu-site.com.br/index.php
, is its main thing it has besides other things it wants, has a link tohttps://.../noticias.php
which in turn has a series of news links, which will lead to the page(url).../noticiax.php
, where in fact in the place ofx
, there will be aquerystring
(a query word, that is the variable parameter that dynamically mounts the page):https://seu-site.com.br/noticia.php?id=14
or?id=15
orhttps://seu-site.com.br/o-segredo-do-7
, but the file will still benoticia.php
.– MagicHat
Consider if it is not time to take a break and familiarize yourself with the basic concepts of the web, those above, is already a start. Good luck.
– MagicHat
Thank you very much!
– mysql123
Not at all, man. If you have any conceptual questions, that you no longer have on the platform, you can do, but try to follow the notes of [help].
– MagicHat