1

is somthing like that possible?

for ($i = 1; $i <= 15; $i++) {
        $var = "bild" . $i;
        if (!empty ($row->bild.$i)) {
            echo '
                <div class="item">
                        <img border="no" class="bildschatten" src="include/images/projekte/'.$row->bild . $i.'" />
                </div>
            ';
        }
    }

how should it look correctly? problem is:

I fetch objects from a mysql database, in this table each object has 15 images

bild1 .. bild15

Now I want to iterate over this 15 images and want to check if the colum is empty or not.

the problem I have is here:

$row->bild.$i

that contains not the value of column bild1.. bild15 ... it only contains the value of $i

thanks

3
  • 2
    try $row->$var. for more details check the manual Commented Oct 15, 2016 at 14:03
  • mysterious tried this bevore ... its working now sorry for this stupid question Commented Oct 15, 2016 at 14:07
  • @Felix consider to normalize your table. Commented Oct 15, 2016 at 14:12

3 Answers 3

1

This code should work:

$row->{'bild'.$i}
Sign up to request clarification or add additional context in comments.

Comments

1

This is called variable of variables, You need to access something like this $row->$var.

Complete Solution:

for ($i = 1; $i <= 15; $i++) {
    $var = "bild" . $i;
    if (!empty ($row->$var)) {
        echo '
            <div class="item">
                    <img border="no" class="bildschatten" src="include/images/projekte/'.$row->$var.'" />
            </div>
        ';
    }
}

For more details checkout the Manual.

Comments

1

You could fetch the result as an array uning

    $row  =  db_fetch_array($result) 

and then accessing

    $var = "bild" . $i;
    if (!empty ($row[$var])) {

2 Comments

then I got this: Fatal error: Cannot use object of type stdClass as array in the row var is this: while ($row = db_fetch_object($sql)) {
@Felix . like indicated at the beginning of the answer you shuold use fetch the result as array using db_fetch_array($result)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.