user5153647
user5153647

Reputation:

Appending loop number to SQL row variable

"Undefined variable: payout_item_1" so it's getting the variable name correctly but I must have the format wrong.

for ($x = 1; $x <= 5; $x++) {
    echo "<input name = 'payout_item_" . $x . "' type = 'text' value = '" . $row[${"payout_item_" . $x}] . "' style = 'width : 150px;' ";
} 

Upvotes: 0

Views: 50

Answers (1)

domwrap
domwrap

Reputation: 443

I'm making a couple assumptions

  1. You have a table with columns "payout_item_1" through "payout_item_5"
  2. You do not have variables called $payout_item_1 through $payout_item_5 in which the actual column names are stored.

Currently your code is building variable variables:

This statement builds a variable name with payout_item_1 (in the first iteration). Effectively $payout_item_1.

${"payout_item_" . $x}

The code is then looking for a value in that variable to use as the column header name. Effectively, it's expecting somewhere further up for there to be something akin to

$payout_item_1 = "column1";

Which, as the error suggests, it cannot find. If my assumption in 1. was correct, all you need to do is reformat to

$row["payout_item_" . $x]

and you will be referencing the column payout_item_1 (through 5) from your $row object. Written literally:

$row["payout_item_1"]

Upvotes: 1

Related Questions