I've never had to use a variable in this position, but I am obviously misunderstanding something, as this is the code...
$row = mysql_fetch_object( mysql_query("SELECT * FROM book_bundle WHERE bundle_id='$bundleID'") );
This selects a single row from my database.
if ( !empty($row) ) {
if (!empty($row->book_name_4)) {$field_count=4;}
elseif (!empty($row->book_name_3)) {$field_count=3;}
elseif (!empty($row->book_name_2)) {$field_count=2;}
elseif (!empty($row->book_name_1)) {$field_count=1;}
else {}
for ($i=1; $i<=$field_count; $i++){
echo"
<img src='/images/thumbs/$row->book_image_$i.png' />
$row->book_name_$i
<br />
";
}
}
The issue is in the FOR statement. Before this was a FOR statement, I just repeated the same code 4 times, and inside the difference was that instead of $row->book_image$i and $row->book_name$i, the $i's were '1' in the first block of code, '2' the second time, and so on. It was manually coded in.
I tried to shrink the code using the FOR statement, but I can't seem to pass a variable inside the $row-> call, without breaking it. This code never looks up the $row, it outputs just '1' the first time, '2' the second time. It disregards the $row-> and disregards all text before the $i as well. I don't understand this behavior.
On the other hand, if I change $i to '1', then it works as expected, giving me the book name and picture from the database, only it repeats the same one 4 times as expected from the FOR statement. I just can't figure out how to turn that number into a variable and have the statement work.
Also, while testing, I changed "$row->book_name_$i" to "$row->$i". This broke all code from this point forward when displayed.
I'm sure there's a simple reason why variables don't work like this, or how to properly do it, but I don't know the terminology for these. I've been searching google and php.net/manual, but without knowing what to call them, I've had no luck discovering the answer. Thank you for any help.