echo image according to a condition

前端 未结 1 1818
耶瑟儿~
耶瑟儿~ 2021-01-17 06:42

I have an HTML tag here :


    
相关标签:
1条回答
  • 2021-01-17 07:34

    The output from your code was putting <img> tags inside the src attribute of the tag.
    That by definition doesn't work in HTML. If everything else was right, this should work:

    <?php
    
    function get_random_elements( $array,$limit = 0 ) {
    
        shuffle($array);
    
        if ( $limit > 0 ) {
            $array = array_splice($array, 0, $limit);
        }
        return $array;
    }
    
    function render_images() {
        global $stmt3;
        $output = '';
    
        if ($count = sqlsrv_num_rows($stmt3) > 0) {
            while ($recentBadge = sqlsrv_fetch_array($stmt3)) {
                $result[] = $recentBadge;
            }
    
            if ($count > 3) {
                $result = get_random_elements(result, 3);
            }
    
            foreach ($result as $recentBadge) {
                $output .= $recentBadge['BadgeName'];
                $output .= '<img src="' . $recentBadge['BadgeImage'] . '" alt="">';
                $output .= '<br>';
            }
        } else {
            $output = 'no results';
        }
    
        return $output;
    }
    ?>
    
    <span class="fa-stack fa-5x has-badge" >
    
        <div class="badgesize">
    
            <?php echo render_images(); ?>
    
        </div>
    
    </span>
    

    As a tip: please try to keep your code separated, the logic separated from the view.

    0 讨论(0)
提交回复
热议问题