whats up everybody? im using the Web Database Applications: with PHP and MySQL book and tried to modify their generic browse function to make a front-end that looks at a table 20 records at a time. suffice it to say, i can't get it to work. the function is in a seperate file functions2.php. the error messages i am getting tho are related to the 2 foreach loops in the function.

heres the first one

// (4b) print out the column headers from $header
foreach ($header as $element)
{
	echo "\n\t<th>" . $element["header"] . "</th>";
}
echo "\n</tr>";

and the second one

// (5b) for each of the attributes in a row
foreach($header as $element)
{
	echo "\n\t<td>";

	// get the database attribute name for the
	// current attribute
	$temp = $element["attrib"];

	// print out the value of the current attribute
	echo $row["$temp"];

	echo "</td>";
} // end foreach attribute
			
echo "\n</tr>\n";

which is inside of a for loop. the error messages im getting are

Warning: Invalid argument supplied for foreach() in /shared/homedir/henry/public_html/functions2.php on line 55

Warning: Invalid argument supplied for foreach() in /shared/homedir/henry/public_html/functions2.php on line 71

its straight outta the book so im not sure what im doing wrong. any ideas?

Recommended Answers

All 16 Replies

Could it be that the double quotes are conflicting

i dont think so because both errors refer to the first line of the foreach statement.


foreach ($header as $element)

and the other one which is the same

foreach ($header as $element)

please help me this is annoying the shit outta me.

i dont think so because both errors refer to the first line of the foreach statement.


foreach ($header as $element)

and the other one which is the same

foreach ($header as $element)

please help me this is annoying the shit outta me.

You don't show the assignment of $header. Are you sure that it is an array?

$header is assigned in a different php file, the file from which it is called.....this may be part of the problem, in that it's not recognizing it as an array after it's passed to the function definition in a separate file. but $header is initialized as such:

// HTML <TABLE> column headers
$header[0]["header"] = "Id";
$header[1]["header"] = "Network";
$header[2]["header"] = "Mask";
$header[3]["header"] = "Size";
$header[4]["header"] = "Type";
$header[5]["header"] = "Router Info";
$header[6]["header"] = "Description";
$header[7]["header"] = "Member Id";
$header[8]["header"] = "User";
$header[9]["header"] = "Assigned";

// query attributes to display in <TABLE> columns
$header[0]["attrib"] = "id";
$header[1]["attrib"] = "network";
$header[2]["attrib"] = "mask";
$header[3]["attrib"] = "size";
$header[4]["attrib"] = "type";
$header[5]["attrib"] = "router_info";
$header[6]["attrib"] = "description";
$header[7]["attrib"] = "member_id";
$header[8]["attrib"] = "user";
$header[9]["attrib"] = "assigned";

$header is assigned in a different php file, the file from which it is called.....this may be part of the problem, in that it's not recognizing it as an array after it's passed to the function definition in a separate file. but $header is initialized as such:

// HTML <TABLE> column headers
$header[0]["header"] = "Id";
$header[1]["header"] = "Network";
$header[2]["header"] = "Mask";
$header[3]["header"] = "Size";
$header[4]["header"] = "Type";
$header[5]["header"] = "Router Info";
$header[6]["header"] = "Description";
$header[7]["header"] = "Member Id";
$header[8]["header"] = "User";
$header[9]["header"] = "Assigned";

// query attributes to display in <TABLE> columns
$header[0]["attrib"] = "id";
$header[1]["attrib"] = "network";
$header[2]["attrib"] = "mask";
$header[3]["attrib"] = "size";
$header[4]["attrib"] = "type";
$header[5]["attrib"] = "router_info";
$header[6]["attrib"] = "description";
$header[7]["attrib"] = "member_id";
$header[8]["attrib"] = "user";
$header[9]["attrib"] = "assigned";

and you have an include for that file in your file with the foreach? I don't see a reason that $header would not be a valid parameter for the foreach, unless it's not been defined yet as that array.

ok i figured this out. i was being an idiot and didnt realize that the file that calls the function wasn't the right copy. haha. now i have a different problem tho. i can't get the Next link to work. i will post the entire function. help me if you can.

define(ROWS, 20);
	
	// browse through the $connection by the running $query

	// begin the display of data with row $rowOffset
	// put a header on the page, $pageHeader
	// use the array $header[]["header"] for headers on
	// each <table> column
	// use the array $header[]["attrib"]for the names
	// of the database attributes to show in each column

	// use $browseString to prefix an embedded link
	// to the previous, next, and other pages

	function browse(  $scriptName, 
				$connection, 
				$browseString, 
				$rowOffset, 
				$query, 
			 
				$header)
	{
		// (1) run the query on the database
		// through the connection
		if (!($result = @ mysql_query ($query, $connection)))
			showerror();

		// find out how many rows there are
		$rowsFound = @ mysql_num_rows($result);

		// is there any data?
		if ($rowsFound != 0)
		{
			// yes, there is data.

			// (2a) the "previous" page begins at the current
			// offset LESS the number of rows per page
			$previousOffset = $rowOffset - ROWS;

			// (2b) the "next" page begins at the current offset
			// PLUS the number of rows per page
			$nextOffset = $rowOffset + ROWS;

			// (3) seek to the current offset
			if (!@ mysql_data_seek($result, $rowOffset))
				showerror();

			// (4a) start a table
			echo "<table border=\"1\">\n<tr>";

			// (4b) print out the column headers from $header
			foreach ($header as $element)
			{
				echo "\n\t<th>" . $element["header"] . "</th>";
			}
			echo "\n</tr>";

			// (5a) fetch one page of results (or less if on last page)
			for ( $rowCounter = 0;
			    (($rowCounter < ROWS) &&
			     ($row = @ mysql_fetch_array($result)) );
			    $rowCounter++)
			{
				// print out a row
				echo "\n<tr>";

				// (5b) for each of the attributes in a row
				foreach($header as $element)
				{
					echo "\n\t<td>";

					// get the database attribute name for the
					// current attribute
					$temp = $element["attrib"];

					// print out the value of the current attribute
					echo $row["$temp"];

					echo "</td>";
				} // end foreach attribute
			
				echo "\n</tr>\n";
			} // end for rows in the page

			// finish the results table, and start a footer
			echo "\n</table>\n<br>";

			// (6) show the row numbers that are being viewed
			echo ($rowOffset + 1) . "-" .
				($rowCounter + $rowOffset) . " of ";
			echo "$rowsFound records found matching " .
				"your criteria\n<br>";

			// (7a) are there any previous pages?
			if ($rowOffset > 0)
				// yes, so create a previous link
				echo "\n\t<a href=\"" . $scriptName .
					"?offset=" . rawurlencode($previousOffset) . 
				//	"&amp;" . $browseString .
					"\">Previous</a> ";
			else
				// no, there is no previous page so dont print a link
				echo "Previous ";

			// (7b) are there any next pages?
			if (($row != false) && ($rowsFound > $nextOffset))
				// yes, so create a next link
				echo "\n\t<a href=\"" . $scriptName .
					"?offset=" . rawurlencode($nextOffset) .
				//	"&amp;" . $browseString .
					"\">Next</a> ";
			else
				// no, there is next page so dont print a link
				echo "Next ";

		} // end if rowsFound != 0
		else
		{
			echo "<br>No rows found matching your criteria.\n";
		}
		// (7c) create a link back to the query input page 
		echo "<br><a href=\"" . $scriptName .
			"\">Back to Search</a><br>";
	}

What does the 'next' link look like when you hover over it? Is the script name correct? Where is the code in your page that calls the browse() function? It may not be passing the correct parameters.

it looks like this

http://gothics.aanet.org/~henry/display_all.php?offset=20

but its not getting that value into the variable $offset (in the file the function is called from). heres the code from where this function is called.

<?php

include	"iprange.inc";
include	"error.inc";
include	"paths.inc";
include	"functions2.php";


$scriptName = "display_all.php";

// (1) Open the database connection
$connection = mysql_connect($hostName,$username,$password);

// (2) Select the database
mysql_select_db($databaseName, $connection);


// set $offset to zero if not previously set
if (empty($offset) )
	$offset = 0;

// a query to see everything in subnets table
$query = "SELECT * FROM subnets ORDER BY network";

// initialize the browse() function params

// query prefix for the next previous links
$browseString = "";

// page header for the browse screen
$pageHeader = "Browse All Networks";

// HTML <TABLE> column headers
$header[0]["header"] = "Id";
$header[1]["header"] = "Network";
$header[2]["header"] = "Mask";
$header[3]["header"] = "Size";
$header[4]["header"] = "Type";
$header[5]["header"] = "Router Info";
$header[6]["header"] = "Description";
$header[7]["header"] = "Member Id";
$header[8]["header"] = "User";
$header[9]["header"] = "Assigned";

// query attributes to display in <TABLE> columns
$header[0]["attrib"] = "id";
$header[1]["attrib"] = "network";
$header[2]["attrib"] = "mask";
$header[3]["attrib"] = "size";
$header[4]["attrib"] = "type";
$header[5]["attrib"] = "router_info";
$header[6]["attrib"] = "description";
$header[7]["attrib"] = "member_id";
$header[8]["attrib"] = "user";
$header[9]["attrib"] = "assigned";

// call generic browsing code to browse query
browse( $scriptName, $connection, $browseString, 
		$offset, $query, $header);

// close the connection
if (!(mysql_close($connection)))
	showerror();

Do you have "register_globals = Off" or is it on? The default is off I believe, which means you cannot access the offset with just $offset. You will need use:

$offset = $_GET['offset'];

and that will grab the value from the URL and pass it to the variable?

thanks that worked perfectly.

thanks that worked perfectly.

Glad to hear it. Things like that can be very confusing with differences in runtime settings.

With register_globals turned on, all variables from GET and POST are automatically available as a global variable and can be accessed as $variableName. When register_globals is off, you must retrieve the variable from $_GET[] or $_POST[] yourself to use it. The code that you were using from the book obviously had register_globals turned on.

commented: very helpful +11

the actual code from the book didnt have register_globals turned on, but i think that is the default setting for the vbersion on php that i'm using. that is a config file setting, right?

also, can you help with my other thread? its near the top of this forum.

the actual code from the book didnt have register_globals turned on, but i think that is the default setting for the vbersion on php that i'm using. that is a config file setting, right?

Yes, it's a setting in your php.ini file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.