Jump to content

Recommended Posts

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);

		}
	});
});
});

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/251134-invalid-argument-supplied-for-foreach/
Share on other sites

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

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;
}

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.

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;
}

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.

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.