2012-10-12 15 views
0

基本上我有一个查询称为使用PHP:选择一个由查询PHP中称为数组值

<?php $result = mssql_query("SELECT * FROM Colours WHERE Type = 'type1' "); 


while ($row = mssql_fetch_array($result)) { 

if ($row['ColourID'] == "1") { 

    $sayclass1="imgactive"; 

    }else{ 

    $sayclass1="imginactive"; } 


      ?> 

正如你所看到的,一旦我执行查询我然后循环它的问题是,它返回一个数组,现在在某些情况下,我需要使用完整的数组,但是我希望能够从if语句等中选择一个条目。例如:

<img id="h" src="<?php echo $row['thumbimg']; ?> 

现在thumbimg是我的数据库中的一列,它只是一个URL。然而,由于它的数组图片不显示,因为它回显所有的值,所以而不是images/image1.png例如它回显images/image1.png images/image2.png images/image3 .png等etc ...

我希望这是有道理的,任何人都可以告诉我如何操纵查询/代码轻微仍然返回所有条目,但从数组中选择某些值吗?

+0

您需要选择提取的行之一,并使用在使用img标签为每个在您使用$行说['thumbimg “];请检查这个链接:http://php.net/manual/en/function.mysql-fetch-row.php – Rob

+3

顺便说一句,你有一个错字'if($ row ['ColourID'] =“1”)''。 '='应该是'=='。 – Leri

+0

无法解码您的问题:( –

回答

0
<?php $result = mssql_query("SELECT * FROM Colours WHERE Type = 'type1' "); 


while ($row = mssql_fetch_array($result)) { 

    if ($row['ColourID'] == "1") { 

     $sayclass1="imgactive"; 

    }else{ 

     $sayclass1="imginactive"; 
    } 
echo '<img id="h" src="'.$row['thumbimg'].'">'; 
} 

?> 

使用上面的代码,您将从数据库中获得每行一个图像。您复制粘贴的代码部分不足以确定您的错误的位置,以及为什么$row['thumbimg']包含连接的结果值。

1

你需要的,如果你想显示所有图像

foreach($row[thumbimg] as $img): 
<img id="h" src="<?php echo $img; ?> 
end foreach;