473,881 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I retrieve items from an array in random order?

1 New Member
Hi. Can you help me please?
Is there any way to show a unique random item from an array, every time you refresh the page? In other words how do I show the items from an array in random order, one by one, by reloading the page?
Moreover the page have to be refreshed by pressing the "submit" button...

Thats what I achieved so far :) but it doesn't display the UNIQUE array item every time the page is reloaded...
as you may guess :)
Expand|Select|Wrap|Line Numbers
  1.  <html>
  2. <head><title></title></head>
  3. <body>
  4. <?php
  5. $testArray = array ("one","two","three","four","five");
  6. $number = rand(0,4);
  7. echo "$testArray[$number]";
  8. ?>
  9. <form method="post" action="test.php">
  10. <input type="submit" value="Submit">
  11. </form>
  12. </body>
  13. </html>

I'm a designer/ActionScript sort of a guy and know nothing about php,
so please don't laugh. :)
Any help is appreciated

Philip
May 10 '10 #1
2 2341
Dormilich
8,658 Recognized Expert Moderator Expert
it works as expected (at least for me), although you have to bear in mind that one out of five doesn’t give you a good distribution. it scrambled a bit better when using a larger base:
Expand|Select|Wrap|Line Numbers
  1. $number = rand(0, 100) % 5;
May 10 '10 #2
Markus
6,050 Recognized Expert Expert
array_rand() could maybe... possibly... work for you? ;)
May 10 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1728
by: Jefferis NoSpamme | last post by:
I have a query that is showing new items in a store based upon their inventory date. The problem with my Random order sort is that when the "next" link is pressed, the random function is called again and I may see the image previously displayed rather than go through the entire new inventory. Any suggestions on how to prevent that occurrence? SELECT * FROM `Catalog`
0
2235
by: Tom | last post by:
Using AccessXP - Access2000 In my application, when an order is entered, inventory is automatically adjusted, a pull list is created, items are pulled to fill the order, and the order is packaged for shippment. The order is then held pending payment and finally shipped when payment is received. Sometimes prior to shipment the customer calls and wants to change one or more items, wants to change the quantity of one or more items or wants...
22
17950
by: Nhmiller | last post by:
Is there a way to do this? Thanks. Neil Cat Paintings At Carol Wilson Gallery http://www.carolwilsongallery.com
9
28851
by: gl | last post by:
How do I take an array or arraylist, and sort it randomly? Like suppose the items in it are (1,2,3,4,5) and I want to get it to be in a random order (2,3,1,4,5). How do you do that? I think it's a call to the array or array lists sort method, but i'm not exactly sure how you do it.
2
1375
by: Paul Evans | last post by:
Hi, I've created a datagrid which works fine accept the id field (the primary key field) comes out in a random order. How do I get the fields to display sequencially? Thanks for your time Paul Evans
6
5482
by: InnoCreate | last post by:
Hi everyone. I've recently written a classic asp website which uses an MS Access datasource. I know this is less than an ideal data source as it has limited functionality. I have a search form on my website which allows users to define parameters and return results accordingly. The problem i have is a need to return these results in a random order each time. With SQLServer i know NEWID() would do the trick - used this many times before...
9
3545
by: Greg Collins [Microsoft MVP] | last post by:
I've done a bit of research on this topic in the newsgroups and on MSDN, and though it sounds possible, I still don't understand how I would make it work. I want to be able to use named groups to capture from a path items that can be in any order. For example, the path might be: /root/item1/item2/item2/item3/item4 or it might be mixed up:
4
2286
by: kevincw01 | last post by:
Anyone have a clever way to retrieve for example, items 0-29 from an array of size N>29, in random order? The catch is that I dont want to print any items more than once and I dont want to miss any. This is for a tag cloud where I have sorted them by popularity but now I need to display only the top 30. I do this now but they are in order of popularity and its not really a 'cloud'. If I could retrieve the top 30 in some random order,...
9
5360
by: Tuxedo | last post by:
I'd like to reorganize the third, fourth, fifth and sixth, as well as any elements thereafter in an array in random order: var a = new Array('first','second','third','fourth','fifth','sixth','etc') In other words, the first, second and third element should remain in position 0, 1 and 2, while the fourth, fifth and sixth, etc. should appear in random order. Can anyone recommend a method to do this?
2
2591
by: federicog | last post by:
I have to create a listing in random order from a few mysql tables. I can't use ORDER BY RAND() for a few reasons: - The random order has to change only every 24hs, ie: today it will show rows number 4 2 3 1 and tomorrow 2 3 1 4, but it should be the same all day long. - I'm dividing the results in pages using LIMIT x, y. I suppose I would have to save the random order in a mysql table, but how can I order the results based on that...
0
9931
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10724
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10827
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9556
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5978
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4599
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.