Jump to content

Recommended Posts

Hi, my name is Tim and I am just starting to learn PHP. But I need some help.

I want, when a user goes to the page /images.php?id=*, the script will open up an external text file, and save the text at line (id) to the variable $image. It will then open up another external .txt file, and save the text at line (id) to the variable $title.

I realise that this may be hard, but please help me.

Thanks in advance, Tim

Link to comment
https://forums.phpfreaks.com/topic/45875-solved-php-help/
Share on other sites

Tim - you need to explain what you are trying to do in far more detail than we have at present.

 

I want, when a user goes to the page /images.php?id=*, the script will open up an external text file, and save the text at line (id) to the variable $image.

 

What 'text' and where does it come from?

What structure does this external text file have? Examples, please.

 

The better you explain what you are trying to accomplish, the more likely it is that someone here will provide a solution - or suggest a better way of doing what you're hoping to do.

Link to comment
https://forums.phpfreaks.com/topic/45875-solved-php-help/#findComment-222871
Share on other sites

ok, the first text document will look like:

/images/imagename.jpg

/images/anotherimagename.jpg

/images/logoforsite.gif

 

it will list down image locations.

 

the second text document will look like:

Title for Image1

Title for Image2

Title for Image3

 

it will list the name of each image.

 

So, when they go to /images.php?id=3

 

it can display the image located at line 3, and display the title located at line 3

Link to comment
https://forums.phpfreaks.com/topic/45875-solved-php-help/#findComment-223199
Share on other sites

So the id corresponds to a line in the text files? correct?

 

try this simple bit of code:

<?php

if(isset($_GET['id']) && is_numeric($_GET['id']))
{
    // this holds the line number for our image/title in the txt file
    $i = ($_GET['id'] - 1);

    // file grabs every line in a document in to an array
    // read more on file here:
    // http://www.php.net/file
    $path = file('paths.txt');
    $title = file('titles.txt');

    $html = '<b>' . $title[$i] . '</b><br />
<img src="' . $path[$i] . '" title="' . $title[$i] . '" />';

    echo $html . '<hr />';
}

?>
Choose an image to be displayed:<br />
<a href="?id=1">Get image #1</a>
<a href="?id=2">Get image #2</a>
<a href="?id=3">Get image #3</a>

 

This code gets the paths of the images from a file call paths.txt and the titles for the image from titles.txt

Link to comment
https://forums.phpfreaks.com/topic/45875-solved-php-help/#findComment-223505
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.