Can I separate data and layout better? (PHP, mysql, html)
Posted: Wed Jul 29, 2015 7:24 am
Sorry if I am very much a newbie asking this
, but:
- How do I in general keep data and design apart?
- Can I keep my databaseAccess file more "clean", only obtaining the data, and do the work of viewing/layout in my other file?
As you can see I am using variables to try and make the databaseAccess file more reusable,
but as soon as I need to extract data from the table, I am ruining that as I am locked at using a given amount of columns (and I currently have HTML code inside it, which seems messy to me).
Any suggestions for a cleaner design?
Part of "Books.php":
databaseAccess.php:
- How do I in general keep data and design apart?
- Can I keep my databaseAccess file more "clean", only obtaining the data, and do the work of viewing/layout in my other file?
As you can see I am using variables to try and make the databaseAccess file more reusable,
but as soon as I need to extract data from the table, I am ruining that as I am locked at using a given amount of columns (and I currently have HTML code inside it, which seems messy to me).
Any suggestions for a cleaner design?
Part of "Books.php":
Code: Select all
<?php
$table='books';
$db='test';
$row1='title';
$row2='author';
include 'databaseAccess.php';
?>databaseAccess.php:
Code: Select all
<?php
define("SERVER", "localhost");
define ("BRUKERNAVN", "....");
define ("PASSORD", "...");
define ("DATABASENAVN", $db);
$db_handle=@mysql_connect(SERVER, BRUKERNAVN, PASSORD);
if (! $db_handle)
die('Klarte ikke koble til ');
$returverdi=@mysql_select_db(DATABASENAVN);
if (! $returverdi)
{
@mysql_close($db_handle);
die ('klarte ikke velge db');
}
/* Queries */
$query="select * from " .$table;
$resultat=@mysql_query(mysql_real_escape_string($query));
echo "<table>";
echo "<th> Title </th> <th> Author </th>";
while($rad=@mysql_fetch_array($resultat))
{
echo "<tr><td>" . $rad[$row1] . "</td><td> " . $rad[$row2] . "</td></tr>";
}
echo "</table>";
@mysql_free_result($resultat);
@mysql_close($db_handle);
?>