基于mysql行结果显示不同的图像

qc6wkl3g  于 2021-06-24  发布在  Mysql
关注(0)|答案(4)|浏览(325)

对不起我的英语。我正在学习php,我尝试创建一个小的成员区,我认为这是一个很好的学习方法。在我的会员区,有些会员是“已验证”的,有些则不是。
如何根据数据显示不同的图片 stored in Mysql 使用php?我想展示的是 "Picture1" if "1" is the value stored in a MySQL column and "Picture2" if "2" 是价值等。。。你是“未验证成员”,所以你看到图片1,验证成员看到图片2。。。
我知道怎么做 SELECT data using MySQLi 但我找不到下一步要做什么?
谢谢您。

66bbxpm5

66bbxpm51#

试试这个:

<img src="<?php echo "/pictures/Picture{$number}.jpg"; ?>" />
jc3wubiy

jc3wubiy2#

这取决于图像如何Map到数据库中的值,但假设您有以下简单关系: 1 :
/images/1.jpg 2 : /images/2.jpg 只需从数据库中获取数据并将其Map到变量(在我的示例中是 $image ),然后将其与 echo 语句中的语句 <img src> 属性:

<img src="<?php echo '/images/' $image . '.jpg'; ?>"/>

这将输出:

<img src="/images/1.jpg"/>
<img src="/images/2.jpg"/>

基于选定的图像。
现在您只需根据条件将该变量设置为正确的值:

<?php

if ($authenticated) {
  $image = 1;
}
else {
  $image = 2;
}

?>

<img src="<?php echo '/images/' $image . '.jpg'; ?>"/>

或者更简洁地用三元组:

<?php ($authenticated ? $image = 1 : $image = 2) ?>
<img src="<?php echo '/images/' $image . '.jpg'; ?>"/>
toiithl6

toiithl63#

假设您有一个名为db的数据库和一个名为tb的表,其中列为name和verify。name为string,verify为boolean。如果用户已验证,则还验证存储的1,如果用户未验证,则验证存储的0。
因此,您可以通过迭代语句(使用if-else或switch语句)来完成

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$query="SELECT * FROM `tb`";
$sql=$conn->query($query);

if($sql->num_rows>0)
{
while($row=$sql->fetch_assoc())
{
//display name
echo $row['name'];

//check if user is verified or not

//using if else statement
if($row['verify']=="0")
{
echo '<img src="./picture1.jpg">';
}
else
{
echo '<img src="./picture2.jpg">';
}

//using switch case 
switch($row['verify'])
{
case(0):
{
echo '<img src="./picture1.jpg">';
}
case(1);
{
echo '<img src="./picture2.jpg">';
}
}

//remember i have used both methods hence it will show the image twice , i did it just you possible ways , you choose any one from it.(if else or switch case)
}
}

$conn->close();

?>
yqkkidmi

yqkkidmi4#

有几种方法。如果它总是遵循数字系统。。。使用此选项:

$picture = "Picture" . $verified; 
// example: $picture = "Picture1", if $verified = 1

有很多连接的方法。。。

$pic = "Picture$verified.png"; 
$picture = "Picture" . $verified . ".jpeg";

这将取决于你如何设置它。。

switch($verified){
 case 1: 
  $picture = "Picture1";
  break;
 case 2: 
  $picture = "Picture2"; 
  break; 
 case default:
  $picture = "Picture1"; 
  break;
}

另一种方式。。。

if($verified == 1){ $picture = "Picture1"; } else { $picture = "Picture2"; }

switch语句或此if/else语句的原因是,它可以解释verified等于0、null或其他非1或2的值,这些值可能由于任何原因存储在数据库中。在第一个示例中,如果$verified(verified column=to 1或2)的值为奇数,则图片#实际上可能不是真的。
当然,这只是为了处理不同的方法。你必须确保它与你的真实形象吻合。。。

$picture = "Picture1"; // would be dynamic to code above
$ext = ".png"; // optional, could be added into the $picture variable above easily too 
$filePath = "/images/user_uploads/"; // make sure path is correct
$filePathName = $filePath . $picture . $ext;
// see? We combine path/pictureName/extension to create an image URL
if(file_exists($filePathName) !== false){ // make sure exists 
 $imageCode = "<img src='$filePathName' alt='Not Found'>"; 
} else {
 // file does not exist! ut oh! 
}

相关问题