ibnclaudius Posted November 14, 2011 Share Posted November 14, 2011 When I call load_new_flows.php with Ajax to update the content, I got "Warning: Invalid argument supplied for foreach() in load_new_flows.php on line 4", but with the load_flows.php everything is ok. index.php <?php error_reporting(0); include 'includes/db.php'; include 'includes/functions.php'; include 'includes/tolink.php'; include 'includes/time_stamp.php'; include 'includes/facebook/fbmain.php'; $brainflow = new brainflow(); // TEST // ------------------------------------ $level = 1; //overflowing = 3, flowing = 2, fresh = 1 // ------------------------------------ $flowsarray = $brainflow -> get_flows($level); $newflowsarray = $brainflow -> get_new_flows($level); ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type = "text/javascript" src = "js/brainflow.js"></script> <title>test</title> </head> <body> <br /><br /> <div id="new"></div> <br /><br /> <div id ="flows"> <?php // LOAD FLOWS // ------------------------------------ include 'load_flows.php'; // ------------------------------------ ?> </div> </body> </html> load_new_flows.php <?php foreach($newflowsarray as $data) { $new_flow_id = $data['flow_id']; $new_owner_id = $data['user_id_fk']; $new_owner_facebook_id = $data['facebook_id']; $new_flow = tolink(htmlentities($data['flow'])); $new_time = $data['time']; $new_owner_name = $data['name']; //$commentsarray = $brainflow -> get_comments($flow_id); ?> <div class="flow" id="<?php echo $new_flow_id; ?>"> <img src="images/pictures/<?php echo $new_owner_facebook_id; ?>.png"> <?php echo $new_owner_name; ?> <?php echo $new_flow; ?> <?php echo time_stamp($new_time); ?> </div> <?php } ?> load_flows.php <?php foreach($flowsarray as $data) { $flow_id = $data['flow_id']; $owner_id = $data['user_id_fk']; $owner_facebook_id = $data['facebook_id']; $flow = tolink(htmlentities($data['flow'])); $time = $data['time']; $owner_name = $data['name']; //$commentsarray = $brainflow -> get_comments($flow_id); ?> <div class="flow" id="<?php echo $flow_id; ?>"> <img src="images/pictures/<?php echo $owner_facebook_id; ?>.png"> <?php echo $owner_name; ?> <?php echo $flow; ?> <?php echo time_stamp($time); ?> </div> <?php } ?> includes/functions.php <?php class brainflow { // GET FLOWS public function get_flows($level) { $query = mysql_query("SELECT F.flow_id, F.user_id_fk, F.flow, F.time, U.name, U.facebook_id FROM flows F, users U WHERE F.user_id_fk = U.user_id AND F.level = '$level' ORDER BY F.flow_id DESC ") or die(mysql_error()); while($row = mysql_fetch_array($query)) $data[] = $row; return $data; } // GET NEW FLOWS public function get_new_flows($level) { $last_flow_id = $_POST['id']; $query = mysql_query("SELECT F.flow_id, F.user_id_fk, F.flow, F.time, U.name, U.facebook_id FROM flows F, users U WHERE F.user_id_fk = U.user_id AND F.flow_id > '$last_flow_id' AND F.level = '$level' ORDER BY F.flow_id DESC ") or die(mysql_error()); while($row = mysql_fetch_array($query)) $data1[] = $row; return $data; } } js $(document).ready(function() { $("#new").click(function() { var id = $(".flow:first").attr("id"); datastring = 'id=' + id; $.ajax({ type: "POST", url: "load_new_flows.php", data: datastring, cache: false, success: function(html){ $("#flows").prepend(html); } }); }); }); Quote Link to comment https://forums.phpfreaks.com/topic/251134-invalid-argument-supplied-for-foreach/ Share on other sites More sharing options...
Psycho Posted November 14, 2011 Share Posted November 14, 2011 You must be defining $flowsarray somewhere before you are are including load_flows.php (which is why it does not create an error) and you must NOT be defining $newflowsarray before including load_new_flows.php - or it is not defined as an array (which is why it is failing). I'm guessing there is something in one of the other included files that is defining $flowsarray Quote Link to comment https://forums.phpfreaks.com/topic/251134-invalid-argument-supplied-for-foreach/#findComment-1288104 Share on other sites More sharing options...
Maq Posted November 14, 2011 Share Posted November 14, 2011 Double check your get_new_flows() function, specifically the SQL, to ensure you're actually building and returning a valid array. Quote Link to comment https://forums.phpfreaks.com/topic/251134-invalid-argument-supplied-for-foreach/#findComment-1288106 Share on other sites More sharing options...
Psycho Posted November 14, 2011 Share Posted November 14, 2011 Double check your get_new_flows() function, specifically the SQL, to ensure you're actually building and returning a valid array. Ah, I didn't catch that. He's got an "or die" clause on the query - so it isn't failing. Just isn't' returning a result set - which may be valid. I'd suggest defining the array beforehand so that at least an empty array is returned when there are no results. But, on second inspection I see he is adding the results to "$data1", but is returning "$data" ! public function get_new_flows($level) { $last_flow_id = $_POST['id']; $query = "SELECT F.flow_id, F.user_id_fk, F.flow, F.time, U.name, U.facebook_id FROM flows F JOIN users U ON F.user_id_fk = U.user_id WHERE F.flow_id > '$last_flow_id' AND F.level = '$level' ORDER BY F.flow_id DESC " $result = mysql_query($query) or die(mysql_error()); $data = array(); //Define array while($row = mysql_fetch_array($result)) { $data[] = $row; } return $data; } Quote Link to comment https://forums.phpfreaks.com/topic/251134-invalid-argument-supplied-for-foreach/#findComment-1288134 Share on other sites More sharing options...
ibnclaudius Posted November 14, 2011 Author Share Posted November 14, 2011 I fixed the $data1, but the error persists... Quote Link to comment https://forums.phpfreaks.com/topic/251134-invalid-argument-supplied-for-foreach/#findComment-1288145 Share on other sites More sharing options...
Psycho Posted November 14, 2011 Share Posted November 14, 2011 I fixed the $data1, but the error persists... Did you validate that the query is returning results as Maq suggested? If the query can legitimately have no results then you need to define $data as an empty array as I provided in the code I posted above. Quote Link to comment https://forums.phpfreaks.com/topic/251134-invalid-argument-supplied-for-foreach/#findComment-1288151 Share on other sites More sharing options...
ibnclaudius Posted November 15, 2011 Author Share Posted November 15, 2011 Sorry, I'm noob. Could you please be more specific? load_flows.php and load_news_flows.php are exactly the same, it's almost the same thing. I don't know why just the load_news_flows.php is giving error. Quote Link to comment https://forums.phpfreaks.com/topic/251134-invalid-argument-supplied-for-foreach/#findComment-1288185 Share on other sites More sharing options...
Psycho Posted November 15, 2011 Share Posted November 15, 2011 You obviously didn't try the code I posted, did you? Here is the same code with some debugging information added. Use it. If it does not show you where the errors are, then post what it does output. public function get_new_flows($level) { $last_flow_id = $_POST['id']; $query = "SELECT F.flow_id, F.user_id_fk, F.flow, F.time, U.name, U.facebook_id FROM flows F JOIN users U ON F.user_id_fk = U.user_id WHERE F.flow_id > '$last_flow_id' AND F.level = '$level' ORDER BY F.flow_id DESC " $result = mysql_query($query) or die(mysql_error()); //Debugging info echo "Query: $query<br>\n"; echo "Num results: " . mysql_num_rows($result) . "<br>\n"; $data = array(); //Define array while($row = mysql_fetch_array($result)) { $data[] = $row; } return $data; } Quote Link to comment https://forums.phpfreaks.com/topic/251134-invalid-argument-supplied-for-foreach/#findComment-1288230 Share on other sites More sharing options...
Maq Posted November 16, 2011 Share Posted November 16, 2011 Sorry, I'm noob. Could you please be more specific? load_flows.php and load_news_flows.php are exactly the same, it's almost the same thing. I don't know why just the load_news_flows.php is giving error. But they're not. The difference is the SQL, that is why mjdamato gave you code to debug that function. Quote Link to comment https://forums.phpfreaks.com/topic/251134-invalid-argument-supplied-for-foreach/#findComment-1288699 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.