Someone may help me? It's urgent!

Started on the old forum in May 2010 · view the original in the archive · old links redirect here

12 replies · 823 views · started 16 years ago · 1 min read

TL;DR · summary of 12 replies
  • wapmetal provided a solution using mysql_query and ORDER BY RAND() DESC LIMIT 0,4 to fetch four random rows.
  • Mysterio wanted to print the values in a specific order from these fetched rows.
  • Mysterio's goal was to have the output like: $all[2]; $all[0]; $all[3]; $all[1]; but with random selection of columns and their values.

Written automatically from the replies by a language model on our own server. It can be wrong; the posts below are the source.

6 participants
Mysterio Lead · 630 posts #1

Hello guys, can you please give me some minutes?

I have stuck somewhere, here is the problem:

$all = mysql_fetch_array(mysql_query("SELECT a, b, c, d FROM them WHERE nr='1' "));

From this, I want to print the four values in random order, like:

print '$all[2]'; print '$all[0]'; print '$all[3]'; print '$all[1]';

But whats the way to go there?

Thanks to all!

mysterio.al - programming is a functional art

thanatos Senior · 261 posts #2

i dnt get da point of doing this

Mysterio Lead · 630 posts #5

kevk3v wrote: What is the output you get with this?

The goal is to get four values from four columns of the same row. But How can I print them in a random order? For example, everytime the page is refreshed, the order has to change.

You get what I mean?

mysterio.al - programming is a functional art

wapmetal Lead · 609 posts #6

$sql = mysql_query("SELECT DISTINCT a, b, c, d FROM them WHERE nr='1' ORDER BY RAND() DESC LIMIT 0,4"); while ($items = mysql_fetch_array($sql)) {

echo "$items[0]";

}

RAND() funtion is function where you can get random datas from ur database...now mate u can have that idea ..i use While loop

or

$sql = mysql_query("SELECT DISTINCT id, column, etc FROM them WHERE nr='1' ORDER BY RAND() DESC LIMIT 0,4"); while ($items = mysql_fetch_array($sql)) {

echo "$items[0] ";

}

it will show 4 lines datas

hope it helps

centi Developer · 159 posts #7

is this for a game?

😳 .................................... http://photomag.lk/ .................................... 🤨

Mysterio Lead · 630 posts #8

Ok Wapmetal, lets say we have:

$a = 1; $b = 2; $c = 3; $d = 4;

Now I want to print those four variables in a random order by using less code as possible. You got me?

mysterio.al - programming is a functional art

something else Fellow · 2,296 posts #9

There would be 24 different ways if you hard coded this (im guessing its for a multiple choice question thing)

I would Use Something on the lines of this:

$all = mysql_fetch_array(mysql_query("SELECT a, b, c, d FROM them WHERE nr='1' "));
$rand = array_rand($all, 4);
print $all[$rand[0]]."<br/>";
print $all[$rand[1]]."<br/>";
print $all[$rand[2]]."<br/>";
print $all[$rand[3]]."<br/>";
Mysterio Lead · 630 posts #10

Yes, its a multiple answer thing.

In your code, there are two problem. The variables are ordered from lowest to highest, and sometimes the same variable with be twice in the list.

My mind is bleeding, I try to find a trick but I can't. Any idea?

mysterio.al - programming is a functional art

Mysterio Lead · 630 posts #12

wapmetal wrote: you mean in every column lets says theres a certain word that u want to be random?

can u give me your sql so we can help br0...thanks!

Yes, that is!

And there is no SQL to show, only a table with five columns (nr, a, b, c,d).

mysterio.al - programming is a functional art

something else Fellow · 2,296 posts #13

So more Like This ?

$all = mysql_fetch_array(mysql_query("SELECT a, b, c, d FROM them WHERE nr='1' ")); 
$randa = (0,3);
$randb = (0,3);
$randc = (0,3);
$randd = (0,3);
print $all[$randa]."<br/>"; 
print $all[$randb]."<br/>"; 
print $all[$randc]."<br/>"; 
print $all[$randd]."<br/>";

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close