Reply to Re: Database structure decision

Your name:

Reply:


Posted by Jerry Stuckle on 01/27/07 22:46

Geoff Berrow wrote:
> Message-ID: <b-ydnfwzS5NGISfYnZ2dnUVZ_qunnZ2d@comcast.com> from Jerry
> Stuckle contained the following:
>
>> I suggest you try it before you start telling me how bad it is. As I
>> said - I've done it for a number of sites. It works great.
>
> Let's say I'm interested enough to try it Jerry, have you got a little
> sample code I could play around with?
>

Geoff,

No little pieces of code - it's pretty well integrated into the systems,
and I'm not on that computer today. But some ideas from memory follow.

But basically, I keep the picture itself in a separate table, containing
only the picture and an id column. This increases performance, because
most of the time you don't want the picture itself. And keeping it in a
separate table means the database doesn't have to skip over the data
when gathering the rest of the info.

The main table has things like the picture id, caption, size, and
anything else I want about the picture. These are the things you would
want/need in the HTML itself.

The picture is loaded via a standard <img... tag, pointing at a short
PHP script. For instance, to display a thumbnail for product "$prodid",
you could have something like:

$result=mysql_query("SELECT pixid,height,width,alt from Pix WHERE " .
"prodid=$prodid AND thumbnail=1");
if ($result) {
if (mysql_numrows == 1) {
$list($pixid, $height, $width, $alt)= mysql_fetch_array($result);
echo "<img src=\"pix.php?pixid=$pixid\" height=$height " .
"width=$width alt=\"$alt\" border=0>"
}
mysql_free_result($result);
}

height and width contain the picture size in pixels, pixid is the id of
the picture, alt is the alt text, and thumbnail is either 1 or 0,
depending on whether it's a thumbnail (1) or not. Primary key is on
pixid and thumbnail (this would have to change if you have more than 1
picture per product).

The PHP script called in the img statement is similar to: which just
gets the picture, sends the header and then echoes the picture data.
Something like:

<?php
if (!isset($_GET('pixid') !! trim($_GET('pixid') == "")
exit(0);

$pixid = $_GET('pixid');

$conn=mysql_connect('localhost', $user, $pw);
if ($conn) // Can't connect
exit(0);

$result = mysql_fetch("SELECT ptype, picture from pix WHERE id=$pixid");
if (!$result)
exit(0);

if (mysql_num_rows($result) != 1)
exit(0);

$data = mysql_fetch_array($result);

header("Content-type: {data['ptype']");
echo $data['picture'];

msyql_free_result($result);
mysql_close($conn);

?>

Code for inserting into the database is basically similar to uploading,
except you move it from the temp directory to a work directory
(temporary name), read it and store it in the database, then delete it.

Hope this helps. It really isn't too bad.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация