0

"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;' ";
} 
3
  • please check you have passed payout_item from server side or not.. Commented Apr 6, 2017 at 3:22
  • What does a var_dump($row) show? Commented Apr 6, 2017 at 3:34
  • Can you supply more code in your question to help people help you? Commented Apr 6, 2017 at 6:54

1 Answer 1

1

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"]
Sign up to request clarification or add additional context in comments.

1 Comment

Yup, that did the trick. I had tried many variations of that but must have missed the magic combination. Lack of sleep probably didn't help either. Thanks! You rock!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.