Reputation: 25
First of all, I'm a noob and probably I'm asking something easy to do. I have this small template saved in template2.html :
<table border=”0″> <tr>
<td align=”center”><!-- IDCAT --></td>
<td><!-- CATEGORIA --></td>
</tr>
</table>
This db connection file in php mysql3.php :
<?php
$connessione = @mysqli_connect("localhost","root","","tecwebdb") or die("La connessione al Database è fallita !");
$sql = "SELECT ID_Categoria,Nome FROM categoria";
$query=@mysqli_query($connessione, $sql) or die("Esecuzione Query fallita !");
?>
And the php code for make it all works in this file test.php:
<?php
include("mysql3.php");
$file_content = implode("",file("template2.html"));
while($ris=mysqli_fetch_array($query)) {
$output = preg_replace("<!-– IDCAT -->", $ris["ID_Categoria"], $file_content);
$output = preg_replace("<!–- CATEGORIA -->", $ris["Nome"], $output);
echo $output;
}
@mysqli_close($connessione);
?>
The problem is that the query results are not showing probably I'm missing something on the while cicle, how to make it work?
Upvotes: 0
Views: 93
Reputation: 21681
It doesn't work because your Regx is flawed ( as I said in the comments )
$output = preg_replace("<!-– IDCAT -->", $ris["ID_Categoria"], $file_content);
Try:
$output = preg_replace("/<!-– IDCAT -->/", $ris["ID_Categoria"], $file_content);
Your basically missing the delimiters /
at the start and end of the Regx, I imagine PHP is issuing a warning for this, too. Which if you had display errors on ini_set('display_errors', 1)
you would see.
Or just use str_replace()
which is more the appropriate function to use in this case, IMO.
$output = str_replace("<!-– IDCAT -->", $ris["ID_Categoria"], $file_content);
You can try it here
https://regex101.com/r/ObyMbZ/1
But note they put the delimiters in for you automatically, which is what you are missing.
Update: change this
while($ris=mysqli_fetch_array($query)) {
to
while($ris=mysqli_fetch_assoc($query)) {
So your array will have keys, http://php.net/manual/en/mysqli-result.fetch-array.php
Upvotes: 1
Reputation: 1267
replace this :
$output = preg_replace("<!-– IDCAT -->", $ris["ID_Categoria"], $file_content);
$output = preg_replace("<!–- CATEGORIA -->", $ris["Nome"], $output);
With this :
$output = preg_replace("<!-- IDCAT -->", $ris["payer_id"], $file_content);
$output = preg_replace("<!-- CATEGORIA -->", $ris["description"], $output);
Upvotes: 0
Reputation: 775
You are complicating it a bit too much.
You can do it all in 1 file (e.g. test.php
) as below:
<?php
//connect to DB
$connessione = @mysqli_connect("localhost","root","","tecwebdb") or die("La connessione al Database è fallita !");
$sql = "SELECT ID_Categoria,Nome FROM categoria";
$query=@mysqli_query($connessione, $sql) or die("Esecuzione Query fallita !");
//render the table
?>
<table border=”0″>
<?php
while($ris=mysqli_fetch_array($query))
{
echo "<tr>";//row start
echo "<td align='center'>".$ris["ID_Categoria"]."</td>"; //col
echo "<td>".$ris["Nome"]."</td>";//col
echo "</tr>"; //row end
}
?>
</table>
<?php
//close the connection
@mysqli_close($connessione);
?>
Upvotes: 0