Permlink Replies: 47 - Pages: 4 [ Previous | 1 2 3 4 | Next ] - Last Post: 20 Jul 15, 13:45 Last Post By: RobM Threads: [ Previous | Next ]
tyra

Posts: 12
Registered: 15-Mar-2008
Re: Reading comments from an external database
Posted: 27 Feb 09, 13:43   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
Can someone please help me. I think I am misunderstanding the syntax or something. I am exhausted from trying to get this working into the early hours the last two nights. Hope someone can help.

I am wanting to show a comment for each slide from my Access database. I actually need to show a different price for each item of furniture shown in each photograph. I have tried to incorporate David's solution mentioned here but using my database. This didn't work - I didn't get any comments. So then to prove that it actually does work I created a new Access database with a comments table and an ID, fileName and comments field with three records. The table looks like this:

ID fileName comment
1 chinesebench.JPG sadfwertqerq
2 LINOCHBK.jpg htyutyutyuj
3 displayProductImage1.jpg yiluioijomio

I then created a new Jalbum project with Chameleon with these three photographs in the album (chinesebench.jpg, linochbk.jpg and displayproductimage1.jpg). I ticked the 'Text to Display = Comment' checkbox and unticked the 'Use label if there is no comment' checkbox.

I created an ODBC System DSN with the data source name of commentdb.

I put the exact information from the thread above into the index.htt.

I placed the <%= comments.get(fileName) %> into the slide.htt in two places as there are two IMG tags in the file. See below:

<img id="slide" src="$imagePath" class="slideImage" width="${mImageWidth}" height="${mImageHeight}" ${copyProtectStr}alt="$fileName<ja:if exists="comment"> - <%= comments.get(fileName) %></ja:if>"<ja:if test=<%=!chameleon.imageNavigationNone%>> usemap="#imagemap"</ja:if><ja:else> onclick="window.open('${originalPage}','originalslide','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes')"</ja:else> /></a></td>
</ja:if><ja:else><img id="slide" src="$imagePath" class="slideImage" width="${mImageWidth}" height="${mImageHeight}" ${copyProtectStr}alt="$fileName<ja:if exists="comment"> - <%= comments.get(fileName) %></ja:if>"<ja:if test=<%=!chameleon.imageNavigationNone%>> usemap="#imagemap"</ja:if> />
</td>

I did a ALL make album build and clicked Preview to see the index page. I then clicked onto one of the three images showing and I would expect so see my comment showing but there is no comment. When the comment is in the ALT tag, am I supposed to see the comment text when I hover over the image. Even when I View Source, the ALT tag is empty.

I then changed the ALT text to this (removing the 'ja:if exists') test to see if it would work:
alt="$fileName<ja:if exists="comment"> - <%= comments.get(fileName) %></ja:if>"

And still no luck.

Can someone offer some advice, I'm about to lose my mind (and another night's sleep).

Cheers,
Howie
davidekholm

Posts: 3,442
Registered: 18-Oct-2002
Re: Reading comments from an external database
Posted: 1 Mar 09, 13:11   in response to: tyra in response to: tyra
  Click to reply to this thread Reply
Can you see any error message printed if you open Jalbum's system console after making an album? (press F7).
Also, try this with the Minimal skin first so you aren't distracted by all the code in the Chameleon skin.
davidekholm

Posts: 3,442
Registered: 18-Oct-2002
Re: Reading comments from an external database
Posted: 1 Mar 09, 13:12   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
You can also test run that script from within Jalbum's system console window and evaluate what makes it into the "comments" Map. Just drop the <% %> tags.
tyra

Posts: 12
Registered: 15-Mar-2008
Re: Reading comments from an external database
Posted: 2 Mar 09, 02:28   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
I did Make Album and then clicked F7 to open the system console window. I copied into the top window the following:

comments.get(fileName)

And pressed Ctrl E and the following information came up regarding the evaluation of that phrase:

Evaluation error on line 1:

Details:
Sourced file: inline evaluation of: ``comments.get(fileName) ;'' : Undefined argument: fileName

Does this mean that my DSN connection is not working? Could this be an issue with Vista security?

My problems seem deeper than that, because if I replace:
alt="$fileName - <%= comments.get(fileName) %>"
with:
alt="Hello World"
I still don't see the Hello World text.

I will try with the other skin now as you suggest.
tyra

Posts: 12
Registered: 15-Mar-2008
Re: Reading comments from an external database
Posted: 2 Mar 09, 02:34   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
Also, when I make an album normally there are no errors in the system console - it just lists the elapsed time.
tyra

Posts: 12
Registered: 15-Mar-2008
Re: Reading comments from an external database
Posted: 2 Mar 09, 03:05   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
I have just tried this with the Minimal skin and the same problems - nothing shows up. Every time I hover over the image when I do a PREVIEW is the image name (ie, the file name without the DOT and EXTENSION). Eg, for LINOCHBK.jpg, it just shows LINOCHBK (which is not what I have in my database).

Firstly, I put the code into the index.htt and then placed the <%= comments.get(fileName) %> in the ALT tag and also just outside the IMG tag. Eg, alt="<%= comments.get(fileName) %>".

Second, I tried just plugging the ALT tag with my own text, rather than talking to comments db, just to try and get something to show up. See my snippet below taken from Minimal in the rowiterator code in index.htt where I have put some text after the image tag and also in the ALT tag. None of these show. Even going into View Source and doing a find does not find the 'tyra' text. I also did a similar thing in slide.htt and no luck either.

<ja:if exists="iconPath">
<%-- No frames around icons like folders and movie files --%>
<img src="${iconPath}" width="${thumbWidth}" height="${thumbHeight}" border=0 alt="tyra">tyra
</ja:if>
<ja:else>
<img class="image" src="${thumbPath}" width="${thumbWidth}" height="${thumbHeight}" border=0 alt="tyra">tyra
</ja:else>

Would it be possible to perhaps send me the index.htt and slide.htt that you have used?
davidekholm

Posts: 3,442
Registered: 18-Oct-2002
Re: Reading comments from an external database
Posted: 2 Mar 09, 09:13   in response to: tyra in response to: tyra
  Click to reply to this thread Reply
I don't have the full code index.htt and slide.htt files anymore. You can attach your modifications to the Minimal skin here and I'll look at them. Did you add the script itself to the top of the index.htt file?

When you're testing your code in the system console, be ware that "fileName" is not a known variable in that context as "fileName" is an image specific variable. It is only known within a fileiterator or coliterator or inside the slide.htt file. When testing the script via the system console, type a known file name instead (some filename you know you have in your database, for instance:
comments.get("chinesebench.JPG");

Followed by CTRL+E to execute
tyra

Posts: 12
Registered: 15-Mar-2008
Re: Reading comments from an external database
Posted: 2 Mar 09, 11:06   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
Attachment commentdb DSN.jpg (51.5 KB)
Attachment comments.mdb (152.0 KB)
Attachment slide.htt (1.8 KB)
Attachment index.htt (2.0 KB)
Thanks for the further suggestions. Yes, I have the relevant script in the index.htt. I have attached index.htt and slide.htt for you.

When I put in the system console (comments.get("chinesebench.JPG");) I get this message which seems to indicate that the comments object doesn't exist. So perhaps the connection to the database via DSN isn't working???

Evaluation error on line 1:

Details:
Sourced file: inline evaluation of: ``comments.get("chinesebench.JPG") ;'' : Attempt to resolve method: get() on undefined variable or class name: comments

My dsn is a System DSN and a screenshot of the DSN settings is attached too. Could there be some overly onerous Vista security issues preventing the DSN from being called?
davidekholm

Posts: 3,442
Registered: 18-Oct-2002
Re: Reading comments from an external database
Posted: 2 Mar 09, 15:53   in response to: tyra in response to: tyra
  Click to reply to this thread Reply
Well, neither of those attached files contain the script as far as I can see. That also goes along with the error message you got on your last attempt.
tyra

Posts: 12
Registered: 15-Mar-2008
Re: Reading comments from an external database
Posted: 3 Mar 09, 13:11   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
Attachment index.htt (2.5 KB)
Attachment slide.htt (1.9 KB)
Sorry - see attached.
davidekholm

Posts: 3,442
Registered: 18-Oct-2002
Re: Reading comments from an external database
Posted: 3 Mar 09, 17:13   in response to: tyra in response to: tyra
  Click to reply to this thread Reply
That looks ok, try modifying the script so you insert some debug prints, like this:
<%!
  import java.sql.*;
  Map comments = new HashMap();
  System.out.println("Running script...");
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  System.out.println("Connecting to database...");
  Connection con = DriverManager.getConnection("jdbc:odbc:commentdb");
  Statement stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("select * from comments");
  System.out.println("Table data:");
  while (rs.next()) {
    String fileName = rs.getString(1);
    String comment = rs.getString(2);
    comments.put(fileName, comment);
    System.out.println(fileName + "\t" + comment);
  }
  rs.close();
  stmt.close();
  con.close(); 
%>

Now make the album and press F7 to inspect the debug printout. Can you see the table data now? What does it print?
tyra

Posts: 12
Registered: 15-Mar-2008
Re: Reading comments from an external database
Posted: 4 Mar 09, 10:54   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
The system console outputs the information as expected now (YAY!) which makes me realise that I am outputting the wrong information because I am using the wrong index (because I have an ID field as the first field in the table AND I now realise that the indexes are not ZERO based!)

I am outputting:

String fileName = rs.getString(1);
String comment = rs.getString(2);

Instead I need to output this:

String fileName = rs.getString(2);
String comment = rs.getString(3);

So basically because I was pulling out the wrong indexed information, it meant that the album was displaying the filename as the comment, instead of the actual comment, and the default behaviour is to show the filename! So hence I thought nothing was working!

David, you deserve a medal for your patience in helping me with this.

Much appreciated :-)

Cheers!
davidekholm

Posts: 3,442
Registered: 18-Oct-2002
Re: Reading comments from an external database
Posted: 4 Mar 09, 13:54   in response to: tyra in response to: tyra
  Click to reply to this thread Reply
Debug prints is a wonderful thing :-). I'm glad it sorted out.
tyra

Posts: 12
Registered: 15-Mar-2008
Re: Q. for David
Posted: 7 Mar 09, 11:08   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
David,
Could you possibly provide a snippet of code to explain how to put several elements of data from the database into a map object and also a snippet to describe how to get each element out?
Kind regards
Tyra
davidekholm

Posts: 3,442
Registered: 18-Oct-2002
Re: Q. for David
Posted: 7 Mar 09, 12:07   in response to: tyra in response to: tyra
  Click to reply to this thread Reply
Sure. Instead of just stuffing a String comment as the value of the comments Map, put an object that contains all the fields you need to access.
Let's assume you have a database table called users containing the following fields: filename, name, address and comment. You can now create a representation of such a user row in Java by creating a "User" class, for instance like this (index.htt):
<%!
	class User {
		String name;
		String address;
		String comment;
 
		public User(String name, String address, String comment) {
			this.name = name;
			this.address = address;
			this.comment = comment;
		}
	}
%>


Now peek at this demo code that populates a "users" Map object with two users:
<%!
	Map users = new HashMap();
	users.put("john.jpg", new User("John Doe", "Burbon St", "Don't we just love John Doe"));
	users.put("sam.jpg", new User("Sam Nielssen", "West St", "Another fake user"));
%>

In my example, the "users" map replaces the comments map used in the script previously. Now, to refer to the relevant fields of this "users" Map from the slide.htt file, you can do like this:
<%
	User user = (User)users.get("sam.jpg"); // In a real world example, "sam.jpg" would be fileName
%>

Now you can insert the field values of this user whereever you like, by writing like this (slide.htt):
<ja:if exists="user">
	The user's name is <%= user.name %>, his addresss is <%= user.address %> and he comment is <%= user.comment %>
</ja:if>
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums