Click to See Complete Forum and Search --> : Dynamic Friend Pik_n_Link_Text!
SarahBab
04-29-2008, 10:32 PM
Please Help!
Me and my friends are developing a simple web page and we need a little help.
We have..
links/links.txt and blabber/blabber.text each with lines of text where one line equeal ones thing we need to use.
Also we got a folder named pic containing pictures names pic1.jpg, pic2.jpg etc
What we are looking for is php code to decide on a simple number and then to pull that picture and the relevant lines from both text files. The line from the links file needs to create a <a>link</a>
So if you load the page and PHP picked 5 (for example!) we'd display pic5.jpg with line 5 of blabber.txt and a link created from line 5 of urls in links.txt
Does that make sense? Will somebody help please!
SarahBab
04-30-2008, 11:27 PM
No help from anybody.
Did I say something wrong? :o
NogDog
05-01-2008, 12:40 AM
The file (http://www.php.net/file)() function will read a file into an array, with each line of the file being a separate array element. You could then select which line you want by specifying an array index which is 1 less than the desired number (since the array starts its numbering at 0).
MrCoder
05-01-2008, 05:13 AM
<?php
$id = (int)$_GET["id"];
?>
<img src="pic<?php echo $id; ?>.jpg">
<?php
// Depending on you OS try "\r\n" instead of "\n" if you have extra line breaks.
$blabber = explode("\n", file_get_contents("blabber.txt"));
$links = explode("\n", file_get_contents("links.txt"));
// May need to +/- the $id value here depending on your line number vs image number values.
?>
<a href="<?php echo $links[$id]; ?>"><?php echo $blabber[$id]; ?></a>
NogDog
05-01-2008, 12:23 PM
<?php
$id = (int)$_GET["id"];
?>
<img src="pic<?php echo $id; ?>.jpg">
<?php
// Depending on you OS try "\r\n" instead of "\n" if you have extra line breaks.
$blabber = explode("\n", file_get_contents("blabber.txt"));
$links = explode("\n", file_get_contents("links.txt"));
// May need to +/- the $id value here depending on your line number vs image number values.
?>
<a href="<?php echo $links[$id]; ?>"><?php echo $blabber[$id]; ?></a>
Since file() does the same thing as explode("\n", file_get_contents()), you might as well just use file() to save a little complexity and most likely save a millisecond or two. Although it does leave the newlines intact at the end of each field, so you'd probably have to trim() whichever value you select; so maybe it's six of one and a half dozen of the other?
SarahBab
05-03-2008, 01:04 AM
Thanks so much for the assistance!
I came up with a slight modification so the page only needed to be included/required once.
For this I used the rand function.
Also added a text file in an Iframe (to load individual text files like news1.txt, news2.txt) although may delete this later. :)
So the final code...
<?php
$id = rand(1, 149);
?>
<img src="pics/pic<?php echo $id; ?>.jpg">
<br>
<?php
// Depending on you OS try "\r\n" instead of "\n" if you have extra line breaks.
$blabber = explode("\n", file_get_contents("blabber.txt"));
$links = explode("\n", file_get_contents("links.txt"));
// May need to +/- the $id value here depending on your line number vs image number values.
?>
<?php echo $blabber[$id]; ?><br>
<iframe src="news/news<?php echo $id; ?>.txt" width="200px" height="400px" frameborder="2" scrolling="yes"></iframe>
<br>
<a href="http://<?php echo $links[$id]; ?>"><?php echo $links[$id]; ?></a>
<br>
Once again thanks for the help!