What's the correct method using PHP & HTML -


i have created many sites requires php, html , css. work fine i'm thinking if time in wrong way. usually, first build site template , styling , code php. include php functions etc in html. example take of table lists posts (index.php):

        <div class="posts">             <h1 class="content-title">new posts</h1>             <?php                 $rows = list_posts();                 while ($row=mysql_fetch_array($rows)) {                     echo "                                                           <div>                             <img class='post-avatar' src='img/icon_newsletter.jpg'>                             <h2 class='post-title'>".$row['post_title']."</h2>                             <p class='post-meta'>                                 Από <a href='#'>".$row['post_user']."</a> | ".$row['post_date']." | <a class='post-views' href='viewpost.php?id=".$row['post_id']."#comments'>comments: ".total_comments($row['post_id'])."</a>                             </p>                             <p>                                 ".substr($row['post_body'], 0, 180)."...                             </p>                             <img class='post-icon-read' src='img/read.png' /> <a href='viewpost.php?id=".$row['post_id']."'>Συνέχεια άρθρου..</a>                        </div>                                    ";                  }             ?>               </div> 

there room improvement.

keep html within php. iss not personal opinion, there technical reasons.

when this:

html  <?php  php   ?>  html  <?php      ?>  html  <?php   php   ?>  html 

there overhead within php when switch , forth between html mode , php mode. , makes word press hack.


a few things: - heredoc syntax - numeric arrays - escape quotes

start basic template:

echo <<<eot      // top of page eot;  while ($row=mysql_fetch_array($rows), mysql_num) {      } echo <<<eot       // bottom of page eot; 

the reason use mysql_num column values can use without concatenation.

instead of

 <a href='#'>".$row['post_user']."</a> | ".$row['post_date']." | <a class='post-views' href='viewpost.php?id=".$row['post_id']."#comments'>comments: ".total_comments($row['post_id'])."</a> 

you can this:

$comment = total_comments($row[2]) ; echo "<a href=\"#\">$row[0]</a>$row[1]<a class=\"post-views\" href=\"viewpost.php?id=$row[2]#comments\">comments: $comment</a>"; 

the backslash escape double quotes within double quotes. quote marks not need escaped within heredoc.

single dimension numeric arrays can used within double quotes , within heredoc.


top of page:

echo <<<eot <!doctype html> <html lang="en"><head><title>page title</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>edit rotation diet</title> <style type="text/css">  </style></head> <body><div id="page"> eot; 

bottom of page:

echo <<<eot <script type="text/javascript">//<![cdata[   //]]> </script></body></html> eot; 

let's page content table

<body><div id="page"><table> eot;  while ($row=mysql_fetch_array($rows), mysql_num) {     echo <<<eot  <tr>   <td>$row[0]</td>   <td>$row[1]</td>   <td>$row[2]</td>   <td>$row[3]</td> </tr> eot;   } echo <<<eot </table> <script type="text/javascript">//<![cdata[ 

indentation of html along php wrong. should think how html going when views source. view source on word press page.. looks crap.

the blank line between echo , first html gives line feed.

when echo html did ends looking real bad when view source, word press.

personally, not 4 space indent. before know code hidden beyond right border. use 2 spaces.

i think ugly , waste of vertical space:

if($x = 1) {   echo '<p>one</p>'; } else }   echo '<p>two</p>' } 

i think plenty clear. without having excessive vertical scrolling/

if($x = 1){   echo '<p>one</p>'; } else{   echo '<p>two</p> } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -