PDA

View Full Version : Random Table PHP


dougg
06-12-03, 06:45 AM
I am a complete novice to this stuff, so please pardon my ignorance.

I would like to have a table, 1 column , x number of rows (9 currently, will be as many as 90)

each row would display a random image
that image if clicked on would go to a web page that would bring up information about that picture, so the link needs to stay with the picture.
The pictures cannot repeat
below each picture I would like to have some formatted text
For example, the contents of a cell is currently...
<tr>
<td height="257"> <p><a href="natasha.htm" target="mainFrame"><img src="natasha.jpg" width="100" height="201" border="0"></a></p>
<p align="center"><strong>Natasha<br>
<font size="1">Detroit - Ann Arbor</font></strong></p></td>
</tr>

I was trying to use the code from the random link generator, but adding the text below each picture is beyond my capacity.
I hope someone who knows more than this can help. I am learning, but this is beyond me.

daynah
06-12-03, 02:26 PM
I have to ask you how the data is being stored? Maybe it's easier to store in a mysql database instead? That way you can have a field for image name and description.

Then you can select random queries like this:

SELECT * FROM table_name ORDER BY RAND();

and this if you just want one:

SELECT * FROM table_name ORDER BY RAND() limit 1;

or this if you want 20 (or however many you want)

SELECT * FROM table_name ORDER BY RAND() limit 20;


If you prefer to use a text file, you can store the data as:

picture1.jpg|picture1.htm|description 1 goes here|name1
picture2.jpg|picture2.htm|description 2 goes here|name2
picture3.jpg|picture3.htm|description 3 goes here|name3

(save as data.txt)

Then count the number of lines in that file. Run the rand() function, using the number of lines as the max. Then print the line with the randomly chosen number.

Code can be something like this:

$fd = fopen ("data.txt", "r");
while (!feof ($fd))
{
$buffer = fgets($fd, 4096);
$lines[] = $buffer;
}
fclose ($fd);

$counter = count($line); // number of lines

$random = rand(0, $counter);

$myline = explode('|', $line[$random]);

echo '<table>';

// Put this in a while loop if you want more than one row
echo '
<tr>
<td height="257"> <p>
<a href="'.$myline[1].'" target="mainFrame"><img
src="'.$myline[0].'" width="100" height="201" border="0"></a>
</p>
<p align="center">
<strong>'.$myline[3].'<br>
<font size="1">'.$myline[2].'</font>
</strong></p></td>
</tr>';

// end the loop

echo '</table>';



I hope this helps. :)

prettypretty
06-14-03, 12:29 PM
Thank you daynah :) I learn much from you today.

but that way you may get "repeated pictures"...;)

ChristGuy
06-14-03, 06:03 PM
For non repeating pictures have a look at this post: http://forums.hotscripts.com/showthread.php?s=&threadid=202

ridwank
06-16-03, 05:15 AM
Originally posted by prettypretty
Thank you daynah :) I learn much from you today.

but that way you may get "repeated pictures"...;)

Hi pretty˛,
I think daynah's script good enogh to solve your problem
Then here..
I modified her script with just a few more lines
and it won't show a repeated image

and just try it :

<?
$fd = fopen ("data.txt", "r");
while (!feof ($fd))
{
$buffer = fgets($fd, 4096);
$lines[] = $buffer;
}
fclose ($fd);

$counter = count($lines)-2; // number of lines


while ($hit<($counter)){

$random = rand(0, $counter);

$findrandom="$random|";
if (!strstr($rndprev,$findrandom)){
$rndprev.=$findrandom;
$hit++;
}


}


echo '<table border=1>';

$new=explode("|",$rndprev);
for ($i=0;$i<count($new)-1;$i++){
$j=$new[$i];
$myline = explode('|', $lines[$j]);


echo '
<tr>
<td height="257"> <p>
<a href="'.$myline[1].'" target="mainFrame"><img
src="'.$myline[0].'" width="100" height="201" border="0"></a>
</p>
<p align="center">
<strong>'.$myline[3].'<br>
<font size="1">'.$myline[2].'</font>
</strong></p></td>
</tr>';


}

echo '</table>';

?>

FOR file data.txt could be contains :

image1.jpg|picture1.htm|description 1 goes here|name1
image2.jpg|picture2.htm|description 2 goes here|name2
image3.jpg|picture3.htm|description 3 goes here|name3
image4.jpg|picture4.htm|description 4 goes here|name4
image5.jpg|picture5.htm|description 5 goes here|name5
image6.jpg|picture0.htm|description 6 goes here|name6



But I suggest you use a database to store data,
because in file TXT ,
one line breaking or space will be read as new record.
and it's more difficult on editing data if there's more than 100 line stored in that TXT file.

dougg
07-20-03, 01:41 PM
The code leaves out 1 of the entries.

For instance, if there are 12 entries, only 11 pictures show up. I have racked my brain and a lot of books trying to figure out why. Can someone help?