Thread: PHP limits
View Single Post
  #8 (permalink)  
Old 03-02-09, 12:34
RichardM RichardM is offline
BOD Member
 
Join Date: Jan 2008
Location: NYC
Posts: 94
Default

Quote:
Originally Posted by Shane Phillips View Post
Richard, i would like you to show us a demonstrations of php codes and errors, that'll help understand better.

Regards,

Shane Phillips
Okay, here's a simple gallery script (it also incorporates a JavaScript for the greybox effect, which I didn't write):

PHP Code:
$username="(hidden)";
$password="(hidden)";
$database="(hidden)";

mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM img_list WHERE page_name='$page'";
$result=mysql_query($query);

$num=mysql_numrows($result);
$i=0;
while (
$i $num) {
    
$thumb=mysql_result($result,$i,"thumb");
    
$full_size=mysql_result($result,$i,"full_size");
    
$caption=$title=mysql_result($result,$i,"caption");
    
$alt=mysql_result($result,$i,"alt");
    print 
"[IMG]http://www.bodhost.com/forum/%5C%22Images/gallery/%22[/IMG]" PHP_EOL;
    print 
"" $caption "
"
PHP_EOL;
    
$i++;
}
mysql_close();
?> 
What it does is calls a database that contains the locations of the image files associated with a page name, writes the HTML code for the gallery, and stops writing when it runs out of pictures for that page. It also writes the captions, ALT tags, and TITLE tags.

The browser sees only the HTML code, not the PHP; so it crawls and optimizes like any other page.

The individual pages only have to contain the single line:

PHP Code:
 
This prevents me from having to re-code the gallery for every single page that has one. All I have to do is reference the page name and include the gallery script, and PHP writes it.

The associated CSS is:

Code:
#gallery {
    position: relative;
    margin-left: 0;
    width: 95%;
    display: inline-table;
    font-size: 9px;
}

#gallery a { border: none; text-decoration: none; }

#gallery li {
    display: inline;
    float: left;
    width: 100px;
    height: 160px;
    margin: 7px;
    text-align: center;
    border-bottom: #FF6600 1px solid;
}
This causes the gallery thumbnails to render inline, and also allows the number of columns to be liquid and adjust with the width of the browser window.

That's a rebuild of a current site that I inherited from someone else, which I'm rebuilding using CSS, PHP, and MySQL. By getting all the formatting junk off the pages and onto the stylesheet, I'm lightening the pages and improving the content density; and by using PHP and MySQL to generate repetitive things like galleries, I'm reducing the rewrite time and making updates easier in the future. To add or remove pics, all I have to do is make the changes to the database instead of re-coding the pages.

It does take some time to learn PHP, but this simple code example (which actually is pretty entry-level PHP) will save me many hours of development time now and in the future; so I would say learning PHP is well worth the effort for a serious designer or developer, especially when using a Linux platform.

I'll intentionally code something with errors and put it up when I have a few minutes, so anyone interested can see what the errors look like.

-Richard

P.S. -- the rebuild is a work in progress, so some of the links on the page may be dead.
Reply With Quote