否则/如果比较问题


Else/If compare issue

我有一个金发的时刻,这是我的问题:

㧪:

我有一个包含 5 张图像的编辑页面,当您单击图像时,它会消失并出现一个上传新图像框。我现在正在用头撞墙更新图像代码。我需要一些关于如何检查图像是否已更改或是否已选择删除图像以及删除该图像的帮助。

所以它就像这样 现有图像> 单击以更改或复选框删除,如果更改则更新新图像并发布回显说图像已更新,如果图像未更改,则不执行任何操作或执行某些操作(如果未更改并且已删除)已被选中。

下面的代码块将为每个图像复制:

$image_new  = "test_new_image"; // New file name
$image_hidden  = "test_db_image"; // This is the existing image name in a hidden input
$image_db = "test_db_image"; // Image name in the DB
if ($image_hidden == $image_db) {
    echo "leave image as is in the db<br>";
} elseif ($image_new != $image_db) {
    echo "change image in the db and delete existing image<br>";
} else {
    echo "New image!<br>";
}

或者,如果有人有一些更好的代码来做我想做的事情会更好。

//check if variables are empty, else set them null
$image_new  = (!empty($image_new)) ? $image_new : null;
$image_db  = (!empty($image_db)) ? $image_db : null;

如果您此时知道数据库中的image_name,隐藏字段值是否相同?在这种情况下,比较old_name和像这样的new_name就足够了:

//image_new is the same as image_db
if( $image_new === $image_db){
  //leave image as is in the db
}
//there is a new image and it's not the same as image_db
elseif( $image_new !== null && $image_new !== $image_db ){
  //change image in the db and delete existing image (image_db)
}
//there is a new image, and there is nothing in db, yet
elseif( $image_new !== null && $image_db === null ){
  //new image
}
//anything else
else{
  //no image
}