Jump to content

[SOLVED] while loop or shuffle?


PC Nerd

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.