Hello,
I'm curious on a few things here in regards to optimizing and consolidating code. Any advice would be much appreciated and I thank you kindly.
I have a dynamic menu which uses images as links for each item. 5 items are static and about 7 are conditional (e.g. "only display this menu item if $member_website_url != ""). For each dynamic menu item that is true, it prints another row, inserts the proper image, and sets it's url.
Instead of redundantly describing the menu item layout for each conditional like this:
if($website){
echo"<tr><td colspan=\"3\"><a href=\"$website\" class=\"nav\"><img src=\"imgs/website_menu_pic.gif\" width=\"280\" height=\"27\" border=\"0\" /></a></td></tr>";
..I describe it once in a function which allows variables for the image and link like so:
function echo_rollover($link, $roll_img){
echo"<tr><td colspan=\"3\"><a href=\"$link\" class=\"nav\"><img src=\"imgs/band/$roll_img\" width=\"280\" height=\"27\" border=\"0\" /></a></td></tr>";
}
//then..
if($website){
echo_rollover($website, "website.gif");
}
Okay,..so my questions are:
//////////////////REDUCING CODE WITH FUNCTIONS////////////////
1. If the server is already in the "echoing menu-item mode"; should I print the static menu items in the same way? I'm liking the reduced code but is that just un-necessary strain on the server?
- Along the same lines; what if I have a page with 8 identical tables, but only the center content changes. Should I declare the table code in a function and reduce filesize?
///////////////////SEPERATING CODE AND CONTENT////////////////
I've read that it is good practice to keep your php and html seperated as much as possible. On the above menu example; instead of running the if statements in the menu section of the html; should I be doing it at the top with the rest of my php? If so, how would I append the results of conditionals to a variable so I only need to echo $menu_output where needed? I'm not sure how I would do it while using that function.
Some pages on my site have includes that require queries. I put the query code on each individual include because they would only need to be run if that file was loaded. I put them all at the top of the include files, but in the long run I don't think that I'm properly seperating content because the code would run in the middle of the main page somewhere. Is that a big deal? Is it better to do a check at the top of the main page to see which section it's on, and then run the queries for that section's include along with the rest of the page's queries? One drawback I see there is that I would have to code a lot of queries and inflate the main page's filesize when only 10% of it would be used at any time.
///////////////////////////GENERAL PHP////////////////////////////////////
1. I typically set variables for mysql query results and url variables at the top of the page like this:
$name=$row['name'];
..and call them later in the page like this "The name is <? echo $name ?>". Is it better to bypass the $name variable and just use "The name is <? echo $row['name']; ?>" or "The number is <? echo $_GET['number']" ?> ?
- Lastly; my site has personal pages for members with several different sections such as Images, Bio,...etc,.. These pages use includes for the header, footer, and the left and right columns. Only the center section is updated when browsing between each member's pages and I would like to reduce mysql queries. Can anybody recommend a good method for this? I've considered just using hidden fields on each page and checking for variables there before querying but there has to be a better way.
Ok all, sorry for the bombardment but I've had these questions in the back of my mind for a long time. If anybody could shed some advice on any of them I'd really appreciate it.
Thanks