php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #5395 "for" loop functioning with list()each() as loop control
Submitted: 2000-07-06 11:35 UTC Modified: 2000-07-06 14:47 UTC
From: derrelldld at netzero dot net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.1pl2 OS: Windows 98 2nd Ed.
Private report: No CVE-ID: None
 [2000-07-06 11:35 UTC] derrelldld at netzero dot net
Dear PHP Folks,
	I've been using PHP 4.0.0 (and today I downloaded PHP 4.0.1pl2) with Apache 3.1.12 on Windows 98 2nd edition.  I believe I've found a bug in the PHP "for" loop functioning; first I'll give you the bare-bones details

1) I've been working through the PHP Essentials examples; in one example I used the list()/each() in a "while" loop as usual:
	$selectArray = array( "itemID" => "ITEM_ID",
					"itemTitle" => "ITEM_TITLE",
					"itemDescription" => "ITEM_DESCRIPTION",
					"itemPrice" => "ITEM_PRICE" );
	$selectStatement = "SELECT ";
	
	$fieldIndex = 0;
	while ( list( $key, $value ) = each( $HTTP_POST_VARS ) ) {
		if ( ( strtoupper( substr( $key, 0, 4 ) ) == "ITEM" )
			&& ( $value == "on" ) ) {
			$selectStatement .= "$selectArray[$key], ";
			$fieldArray[$fieldIndex] = $selectArray[$key];
			$fieldIndex++;
		}
	}
.
.
.
.
	echo "<p>$selectStatement";

	echo "<p>$fieldArray[0]<br>
		$fieldArray[1]<br>
		$fieldArray[2]<br>
		$fieldArray[3]<br>";

with the expected result of the four field names appearing in both "echo"s.
SELECT ITEM_ID, ITEM_TITLE, ITEM_DESCRIPTION, ITEM_PRICE FROM MY_PRODUCTS ORDER BY ITEM_ID ASC 

ITEM_ID
ITEM_TITLE
ITEM_DESCRIPTION
ITEM_PRICE
*******************************************************************************



*******************************************************************************
2) I decided to change the "while" loop to a "for" loop:
	$selectArray = array( "itemID" => "ITEM_ID",
					"itemTitle" => "ITEM_TITLE",
					"itemDescription" => "ITEM_DESCRIPTION",
					"itemPrice" => "ITEM_PRICE" );
	$selectStatement = "SELECT ";


	for ( $fieldIndex = 0;
		list( $key, $value ) = each( $HTTP_POST_VARS );
		$fieldIndex++ ) {
		if ( ( strtoupper( substr( $key, 0, 4 ) ) == "ITEM" )
			&& ( $value == "on" ) ) {
			$selectStatement .= "$selectArray[$key], ";
			$fieldArray[$fieldIndex] = $selectArray[$key];
		}
	}
.
.
.
.
	echo "<p>$selectStatement";

	echo "<p>$fieldArray[0]<br>
		$fieldArray[1]<br>
		$fieldArray[2]<br>
		$fieldArray[3]<br>";

with the unexpected result in the second "echo".
SELECT ITEM_ID, ITEM_TITLE, ITEM_DESCRIPTION, ITEM_PRICE FROM MY_PRODUCTS ORDER BY ITEM_ID ASC 

ITEM_ID

ITEM_TITLE
ITEM_DESCRIPTION
*******************************************************************************



*******************************************************************************
3) The "for" loop worked when I moved the increment portion down into the body of the loop.
	$selectArray = array( "itemID" => "ITEM_ID",
					"itemTitle" => "ITEM_TITLE",
					"itemDescription" => "ITEM_DESCRIPTION",
					"itemPrice" => "ITEM_PRICE" );
	$selectStatement = "SELECT ";


	for ( $fieldIndex = 0;
		list( $key, $value ) = each( $HTTP_POST_VARS ); ) {
		if ( ( strtoupper( substr( $key, 0, 4 ) ) == "ITEM" )
			&& ( $value == "on" ) ) {
			$selectStatement .= "$selectArray[$key], ";
			$fieldArray[$fieldIndex] = $selectArray[$key];
			$fieldIndex++;
		}
	}
.
.
.
.
	echo "<p>$selectStatement";

	echo "<p>$fieldArray[0]<br>
		$fieldArray[1]<br>
		$fieldArray[2]<br>
		$fieldArray[3]<br>";

backk again with the expected results.
SELECT ITEM_ID, ITEM_TITLE, ITEM_DESCRIPTION, ITEM_PRICE FROM MY_PRODUCTS ORDER BY ITEM_ID ASC 

ITEM_ID
ITEM_TITLE
ITEM_DESCRIPTION
ITEM_PRICE
*******************************************************************************



*******************************************************************************
THE FOLLOWING ARE THE .HTML AND .PHP FILES i USED, IN THEIR ENTIRETY, SORRY IF THIS IS TOO MUCH DETAIL:
*******************************************************************************

*******************************************************************************
.HTML file
*******************************************************************************
<html>

<head>
<title>Select Record(s) from MY_PRODUCTS</title>
</head>

<body>
<h1>Select Record(s) from MY_PRODUCTS:</h1>

<form method="post" action="MySQL5DoSelectRecords.php4">

<table cellspacing=5 cellpadding=5>
<tr>
<th valign="top"><strong>Show Field(s):</strong?</th>
<th valign="top"></th>
<th valign="top"><strong>&nbsp;Sort By:</strong?</th>
</tr>

<tr>
<td valign="top"><strong>Item ID:</strong?</td>
<td valign="top">
<input type="checkbox" name="itemID" checked>
</td>
<td valign="top">
<input type="radio" name="sortBy" value="itemID" checked>
</td>
</tr>

<tr>
<td valign="top"><strong>Item Title:</strong?</td>
<td valign="top">
<input type="checkbox" name="itemTitle" checked>
</td>
<td valign="top">
<input type="radio" name="sortBy" value="itemTitle">
</td>
</tr>

<tr>
<td valign="top"><strong>Item Description:</strong?</td>
<td valign="top">
<input type="checkbox" name="itemDescription" checked>
</td>
<td valign="top">
<input type="radio" name="sortBy" value="itemDescription">
</td>
</tr>

<tr>
<td valign="top"><strong>Item Price:</strong></td>
<td valign="top">
<input type="checkbox" name="itemPrice" checked>
</td>
<td valign="top">
<input type="radio" name="sortBy" value="itemPrice">
</td>
</tr>

<tr>
<td align="center" colspan=2>
<input type="submit" value="Select Record(s)">
</td?
</tr>
</table>

</form>

</body>

</html>



*******************************************************************************
.PHP file
*******************************************************************************
<?php
//
// PART 1:
//	Identify the fields that were checked in the selection form.
// PRE: $HTTP_POST_VARS with
//		a) keys:
//			variable names (without the $) from the name= attributes
//				of the form, that correspond
//				to the MY_PRODUCTS fields,
//				e.g., "item" .= ID || Title || etc. 
//		   values:
//			"on" key is present (was checked).
//
//		b) keys: "sortBy", "whenCondition".
//		   values: field names of MY_PRODUCTS.
//
//		c) key: "when" .= variable names as a (above).
//		   value: a condition string, e.g., < 20.
//
// POST: a) a record selection and sort SQL statement: "$selectStatement".
//
//	   b) an array of MY_PRODUCTS field names that were selected
//			by the form, in the MY_PRODUCTS field order:
//			"$fieldArray".
//
		
	$selectArray = array( "itemID" => "ITEM_ID",
					"itemTitle" => "ITEM_TITLE",
					"itemDescription" => "ITEM_DESCRIPTION",
					"itemPrice" => "ITEM_PRICE" );
	$selectStatement = "SELECT ";


	for ( $fieldIndex = 0;
		list( $key, $value ) = each( $HTTP_POST_VARS ); ) {
		if ( ( strtoupper( substr( $key, 0, 4 ) ) == "ITEM" )
			&& ( $value == "on" ) ) {
			$selectStatement .= "$selectArray[$key], ";
			$fieldArray[$fieldIndex] = $selectArray[$key];
			$fieldIndex++;
		}
	}
	if ( $selectStatement != "SELECT " ) {
		$selectStatement = substr( $selectStatement, 0, -2 );
	}
	$selectStatement .=
		" FROM MY_PRODUCTS ORDER BY $selectArray[$sortBy] ASC";

//
// PART 2:
//	The HTML page(s) that will show the records of MY_PRODUCTS
//	in an HTML table.
//
	echo "
		<html>

		<head>
		<title>MY_PRODUCTS Result</title>
		</head>

		<body>
		<h1>MY_PRODUCTS Result:</h1>
		";

	echo "<p>$selectStatement";

	echo "<p>$fieldArray[0]<br>
		$fieldArray[1]<br>
		$fieldArray[2]<br>
		$fieldArray[3]<br>";

	$connection = mysql_connect("localhost","Derrell","")
	or die("<p>Couldn't connect to MySQL server.");
	echo "<p>Connection: $connection";

	$dataBase = mysql_select_db("Inventory",$connection)
	or die("<p>Couldn't select Inventory database.");
	echo "<p>Database: $dataBase";

	$queryResult = mysql_query($selectStatement,$connection)
	or die("<p>Couldn't select from MY_PRODUCTS.");
	echo "<p>Query Result: $queryResult";

	echo "<p><table border=1>";
	echo "<tr>";
	for ( $x = 0; $x < count( $fieldArray ); $x++ ) {
		echo "<th>";
		echo ereg_replace( "_", " ", $fieldArray[$x] );
		echo "</th>";
	}
	echo "</tr>";

	while ( $row = mysql_fetch_array( $queryResult ) ) {
		echo "<tr>";
		for ( $x = 0; $x < count( $fieldArray ); $x++ ) {
			if ( preg_match( "/price/i", "$fieldArray[$x]" ) ) {
				echo "<td align=\"right\">";
			} else {
				echo "<td>";
			}
			echo $row[$fieldArray[$x]];
			echo "</td>";
		}
		echo "</tr>";
	}

	echo "</table>
		</body>
		</html>
		";

	mysql_free_result( $queryResult );
	mysql_close( $connection );
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-07-06 14:47 UTC] stas at cvs dot php dot net
You have bug in your script.
Second example increments index even if field name does not start with ITEM.

Please, don't post so long examples, if you feel there's a bug, please make short reproducing example. 
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Tue Dec 18 18:01:27 2018 UTC