FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Shuffle problem
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Shuffle problem [message #172196] Mon, 30 September 2013 07:26 Go to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
On my site I want to create a play list that is randomly selected when the
page is loaded.
I have ten tables, each with 100 rows.
I want to extract the first 20 rows from each table.
Then shuffle that and create the list.
Somehting similar to shuffling a deck of cards.

So that my playlist might look like:

1965 #4
1963 #13
1969 #18
1961 #10
1960 #3

What ways can I do this?
Re: Shuffle problem [message #172199 is a reply to message #172196] Mon, 30 September 2013 10:18 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Mon, 30 Sep 2013 03:26:58 -0400, richard wrote:

> ... tables ... rows

This sounds like a database thing. Find an appropriate database group.

> Then shuffle that and create the list.

Have you looked at array functions?

http://www.php.net/manual/en/function.shuffle.php

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: Shuffle problem [message #182973 is a reply to message #172196] Mon, 30 September 2013 13:48 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 9/30/2013 9:26 AM, richard wrote:
> On my site I want to create a play list that is randomly selected when the
> page is loaded.
> I have ten tables, each with 100 rows.

[sidestepping]
That strikes me as pretty strange database design.
Why do you have 10 tables if they contain the same things?
In case each table represents a year, you might want to add a
year-column to the table, and fill that with the right value.
That way you'll have one clear table.
[end sidestepping]


> I want to extract the first 20 rows from each table.

Ask yourself: What are the FIRST 10 rows?
What "first 10" means depends on your ORDER BY clause.

In general: getting the first 10 is done in a database specific manner.
For example SQL Server:
SELECT TOP 10 FROM sometablename;

In Postgres you'll use: LIMIT and OFFSET

etc.

Look it up for your database.

> Then shuffle that and create the list.
> Somehting similar to shuffling a deck of cards.

Solution: Do 10 selects, each returning 10 rows.
Store them in an array.
OR
Create one query using UNION to glue them together.

Then shuffle it in some way, for example with the shuffle function.

Good luck,
Erwin Moller

>
> So that my playlist might look like:
>
> 1965 #4
> 1963 #13
> 1969 #18
> 1961 #10
> 1960 #3
>
> What ways can I do this?
>


--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
Solved my shuffle problem! [message #182978 is a reply to message #172196] Mon, 30 September 2013 18:52 Go to previous messageGo to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
http://mroldies.net/play200.php

Decided to use an array on the page rather than using the tables.
Each time you refresh the page, you get a different list.
Re: Shuffle problem [message #182980 is a reply to message #182973] Mon, 30 September 2013 22:24 Go to previous messageGo to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
On 9/30/2013 6:48 AM, Erwin Moller wrote:
> On 9/30/2013 9:26 AM, richard wrote:
>> On my site I want to create a play list that is randomly selected when
>> the
>> page is loaded.
>> I have ten tables, each with 100 rows.
>
> [sidestepping]
> That strikes me as pretty strange database design.
> Why do you have 10 tables if they contain the same things?
> In case each table represents a year, you might want to add a
> year-column to the table, and fill that with the right value.
> That way you'll have one clear table.
> [end sidestepping]
>

Oh Erwin.

Welcome to Richards world of RDB design.

I am no expert but we have all asked the same question and the answers
will make your eyes bleed.

Scotty
Re: Shuffle problem [message #182999 is a reply to message #172196] Wed, 02 October 2013 02:02 Go to previous message
Lew Pitcher is currently offline  Lew Pitcher
Messages: 60
Registered: April 2013
Karma: 0
Member
On Monday 30 September 2013 03:26, in comp.lang.php, "richard"
<noreply(at)example(dot)com> wrote:

> On my site I want to create a play list that is randomly selected when the
> page is loaded.
> I have ten tables, each with 100 rows.
> I want to extract the first 20 rows from each table.
> Then shuffle that and create the list.
> Somehting similar to shuffling a deck of cards.
>
> So that my playlist might look like:
>
> 1965 #4
> 1963 #13
> 1969 #18
> 1961 #10
> 1960 #3
>
> What ways can I do this?

$numbers = range(1, 200);
shuffle($numbers);
foreach ($numbers as $number)
{
$seq = $number % 20;
$year = 1950 + floor($number / 20);
printf("%4u #%d<br/>",$year,$seq);
}

--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Help with Security
Next Topic: configuration problem rsyslog with php
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Jun 05 04:13:35 GMT 2024

Total time taken to generate the page: 0.02888 seconds