-1

So I was just wondering if it was considered bad practice to open and close PHP statements, and let me explain what I mean. I know I can make variables at the beginning of my code but I like to group stuff together.I'm not sure if making one big PHP statement with all my variables is better / worse / same as opening and closing PHP statements similar to the example below.

<html>
<head></head> <---- HTML STUFF
<?php
(php stuff where connection to mysql db goes and other variables and errors)
?>
<body>
<html> <----- HTML stuff 
<?php
(php stuff to call a specific table from DB)
?>
<html> <----- HTML stuff 
<?php
(php stuff to call a specific table from DB)
?>
<html> <----- HTML stuff 
<?php
(php stuff to call a specific table from DB)
?>
</body>
<html> 

BTW the php variables I'm talking about are specific select statement from the DB.

ACTUAL CODE: or should select statements be at beg or sep file?

<table>
<tr>
<td align="left" width="200px">
Cover: Original Total
</td>
<td width="200px" align="center">
<?php
$original = "SELECT * FROM `comic_db`.`comic_db` WHERE comic_cover=\"original\"";
$orig_con = mysqli_query($comic_connect, $original);
$orig_total = mysqli_num_rows($orig_con);
echo $orig_total;
?>
</td>
</tr>
<tr>
<td width="200px" align="left">
Cover: Variants Total
</td>
<td width="200px" align="center">
<?php
$variants = "SELECT * FROM `comic_db`.`comic_db` WHERE comic_cover=\"variant\"";
$variant_con = mysqli_query($comic_connect, $variants);
$variant_total = mysqli_num_rows($variant_con);
echo $variant_total;
?>
</td>
</tr>
<tr>
<td align="left" width="200px">
Cover: Baby Totals
</td>
<td width="200px" align="center">
<?php
$baby = "SELECT * FROM `comic_db`.`comic_db` WHERE comic_cover=\"baby\"";
$baby_con = mysqli_query($comic_connect, $baby);
$baby_total = mysqli_num_rows($baby_con);
echo $baby_total;
?>
Androidd
  • 13
  • 5
  • 5
    Why do you have multiple `` and `` tags in the same document? That is incorrect markup. – kittycat May 11 '13 at 05:21
  • can you explain more? and I think you need to edit your question's code – Sina R. May 11 '13 at 05:21
  • It doesn't matter, but mixing the html and programming logic is always a bad idea, at least now a days, I can't think of this kind of procedural code. – The Alpha May 11 '13 at 05:25
  • You may want to look into using a Model-View-Controller framework like CodeIgniter or Zend. – Michael Benjamin May 11 '13 at 05:29
  • @cryptic ツ your focusing on the html portion sorry I didn't go through that a lil more I'm not looking at syntax so much as the question of opening and closing php statements – Androidd May 11 '13 at 05:30
  • I added the real code to the OP I'm a lil disappointed at the lash back from this question. since at least to me I don't see an obv problem and am reaching out for help :/ – Androidd May 11 '13 at 05:35
  • IMHO I don't think it is a great sin to structure code this way as long as code preceding each of the html blocks is specific to that component of the page and anything that is used in the block but defined in either the preamble code block is documented. Given as complexity increases the pains of this approach grow. – Orangepill May 11 '13 at 05:40
  • @orangepill This is a small comic inventory app I was making for myself incredibly low usage. I'm learning this as I go so it's sometimes frustrating to ask something that seems so obvious to others. TY for the help and guidence. – Androidd May 11 '13 at 05:44

1 Answers1

1

I'm assuming the repeated <html> tags in your example are just placeholders and would actually be <div>s and other elements making up the actual content of the page.

Your example is what's commonly called "spaghetti code" because it can quickly turn into an unmaintainable mess because you can't clearly see an overview of the HTML, nor can you see all the PHP code in one place.

The main thing to keep in mind is separating application logic (such as your database queries) from presentation (HTML and presentation logic like looping over an array to display it as an HTML list).

At the very least you'd want to put the main PHP code at the top of the file like you said, but it would be much better if it was in a separate file.

P.S. Any beginner book on PHP will discuss this in detail.

Matt Browne
  • 12,169
  • 4
  • 59
  • 75
  • The three books I have on php and mysql don't go over it – Androidd May 11 '13 at 05:29
  • Really? They don't discuss the separation of HTML / presentation code from database / application logic code? It's been a long time since I read a beginning PHP book myself but if this is true then the books are getting worse these days instead of better... – Matt Browne May 11 '13 at 05:34
  • It shows mixing all throughout "PhP for Dummies" Php/Mysql for absolute begginers" "PhP and MySql" only ref to outside files is mostly for connection params not for select statements to the db – Androidd May 11 '13 at 05:37
  • In truth this code usage is what php was designed to do and for trivial problems I see absolutely no problem in using it this way, but you are correct in any non-trivial solution strict separation of responsibilities has a huge payout. – Orangepill May 11 '13 at 05:45
  • Check out page 139 of "PHP for Absolute Beginners" ("Planning Our Scripts"). I can understand why this concept wouldn't be introduced in the first couple chapters of most beginner books, but for any but the most trivial applications it's an extremely important principle. – Matt Browne May 11 '13 at 05:46
  • Haha...well people might not necessarily intend to be condescending; it's easy to forget how many tutorials and books are sorely lacking at keeping up with best practices, but you've clearly put some effort into learning. I suppose many beginning coders may be introduced to the concept of MVC (Model-View-Controller) in intermediate books before they ever see mention of the more basic concept of separating logic from presentation...MVC ensures that presentation logic and HTML is separated into the "View" and introduces a further breakdown with the idea of the "Model". – Matt Browne May 11 '13 at 05:53
  • no 139 for me I got php5/mysql programming for the absolute beginner and in all of these examples they have a file.php that mixes html and php. – Androidd May 11 '13 at 05:56
  • @Matt Browne - I might have taken the down votes a lil too seriously as well (I'm a reddit reader keep in mind XD) I like reading these books but sometimes a good discussion is necessary to set one on track and get a perspective you can't get from a static object. Once I have enough loot to pay for proper classes and guidance I will know more of the details until then I guess here is where I will be. and google of course. lol – Androidd May 11 '13 at 06:00
  • Ah, I found a similarly named book ("PHP for Absolute Beginners") that I mistook as being the same one. Here's a link to the specific page I mentioned: http://books.google.com/books?id=Ze7euYaSKj0C&lpg=PA140&pg=PA139#v=onepage&q&f=false – Matt Browne May 11 '13 at 06:02
  • Sounds like I have another book to purchase :) TY – Androidd May 11 '13 at 06:03
  • :) The APress books are generally good, and they go all the way up to advanced levels. "PHP Objects, Patterns, and Practice" is more intermediate but is particularly good. One comment on that book...make sure you check out git for version control in addition to subversion...it used to be that most PHP developers used subversion, but git is becoming increasingly popular with good reason. – Matt Browne May 11 '13 at 06:08
  • Any ideas for rapid absorption of information? I was thinking water -> sponge -> book -> me = knowledge but I might be flawed in my logic on this one ;) – Androidd May 11 '13 at 06:12
  • Haha, I think that's a good note to end on since we're supposed to "avoid extended discussions in comments" :) – Matt Browne May 11 '13 at 06:17
  • 1
    There is no book that's worth just doing it ... I would suggest if you are learning, to rewrite your toy app a couple of different times using a couple of different development paradigms and see where the break points are. Enacting MVC on trivial tasks can be inefficient whereas trying to string together procedural code for anything complex will lead to an unmaintainable mess. – Orangepill May 11 '13 at 06:19