Suggestions for improving my PHP code

thirtythree

Diamond Member
Aug 7, 2001
8,680
3
0
Hi,

So I've learned some web coding on my own, and I was wondering if someone would be willing to look at my code and make suggestions for things I can improve, topics I should learn about, etc. The sample files are a fairly basic blog I made from scratch. I know I could add more features as needed, but I want to see if I'm making any major errors first. I picked things up sort of haphazardly, rather than following a course or something of that sort.

I already know that I should have more comments in it, because I worked on it a while ago and it can be a bit tricky figuring things out looking at it now. There's no password to the database because the files are on my computer (using WAMP), not hosted online.

Thanks!

Zip file
 

thirtythree

Diamond Member
Aug 7, 2001
8,680
3
0
Originally posted by: wetcat007
I don't see anything in particular that would be cause for issues, I didn't run it at all though. Was there anything in particular you wanted people to look at? You could try using prepared statements for your sql queries if you're looking for something new to do. http://www.php.net/manual/en/class.pdostatement.php

No, there wasn't anything in particular. Just wanted to see if I was making any obvious mistakes. For example, I just found out that I don't need to be using double quotes for everything (which I already changed in the files I linked). I guess one thing I'm curious about is, is it common for people to have a separate file that does nothing but add or remove something from the database and then forward to another page? Or would this normally be handled at the top of an existing page? Does it matter? Thanks for the suggestion, I'll look into that.

EDIT: Also, how often is shorthand used? For example, instead of:

if ($comment_count == 1)
$comment_suffix = 'comment';
else
$comment_suffix = 'comments';

I could put this in place of where I call $comment_suffix later:

($comment_count == 1 ? 'comment' : 'comments')

Obviously I wouldn't want to do it for anything too complex.
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
that's called a ternary, and it's pretty popular for conditional variable setting

i only peeked at a single file, the first thing i noticed was an echo with several lines of html.
it's a LOT easier to read (and type) if you just break out of php and have pure html.

if you need to echo a variable inside of the html you can just do the <?=$varname;?>

so instead of:
echo '<table cellspacing="0" class="entry_table">
<tr><td class="title">' . $row['title'] . '</td><td class="date">' . $date.'</td></tr>
<tr><td colspan="2" class="content">' . $content.'<br/><br/>
<div align="right">';

you can just do
?>
<table cellspacing="0" class="entry_table">
<tr><td class="title"><?=$row['title'];?></td><td class="date"><?=$date;?></td></tr>
<tr><td colspan="2" class="content"><?=$date;?><br/><br/>
<div align="right">
<?
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
one of my favorite coding tricks for big blocks of conditional html is use the :, endif; syntax
so
if($var){ ?>big block of html here<? }

could be written as
if($var): ?> big block of html here <?
endif;
 

thirtythree

Diamond Member
Aug 7, 2001
8,680
3
0
Originally posted by: troytime
that's called a ternary, and it's pretty popular for conditional variable setting

i only peeked at a single file, the first thing i noticed was an echo with several lines of html.
it's a LOT easier to read (and type) if you just break out of php and have pure html.

if you need to echo a variable inside of the html you can just do the <?=$varname;?>

Good suggestion, that probably is easier to read. I wasn't sure if <?= was recommended since it only works when short_open_tag is enabled, but I guess the worst-case scenario would be doing a find and replace.

Is the endif syntax just so it's clear that the block of HTML is conditional? (As opposed to seeing a lone } at the end and wondering what it's for.)
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
it doesn't have to be an html block in the conditional, but that's really the only time i use it.

the lone <? } ?> in between a bunch of html can be confusing. the reader doesn't know if its from a function or a conditional. the endif; makes it a little more clear.

i guess if there's really a lot of html that is conditionally controlled it could be separated into includes, that way the conditional statements are nice and clean.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |