PHP / XML Returning results in random order

jjones

Lifer
Oct 9, 2001
15,424
2
0
Well this was originally about MySQL but in fact what I'm looking to do is display results from an XML file, using Simple XML, in random order.
 

jjones

Lifer
Oct 9, 2001
15,424
2
0
Thanks, actually I found that but it's not going to help. What I'm really doing is trying to get the results from an XML file and iterate through them in random order. I'd never had a need to do anything random with MySQL and I thought that might give me some insight in how to proceed with this task. Unfortunately, the MySQL solution is too easy and won't apply at all.

To make it even more difficult, I'll need to not only display results from the XML file in random order, but also only a third of those results at a time, while making sure I don't repeat any results from the first set, when I display the second and third parts.
 

QED

Diamond Member
Dec 16, 2005
3,428
3
0
Load the results in an indexed array, with say N items.

Create a new array of N integers where the initial value of each array is its index. Use the card-shuffling algorithm on this array to order it randomly.

Use this new array's values as the index for the array of items from the XML file.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Create a list with all things you can choose from.
Create another empty list.
Select items from the first list at random.
Once you've selected an item in the first list, delete it from that list, and add it to the other list.
Keep selecting items until you have as many as you want to display in the second list.
Display the second list.
 

jjones

Lifer
Oct 9, 2001
15,424
2
0
Well, I was thinking to create the array then use shuffle followed by slice to get the three sections.

Oh, and to add to the fun, it's a multidimensional array. I'm plugging away at it though and making some headway.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: jjones
Oh, and to add to the fun, it's a multidimensional array.

That doesn't make any difference. Neither does the fact that your data was originally XML. All your doing is shuffling an array and displaying 1/3 of it.
 

jjones

Lifer
Oct 9, 2001
15,424
2
0
Well, I got what I wanted very nicely. Ended up using shuffle and array_chunk to randomize the results and divide them appropriately. And now for something completely different...

Not really.

So, to fully explain what I'm doing, say I have a list of 20 names and I have them in an array in a nicely randomized order, ready for display on a page. I want to show only 5 names at any one time, so the first page would display names 1 - 5 of 20 total names. The second page would show names 6 - 10, and so on through page 4.

I'm using shuffle to randomize the names, and then using array_chunk to break them up into 4 arrays of 5 names each. Displaying the first 5 names on the first page is easy enough, but what I'm trying to decide is how best to tackle displaying the second, third and fourth arrays on the subsequent pages. Basically, I'm thinking I need to store the arrays temporarily so I can use them on the additional pages. I'm trying to avoid using the database or any temp files and I'm thinking that I can do it with session variables.

Any comments or suggestions?
 

QED

Diamond Member
Dec 16, 2005
3,428
3
0
Yes, session variable.

Although you don't need 4 arrays... all you need is your original array, with items numbered 1 through N.

You can pass this array as an ordered list if need be.

For page number n, display array items n*5 - 4 through n*5.

The beauty of this is it is easily adaptable.... if instead you want to display 20 results per page, just display items n*20 - 19 through n*20. In general, with m results per page you would display items (n-1)*m + 1 through n*m. You can even set these variables on the URL line (like Google and Ebay do).

Just make sure that you are NOT re-reading the XML file and re-ordering the array in a random fashion between pages.
 

jjones

Lifer
Oct 9, 2001
15,424
2
0
Yeah, you're right, I don't need to store 4 separate arrays. And from the looks of it, I probably shouldn't use array_chunk. At first I was thinking I needed to get separate arrays for the different pages, because initially I wasn't sure how I was going to handle the data, but as you point out I can just iterate through relevent parts of the original array as needed for each page. And yeah, I had already thought to be sure not to re-order the array; that would not be desired at all. :)

I'm trying to avoid passing variables in the URL but I might be stuck with that for this operation; we'll see how it works out.