• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • 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:


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


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.
  • 4
  • 4
1 Solution
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.
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.
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:

// 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'];
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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?
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);
cagsmithAuthor Commented:
Simple question I guess, but how do I define the id of each particular line of text in the text file?
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
cagsmithAuthor Commented:
That's great. Thanks a lot.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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