I am developing and maintaining a product document database using PHP and MySQL for a client and I am facing a challenge I hope someone will help me with
Short description:
The database structured is like this:
| partno | partname |
('part number (6numbers)' and 'name of the product')
An example of data will then be:
| 096264 | Lens 6mm |
For each product there exists X files in a directory at the server
all the file names is build like this:
AB096264A.pdf
AB096264B.pdf
GE096264A.pdf
where AB = what kind of document, 096264 = the part number, and A = revision
So my solution is to:
- send query to the database
- create a loop
- inside the loop, open the file directory
- loop through the files and find matching partno from the DB with strings in the filename
THE CHALLENGE:
I only want the last revision to be printed (eg "B", "C", "D" ...)
how can I do that ?
please help me,
Here is the code I have so far (it prints all documents eg A,B,C revisions which we don't want):
//Start looping through the query
foreach($getDocs as $getDocs) {
$dir = @opendir("../docweb/");
echo "<tr>\n";
echo "<td class=\"".$cssClass."\">".$getDocs->partno."</td>\n";
echo "<td class=\"".$cssClass."\">".$getDocs->partname."</td>\n";
echo "<td class=\"".$cssClass."\">";
//loop through the directory and find matches
while (false !== ($file = readdir($dir))) {
if (strstr($file,$getDocs->partno)) {
echo "<a href=\"download.php?file=".$file."\" target=\"_blank\">";
if (substr($file,0,2) == "DS") {
echo "Datasheet";
}
elseif (substr($file,0,2) == "GA") {
echo "G.A. Dwg";
}
elseif (substr($file,0,2) == "IP") {
echo "Installation Procedure";
}
elseif (substr($file,0,2) == "MP") {
echo "Maintenance Procedure";
}
elseif (substr($file,0,2) == "OP") {
echo "Operating Procedure";
}
elseif (substr($file,0,2) == "WD") {
echo "Wiring Diagram";
}
else {
}
echo "</a><br>";
}
else {
}
}
echo "</td>\n";
echo "</tr>\n";
closedir($dir);
}
echo "</table>";