0

I was wondering if there is a way to separate the HTML from the PHP PDO with Mysql, but I REALLY do not have time to use a frame (not my website, just helping a little). So I have this code in one file.

<?php
error_reporting( ~E_NOTICE );
require_once 'DB/dbconfig.php';
if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
{
    $id = $_GET['edit_id'];
    $stmt_edit = $DB_con->prepare('SELECT * from person where personId=:uid');
    $stmt_edit->execute(array(':uid'=>$id));
    $edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
    extract($edit_row);
}

and I can access all the data just like:

<?php echo $personName; ?>

The problem is when I try to use this php stmt code in ANOTHER file and call it Person::PersonSelect() I can not use the database data in my HTML file.

I saw a lots of cases and each one of them use the HTML tag inside the echo just like echo '<td>$personName</td>'; but I do no want to build the html through the php echo. So, any ideas? Thank you

mister martin
  • 6,197
  • 4
  • 30
  • 63
Jeú Casulo
  • 139
  • 2
  • 11
  • Not sure I entirely understand your question or what this has to do with MVC... The only alternative to `echo`ing PHP in your HTML would be to use a [template engine](http://stackoverflow.com/questions/731743/php-vs-template-engine), but PHP basically is a template engine. How else do you expect to access your data? – mister martin Jul 26 '16 at 03:46
  • You can do a crappy MVC in a few minutes by keeping the code and template separate using something like [Pug](https://packagist.org/packages/kylekatarnls/jade-php). – tadman Jul 26 '16 at 03:59
  • @mistermartin I am sorry, I just need to files, one file is the HTML file that includes or require the PHP file with thel select/mysql/pdo. And then I want to use a var from database in my HTML file. And everywhere I search for this they say I have to use a template engine or a mvc (I wonder if I can use a template engine WITHOUT mvc) – Jeú Casulo Jul 26 '16 at 22:52
  • @tadman I am not using composer or laravel or sinphony, it is a "stand alone" php website, when you create all the folders and files by your self, it is a old website from a friend and I am fixing it – Jeú Casulo Jul 26 '16 at 22:52
  • A framework-based site is "stand alone" just the same, it's some PHP files and data, the server itself doesn't care. If you mean it doesn't have a lot of dependencies, that's not really a point of pride. Good luck with that code base, you'll need it. – tadman Jul 27 '16 at 03:56
  • Ah ok, my mistake, sorry, so I going to try this, thank you :) – Jeú Casulo Jul 28 '16 at 00:13

1 Answers1

2

You can use include to separate PHP code with HTML code. In the HTML code, use <?=$personName?> to access PHP variable.

In your case:

<?php
error_reporting( ~E_NOTICE );
require_once 'DB/dbconfig.php';
if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
{
    $id = $_GET['edit_id'];
    $stmt_edit = $DB_con->prepare('SELECT * from person where personId=:uid');
    $stmt_edit->execute(array(':uid'=>$id));
    $edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
    extract($edit_row);

    //include the view here
    include('display.php');
}

In the file display.php, write something like :

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <body>
      Hello <?=$personName?>
  </body>
</html>
anhquan
  • 1,338
  • 14
  • 21
  • Thank you, it really works, but when a use the same code within a function I can not access the vars from database, maybe using a php object and put it in the function parameter? function SelectPerson($obj Person) – Jeú Casulo Jul 26 '16 at 22:53
  • 1
    It's worth noting that there's no need to use `$id` as an intermediate variable here. If you're going to do that, do it before you repeat `$_GET['edit_id']` three times. I'd also suggest that `if ($_GET['edit_id'])` is sufficient since if that's empty, zero, or whatever, that's not a valid ID and you can skip that part. – tadman Jul 27 '16 at 04:02
  • Yeah, make senses, do not know why I did that really – Jeú Casulo Jul 28 '16 at 00:13