Well, I'm not going to hold your hand and do it for you (as this forum does encourage self initiatives). But as for your image / ID issue, if any images in your database will only show up once in any given page, could you not have an extra column in your image table that contains unique ID? That way, in PHP, when you get an image from your database, you can also assign the ID number that comes with that image (in that row).
Again, this would work if the image being used from the database is unique to the page. If the image in question is going to be used more than once on a webpage, you could also use a class column and assign the class of your image to the corresponding class item in your table).
Just a thought.