r/PHPhelp • u/mrwafflezzz • Dec 29 '20
question about mixing php and html
I currently have a php page set up that consists of mainly html codeI would like to have 1 php chunk dedicated to database queryingI would like to use the variables from that chunk in other php chunks in the same PHP file
example:
some html
<?php include "databasequery.php"; ?>
some html
<?php echo $queryvariable; ?>
some html
Is this an illegal move because it does seem to work, although my IDE doesn't appreciate it?
4
u/PhyterNL Dec 29 '20
It is legal. It is arguably bad form, there are far better ways to template. But if your code is very simple then sure why not.
You can ease your pain slightly by using the <?= in place of <?php and echo.
<?= $queryvariable ?>
On that note, which IDE are you using? Visual Studio Code does a good job of parsing HTML, JS and PHP inline.
1
u/mrwafflezzz Dec 29 '20
PHPstorm is what I'm currently using. But can I ask, what would be a better way of templating in this case?
1
0
1
u/MSDakaRocker Dec 29 '20
I mix php and html all the time. This is my usual layout for pages:
<head></head>
<?php include "functionstorunbeforebody.php"; ?>
<body>
<header></header>
<main></main>
<footer></footer>
</body>
<?php include "functionstorunafterbody.php"; ?>
---
The top include is for most of the php work, the bottom include is for any references to js I may want, without including it or having it visible I'm still a js n00b so my limited ability with it means I don't obfuscate well).
If anyone also has any advice for/against this (especially with the possibly unnecessary hiding js in php files) I'm happy to hear it but this works for me for the scale of projects I work on as I'm a solo developer for a company.
2
u/mrwafflezzz Dec 29 '20
I definitely learned a lot about templating by asking this question, but it would seem that you'll inevitably use variables from one php chunk in another.
Also, your header and footer aren't includes?
1
u/MSDakaRocker Dec 29 '20
You can definitely swap any of the html elements out for php includes providing references and ordering is done right (ensure you don't reference anything below itself in the load order).
I was mainly sharing my structure for any scruipting includes, sorry for not being clear.
I've seen pages that are like the following that work absolutely fine:
<?php include "../elements/head.php";?>
<?php include "../scripts/scripts.php";?>
<?php include "../elements/header .php";?> ##containing opening of body element
<?php include "../elements/body.php";?> ##anything between header and footer
<?php include "../elements/footer.php";?> ##containing closing of body element
The only real benefit to this is you can make the includes non-visible in a subdirectory with .htaccess limits set
6
u/[deleted] Dec 29 '20
that is not "illegal" - you are probably referencing vars that the ide doesn't know is from
databasequery, so throwing some errors makes sensebut to be clear, this is not modern or recommended.
ideally your php page would do the processing, then include html file.
rename
databasequery.phptoQuery.phpand make it OOP likemove your html file to a templates dir and name it to describe what it presents. for example
templates/main.htmlthen your php page would get the result from that class and then require the html file
then finally in your
main.htmlyou have access to the$datavar so you can display it