Jump to content

Which Way Is Better?


mds1256

Recommended Posts

Hi

 

I have read that you should keep the views away from the business logic so my question is which one of the below is better, the below are just examples but hopefully you can grasp what I am getting at.

 

keeping the php logic out of the html:

 

<?php


function displayRows()
{
$Con = mysqli_connect('localhost', 'usr', 'pass', 'db');

$query = mysqli_query($Con, "select id, name from table");

while($row = mysqli_fetch_object($query))
{
 echo "<a href=\"#\">".$row->name."</a>";
}
}
?>



<html>
<body>


<?php displayRows(); ?>



</body>
</html>

 

 

OR keeping the html out of the php function but putting some php into the body of the html

 

<?php


function displayRows()
{
$Con = mysqli_connect('localhost', 'usr', 'pass', 'db');

$query = mysqli_query($Con, "select id, name from table");

return $query;
}
?>



<html>
<body>


<?php


$myRes = displayRows();


while($row = mysqli_fetch_object($myRes))
{
echo "<a href=\"#\">".$row->name."</a>";
}


?>



</body>
</html>

Link to comment
Share on other sites

Second because it has all the HTML being generated at once. If I read the first one I'd have to look up what displayRows() did just to know what the output HTML was (for starters).

 

Suggestion: make displayRows() return an array so the "view" doesn't have to know anything about the raw data source (a MySQL database accessed through mysqli).

Edited by requinix
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.