简体   繁体   中英

php concating variable and accessing array

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

You could fetch the result as an array uning

    $row  =  db_fetch_array($result) 

and then accessing

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

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 .

这段代码应该有效:

$row->{'bild'.$i}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM