PC Nerd Posted April 10, 2007 Share Posted April 10, 2007 i think the following code is stuffing up... my code: while($Def_Start = mysqli_fetch_array($Def_Start_Query, MYSQLI_ASSOC)) { $Def_Options[] = $Def_Start['User_ID']; } shuffle($Def_Options); $Def_ID = $Def_Options[0]; Run_Attack($Page_Data, $Def_ID, $DB_Server); its simply meant to select a random element from that array, but i think that shuffle is stuffing up..... instead of getting the 20 or so entries, and rearranging them, its randomising them, and copying then appending the copy to itself, leaving me with an array about 9999 elements long, and just copies of itself..... is this my loop or shuffle??? i think its the while, becauase i still get the long array when i mute the shuffle thanks, PC Nerd Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/ Share on other sites More sharing options...
jitesh Posted April 10, 2007 Share Posted April 10, 2007 This is just example may be useful $value= array("Rabin","Reid","Cris","KVJ","John"); $rand_keys=array_rand($value,2); echo "First random element = ".$value[$rand_keys[0]]; echo "<br>Second random element = ".$value[$rand_keys[1]]; Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-225781 Share on other sites More sharing options...
jitesh Posted April 10, 2007 Share Posted April 10, 2007 another example do { $x = rand(0, count($array)-1); } while ($array[$x] == '0'); $selection = $trait_array[$x]; $array[$x] = '0'; //zero it out as invalid now Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-225784 Share on other sites More sharing options...
PC Nerd Posted April 10, 2007 Author Share Posted April 10, 2007 um, how would this work in my case? Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-225788 Share on other sites More sharing options...
jitesh Posted April 10, 2007 Share Posted April 10, 2007 Replace your array in above examples. Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-225790 Share on other sites More sharing options...
PC Nerd Posted April 10, 2007 Author Share Posted April 10, 2007 i dont understand: $array[$x] = '0'; //zero it out as invalid now about the zero it out???? Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-225798 Share on other sites More sharing options...
jitesh Posted April 10, 2007 Share Posted April 10, 2007 This is enought do { $x = rand(0, count($array)-1); } while ($array[$x] == '0'); $selection = $trait_array[$x]; Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-225803 Share on other sites More sharing options...
PC Nerd Posted April 10, 2007 Author Share Posted April 10, 2007 thankx for the fast reply ill try it now Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-225805 Share on other sites More sharing options...
PC Nerd Posted April 10, 2007 Author Share Posted April 10, 2007 for some reason, sometimes i get 9999 and sometimes i get 99, and sometimes i get the $def_Choise and sometiems i dont........... im convinced its my while..... code: while($Def_Start = mysqli_fetch_array($Def_Start_Query, MYSQLI_ASSOC)) { $Def_Options[] = $Def_Start['User_ID']; } #shuffle($Def_Options); #$Def_ID = $Def_Options[0]; $Def_ID = array_rand($Def_Options, 1); Run_Attack($Page_Data, $Def_ID, $DB_Server); thankx Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-225826 Share on other sites More sharing options...
PC Nerd Posted April 10, 2007 Author Share Posted April 10, 2007 *** any ideas? *** Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-226318 Share on other sites More sharing options...
Psycho Posted April 10, 2007 Share Posted April 10, 2007 Well, it's entirely possible that there is something else happening before this code that is causing $Def_Start_Query to be a much larger result set than it should be. Have you verified the number of records that exist in $Def_Start_Query each time you run the script? Try adding this line before[ you start the loop: echo mysqli_num_rows($Def_Start_Query); Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-226347 Share on other sites More sharing options...
PC Nerd Posted April 10, 2007 Author Share Posted April 10, 2007 its 20 long... but now instead of getting 9999 records, i get 100, but i have only tested it about 10 -15 times, im sure ill get the 1000 long one again... the Def_Start didnt return anything, not even mysqli_object.: [Def_Start_SQL] => SELECT User_ID, Points FROM General_Stats WHERE Points > '1' AND Points < '30' [Def_Start_Query] => mysqli_result Object ( ) [] => 20 [Def_Start] => [Def_Options] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 7 [4] => 8 [5] => 9 [6] => 10 [7] => 11 [8] => 17 [9] => 37 [10] => 36 [11] => 35 [12] => 34 [13] => 33 [14] => 32 [15] => 31 [16] => 77 [17] => 78 [18] => 90 [19] => 87 [20] => 1 [21] => 2 [22] => 3 [23] => 7 [24] => 8 [25] => 9 [26] => 10 [27] => 11 [28] => 17 [29] => 37 [30] => 36 [31] => 35 [32] => 34 [33] => 33 [34] => 32 [35] => 31 [36] => 77 [37] => 78 [38] => 90 [39] => 87 [40] => 1 [41] => 2 [42] => 3 [43] => 7 [44] => 8 [45] => 9 [46] => 10 [47] => 11 [48] => 17 [49] => 37 [50] => 36 [51] => 35 [52] => 34 [53] => 33 [54] => 32 [55] => 31 [56] => 77 [57] => 78 [58] => 90 [59] => 87 [60] => 1 [61] => 2 [62] => 3 [63] => 7 [64] => 8 [65] => 9 [66] => 10 [67] => 11 [68] => 17 [69] => 37 [70] => 36 [71] => 35 [72] => 34 [73] => 33 [74] => 32 [75] => 31 [76] => 77 [77] => 78 [78] => 90 [79] => 87 [80] => 1 [81] => 2 [82] => 3 [83] => 7 [84] => 8 [85] => 9 [86] => 10 [87] => 11 [88] => 17 [89] => 37 [90] => 36 [91] => 35 [92] => 34 [93] => 33 [94] => 32 [95] => 31 [96] => 77 [97] => 78 [98] => 90 [99] => 87 ) thats the outcome. im using the display all variables function... Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-226364 Share on other sites More sharing options...
PC Nerd Posted April 11, 2007 Author Share Posted April 11, 2007 ***BUMP*** Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-226495 Share on other sites More sharing options...
PC Nerd Posted April 11, 2007 Author Share Posted April 11, 2007 ***BUMP*** Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-226727 Share on other sites More sharing options...
Psycho Posted April 11, 2007 Share Posted April 11, 2007 the Def_Start didnt return anything, not even mysqli_object.: Are you saying that mysqli_num_rows($Def_Start) didn't have any value? if so, you are doing something wrong. There is no way for your loop to execute if there are no records int he result set. I still think your problem is in the generation of the result set, not the loop. Place this line right before the loop. <?php echo '<br><br>There are ' . mysqli_num_rows($Def_Start) . 'records in the result set<br><br>'; ?> If you are getting larger numbers than expected then the error is in the query. If the number returned is differnt than the records created by the loop, then there is something wrong with the loop, but I don't see anything wrong with it. Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-226973 Share on other sites More sharing options...
PC Nerd Posted April 12, 2007 Author Share Posted April 12, 2007 ok, its returning a length of 20..... which is correct, but it looks like the while loop ( gettign the records ) is looping 5 times over, but there isnt a second argument....... whats happening? thankx Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-227318 Share on other sites More sharing options...
PC Nerd Posted April 12, 2007 Author Share Posted April 12, 2007 just a thought, could i loop through the result using a for loop insteadof the while, and if so , how??? thanks again! Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-227332 Share on other sites More sharing options...
Psycho Posted April 12, 2007 Share Posted April 12, 2007 Yes, you can use a for loop. It should be a viable workaround for this problem. But, I can't figure it out. It just doesn't make any sense. Anyway, here is a workaround using a FOR loop. for ($i=0; $i<mysqli_num_rows($Def_Start); $i++) { $Def_Start = mysqli_fetch_array($Def_Start_Query, MYSQLI_ASSOC); $Def_Options[] = $Def_Start['User_ID']; } Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-227375 Share on other sites More sharing options...
PC Nerd Posted April 12, 2007 Author Share Posted April 12, 2007 thankx ill give it a go Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-227549 Share on other sites More sharing options...
PC Nerd Posted April 13, 2007 Author Share Posted April 13, 2007 um, thats wort of getting there, its eliminating the repeated array, but now for some reason its not returning an array my code as it is....... $Def_Start_len = mysqli_num_rows($Def_Start_Query); echo $Def_Start_len; echo "<br><br><Br><Br><Br><Br>"; for ($i=0; $i<$def_start_len; $i++) { $Def_Start = mysqli_fetch_array($Def_Start_Query, MYSQLI_ASSOC); $Def_Options[$i+1] = $Def_Start['User_ID']; } shuffle($Def_Options); $Def_ID = $Def_Options[0]; #$Def_ID = array_rand($Def_Options); thankx guys Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-228145 Share on other sites More sharing options...
Psycho Posted April 13, 2007 Share Posted April 13, 2007 The variables are named differently! $Def_Start_len = mysqli_num_rows($Def_Start_Query); echo $Def_Start_len; echo " <Br><Br><Br><Br>"; for ($i=0; $i<$def_start_len; $i++) { $Def_Start = mysqli_fetch_array($Def_Start_Query, MYSQLI_ASSOC); $Def_Options[$i+1] = $Def_Start['User_ID']; } Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-228180 Share on other sites More sharing options...
PC Nerd Posted April 13, 2007 Author Share Posted April 13, 2007 thankx this is my error, Warning: shuffle() expects parameter 1 to be array, null given in FILEon line 20 $Def_Start_Query = mysqli_query($DB_Server, $Def_Start_SQL) or die("Could not retrieve Defenese Players."); $Def_Start_len = mysqli_num_rows($Def_Start_Query); echo $Def_Start_len; echo "<br><br><Br><Br><Br><Br>"; for ($i=0; $i<$Def_start_len; $i++) { $Def_Start = mysqli_fetch_array($Def_Start_Query, MYSQLI_ASSOC); $Def_Options[$i+1] = $Def_Start['User_ID']; } $Def_ID = shuffle($Def_Options); $Def_ID = $Def_Options[0]; #$Def_ID = array_rand($Def_Options); whats happening do you think, shouldnt that work as reassigning the array? Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-228227 Share on other sites More sharing options...
PC Nerd Posted April 13, 2007 Author Share Posted April 13, 2007 any ideas or suggestions????? thankx Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-228357 Share on other sites More sharing options...
PC Nerd Posted April 13, 2007 Author Share Posted April 13, 2007 *** bump *** Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-228483 Share on other sites More sharing options...
Psycho Posted April 13, 2007 Share Posted April 13, 2007 The last code you posted still has the same error I stated above. The variable is not the same. Variable names are case sensitive. $Def_Start_len <> $Def_start_len Link to comment https://forums.phpfreaks.com/topic/46425-solved-while-loop-or-shuffle/#findComment-228681 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.