View Full Version : Random Link
kochier
08-01-2008, 08:12 PM
<?php
include 'opendb.php';
mysql_select_db('comic') or die(mysql_error());
$result = mysql_query("SELECT Number FROM comics") or die(mysql_error());
while($row = mysql_fetch_array($result)){
$Number = $row['Number'];
$Number2 = mysql_num_rows($result);
$url = 'absurd' .$Number .'.php';
$urls = array($url);
srand(time());
$random = (rand()%$Number2);
if ($urls[$random] != null)
{
echo $urls[$random];
break;
}
}
include 'closedb.php';
?>
Basically I want it to display a random link, and right now half the time it displays a blank link, instead of absurd1.php, there are currently two variables, 1 and 2, it doesn't display 2 at all.
oesxyl
08-01-2008, 08:29 PM
use mysql_fetch_assoc instead of mysql_fetch_array
<?php
include 'opendb.php';
mysql_select_db('comic') or die(mysql_error());
$result = mysql_query("SELECT Number FROM comics") or die(mysql_error());
$urls = array();
if($result){
while($row = mysql_fetch_assoc($result)){
$urls[] = 'absurd'.$row['Number'].'.php';
}
}else{
echo "oops, a boo boo";
}
if(!empty($urls)){
$randurl = random(0,count($urls));
echo $urls[$randurl];
}else{
echo "oops, another boo boo";
}
include 'closedb.php';
?>
regards
kochier
08-01-2008, 08:39 PM
Well I'm getting 1 and 2 to appear correctly, but every now and then I still get an empty link, nothing appears. *Well gone for the weekend, will work on it when I get back* Thanks for always being so helpful oesxyl, you are easily one of the most helpful people on this forum.
Oh and I needed to change it from random to rand for it to work.
oesxyl
08-01-2008, 09:03 PM
Well I'm getting 1 and 2 to appear correctly, but every now and then I still get an empty link, nothing appears. *Well gone for the weekend, will work on it when I get back* Thanks for always being so helpful oesxyl, you are easily one of the most helpful people on this forum.
what is 1 and 2? what mean empty link?
maybe you know but just in case, this code only output the page name.
to be a link it must be something like:
echo '<a href="'.$urls[$randurl].'">'.$urls[$randurl].'</a>';
and the page must exist.
regards
kochier
08-01-2008, 10:31 PM
Sorry if I was unclear. It will display absurd1.php and absurd2.php but it sometimes just displays nothing.
oesxyl
08-01-2008, 10:46 PM
Sorry if I was unclear. It will display absurd1.php and absurd2.php but it sometimes just displays nothing.
no problem, :)
run this:
<?php
include 'opendb.php';
mysql_select_db('comic') or die(mysql_error());
$result = mysql_query("SELECT Number FROM comics") or die(mysql_error());
if($result){
print 'Total number of rows: '.mysql_num_rows($result).'<br/>';
$i = 1;
while($row = mysql_fetch_assoc($result)){
print $i.' number is "'.$row['Number'].'"<br/>';
$i++;
}
}else{
echo "oops, a boo boo";
}
include 'closedb.php';
?>
regards
kochier
08-02-2008, 01:48 AM
Hmm, I don't think we're on the same page right now, I want it to randomly display either absurd1.php or absurd2.php. The first code you gave me was a lot closer to what I need, however every now and then it wouldn't display anything at all, not even the absurd or .php part. I'm not sure why it would be this, it seems every now and then $randurl is blank, causing it not to display any of the echo statement. I've tried an if statement to make sure $randurl isn't empty, but that didn't change anything, except make absurd1.php no longer appear and only absurd2.php, and the occasional blank page.
<?php
include 'opendb.php';
mysql_select_db('comic') or die(mysql_error());
$result = mysql_query("SELECT Number FROM comics") or die(mysql_error());
$urls = array();
if($result){
while($row = mysql_fetch_assoc($result)){
$urls[] = 'absurd'.$row['Number'].'.php';
}
}else{
echo "oops, a boo boo";
}
if(!empty($urls)){
$randurl = rand(0,count($urls));
if(!empty($randurl)){
echo $urls[$randurl];
}
}
else{
echo "oops, another boo boo";
}
include 'closedb.php';
?>
oesxyl
08-02-2008, 02:03 AM
Hmm, I don't think we're on the same page right now, I want it to randomly display either absurd1.php or absurd2.php.
The first code you gave me was a lot closer to what I need, however every now and then it wouldn't display anything at all, not even the absurd or .php part. I'm not sure why it would be this, it seems every now and then $randurl is blank, causing it not to display any of the echo statement. I've tried an if statement to make sure $randurl isn't empty, but that didn't change anything, except make absurd1.php no longer appear and only absurd2.php, and the occasional blank page.
the code from my last post must display a list of all numbers you have in mysql table comics if the query is correct. If you don't have data in mysql or if are incorrect first script have no chance to work.
You must fix that before anything else.
regards
kochier
08-04-2008, 08:48 PM
Well I ran the code from your last post and it worked fine, said there were 2 rows, absurd1.php and absurd2. php. I'm back from my little vacation, this is what I came back with <?php
include 'opendb.php';
mysql_select_db('comic') or die(mysql_error());
$result = mysql_query("SELECT Number FROM comics") or die(mysql_error());
$urls = array();
while($row = mysql_fetch_assoc($result)){
$urls[] = 'absurd'.$row['Number'].'.php';
}
$randurl = rand(0,count($urls));
$urlsr = $urls[$randurl];
if(!empty($urlsr)){
echo $urlsr;
}else{
echo 'something went wrong...';
}
include 'closedb.php';
?>
It will echo, absurd1.php, abusrd2.php and "something went wrong..." so for some reason it must be empty every now and then. I added echo statements for the variables for something went wrong, and it will echo Array for $urls and 2 for $randurl. Upon further testing, when randurl is 0, absurd1 displays, when it's 1, absurd2 displays, so of course when it's 2 there's nothing for it to display.
kochier
08-04-2008, 09:25 PM
<?php
include 'opendb.php';
mysql_select_db('comic') or die(mysql_error());
$result = mysql_query("SELECT Number FROM comics") or die(mysql_error());
$urls = array();
while($row = mysql_fetch_assoc($result)){
$urls[] = 'absurd'.$row['Number'].'.php';
}
$randurl = rand(0,count($urls) -1);
echo $randurl;
$urlsr = $urls[$randurl];
if(!empty($urlsr)){
echo $urlsr;
}else{
echo 'something went wrong...';
}
include 'closedb.php';
?>
Seems to work, I don't get the error message after many attempts.
oesxyl
08-04-2008, 09:55 PM
It will echo, absurd1.php, abusrd2.php and "something went wrong..." so for some reason it must be empty every now and then. I added echo statements for the variables for something went wrong, and it will echo Array for $urls and 2 for $randurl. Upon further testing, when randurl is 0, absurd1 displays, when it's 1, absurd2 displays, so of course when it's 2 there's nothing for it to display.
seems normal.
<?php
include 'opendb.php';
mysql_select_db('comic') or die(mysql_error());
$result = mysql_query("SELECT Number FROM comics") or die(mysql_error());
$urls = array();
while($row = mysql_fetch_assoc($result)){
$urls[] = 'absurd'.$row['Number'].'.php';
}
$randurl = rand(0,count($urls) -1);
echo $randurl;
$urlsr = $urls[$randurl];
if(!empty($urlsr)){
echo $urlsr;
}else{
echo 'something went wrong...';
}
include 'closedb.php';
?>
Seems to work, I don't get the error message after many attempts.
if you have only 2 records in mysql, is ok else must be a problem with your data. For example if Number is char/varchar field can be '' or NULL and in this case the script will fail.
regards
kochier
08-06-2008, 05:04 AM
Alright now I was trying to make sure the random link doesn't link to the same page, so I decided to remove it from the list, however I get the error "Warning: array_filter() [function.array-filter]: The second argument, '<a href="absurd2.php">Random</a>', should be a valid callback in " My code now looks like <?php
include 'opendb.php';
$path_parts = pathinfo(($_SERVER["SCRIPT_FILENAME"]));
$test = $path_parts['basename'];
$result = mysql_query("SELECT Name FROM map WHERE Pathname = '$test'") or die(mysql_error());
$name = $row['Name'];
mysql_select_db('comic') or die(mysql_error());
$result = mysql_query("SELECT Number FROM comics WHERE Title = '$test'") or die(mysql_error());
$number = $row['Number'];
$number2 = '<a href="absurd'.$number.'.php'.'">Random</a>';
$result = mysql_query("SELECT Number FROM comics") or die(mysql_error());
$urls = array();
while($row = mysql_fetch_assoc($result)){
$urls[] = '<a href="absurd'.$row['Number'].'.php'.'">Random</a>';
}
$randurl = rand(0,count($urls) -1);
$urlsr = $urls[$randurl];
if ($number2 == $urlsr)
{
$urls = array_filter($urls, "$number2");
echo ' Random';
}
else
{
if(!empty($urlsr)){
echo $urlsr;
}else{
echo ' Random';
}
}
?>
oesxyl
08-06-2008, 06:30 AM
Alright now I was trying to make sure the random link doesn't link to the same page, so I decided to remove it from the list,
you have 2 links, remove 1, remain 1. you still want to display this single link 'random'?
you must explain what you want to achive because now is not clear at all. :)
however I get the error "Warning: array_filter() [function.array-filter]: The second argument, '<a href="absurd2.php">Random</a>', should be a valid callback in "
second argument of array_filter must be a function which apply to each item of the array. It must have as argument the value of item and must return true or false. For example
function p2is16($v){
if($v*$v == 16){
return true;
}
return false;
}
$a = array(1, 3, 4, 12);
print_r(array_filter($a,"p2is16"));
<?php
include 'opendb.php';
$path_parts = pathinfo(($_SERVER["SCRIPT_FILENAME"]));
$test = $path_parts['basename'];
$result = mysql_query("SELECT Name FROM map WHERE Pathname = '$test'") or die(mysql_error());
// where did you get this $row['Name']?
$name = $row['Name'];
// why do you select the database here?
mysql_select_db('comic') or die(mysql_error());
$result = mysql_query("SELECT Number FROM comics WHERE Title = '$test'") or die(mysql_error());
// where did you get $row['Number']?
$number = $row['Number'];
$number2 = '<a href="absurd'.$number.'.php'.'">Random</a>';
$result = mysql_query("SELECT Number FROM comics") or die(mysql_error());
?>
a way to generate a random url which is not $number2 and is not empty:
do{
$randurl = rand(0,count($urls) -1);
$urlsr = $urls[$randurl];
}while($number2 !== $urlsr && !empty($urlsr));
echo $urlsr;
regards
kochier
08-06-2008, 07:08 AM
Ok well what I want it to do is display a link named 'random' but if I'm in absurd1.php I don't want the random link to link to absurd1.php. As for all of that selecting the database it is to build my current location, $number would equal my current location. As for the do while loop you offered me, it seems that I get absurd1.php a lot less when I'm in absurd1.php, however it still turns up every now and then.
EDIT:
<?php
include 'opendb.php';
$path_parts = pathinfo(($_SERVER["SCRIPT_FILENAME"]));
$test = $path_parts['basename'];
$result = mysql_query("SELECT Name FROM map WHERE Pathname = '$test'") or die(mysql_error());
$name = $row['Name'];
mysql_select_db('comic') or die(mysql_error());
$result = mysql_query("SELECT Number FROM comics WHERE Title = '$test'") or die(mysql_error());
$number = $row['Number'];
$number2 = '<a href="absurd'.$number.'.php'.'">Random</a>';
$result = mysql_query("SELECT Number FROM comics") or die(mysql_error());
$urls = array();
while($row = mysql_fetch_assoc($result)){
$urls[] = '<a href="absurd'.$row['Number'].'.php'.'">Random</a>';
}
do{
$randurl = rand(0,count($urls) -1);
$urlsr = $urls[$randurl];
}while($number2 == $urlsr && !empty($urlsr));
$urlsr = $urls[$randurl];
if(!empty($urlsr)){
echo $urlsr;
}else{
echo ' Randomer?';
}
?>
Seems to work now, haven't had it link me to the page I'm on once now. Thanks for your help once again.
oesxyl
08-06-2008, 07:33 AM
Seems to work now, haven't had it link me to the page I'm on once now. Thanks for your help once again.
no, it don't work.
learn to debug. Take small parts of the script and make it work. Don't go further if don't work.
first, let's see if the $test is what you expect:
<?php
include 'opendb.php';
$path_parts = pathinfo(($_SERVER["SCRIPT_FILENAME"]));
$test = $path_parts['basename'];
echo $test;
if $test is what you want, we go to next step and see if you retrive from database what you expect. So add to the previous part this one:
$result = mysql_query("SELECT Name FROM map WHERE Pathname = '$test'") or die(mysql_error());
$name = mysql_result($result,0,'Name');
echo $name;
if untill here everything is ok, go further. Let's see if the second query work. Add this part to what you have untill this step:
$result = mysql_query("SELECT Number FROM comics WHERE Title = '$test'") or die(mysql_error());
$number = mysql_result($result,0,'Number');
echo $number;
now we can find what value have $number2 and build the url array from database:
$number2 = '<a href="absurd'.$number.'.php'.'">Random</a>';
echo $number2;
$result = mysql_query("SELECT Number FROM comics") or die(mysql_error());
$urls = array();
while($row = mysql_fetch_assoc($result)){
$urls[] = '<a href="absurd'.$row['Number'].'.php'.'">Random</a>';
}
print_r($urls);
and now final step generate random link:
do{
$randurl = rand(0,count($urls) -1);
$urlsr = $urls[$randurl];
}while($number2 == $urlsr && !empty($urlsr));
$urlsr = $urls[$randurl];
if(!empty($urlsr)){
echo $urlsr;
}else{
echo ' Randomer?';
}
?>
regards
kochier
08-06-2008, 08:41 AM
I actually did do all of that earlier, I made sure $test, $name and $number2 where what I needed before coming back here, I did do a lot of testing to make sure it was working right, and put in various echo statements to make sure the values were all correct, however I simply didn't tell you because I thought it didn't need telling. I did some more testing and it's working perfectly now. Thanks for the debugging warning though, I know sometimes I gloss over that step.
kochier
08-06-2008, 09:05 AM
oops double post.
vBulletin® v3.8.2, Copyright ©2000-2009, Jelsoft Enterprises Ltd.