Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #10935 > unrolled thread

Shuffle problem

Started byrichard <noreply@example.com>
First post2013-09-30 03:26 -0400
Last post2013-10-01 22:02 -0400
Articles 5 — 5 participants

Back to article view | Back to comp.lang.php


Contents

  Shuffle problem richard <noreply@example.com> - 2013-09-30 03:26 -0400
    Re: Shuffle problem Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-30 10:18 +0000
    Re: Shuffle problem Erwin Moller <erwinmollerusenet@xs4all.nl> - 2013-09-30 15:48 +0200
      Re: Shuffle problem Scott Johnson <noonehome@chalupasworld.com> - 2013-09-30 15:24 -0700
    Re: Shuffle problem Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2013-10-01 22:02 -0400

#10935 — Shuffle problem

Fromrichard <noreply@example.com>
Date2013-09-30 03:26 -0400
SubjectShuffle problem
Message-ID<ve2rt8qbc3oa.zxz9g5uc086p.dlg@40tude.net>
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?

[toc] | [next] | [standalone]


#10936

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-09-30 10:18 +0000
Message-ID<l2bj4s$68f$1@dont-email.me>
In reply to#10935
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@gmail.com

[toc] | [prev] | [next] | [standalone]


#10939

FromErwin Moller <erwinmollerusenet@xs4all.nl>
Date2013-09-30 15:48 +0200
Message-ID<52498127$0$15886$e4fe514c@news2.news.xs4all.nl>
In reply to#10935
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

[toc] | [prev] | [next] | [standalone]


#10947

FromScott Johnson <noonehome@chalupasworld.com>
Date2013-09-30 15:24 -0700
Message-ID<l2ctm9$gt9$1@dont-email.me>
In reply to#10939
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

[toc] | [prev] | [next] | [standalone]


#10966

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2013-10-01 22:02 -0400
Message-ID<U8L2u.30940$t77.21108@fx06.iad>
In reply to#10935
On Monday 30 September 2013 03:26, in comp.lang.php, "richard"
<noreply@example.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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web