Thanks for the reply!
This is what I have:
// Connect to the database
mysql_connect ('localhost', 'site_testdb', 'XXXXXX') ;
mysql_select_db ('site_testdbuser')or die(mysql_error());
echo "1";
$query = "SELECT url, popularity FROM linkstable";
$result = mysql_query($query);
while ($row = @mysql_fetch_array($result)) {
$array[$row['url']] = $row['popularity'];
}
function array_rand_weighted($values) {
$r = mt_rand(1, array_sum($values));
foreach ($values as $item => $weight) {
if ($r <= $weight) return $item;
$r -= $weight;
}
}
$num_links = 10;
$ct = 0;
$links = array();
while ($ct < $num_links) {
$tmp = array_rand_weighted($array);
if (in_array($tmp, $links)) {
continue;
} else {
$links[$ct++] = $tmp;
}
}
I had to put the @ in front of mysql_fetch.
I get the following error:
Fatal error: Call to undefined function: array_rand_weighted() in /query.php on line 19
So Basically that means I don't have that above function declared and I put it in.
So I then end up with this:
// Connect to the database
mysql_connect ('localhost', 'site_testdb', 'XXXXXX') ;
mysql_select_db ('site_testdbuser')or die(mysql_error());
echo "1";
$query = "SELECT url, popularity FROM linkstable";
$result = mysql_query($query);
while ($row = @mysql_fetch_array($result)) {
$array[$row['url']] = $row['popularity'];
}
function array_rand_weighted($values) {
$r = mt_rand(1, array_sum($values));
foreach ($values as $item => $weight) {
if ($r <= $weight) return $item;
$r -= $weight;
}
}
$num_links = 10;
$ct = 0;
$links = array();
while ($ct < $num_links) {
$tmp = array_rand_weighted($array);
if (in_array($tmp, $links)) {
continue;
} else {
$links[$ct++] = $tmp;
}
}
And then I get this:
Warning: array_sum(): The argument should be an array in /query.php on line 16
Warning: Invalid argument supplied for foreach() in /query.php on line 17
So you may be smacking your face because I am a noob. Any Idea why it is doing that? I tried to edit the code around but still come up with the error.
My Goal is to have 1 link be spit out as well not a bunch of links.
The goal is to have a redirection to the url in the table so I would take a redirection snippit and input the $randomization_result into that and then have it open up a new page with that.
Thank you for your time 🙂 Sorry for being such a newbie.