Skip to content

Instantly share code, notes, and snippets.

@adrian-enspired
Created October 19, 2012 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrian-enspired/3916008 to your computer and use it in GitHub Desktop.
Save adrian-enspired/3916008 to your computer and use it in GitHub Desktop.
PHP-FIRST: a beginning at separating the view from the controller & model
<?php
/*
Not trying to be a complete MVC implementation, obviously,
just a good, easy-to-implement introduction for the average PHP coder/hobbyist.
It WILL make your scripts BETTER! :)
The basic rule is "PHP-FIRST":
meaning, don't end php ?>, do some html, and then start <?php again.
PHP doesn't *actually* work this way,
(you can't "start," "stop," and "start again": breaking into HTML is pretty much the same as echo'ing it)
but it's a very common misconception among beginners.
Likewise, don't echo (print, header(), etc.) anything in the middle of your script:
Wait until *all* PHP processing is completed first.
*/
####################
/* a GOOD example */
// this keeps the script's logic separate from the output.
// makes it easier to debug, maintain, and make changes in the future.
// more importantly, it makes error handling and recovery much, much easier.
$DB = new mysqli( host,user,pass,name );
if( $query = $DB->query( "SELECT `col1`,`col2` FROM `table` WHERE `col1`='something'" ) ){
while( $row = $query->fetch_assoc() ){
list( $col1,$col2 ) = $row;
$tablerow[] = "<tr><td>$col1</td><td>$col2</td></tr>";
}
$table = "
<table>
<tr><th>Column One</th><th>Column Two</th></tr>
".implode( "\n",$tablerow )."
</table>";
}
// all done
// if anything had gone wrong in the loop,
// you could have aborted, or started over, cleanly.
echo $table;
// normally you would exit here, but...
###########################
/* now for a BAD example */
// this results in the same output as the "GOOD" example,
// (excepting some whitespace in the table)
// but basically outputs everything *as it goes*.
// this can be thought of as a "brain->mouth" problem;
// i.e., talking without thinking.
$DB = new mysqli( host,user,pass,name );
if( $query = $DB->query( "SELECT `col1`,`col2` FROM `table` WHERE `col1`='something'" ) ){
echo "<table>
<tr><th>Column One</th><th>Column Two</th></tr>";
while( $row = $query->fetch_assoc() ){
list( $col1,$col2 ) = $row;
echo "<tr><td>$col1</td><td>$col2</td></tr>";
}
echo "</table>";
}
// if anything had gone wrong here,
// you'd be stuck with a half-written table
// (and probably a broken page layout/ DOM).
@artemgordinskiy
Copy link

Hi Adrian,

I've tried to output a table as you suggest here, but I'm getting these errors:
Notice: Undefined variable: result
Fatal error: Call to a member function fetch_assoc() on a non-object
All I did was add my database credentials and fill the cells with my data.

Thanks for help!

@artemgordinskiy
Copy link

Sorry to bother you, I've figured everything out myself :)
Thank you so much for the advice!

@adrian-enspired
Copy link
Author

@ArtemGordinsky - sorry, I wasn't watching this gist! I'm glad you figured it out.

There were some typos in the script, not the least of which was the fact that $result does not exist (it should be $query, and I have now fixed it). There were a few other things as well.

Sorry for the lack of attention. I had written (but didn't end up using) this for a blog post on Dynamic Drive, if you're interested.

Thanks!
-Adrian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment