根据条件回波图像 [英] echo image according to a condition
问题描述
我这里有一个 HTML 标签:
</span>-->
用于检索信息并将其存储在变量中的查询:
$q4 = "SELECT TOP 3 b.BadgeName, b.BadgeImage FROM BadgeImageTable AS bINNER JOIN employee_badge AS eON e.badge_id = b.BadgeID哪里 e.employee_id = 2164ORDER BY e.earned_on DESC ";$stmt3=sqlsrv_query($conn,$q4);如果($stmt3==false){echo '检索信息错误!!<br/>';死(打印_r(sqlsrv_errors(),真));}
在图像标签中,我试图以某种条件回应徽章的图像.
3){$result = array_rand($result, 3);}foreach($result as $recentBadge){回声$recentBadge['BadgeName'],'<img src="'.$recentBadge['BadgeImage'].'">','
';}} 别的 {echo '没有结果';}?>" alt="" >
</span>
以上 PHP 的条件:
如果计数大于 3,我想回显任意三个随机图像,否则回显 '$recentBadge' 给出的任何结果.
问题:根据上述情况,我无法回声.
每当我尝试在没有任何条件的情况下回显一个简单的图像时(使用 PHP 下),我都可以毫无问题地这样做.所以,我能够从数据库中获取图像,问题在于条件 PHP.
您的代码的输出将 <img>
标签放入标签的 src 属性中.
根据定义,这在 HTML 中不起作用.如果其他一切都正确,这应该可以工作:
0 ) {$array = array_splice($array, 0, $limit);}返回 $array;}函数渲染图像(){全球 $stmt3;$输出 = '';如果 ($count = sqlsrv_num_rows($stmt3) > 0) {而 ($recentBadge = sqlsrv_fetch_array($stmt3)) {$result[] = $recentBadge;}如果 ($count > 3) {$result = get_random_elements(result, 3);}foreach ($result as $recentBadge) {$output .= $recentBadge['BadgeName'];$output .= '<img src="' . $recentBadge['BadgeImage'] . '" alt="">';$output .='
';}} 别的 {$output = '没有结果';}返回 $output;}?><span class="fa-stack fa-5x has-badge" ><div class="badgesize"><?php echo render_images();?>
</span>
提示:请尽量保持代码分离,逻辑与视图分离.
I have an HTML tag here :
<span class="fa-stack fa-5x has-badge" >
<div class="badgesize">
<img src="badgeImages/1.png" alt="" >
</div>
</span> -->
Query used to retrieve info and store it in a variable :
$q4 = "SELECT TOP 3 b.BadgeName, b.BadgeImage FROM BadgeImageTable AS b
INNER JOIN employee_badge AS e
ON e.badge_id = b.BadgeID
WHERE e.employee_id = 2164
ORDER BY e.earned_on DESC ";
$stmt3=sqlsrv_query($conn,$q4);
if($stmt3==false)
{
echo 'error to retrieve info !! <br/>';
die(print_r(sqlsrv_errors(),TRUE));
}
In the image tag I am trying to echo an the image of a badge with some condition.
<span class="fa-stack fa-5x has-badge" >
<div class="badgesize">
<img src="
<?php
if($count = sqlsrv_num_rows($stmt3) > 0){
while($recentBadge = sqlsrv_fetch_array($stmt3)){
$result[] = $recentBadge;
}
if($count > 3){
$result = array_rand($result, 3);
}
foreach($result as $recentBadge){
echo
$recentBadge['BadgeName'],
'<img src="'.$recentBadge['BadgeImage'].'">',
'<br>'
;
}
} else {
echo 'no results';
}
?>
" alt="" >
</div>
</span>
Condition of above PHP:
I want to echo any three random images if the count is greater than 3, else echo whatever '$recentBadge' gives as the result.
Problem: I am unable to echo according to the above condition.
Whenever I try to echo a simple image without any condition(using below PHP), I am able to do so without any issue. So, I am able to fetch the image from the DB, problem lies with the conditional PHP.
<img src="<?php echo "".($badgerecent['BadgeImage']).""; ?>" >
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.
这篇关于根据条件回波图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!