Jump to content

[SOLVED] foreach loop error


PC Nerd

Recommended Posts

hi guys,

 

its probably stupid, but i can fin the error in this line:

 

while($Att_Start = mysqli_fetch_array($Att_Start_Query)) {

 

this is the error:

 

Warning: Invalid argument supplied for foreach() in C:\xampp\xampp\htdocs\Battle-Ages\Crons\cron_incs\Cron_Attack.inc on line 14

 

im receiving an endless loop with this error

 

 

this is the entire code:

 

 

 

<?php


$Max_Points = $Page_Date['Points'] * 1.3;
$Min_Points = $Page_Data['Points'] * 0.7;
$Att_Start_SQL = "SELECT User_ID, Points FROM General_Stats WHERE Points > '".$Min_Points."' AND Points < '".$Max_Points."'";
$Att_Start_Query = mysqli_query($DB_Server, $Att_Start_SQL)
or die("Could not retrive Attack Players.");

while($Att_Start = mysqli_fetch_array($Att_Start_Query)) {
$Att_Players[] = $Att_Start['User_ID'];
}

foreach($Att_Players as $Value){
echo $Value."<br>";
}


?>

 

 

 

thanks for any help

PC Nerd

Link to comment
Share on other sites

get ready to kick yourself

 

replace

while($Att_Start = mysqli_fetch_array($Att_Start_Query)) {

with

while($Att_Start == mysqli_fetch_array($Att_Start_Query)) {

or even

while(mysqli_fetch_array($Att_Start_Query)) {

 

Sorry techie your code isn't right, the $Att_Start = mysqli_fetch is correct, what this is doing is while the $Att_Start is true than keep the loop going and assign the value of mysqli_fetch_array to the variable.

 

Now PC Nerd I have 2 options for you to try. 1:

 

while($Att_Start = mysqli_fetch_array($Att_Start_Query) && !is_null($Att_Start)) {

 

Or:

 

$Att_Start = mysqli_fetch_array($Att_Start_Query)
while(!is_null($Att_Start)) {


  //end of processing
  $Att_Start = mysqli_fetch_array($Att_Start_Query)
}

 

The 2nd should work, I am unsure about the first.

Link to comment
Share on other sites

ok, i get some sort of output from my redefinine my array, with the while, in my oro=iginal code

 

now when i want to display that...

 

 

foreach($Att_Players as $Value){

echo $Value."<br>";

}

 

 

i get:

Warning: Invalid argument supplied for foreach() in FILE on line 18

whats happening i cant figure it out?

 

 

thankx

Link to comment
Share on other sites

not 100 % sure what you mean

 

 

i used the mysqli_fetch_array();

 

but i still got the same error

 

its in the foreach loop in my original code, not thw while loop

 

if i say echo $Att_Players, i get "Array"

 

does this mean that its returning an object or somethign instread of the array values?

 

thanks for any and all help, PC Nerd

Link to comment
Share on other sites

Seriously dude, this reminds of something my grandma used to do to me when I was little, I used to say "Pion" instead of "Lion" and she would always go "Read my lips, LION" and I would be stubborn and say Pion.

 

Anyhow try this:

 

<?php


$Max_Points = $Page_Date['Points'] * 1.3;
$Min_Points = $Page_Data['Points'] * 0.7;
$Att_Start_SQL = "SELECT User_ID, Points FROM General_Stats WHERE Points > '".$Min_Points."' AND Points < '".$Max_Points."'";
$Att_Start_Query = mysqli_query($DB_Server, $Att_Start_SQL)
or die("Could not retrive Attack Players.");

while($Att_Start = mysqli_fetch_array($Att_Start_Query, MYSQLI_ASSOC)) {
$Att_Players[] = $Att_Start['User_ID'];
}

foreach($Att_Players as $Value){
echo $Value."<br>";
}


?>

 

The issue is that mysqli_fetch_array returns a NON-ASSOCIATIVE array, meaning that $array['literalvalue'] does not work but $array[0] would. So you are creating an array with the $Att_Players[] line but nothing is being added to that array because there is no data due to the fact you did not specifiy the return of an associative array.

 

You should probably read/look at the examples shown here:

http://us2.php.net/manual/en/function.mysqli-fetch-array.php

 

Read about arrays and associative arrays here:

http://us2.php.net/manual/en/function.array.php

Link to comment
Share on other sites

Array

mysqli_result Object ( ) 7

8

10

 

thats my output.  im not sure about the numbers, but ithin its meant to be one of my randomly chosen user_ID's, but ill look at it, i dont kno about the rest......

 

 

thanks, what shounld i do???

 

Link to comment
Share on other sites

I am thinking but before taking any dicission.

Add

echo "\n".gettype($Att_Start);
echo "\n".gettype($Att_Start_Query)."\n";

Just bellow the print_r()

Add this Line at the top of your Page

header("Content-Type: text/plain");

And Post the output of print_r();

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.