Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying images in Recordset '3 per row' 1

Status
Not open for further replies.

jrhouk

Programmer
Jan 1, 2002
6
US
Have utilized ASP that will display images of art contained in recordset.
Need to have them displayed in a dynamic table that will automatically shift to a new row after 3 images are displayed.
Anybody have suggestions as to how I accomplish this?
 
Just setup a do while loop that starts at one and creates <td> cells until the counter reaches 3, then close the row, reset the counter, and keep doing it until the end of the recordset has been reached.

 
Thanks Jim

However, I am just learning this and, while I tried to understand what you were telling me to do and read all other posts regarding loops (and anything related), I still couldn't figure it out.

Could you give me a brief example of the code that will show me what you mean?

Thanks again.
 
The example below assumes the following:
1. adovbs.inc is in the same directory as the file,
2. your database is in a database folder below the file,
3. your images are in a images folder below the file,
4. you have a products table containing the fields name and thumbnail. The name field is the name of the product and the thumbnail field is the name of the item's image.

Modify these items to suit your current environment. Also, you may not need everything that is presented but then again this was just an example.

Copy the following to an .asp page (i.e. item.asp) and then access it through your browser.

Code:
<%
   response.expires = 0
   response.cachecontrol = &quot;private&quot;
%>
<!-- #include file=&quot;adovbs.inc&quot; -->
<%
   homepage = &quot;index.htm&quot;

   ' Define SQL statements for the page and items.
   itemsql = &quot;select sku, name, price, abstract, show from tblproducts order by name&quot;

   ' Establish a database connection.
   set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
   openstr = &quot;driver={Microsoft Access Driver (*.mdb)};&quot; & _
                  &quot;dbq=&quot; & server.mappath(&quot;database\wpt.mdb&quot;)
   conn.open openstr

   ' Define and open recordsets for the page and items.
   set itemrs = Server.Createobject(&quot;ADODB.RecordSet&quot;)
   itemrs.open itemsql, conn, adopenstatic, adlockreadonly
%>
<html>
	<head>
		<title>Image catalog</title>
	</head>

<body>
<%
	if not itemrs.eof then
		' Create a table for all of the images.
		response.write &quot;<table border=&quot; & chr(34) & &quot;0&quot; & chr(34) & &quot;>&quot; & vbcrlf

		' Counter is the item index.
		counter = 1

		' Limit is the number of items we want per row.
		limit = 3

		do while not itemrs.eof
			' Step through the recordset placing images into their own cells.
			' At LIMIT close the table row and start another table row.

			if counter = 1 then
				' Write table row if this is the first image.
				response.write &quot;<tr>&quot; & vbcrlf
			end if

			' Write table cell with image as a link to detail page.
			response.write &quot;<td valign=&quot; & chr(34) & &quot;top&quot; & chr(34) & &quot;>&quot; & vbcrlf & _
   			&quot; <a href=&quot; & chr(34) & &quot;item.asp?catid=&itemid=&quot; & _
   			itemrs(&quot;sku&quot;) & chr(34) & &quot;>&quot; & vbcrlf & &quot;<img src=&quot; & chr(34) & _
   			&quot;images/&quot; & itemrs(&quot;thumbnail&quot;) & chr(34) & _
   			&quot; border=&quot; & chr(34) & &quot;0&quot; & chr(34) & _
   			&quot; alt=&quot; & chr(34) & server.htmlencode(itemrs(&quot;name&quot;)) & chr(34) & _
   			&quot; width=&quot; & chr(34) & &quot;110&quot; & chr(34) & _
   			&quot; height=&quot; & chr(34) & &quot;110&quot; & chr(34) & _
   			&quot;></a></td>&quot; & vbcrlf

			' Increment the counter.
			counter = counter + 1

			' If limit is reached, close the row and reset the counter.
			if counter > limit then
				response.write &quot;</tr>&quot; & vbcrlf
				counter = 1
			end if

			' Advance the recordset pointer to the next record.
			itemrs.movenext
		loop

		' Fill out the row for any remaining cells.
		if counter > 1 and counter <= limit then
			do while counter <= limit
				response.write &quot;<td></td>&quot; & vbcrlf
				counter = counter + 1
			loop
			'Close the table row.
			response.write &quot;</tr>&quot; & vbcrlf
		end if

		' Close the table.
		response.write &quot;</table>&quot;
	end if
%>
</body>
</html>
 
Well Jim, I'm getting closer.

You're code was way above my head, however, I stuck with it and, after trimming away some of the code I didn't think I needed, I've am close.

Temporarily, I replaced the images I want displayed with the word &quot;items&quot; and was able to see that the code did indeed display the words - 3 per row. See a skeleton view at

However when I tried to enter code referencing the images in the recordset I get an error message which points to that line.

Here is how the code currently looks:

<%
dim conn, query, data

Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
conn.Open &quot;DRIVER={Microsoft Access Driver (*.mdb)};_
DBQ=&quot; & Server.MapPath(&quot;\Kimmyart\db\kimmy.mdb&quot;)

'Create the SQL query and the record set
query = &quot;SELECT * FROM tblSculptures&quot;
Set data = Server.CreateObject( &quot;ADODB.Recordset&quot; )
Call data.Open ( query, conn )
%>

<%
dim counter, limit, item

counter = 1
limit = 3

if not data.EOF then
response.write &quot;<table>&quot;
do while not data.EOF
if counter = 1 then
response.write &quot;<tr>&quot;
end if
response.write &quot;<td>ITEMS</td>&quot;
counter = counter + 1
if counter > limit then
response.write &quot;</tr>&quot;
counter = 1
end if

data.movenext
loop

if counter > 1 and counter <= limit then
do while counter <= limit
response.write &quot;<td></td>&quot;
counter = counter + 1
loop
response.write &quot;</tr>&quot;
end if
response.write &quot;</table>&quot;
end if
%>

NOTE: Along with the images, I also want to pull it's name, description and unit price from the recordset.

An HTML example is at

Thanks again for your help.
 
replace
response.write &quot;<td>ITEMS</td>&quot;
AND
response.write &quot;<td></td>&quot;
with:



response.write (&quot;<td valign=&quot;&quot;top&quot;&quot;>&quot;)

response.write (&quot;<a href=&quot;&quot;item.asp?catid=&itemid=&quot; & itemrs(&quot;sku&quot;) & &quot;&quot;&quot;>&quot;) ' link to item
response.write (&quot;<img src=&quot;&quot;images/&quot; & itemrs(&quot;thumbnail&quot;) & &quot; border=&quot;&quot;0&quot;&quot; alt=&quot; & server.htmlencode(itemrs(&quot;name&quot;)) & &quot; width=&quot;&quot;110&quot;&quot; height=&quot;&quot;110&quot;&quot;></a>&quot;) ' image
response.write (&quot;<br>&quot; & itemrs(&quot;name&quot;)) ' picture name
response.write (&quot;<br>&quot; & itemrs(&quot;description&quot;)) ' picture description
response.write (&quot;<br>&quot; & itemrs(&quot;price&quot;)) ' unit price

response.write (&quot;</td>&quot;)
 
To do the name, price, and image you need that information stored in your tblstructures table. I'll assume that you have that and the field names are NAME, PRICE, and IMAGENAME in the example below. Please note that the field names are capitalized only for illustration purposes in this example.

Take what you have and replace the line:
response.write &quot;<td>ITEMS</td>&quot;

with

response.write &quot;<td>&quot; & data(&quot;IMAGENAME&quot;) & &quot;<br>&quot; & vbcrlf & _
data(&quot;NAME&quot;) & &quot;<br>&quot; & vbcrlf & _
&quot;$&quot; & data(&quot;PRICE&quot;) & &quot;</td>&quot; & vbcrlf

The above assumes that your price field does not contain the $ mark. If it does simply remove the &quot;$&quot; entry.

Also note that if the NAME field contains any extraneous characters (like &quot;, &, or anything else) then replace the data(&quot;NAME&quot;) entry with server.htmlencode(data(&quot;NAME&quot;)). The server.htmlencode directive takes special characters and changes them to be HTML .

Please let me know how that works for you.
 
Thanks again.

That works, with one exception. Instead of displaying the thumbnail image, it's displaying the name of the thumbnail image.

I tried using a HTML img tag around the data(&quot;IMAGENAME&quot;),
Like this:
response.write &quot;<td>&quot; & &quot;<img src=&quot; data(&quot;IMAGENAME&quot;)&quot;>&quot; & &quot;<br> etc.,etc....

but no luck.
 
I totally apologize to you. I completely flubbed in my last post to you. You were on the correct track though.

Your code line should be:
response.write &quot;<img src=&quot; & chr(34) & &quot;images/&quot; & itemrs(&quot;thumbnail&quot;) & server.htmlencode(itemrs(&quot;name&quot;)) & chr(34) & &quot;></a><br>&quot; etc., etc.

Again, I am truly sorry for that mistake.
 
Disregard the above post, I missed a quote mark. This is the real post.

I totally apologize to you. I completely flubbed in my last post to you. You were on the correct track though.

Your code line should be:
response.write &quot;<img src=&quot; & chr(34) & &quot;images/&quot; & itemrs(&quot;thumbnail&quot;) & chr(34) & &quot; alt=&quot; & chr(34) & server.htmlencode(itemrs(&quot;name&quot;)) & chr(34) & &quot;><br>&quot; & server.htmlencode(itemrs(&quot;name&quot;)) & &quot;</a><br>&quot; etc., etc.

Again, I am truly sorry for that mistake.
 
Congratulation, your site came out very nice.

If you ever need any additional help be sure to ask.
 
Hey Jim, et. all.

This code was great! Had to tweak to get it to display okay, but boy it's fantastic.

Any idea how to modify and show like 4 rows per page?
a repeat region of sorts.

Thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top