r/PHPhelp • • 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?

7 Upvotes

12 comments sorted by

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 sense

but to be clear, this is not modern or recommended.

ideally your php page would do the processing, then include html file.

  • rename databasequery.php to Query.php and make it OOP like

  • move your html file to a templates dir and name it to describe what it presents. for example templates/main.html

class Query {
  public function getData() {
     ...get the data from sql
  }
}

then your php page would get the result from that class and then require the html file

<?php
require("Query.php");
$query = new Query();
$data = $query->getData();
require("templates/main.html");

then finally in your main.html you have access to the $data var so you can display it

...html...
<?php echo $data; ?>
...html...

1

u/mrwafflezzz Dec 29 '20

I want to make sure I get this, so if you would hear me out and correct me.

I'm currently just using chunks of php code in between my html code to get the data I need. That's like 4 queries for every new page in some cases, some of which would be redundant through your method.

So instead you use php to do all the processing, to then show the hmtl page as an included (required) file. Your home.php file, where all the server side processing happens, has a templates/home.hmtl file for static elements.

Is this correct?

4

u/[deleted] Dec 29 '20

Yes that's right. You can do the original plan of interjecting php here and there but it becomes hard to manage and extend that way. You need to comb through lots of code to find where you're doing what

Wrap reach query in a method and collect all the data in one spot. That way you're keeping logic and presentation layers seperate. If it's still unclear i can put together a gist or something

5

u/mrwafflezzz Dec 29 '20

The method thing sounds interesting, I'll do you a favour and I'll do some reading before I ask any more questions. Thanks for the help man!

2

u/backtickbot Dec 29 '20

Fixed formatting.

Hello, hurlingsearsethic: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

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

u/Kit_Saels Dec 29 '20

I use XSLT for templating.

0

u/[deleted] Dec 29 '20

[deleted]

2

u/mrwafflezzz Dec 29 '20

I'll read up on it a little more, thx for the help!

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