GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Jan-2007, 21:59
nirvana4lf nirvana4lf is offline
New Member
 
Join Date: Oct 2004
Posts: 8
nirvana4lf is on a distinguished road

Using the For loop with an array and rand();


I'm making a test for myself (and for others to use) on PHP. I made an array called $que. I'm going to add questions to the array, but for now i just put some words in each to test it out to see if it works with a for loop to generate random questions. Here is my code:

PHP Code:

<?php

$que[0] = "this is the 0<br /><br /><br />";

$que[1] = "this is the 1<br /><br /><br />";

$que[2] = "this is the 2<br /><br /><br />";

$que[3] = "this is the 3<br /><br /><br />";

$que[4] = "this is the 4<br /><br /><br />";



for ($i = 1; $i <5; $i++) {
    $x = rand(0,4);
    echo $que[$x];
    }

?>


It works, except that $x sometimes repeats the same number due to the fact that its limited to a very small range of numbers. Is there a way to ensure that $x does not repeat within the for loop?
  #2  
Old 10-Jan-2007, 22:46
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 750
admin will become famous soon enough

Re: Using the For loop with an array and rand();


Until someone else comes up with a better solution, maybe you can try something like this, using array_rand():

PHP Code:

<?php

$que[0] = "this is the 0<br /><br /><br />";

$que[1] = "this is the 1<br /><br /><br />";

$que[2] = "this is the 2<br /><br /><br />";

$que[3] = "this is the 3<br /><br /><br />";

$que[4] = "this is the 4<br /><br /><br />";


/* Add/Remove (2) slashes to enable/disable this block
for ($i = 1; $i <5; $i++) {
    $x = rand(0,4);
    echo $que[$x];
    }
//*/

///* Add/Remove (2) slashes to enable/disable this block
$count_que = count( $que );
$ids = array_rand( $que, $count_que );

echo '<pre>';
print_r( $ids );
die( '</pre>' );
//*/
?>


Try it a couple of times, notice the values displayed are the keys to your original array and sorted randomly each time you refresh the page. If you decide to use this, just do a loop through this $ids and echo the lines. No chance for duplicates in this situation, for sure.
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #3  
Old 10-Jan-2007, 22:59
nirvana4lf nirvana4lf is offline
New Member
 
Join Date: Oct 2004
Posts: 8
nirvana4lf is on a distinguished road

Re: Using the For loop with an array and rand();


thanks, yeah it didnt repeat. but how can i use $ids in my loop? and also, whats the difference between using print_r and echo? i played with it and noticed that echo didnt properly display $ips like print_r did. thanks a lot for the help though, im new to php as you might notice.


Quote:
Originally Posted by admin
Until someone else comes up with a better solution, maybe you can try something like this, using array_rand():



Try it a couple of times, notice the values displayed are the keys to your original array and sorted randomly each time you refresh the page. If you decide to use this, just do a loop through this $ids and echo the lines. No chance for duplicates in this situation, for sure.
  #4  
Old 10-Jan-2007, 23:05
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 750
admin will become famous soon enough

Re: Using the For loop with an array and rand();


I use print_r or var_dump, when I want to see what's in an array (or object). You should read the manual about these functions. I can't explain it better than what's already stated there.

http://www.php.net/manual/en/function.print-r.php
http://www.php.net/manual/en/function.var-dump.php

To complete your script, this could work:

PHP Code:

<?php

$que[0] = "this is the 0<br /><br /><br />";

$que[1] = "this is the 1<br /><br /><br />";

$que[2] = "this is the 2<br /><br /><br />";

$que[3] = "this is the 3<br /><br /><br />";

$que[4] = "this is the 4<br /><br /><br />";


/* Add/Remove (2) slashes to enable/disable this block
for ($i = 1; $i <5; $i++) {
    $x = rand(0,4);
    echo $que[$x];
    }
//*/

///* Add/Remove (2) slashes to enable/disable this block
$count_que = count( $que );
$ids = array_rand( $que, $count_que );

foreach( $ids as $key )
{
    echo $que[$key];
}
//*/
?>

__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help in battleship study007 C Programming Language 14 26-Nov-2006 09:28
Quick, Insertion, and Partition silicon C++ Forum 0 18-May-2005 20:49
Merge and Heap...which is really faster silicon C++ Forum 0 10-May-2005 13:46
Insertion Sort Problem silicon C++ Forum 2 08-May-2005 16:13
Sorting Algorithms by Time silicon C++ Forum 4 02-May-2005 21:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:16.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.