HELP ! ! The each() function is deprecated HELP!

I have this code and dont know how to convert it. could someone please help. i have very little knowledge of php but need a fix for this asap. TIA

while ($idx < $fieldsx)
{
	list(, $valueEE) = each($_POST['fieldE']);
	list(, $valueFF) = each($_POST['fieldF']);
	list(, $valueGG) = each($_POST['fieldG']);
	list(, $valueHH) = each($_POST['fieldH']);
	if($valueEE)
	{
		echo "<input type='text' value='".$valueEE."' name='fieldE[]' /><input type='text' value='".$valueFF."' name='fieldF[]' /><input type='text' value='".$valueGG."' name='fieldG[]' /><input type='text' value='".$valueHH."' name='fieldH[]' /><br />";
	}

	$idx++;
}

Well, I am not sure why you would want to do this. The logic of creating a list from a list seems odd.
But, you should be able to use FOR instead. Here is a link to one explanation on how to do it. It shows five different examples and #5 appears to explain what you need… Replace EACH

The use of each makes it a bit complicated, as that function will move the internal pointer of $_POST. Can you show us more of the surrounding code, so we can see what else is done with $idx and $_POST? It would also be helpful to see what $fieldsx is.

Hi i was given this code as part of an invoice system. can do simple edits but this is way beyond my capabilities. this is more of the code all contained within a div.

<div id="text">
<br><center><b>
&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;Item Name:  &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Item Quantity: &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Item Description: &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; Item Price (Per Item):</b></center><br>
<?

$fieldsx = $_GET['Num'];


while ($idx < $fieldsx)
{
	list(, $valueEE) = each($_POST['fieldE']);
	list(, $valueFF) = each($_POST['fieldF']);
	list(, $valueGG) = each($_POST['fieldG']);
	list(, $valueHH) = each($_POST['fieldH']);
	if($valueEE)
	{
		echo "<input type='text' value='".$valueEE."' name='fieldE[]' /><input type='text' value='".$valueFF."' name='fieldF[]' /><input type='text' value='".$valueGG."' name='fieldG[]' /><input type='text' value='".$valueHH."' name='fieldH[]' /><br />";
	}

	$idx++;
}

?>


</div>

The <div> isn’t of much interest to us; that’s just output. It would be useful to see what’s being done with $idx before and after this block. Could you you find where $idx is declared and where it is last used, and show us that code?

@Duane, when posting code either use bbcode code tags [code][/code] or three markdown ``` back-ticks before/after it so that all the code will be shown, formatted, and color highlighted. I have added these to your posts above.

there is only one other reference to idx in the code other than what i have already posted

$idx = “0”;

it is set at the top of the code.

the full code makes this and if i click add item it adds another set of boxes, so for 4 items i would click the add item button 3 times giving me enough fields for 4 sale items. this little piece of code is for those boxes

OK. I’m assuming that $_GET['Num'] is equal to the number of items on the invoice. The code you’ve given us appears to render the invoice form with items already filled in - I’m guessing for editing or confirming the invoice. This code should do what you want:

for ($itemIdx = 0; $itemIdx < $fieldsx; $itemIdx ++) {
    $valueEE = $_POST['fieldE'][$itemIdx];
    $valueFF = $_POST['fieldF'][$itemIdx];
    $valueGG = $_POST['fieldG'][$itemIdx];
    $valueHH = $_POST['fieldH'][$itemIdx];

    if ($valueEE) {
        // htmlspecialchars prevents some hacking attempts
        echo "
        <input type='text' value='" . htmlspecialchars($valueEE) . "' name='fieldE[]' />
        <input type='text' value='" . htmlspecialchars($valueFF) . "' name='fieldF[]' />
        <input type='text' value='" . htmlspecialchars($valueGG) . "' name='fieldG[]' />
        <input type='text' value='" . htmlspecialchars($valueHH) . "' name='fieldH[]' />
        <br />";
    }
}

You should be able to delete the $idx variable as it’s no longer used.

As an aside; this code appears to use very old PHP coding style, and it isn’t very secure. One thing you should definitely be doing is escaping html output as I’ve done in the example above. I’d advise you get someone familiar with the current state of the language to give the codebase a once over.

@ skawid Thank you so much. this seems to have fixed the errors with that file. now i just need to implement it in all the other php files i have using similar code. Hopefully i will learn from this.

Regards

Duane

Duane, glad you solved this issue. One more thing to mention is that this is old code that indicates that the database used to hold invoices is outdated. Normally, you would design a database with invoice items listed separately and then use simple queries to retrieve them. In this manner, you never need to use complicated code to pull out parts of an invoice. I realize that a complicated invoice system that already works well is hard to rewrite completely. But, going forward, you should considers learning how to design a better database system. It would make the entire system much simpler to code and use. Just FYI…

Sponsor our Newsletter | Privacy Policy | Terms of Service