简体   繁体   中英

Count filled SQL array fields PHP

I´m fetching an array from a mysql query like this.

$viewerSQL = mysql_query("SELECT * FROM `this` WHERE id = ". 
mysql_real_escape_string($_GET[id]) ."");
$pageData = mysql_fetch_array($pageSql);

I get an array with like 40 entries, i want to check seven of them by name if they are filled.

What i´ve tried and what I thought about so far:

$oioi = 0;

                            while($oioi<7){
                            $oioi++;
                            $pic = "pic".$oioi;
                            $active="";
                            $path = "./upload/objectoffers/$viewerData[id]/$pic";
                            $picDesc = "pic".$oi."Desc";
                            $picArray = '$viewerData['.$pic.']';
                            echo $pic."<br>";
                            echo $picArray."<p>";
                            if(!empty($path)){
                            if($oioi==1){$active = "active";}?>
                            <div class="item <?=$active;?>">
                                <img style="width: 100%;" src="./images/<?=$viewerData[$pic];?>" alt="<?=$viewerData[$picDesc]?>">
                            </div>
                            <?
                            }}
                          ?>

But this doesn´t work because: $path is NEVER empty...I wanted to avoid to write " if(!empty) " for every single $viewerData[pic1] to $viewerData[pic7] array manual...

that´s why i´m here...

maybe you can help me improving this or offer some tips.

Thanks!

edit: This is what I get as "output":

pic1
$viewerData[pic1]

pic2
$viewerData[pic2]

pic3
$viewerData[pic3]

pic4
$viewerData[pic4]

pic5
$viewerData[pic5]

pic6
$viewerData[pic6]

pic7
$viewerData[pic7]

I think you should use file_exists instead !empty:

$oioi = 0;

while ($oioi < 7) {
    $oioi++;
    $pic = "pic" . $oioi;
    $active = "";
    $path = "./upload/objectoffers/$viewerData[id]/$pic";
    $picDesc = "pic" . $oi . "Desc";
    $picArray = '$viewerData[' . $pic . ']';

    echo $pic . "<br>";
    echo $picArray . "<p>";

    if (file_exists($path)) {
        if ($oioi == 1) {
            $active = "active";
        }
        ?>
        <div class="item <?= $active; ?>">
            <img style="width: 100%;" src="./images/<?= $viewerData[$pic]; ?>" alt="<?= $viewerData[$picDesc] ?>">
        </div>
    <?php
    }
}

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