Jump to content

Simple problem (i think)


timmah1

Recommended Posts

I have a database with roughly 1800 stock symbols.

Everyday I need to show only 50, but I need to have them be a different 50 everyday.

 

So what I need to do is show a random 50 stock symbols on different days.

I cannot just use random() because that would change everytime you went to that page.

I need the same 50 to stay for the entire day.

 

How would I go about doing this?

Link to comment
https://forums.phpfreaks.com/topic/127538-simple-problem-i-think/
Share on other sites


$n = 1800;
$q = "SELECT stocks_used FROM table";
$r  = mysql_query($q);
$row = mysql_fetch_row($r);

$arr = explode("-", $row[0]);

for ($i = 0; $i < $n; $i++):

$rand = rand(0,1800);

if (!in_array($rand, $arr)):

$arr[] = $rand;

Endif;

//Display stocks here

Endfor;

$new = implode("-", $arr);

mysql_query("UPDATE table SET stocks_used = '$new' LIMIT 1");

/// stocks_used would be like 142-432-653-etc...

 

Duno if you get the idea but I think that would work.

 

I haven't tried this, but I think it should work. It's supposed to display $jumps stock symbols every day, each day showing the next group.

 

<?php

$jumps = 50; //How many stocks per day, can't be more than the number of stocks you have in your DB

//First check how many stocks symbols you have
$result = mysql_query("SELECT COUNT(*) FROM stocks_symbols");
list($num_symbols) = mysql_fetch_assoc($result);

$start = (date('z') * $jumps) % $num_symbols;
$end = $start + $jumps;

$query = "(SELECT * FROM stocks_symbols ORDER BY id LIMIT {$start}, 50)";


if($end > $num_symbols-1)
{
$extra = $end - $num_symbols + 1;
$query .= " UNION (SELECT * FROM stocks_symbols ORDER BY id LIMIT 0, {$extra})";
}

$result = mysql_query($query);
//All the data for today is inside $result, just loop through it

?>

 

 

Orio.

<?php

//First check how many stocks symbols you have
$result = mysql_query("SELECT COUNT(*) FROM stocks_symbols");
list($num_symbols) = mysql_fetch_assoc($result);

?>

 

Have you changed the table's name from stocks_symbols into whatever it should be? If you did, try changing these two lines into:

 

<?php

//First check how many stocks symbols you have
$result = mysql_query("SELECT COUNT(*) FROM stocks_symbols") or die(mysql_error());
list($num_symbols) = mysql_fetch_assoc($result) or die(mysql_error());

?>

 

Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.