Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.