• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

Calling a specific line of text...

Right, basically, I'm creating an image gallery which will consist of 4 frames. Top frame will have a link back to main page in it, so that's a constant. The second frame down will be the page where the images are displayed, and I can do that... that's not a problem... The fourth (yes, I know, I missed one) frame will be where the thumbnails are displayed, again no problem... the third frame will be where the descriptions of the photos are displayed. This is where I find a problem.
My links from the thumbnail frame are going to look something like this:

../fullsize/get_file.php?id=0001&textid=0001

So, 'id=0001' refers to calling the picture 0001.jpg from the fullsize pictures folder.

'textid=0001' will, hopefully, refer to calling the line of text from a certain file...

So, for example...
The photos descriptions text file would be as follows:

0001 = A picture of me
0002 = A picture of my cat
0003 = A picture of me being eaten by my cat

etc...

How would I go about creating a text file or database with lines of text in which I can number and call on using server variables, and how would I go about calling from that file, and inserting the line into the third frame... I hope this is clear...

Thanks in advance.
0
cagsmith
Asked:
cagsmith
  • 4
  • 4
1 Solution
 
snoyes_jwCommented:
You could stick the whole thing in a database.  Each record would contain a unique id (0001, 0002, 0003...), the path to the picture, the path to the thumbnail, and the text.  Then all you have to pass around is the unique id.

Any database will work; MySQL is popular, free, well supported, and integrates very nicely with PHP.  It's pretty easy to install too, if you're happy with default configurations.  See http://www.mysql.com and http://www.php.net/mysql for details.

If you really want to use the text file instead, there are several PHP methods to read a file into either a string or an array of strings, and several string search functions.
http://www.php.net/function.file
http://www.php.net/strstr
http://www.php.net/preg_match
0
 
cagsmithAuthor Commented:
Ok, the MySQL database sounds like a plan... erm... any idea how I go about this one? I'm used to using PHP, just not MySQL.
0
 
snoyes_jwCommented:
Download mysql from www.mysql.com and follow the documentation to get it installed and running.  Create a database, and create a table in that database, using fields as I mentioned above (id, picturePath, thumbnailPath, textDescription).  Add some records to this table.  There is good documentation on how to do all this at the mysql website.

Depending on how much is in each frame, you could write one page to return everything and pass a parameter for what you want returned, or you could use separate pages for each frame.

The page(s) will contain something like:

<?php
// connect to database server
mysql_connect("server', "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

// fetch values from database
$sql = "SELECT textDescription FROM myTable WHERE id = " . $_GET['id'];
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);

echo $row['textDescription'];
?>
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
cagsmithAuthor Commented:
Ok, I decided to stick with PHP after having a look at MySQL and it seems far to hard for someone of my means to try and learn :(
I had a look at those links to php.net and they none of them seemed to answer fully what I wanted...

I guess I'd have to load the file with the text descriptions and then call the respective line of text, but how...
What would I need to write as the link after, eg, 'fullsize/get_image.php?photoId=0001&*what goes here*

and how do I load the file with the text descriptions in? Also, I guess, what's easier than having 4 frames, I could just load the text into a cell in a table under the photo, but what sort of PHP command would I use for that?
0
 
snoyes_jwCommented:
Ok, so you've got a text file.

You load it into an array:
$textFile = file("path/to/file.txt");

You run through the array and look for the id in question:
foreach($textFile as $text) {
    if(substr($text, 0, 4) == $_GET['id']) {
        echo substr($text, 8);
}
0
 
cagsmithAuthor Commented:
Simple question I guess, but how do I define the id of each particular line of text in the text file?
0
 
snoyes_jwCommented:
The code above looks for the id in the first 4 characters of the text file, and takes the text from character 8 on (should be 7, since we're starting from 0).  So, you use the text file like you had it written:
0001 = text
0002 = text
0
 
cagsmithAuthor Commented:
That's great. Thanks a lot.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now