hey guys,
do you reckon you could tell me how to make a page automatically load a random page from an entry in it's database? (probably a cgi one, i've had enough of mysql i think lol)
thanks a lot in advance,
(something similar to www.randomwebsite.com)
ben,
Hi.
Well, if you want to load a random page from a database you need a database. Then there seems that you do not really know thee meaning of cgi? That is just a gateway to run scripts on the server in your language of choice.
The easiest solution is to use php/mysql. However, if you want to skip the database you would either need a texfile containing the urls or code them right into the code.
If there is just a few urls you could solve the problem using simple javascript.
/hd
hmm, no, it could be up to 100,000 links in the database
can you give me an example of the php script i could use? a random function?
also, is there a way to make certain links come up more than once?
or do i just need to add them numerous times?
thanks,
ben,
Hi. mate,
Do you really want to do it? I don't think it is a good idea.
Anyway. you can do it.
First, you can store a lots of webpages in database, certainly, you can know how to get them when you need them. you can download Mambo to see the source codes.
Secodly, once you need a random page to be display, just using php code to generate a random number, then get the relative page from database.
But, there are lots of work to do to realize it, I think.
Hi.
Simple php for selecting a random row (from php.net)
| Quote: |
$tot=mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM `sometable` WHERE (condition)"));
$row=rand(0,$tot);
$res=mysql_fetch_row(mysql_query("SELECT * FROM `sometable` WHERE (condition) LIMIT $row,1")); |
This does not allow to select some rows more often then others. You could either add them numerous times or use two tables where one contains the urls and one that has the url row-ids as a foreign key.
eg.
| Code: |
Table A
---------
1
1
1
2
3
Table B
---------
1 http://www.aik.se
2 http://www.svenskafans.com/fotboll/aik
3 http://www.blackarmy.se |
It is maybe not the best solution, but it works.
Good luck,
hd
sweet,
thanks so much for your help guys
another quick question tho, i want to make the page acutally load in the frame, will this code do this? or will it just show the link?
thanks again
ben,
No.
The code only selects the row. If the url is in the first column of the table the url is found in $res[0]. So you will need this code to load it.
| Code: |
| header("Location: " . $res[0]); |
/hd
learn header() function properly cause it can screw up on you sometimes if you dont know how to use it...
learn header() function properly cause it can screw up on you sometimes if you dont know how to use it...
erm, learn it?
lol
oh yeah and on that
$tot=mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM `sometable` WHERE (condition)"));
$row=rand(0,$tot);
$res=mysql_fetch_row(mysql_query("SELECT * FROM `sometable` WHERE (condition) LIMIT $row,1"));
thing, what do i replace the conditions and all that with? sorry but im still quite a n00b at mysql lol, but im learning
also you know the table A and table B thing? i'm trying to create the tables but im not sure exactly what fields to fill in with what lol
ben,
| Code: |
<script type="text/javascript">
<!--
var pages = new Array('url1','url2',...,'urlN');
function loadRandomPage(){
var newPage = pages[Math.floor(Math.random()*pages.length)]
window.location=newPage;
}
//-->
</script>
<input type="button" onclick="loadRandomPage()" value="Change URL"> |
i found this on the internet
seems it'd be a lot easier
how would you rate using a js version?
ben,
Since 100000 links should be used, I don't think js is a great solution.
You could scip the condition part of the sql-queries since you want to select a random row from all entries.
| Code: |
$tot=mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM `tableA`"));
$row=rand(0,$tot);
$res=mysql_fetch_row(mysql_query("SELECT id FROM `tableA` LIMIT $row,1"));
|
Replace 'tableA' with the name of table A. Now $row[0] will contain the id of the row in table B. Use this code to select the url.
| Code: |
$res=mysql_fetch_row(mysql_query("SELECT url FROM `tableB` WHERE id = $row[0]);
|
Now you could use the headerfunction to forward the user to the url.
Here are som code to create the tables:
| Code: |
create table tableA (id int);
create table tableB (id int, url varchar(255)); |
Good luck!
/hd
hey guys, thanks a lot for your help you rock
anyways, so i've been messing around with dreamweaver trying to get it to work
i get an error though, i think i did it right but im not sure, maybe you could have a look?
my site is here http://www.internetcake.com
and here is the code i used for the random frame:
| Code: |
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><?php require_once('Connections/geturls.php'); ?>
<?php
mysql_select_db($database_geturls, $geturls);
$query_Recordset1 = "$tot=mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM `tableA`")); $row=rand(0,$tot); $res=mysql_fetch_row(mysql_query("SELECT id FROM `tableA` LIMIT $row,1"));";
$Recordset1 = mysql_query($query_Recordset1, $geturls) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
mysql_select_db($database_geturls, $geturls);
$query_Recordset2 = "$res=mysql_fetch_row(mysql_query("SELECT url FROM `tableB` WHERE id = $row[0]); ";
$Recordset2 = mysql_query($query_Recordset2, $geturls) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
$tot=mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM `tableA`"));
$row=rand(0,$tot);
$res=mysql_fetch_row(mysql_query("SELECT id FROM `tableA` LIMIT $row,1"));
?>
<?php header("Location: " . $res[0]);?>
</head>
<body>
</body>
</html>
<?php
mysql_free_result($Recordset1);
mysql_free_result($Recordset2);
?>
|
lol, yeah, so, theres prob some stuff in here that is really stupid but i tried my best
ben,
I'll take a look at your code in the next few if i find some time, there are lots of errors in there.
/hd