Good coding practice
I am graduating this June, so before that I want to prepare myself for working in the industry :)
My Question:
For example I want to list all the songs in the table using a list box and this is how I implemented it:
playlist.php
<tr id='' >
<td width="" class=""> <font color=#000开发者_如何学Go00 />Select Songs</td>
<td width="" colspan="">
<select size='25'multiple='multiple' id="select_songs" name="playlist_songs[]">
<?php
display_songs_list();
?>
</select></td>
</tr>
and I have a seperate php_functions.php
file where I have implemented all the php functions
function display_songs_list(){
$query = "select * from songs order by ID asc";
$result = mysql_query($query);
if(!$result)
echo "<script language = 'javascript'>alert('$result Sorry couldn't connect to the database...');</script>";
else{
$num_rows = mysql_num_rows($result);
if($num_rows > 0){
while($row = mysql_fetch_array($result)){
$ID = $row['ID'];
$title = $row['title'];
$value = $ID.'_'.$title;
echo "<option id=\"$ID\" value=\"$value\">$ID : $title</option>";
}
}
}
}
Is this way of implementation recommended. What else I can do to increace the scalability/maintainability/re-usability
. please guide me on this. Is it recommended to follow any industry coding standards, if so what you perfer. Thank you.
- Inline style attributes (
color=#00000
) are deprecated and kill kittens. Learn about CSS. - Tables are discouraged for anything except tabular data (think spreadsheets), use CSS for layout instead.
- Hardcoding whitespace with
is not desirable either if it doesn't add any meaning, learn to add spacing using CSS. echo
ing either HTML or Javascript from the same function is bad. The Javascript alert will cause invalid HTML syntax at the point you're calling the function.- Mixing database calls and HTML so tightly is not good. Look into MVC separation.
- Outputting a random, rather meaningless Javascript alert to the user in the middle of a half finished page is bad. You should display a dedicated error page instead. See MVC, which helps you accomplish this.
- Mixing single quotes and double quotes for the attributes is inconsistent and makes the code more difficult to read. Stick to one type of quotes instead.
- There is no form in your markup shown to submit the selected select options
Take my advice with a grain of salt, but first, you should really separate the code that talks with the database from the code that's given the user errors, they are two separate things. So an idea of how to achieve that in php might be a function that queries the database and returns an associate array with all the results that another function then prints. Also, you should have a more general database class that all your queries go through. The reason this would be preferred is that you can easily switch databases at a later point in time with minimal changes to your code, but if in every function you call mysql_query() it could become more complicated.
Its a reasonably good approach to take for php.
I normally split up my code into an Model/View/Controller type model.
The "controller" is the program which runs when the URL hits the server. It:-
- grabs and checks any get/posted variables from the screen.
- kicks of any requested actions such as database updates.
- kicks of some "model" functions to get the data for the next screen.
- formats the next screen using a "view" function or functions.
Note the controller doesn't itself emit any html.
The "model" functions do the database access and apply "business" rules.
The "view" functions emit html tags etc.
Apart from general neatness the big advantage of this is that you processing sequences isn't tied to the order the fields appear on the screen. So it really does make the programs more maintainable.
精彩评论