Jump to content

Quality of Coding


ryan.od

Recommended Posts

I am completely new to PHP and have designed my own CMS with an integrated blog. At this point, the blog just posts the entry along with comments about the entry. I have yet to even create a way for a visitor to enter a comment (getting to that).

What I am wondering about is the quality of my coding. I am going to paste my blog code in below. If anyone could look at it and leave some general comments (especially those that point out obvious beginners mistakes), I would be very appreciative. Please try to remember this is the first thing I have ever coded in PHP.

Thanks.

RyanOD

--

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Pod Doctors</title>
<link rel="stylesheet" type="text/css" href="css/stylesheet.css">
</head>

<body>
<?php
include ('db/db_connect.php');
include ('db/db_select.php');

$sql = "SELECT * FROM menu";
$result = mysql_query($sql) or die('<p>Unable to perform menu query. Error: ' . mysql_error() . '</p>');
?>
<div id="container">

<div id = "top">
<img src="images/logo.png" />
</div>

<div id = "ads">

</div>

<div id = "left">
<ul>
<?php include ('menu/menu.php'); ?>
</ul>
</div>

<div id = "main">
<div id = "blog">
<?php
$sql = "SELECT * FROM blog";
$result = mysql_query($sql) or die('<p>Unable to perform menu query. Error: ' . mysql_error() . '</p>');
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo('<div id="entry">');
echo('<h2>' . $row['blog_title'] . '</h2>');
echo('<p>' . $row['blog_main'] . '</h3>');
echo('<h4>Posted by ' . $row['blog_author'] . ' on ' . $row['blog_date'] . '</h4>');
echo('</div>');

$id = $row['id'];

$sql2 = "SELECT * FROM comment WHERE id = '$id'";
$result2 = mysql_query($sql2) or die('<p>Unable to perform comment query. Error: ' . mysql_error() . '</p>');
echo('COMMENTS');
while($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
echo('<div id="comment">');
echo('<h3>' . $row['comment_title'] . '</h3>');
echo('<p>' . $row['comment_body'] . '</p>');
echo('<h4>Posted by ' . $row['comment_author'] . ' on ' . $row['comment_date'] . '</h4>');
echo('</div>');
}
} ?>
</div>
</div>
<div id = "footer">
<p>Copyright xxxxxxxxxx, 2006 | All Rights Reserved | <a href = "#">Contact xxxxxxxxxx</a></p>
</div>
</div>
</body>
</html>
Link to comment
https://forums.phpfreaks.com/topic/25853-quality-of-coding/
Share on other sites

I haven't looked at the code in detail but, one helpfull suggestion I have, is to consider using Object Oriented Programming with projects like this. Down the line it can make adding modifications/features much easier. Not to mention it is just good practice.

Another suggestion, or more just pointing our something.

Instead of:

[code]<?php
              echo('<div id="entry">');
              echo('<h2>' . $row['blog_title'] . '</h2>');
              echo('<p>' . $row['blog_main'] . '</h3>');
              echo('<h4>Posted by ' . $row['blog_author'] . ' on ' . $row['blog_date'] . '</h4>');
              echo('</div>');
?>[/code]

You can do:

[code]<?php

  echo"
  <div id=\"entry\">
  <h2>$row[blog_title] </h2>
  <p>$row[blog_main]</h3>
  <h4>Posted by $row[blog_author] on $row[blog_date]</h4>
  </div>";

?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/25853-quality-of-coding/#findComment-118083
Share on other sites

As Caesar pointed out, there are the basic improvements such as switching to OOP, however that's a high demand for someone right out of the starting gate. ;)

My biggest suggestion is to make sure you plan for growth in your coding. If you have 10 blog entries listed now, plan for 100. If you have 100, you don't want to have them all on one page. Think about adding pagination to it. Beyond that, I'd recommend you look into simply listing your titles on one page as links to the actual entries themselves. It's typically a little easier to follow things that way, too.

Hope these things give you a little direction as you're learning!
Link to comment
https://forums.phpfreaks.com/topic/25853-quality-of-coding/#findComment-118086
Share on other sites

Hey, thanks!! Actually, switching to OOP might not be too bad. I studied C++ in college and have a pretty decent understanding of the concept.

Yes, pagination is a good thing to think about. I planned on getting to that, but I like your suggestion of putting the entries on seperate pages. Maybe I'll go that route.

Once more thing. . .how do I reverse sort my entries based on date? Can rsort be used for this?

Thanks again.

Ryan OD
Link to comment
https://forums.phpfreaks.com/topic/25853-quality-of-coding/#findComment-118093
Share on other sites

[quote author=ryan.od link=topic=113515.msg461358#msg461358 date=1162415254]
Once more thing. . .how do I reverse sort my entries based on date? Can rsort be used for this?
[/quote]

If I understood your question correctly:

[code]<?php

$sql = "SELECT * FROM blog ORDER BY date DESC";

//Asuming you have a table called "date", and that dates are stored in the format of timestamps.

?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/25853-quality-of-coding/#findComment-118107
Share on other sites

[quote author=ryan.od link=topic=113515.msg461437#msg461437 date=1162423638]
Fantastic! Thanks so much. I was thinking I would reverse the blog order with PHP, not MySQL.

RyanOD
[/quote]

Just as a note, if you get into the practice of pulling all your records from a query out into an array, you can use array_reverse() to actually turn it upside-down. Make sure, though, as Caesar suggests, that you pull it from MySQL in the order you want to default to. As I said, this is just an FYI. It's typically better to let SQL to all the work that it can for you.
Link to comment
https://forums.phpfreaks.com/topic/25853-quality-of-coding/#findComment-118393
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.