WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : A simple photo album script, searchable by category or title/comment/keyword information.
Categories : PHP, Complete Programs Update Picture
Adam Hayward
Date : Jul 23rd 2001
Grade : 3 of 5 (graded 10 times)
Viewed : 12615
File : album.php3
Images : No Images for this code example.
Search : More code by Adam Hayward
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

Hello Peeps.

This is my first attempt at php programming, but it seems to work ok nevertheless. Basically, I am unable to use SQL on my server and I wanted to create a searchable photograph archive. This script will open a .csv (comma separated variable, or semi-colon in this case) file, shove it into a string, and act accordingly. Features include a thumbnail preview option, results are split up into blocks of 10, and the searcher can look for a string or jump straight to a category.

If the thumbnail option is selected, then it is assumed that the thumbnail image is stored in a subdirecory of the main image called 'thumbs/'. If no input is given by the user the script will return a file called "pic.inc", which could contain a search form. perhaps something like:

<form action="<? echo $PHP_SELF ?>" method=GET
ENCTYPE="multipart/form-data">
<table width="100%" border=0>
<tr><td align="center" colspan="2">
<b>NEW!!</b> - Search for pictures:</td></tr>
<tr>
<td width="50%" align="center">
Search Query: <input name="q" size="20"><br>
<input type="submit" value="Find">   
</td>
<td width="50%" align="center">
Jump to Album: <select name="c" size="1">
<option value="">?</option>
<option value="cat1">Category 1</option>
<option value="cat2">Category 2</option>
<option value="cat3">Category 3</option>

</select><br>
<input type="submit" value="Jump">   
</td>
</tr>
<tr>
<td colspan="2">
Thumbnail Preview?
<input type=checkbox name="t" value="1" checked>
</td></tr></table></form>



A Simple searchable photo album script by Adam Hayward
<?php
function getmicrotime(){
     list(
$usec, $sec) = explode(" ",microtime());
     return ((float)
$usec + (float)$sec);
     }

/*Define your photo database. make it a .csv file with records on separate lines*/

$db = "db.csv";

/*Define the fields in the database: 0= unique record number (id), 1=category ... etc*/
/*eg. id;holiday2001;images;me_on_beach.jpg;Me on Beach;Getting nicely tanned;Adam Hayward*/

$id=0;
$cat=1;
$dir=2;
$pic=3;
$title=4;
$comment=5;

/*Check which page is required, and set to 1 if unknown*/

if ($p == ""){
   
$p = 1;
}

/*Define Possible categories*/

$catarray = array(holiday, this, that, the_other);


/*Look for pictures in a particular category if $c is given and*/
/*corresponds to a category in $cataray                        */

if (in_array ($c, $catarray)){
echo
"<div class=cyan> Category: $c";
$recs_returned = 0;
$fp = fopen ($db, "r");
while (
$data = fgetcsv ($fp, 1000, ";")) {
   
$srch = "$data[$cat]";
    if (
preg_match ("/$c/i", $srch)) {
           
$recs_returned++;
    }
}

fclose ($fp);

/*Split up records returned into blocks of 10*/

$roundrecs = $recs_returned/10;
$pages = ceil($roundrecs);
echo
". $recs_returned records are in this category. Page $p of
$pages</div>";
if (
$recs_returned > 10){
   
$k=1;
    echo
"<center>";
    while (
$k <= $pages){
        if (
$p == $k){
            echo
"[" . $k . "]";
           
$k++;
        } else {
            echo
"<b><a href=" . $PHP_SELF . "?c=" . $c . "&t=" . $t . "&p=" . $k . ">[" . $k . "]</a></b>";
           
$k++;
        }
    }
    echo
"</center>";
}

echo
"<br><table border=0 cellspacing=4 cellpadding=2 width=\"100%\">";

$fp = fopen ($db, "r");
$l = 0;
$even = 0;
$start_record = 10 * ($p - 1);
$end_record = $start_record + 10;

while (
$data = fgetcsv ($fp, 1000, ";")) {
   
$srch = "$data[$cat]";
    if (
preg_match ("/$c/i", $srch)) {
        if (
$l >= $start_record) {
            if (
$t == 1) {
                if (
$even == 0){
                    print
"<tr>";
                   
$even = 3;
                }
                    print
"<td width=\"50%\"><center>" . $data[$title] . "<br>";
                print
"<a href=pics/process4.php3?id=" . $data[$id] . ">" .
               
"<img src=pics/" . $data[$dir]
                .
"/thumbs/" . $data[$pic] . "></a>";
                    print
"<font size=-1>" . $data[$id] . "</font><br>";
                    print
"<i>" . $data[$comment]. "</center></i></td>";
                if (
$even == 1){
                    print
"</tr><tr><td colspan=2><hr size=1 width=\"50%\"></td></tr>";
                   
$even = 0;
                }
                if (
$even == 3){
                   
$even = 1;
                }
            } else {
                    print
$data[$id] . ": ";
                    print
$data[$title] . "</a> - <i>";
                    print
$data[$comment] . "</i><br>";
            }
/*END if ($t == 1)*/
       
/*END if ($l >= $start_record)*/
       
$l++;
        if (
$l == $end_record){
            break;
        }
    } 
/* END if (preg_match ("/$c/i", $srch))*/

}   /*END while ($data = fgetcsv ($fp, 1000, ";"))*/

print "</table>";

fclose ($fp);


} elseif (
$q == ""){

    include(
"pic.inc");

} else {

/* ~~~~~~~~~~~~~~ Searchy Loopy ~~~~~~~~~~~~~~~~~ */

$i = "0"; /* Count Records Searched */
$start_time = getmicrotime();
$recs_returned = 0;
$fp = fopen ($db,"r");
while (
$data = fgetcsv ($fp, 1000, ";")) {
   
$srch = "$data[3] $data[4] $data[5] $data[6] $data[7]";
    if (
preg_match ("/$q/i", $srch)) {
           
$recs_returned++;
    }
$i++;
}
fclose ($fp);

$roundrecs = $recs_returned/10;
$pages = ceil($roundrecs);
echo
"<div class=cyan><font color=#000000>Searched for
\"
$q\". $recs_returned records found. Displaying page $p of
$pages</font></div>";

/*This bit outputs the other pages available*/

if ($recs_returned > 10){
   
$k=1;
    echo
"<center>";
    while (
$k <= $pages){
        if (
$p == $k){
            echo
"[" . $k . "]";
           
$k++;
        } else {
            echo
"<b><a href=" . $PHP_SELF . "?q=" . $q . "&t=" . $t . "&p=" . $k . ">[" . $k . "]</a></b>";
           
$k++;
        }
    }
    echo
"</center>";
}

$fp = fopen ($db,"r");
$l = 0;
$even = 0;
$start_record = 10 * ($p - 1);
$end_record = $start_record + 10;
echo
"<br><table border=0 cellspacing=4 cellpadding=2 width=\"100%\">";
$fp = fopen ("pics/db/" . $album . ".csv","r");
while (
$data = fgetcsv ($fp, 1000, ";")) {
   
$srch = "$data[3] $data[4] $data[5] $data[6] $data[7]";
    if (
preg_match ("/$q/i", $srch)) {
        if (
$l >= $start_record) {
            if (
$t == 1) {
                if (
$even == 0){
                    print
"<tr>";
                   
$even = 3;
                }
                    print
"<td width=\"50%\"><center>" . $data[$title] . "<br>";
                print
"<a href=pics/process4.php3?id=" . $data[$id] . ">" .
               
"<img src=pics/" . $data[$dir]
                .
"/thumbs/" . $data[$pic] . "></a>";
                    print
"<font size=-1>" . $data[$id] . "</font><br>";
                    print
"<i>" . $data[$comment]. "</center></i></td>";
                if (
$even == 1){
                    print
"</tr><tr><td colspan=2><hr size=1 width=\"50%\"></td></tr>";
                   
$even = 0;
                }
                if (
$even == 3){
                   
$even = 1;
                }
            } else {
                    print
$data[$id] . ": ";
                    print
$data[$title] . "</a> - <i>";
                    print
$data[$comment] . "</i><br>";
               
$j++;
            }
        }
/*END if ($l >= $start_record) {*/
       
$l++;
        if (
$l == $end_record){
            break;
        }
    }
$i++;
}
print
"</table>";
echo
"<hr size=1 width=50%>";

if (
$j == "0") {
    echo (
$i . " records searched<br>Sorry, nothing found.");
} else {
    echo (
$i . " records searched, " . $recs_returned . " items found.");
}
$end_time = getmicrotime();
$long_time = 1000 * ($end_time - $start_time);
$time = round ($long_time);
echo
"<br>Search took $time milliseconds";
}
?>



phpEasyMail: An easy way to send data from HTML-forms via EMail.
Categories : Email, HTML and PHP, Complete Programs, PHP
Tropicalm Genetree Family (MySQL based family tree)
Categories : PHP, Interfaces, Databases, MySQL, Complete Programs
phpYellow Pages Standard
Categories : PHP, Complete Programs, Databases, Directories, Search
Client classes for Dictionary servers UPDATED: 2000-06-06
Categories : Network, Search, Complete Programs, PHP Classes, PHP
BBS system for easy customization. Utilizes mySQL.
Categories : Complete Programs, MySQL, PHP, Databases
Validator 98 - a PHP-script to generate form-validation-code in JavaScript.
Categories : Complete Programs, Java Script, PHP, HTML and PHP
Modification of Shane Caraveo's guestbook. Uses ODBC...some code modifications
Categories : ODBC, Databases, Complete Programs, PHP
simple shopping cart for php3
Categories : PHP, PHP Classes, Complete Programs, Ecommerce
phpFreeChat
Categories : PHP, AJAX, Complete Programs
PHP-MySQL shopping cart
Categories : PHP, Ecommerce, Complete Programs
databases administration remote Web
Categories : mSQL, PHP, Complete Programs, Databases
This is a very simple BBS that uses MySQL
Categories : MySQL, Databases, Complete Programs, PHP
GuestBook Light - a plug and play application for any website.
Categories : PHP, Complete Programs, Filesystem, Sessions
DBE - Database Expander: Edit PostgreSQL individual database tables online via your Web browser!
Categories : PostgreSQL, Complete Programs, Databases, PHP Classes, PHP
GroupIT Content Management and Community Engine
Categories : PHP, Complete Programs, Content Management
 Adam Hayward wrote :664
Sorry, just re-read that submission. I know, I can`t spell. I`ve just spent 8 hours infront of the computer, my eyes are square, my exams are looming, it`s 4am, my nose runs, my feet smell and I don`t love Jesus. Oh well, here`s the script in full for cuttin` `n` pastin` (and seing how naff it is before you download it). And peeps, I REALLY would appreciate helpful feedback. just remove "DONTSPAMMEPLEASE." from my addy.
I forgot to mention another feature: it tells you how long the search took in miliseconds. What more do you want? Oh yeah, it outputs the results in a nice table, 2 to a row.
Here we go ...

&lt;?php 
/*A Simple searchable photo album script by Adam Hayward*/


function getmicrotime(){ 
     list($usec, $sec) = explode(" ",microtime()); 
     return ((float)$usec + (float)$sec); 
     } 

/*Define your photo database. make it a .csv file with records on separate lines*/

$db = "db.csv";

/*Define the fields in the database: 0= unique record number (id), 1=category ... etc*/
/*eg. id;holiday2001;images;me_on_beach.jpg;Me on Beach;Getting nicely tanned;Adam Hayward*/

$id=0;
$cat=1;
$dir=2;
$pic=3;
$title=4;
$comment=5;

/*Check which page is required, and set to 1 if unknown*/

if ($p == ""){
    $p = 1;
}

/*Define Possible categories*/

$catarray = array(holiday, this, that, the_other);


/*Look for pictures in a particular category if $c is given and*/
/*corresponds to a category in $cataray                        */

if (in_array ($c, $catarray)){
echo "&lt;div class=cyan&gt; Category: $c";
$recs_returned = 0;
$fp = fopen ($db, "r");
while ($data = fgetcsv ($fp, 1000, ";")) {
    $srch = "$data[$cat]";
    if (preg_match ("/$c/i", $srch)) {
            $recs_returned++;
    }


fclose ($fp);

/*Split up records returned into blocks of 10*/

$roundrecs = $recs_returned/10;
$pages = ceil($roundrecs);
echo ". $recs_returned records are in this category. Page $p of
$pages&lt;/div&gt;";
if ($recs_returned &gt; 10){
    $k=1;
    echo "&lt;center&gt;";
    while ($k &lt;= $pages){
        if ($p == $k){
            echo "[" . $k . "]";
            $k++;
        } else {
            echo "&lt;b&gt;&lt;a href=" . $PHP_SELF . "?c=" . $c . "&t=" . $t . "&p=" . $k . "&gt;[" . $k . "]&lt;/a&gt;&lt;/b&gt;";
            $k++;
        }
    }
    echo "&lt;/center&gt;";
}

echo "&lt;br&gt;&lt;table border=0 cellspacing=4 cellpadding=2 width=\"100%\"&gt;";

$fp = fopen ($db, "r");
$l = 0;
$even = 0;
$start_record = 10 * ($p - 1);
$end_record = $start_record + 10;

while ($data = fgetcsv ($fp, 1000, ";")) {
    $srch = "$data[$cat]";
    if (preg_match ("/$c/i", $srch)) {
        if ($l &gt;= $start_record) {
            if ($t == 1) {
                if ($even == 0){
                    print "&lt;tr&gt;";
                    $even = 3;
                }
                    print "&lt;td width=\"50%\"&gt;&lt;center&gt;" . $data[$title] . "&lt;br&gt;";
                print "&lt;a href=pics/process4.php3?id=" . $data[$id] . "&gt;" . 
                "&lt;img src=pics/" . $data[$dir]
                . "/thumbs/" . $data[$pic] . "&gt;&lt;/a&gt;";
                    print "&lt;font size=-1&gt;" . $data[$id] . "&lt;/font&gt;&lt;br&gt;";
                    print "&lt;i&gt;" . $data[$comment]. "&lt;/center&gt;&lt;/i&gt;&lt;/td&gt;";
                if ($even == 1){
                    print "&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=2&gt;&lt;hr size=1 width=\"50%\"&gt;&lt;/td&gt;&lt;/tr&gt;";
                    $even = 0;
                }
                if ($even == 3){
                    $even = 1;
                }
            } else {
                    print $data[$id] . ": ";
                    print $data[$title] . "&lt;/a&gt; - &lt;i&gt;";
                    print $data[$comment] . "&lt;/i&gt;&lt;br&gt;";
            } /*END if ($t == 1)*/
        }  /*END if ($l &gt;= $start_record)*/
        $l++;
        if ($l == $end_record){
            break;
        }
    }  /* END if (preg_match ("/$c/i", $srch))*/

}   /*END while ($data = fgetcsv ($fp, 1000, ";"))*/

print "&lt;/table&gt;";

fclose ($fp);


} elseif ($q == ""){

    include("pic.inc");

} else {

/* ~~~~~~~~~~~~~~ Searchy Loopy ~~~~~~~~~~~~~~~~~ */

$i = "0"; /* Count Records Searched */
$start_time = getmicrotime();
$recs_returned = 0;
$fp = fopen ($db,"r");
while ($data = fgetcsv ($fp, 1000, ";")) {
    $srch = "$data[3] $data[4] $data[5] $data[6] $data[7]";
    if (preg_match ("/$q/i", $srch)) {
            $recs_returned++;
    }
$i++;
}
fclose ($fp);

$roundrecs = $recs_returned/10;
$pages = ceil($roundrecs);
echo "&lt;div class=cyan&gt;&lt;font color=#000000&gt;Searched for
\"$q\". $recs_returned records found. Displaying page $p of
$pages&lt;/font&gt;&lt;/div&gt;";

/*This bit outputs the other pages available*/

if ($recs_returned &gt; 10){
    $k=1;
    echo "&lt;center&gt;";
    while ($k &lt;= $pages){
        if ($p == $k){
            echo "[" . $k . "]";
            $k++;
        } else {
            echo "&lt;b&gt;&lt;a href=" . $PHP_SELF . "?q=" . $q . "&t=" . $t . "&p=" . $k . "&gt;[" . $k . "]&lt;/a&gt;&lt;/b&gt;";
            $k++;
        }
    }
    echo "&lt;/center&gt;";
}

$fp = fopen ($db,"r");
$l = 0;
$even = 0;
$start_record = 10 * ($p - 1);
$end_record = $start_record + 10;
echo "&lt;br&gt;&lt;table border=0 cellspacing=4 cellpadding=2 width=\"100%\"&gt;";
$fp = fopen ("pics/db/" . $album . ".csv","r");
while ($data = fgetcsv ($fp, 1000, ";")) {
    $srch = "$data[3] $data[4] $data[5] $data[6] $data[7]";
    if (preg_match ("/$q/i", $srch)) {
        if ($l &gt;= $start_record) {
            if ($t == 1) {
                if ($even == 0){
                    print "&lt;tr&gt;";
                    $even = 3;
                }
                    print "&lt;td width=\"50%\"&gt;&lt;center&gt;" . $data[$title] . "&lt;br&gt;";
                print "&lt;a href=pics/process4.php3?id=" . $data[$id] . "&gt;" . 
                "&lt;img src=pics/" . $data[$dir]
                . "/thumbs/" . $data[$pic] . "&gt;&lt;/a&gt;";
                    print "&lt;font size=-1&gt;" . $data[$id] . "&lt;/font&gt;&lt;br&gt;";
                    print "&lt;i&gt;" . $data[$comment]. "&lt;/center&gt;&lt;/i&gt;&lt;/td&gt;";
                if ($even == 1){
                    print "&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=2&gt;&lt;hr size=1 width=\"50%\"&gt;&lt;/td&gt;&lt;/tr&gt;";
                    $even = 0;
                }
                if ($even == 3){
                    $even = 1;
                }
            } else {
                    print $data[$id] . ": ";
                    print $data[$title] . "&lt;/a&gt; - &lt;i&gt;";
                    print $data[$comment] . "&lt;/i&gt;&lt;br&gt;";
                $j++;
            }
        } /*END if ($l &gt;= $start_record) {*/
        $l++;
        if ($l == $end_record){
            break;
        }
    }
$i++;
}
print "&lt;/table&gt;";
echo "&lt;hr size=1 width=50%&gt;";

if ($j == "0") {
    echo ( $i . " records searched&lt;br&gt;Sorry, nothing found.");
} else {
    echo ($i . " records searched, " . $recs_returned . " items found.");
}
$end_time = getmicrotime();
$long_time = 1000 * ($end_time - $start_time);
$time = round ($long_time);
echo "&lt;br&gt;Search took $time milliseconds";

}
?&gt;