在项目中,我目前正在工作。用户可以在网站的不同部分上传图片。他们可以在他们发布的帖子中上传图片,也可以上传图片来炫耀他们的宠物(已经有一个个人资料页面)。所以我想出了一个图像表,看起来像这样:
TABLE `images` (
`image_id` INT(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` INT(11) unsigned NOT NULL COMMENT 'FOREIGN KEY referencing the user_id of the user, who uploaded the images',
`image_name` VARCHAR(64) NOT NULL,
`dog_pet_id` INT(11) NULL COMMENT 'FOREIGN KEY referencing the pet's, who this image belongs to',
`post_id` INT(11) NULL COMMENT 'FOREIGN KEY referencing the post's, who this image belongs to',
PRIMARY KEY (`image_id`),
FOREIGN KEY (`user_id`) REFERENCES users(user_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
因此,如果user_id = 1 的用户将有一只dog_pet_id = 2 的宠物,并且他将为该宠物上传图片,则输出将如下所示:
+------------------------------------------------------------+
| | image_id | user_id | image_name | dog_pet_id | post_id | |
| | 1 | 1 | bla | 2 | 0 | |
+------------------------------------------------------------+
如果他在帖子中上传图片,则dog_pet_id将为 0,post_id将具有帖子的 id,依此类推。
所有这些都按预期工作。我的问题是显示这些图像!
我试图解决问题(在MVC结构中(没有CMS或框架)):
我想只显示那些user_id等于用户user_id且dog_pet_id等于宠物 id 的图像。我无法解决的问题是如何让宠物的dog_pet_id从视图到模型。
通常我使用表单提交或某种链接来做这样的事情,我可以用它们发送 id,但这是我第一次不能这样做。
因此,我的想法是通过 $_SESSION 超级全局发送它,但是,这总是在我的 foreach 循环中被覆盖。因此,如果每个用户有多个宠物,则只会播放最后一只宠物的图像。
模型
/**
* Display the pet images, which have been uploaded by the user
*/
public static function displayPetImages()
{
$database = DatabaseFactory::getFactory()->getConnection();
$query = $database->prepare("SELECT * FROM images WHERE user_id = :user_id AND dog_pet_id = :dog_pet_id");
$query->execute(array(':user_id' => $_SESSION['user_id'], ':dog_pet_id' => $_SESSION['dog_pet_id']));
return $query->fetchAll();
}
控制器
/**
* Show user's PRIVATE profile
*/
public function showProfile()
{
'pets' => PetModel::getPet(), // get's the pet's information from the database
'petImages' => PetModel::displayPetImages() // is the function shown above
));
}
景观
foreach ($this->pets as $pet) {
foreach ($this->petImages as $petImage) { ?>
$_SESSION['dog_pet_id'] = $pet->dog_pet_id; <----- This is always overwritten!
<img src="<?= /path/to/the/image/ . $pet->dog_pet_id . '/' . $petImage->image_name . '.jpg' ?>" />
}
echo "<form action='" . Config::get('URL') . 'pet/uploadPetImages_action/' . $pet->dog_pet_id . "' method='post' enctype='multipart/form-data'>";
echo "<input type='file' name='images[]' multiple required />";
echo "<input type='hidden' name='MAX_FILE_SIZE' value='5242880' />";
echo "<input type='submit' value='Upload Images' />";
echo "</form>
}
我什至不确定,我想要实现的目标是否可能,或者是否有更好的方法来做到这一点。
无论如何,我将非常感谢任何形式的帮助!
更新
模型
/**
* Display the pet images, which have been uploaded by the user
*/
public static function displayPetImages($dog_pet_id)
{
$database = DatabaseFactory::getFactory()->getConnection();
$query = $database->prepare("SELECT image_name FROM images WHERE user_id = :user_id AND dog_pet_id = :dog_pet_id");
$query->execute(array(':user_id' => Session::get('user_id'), ':dog_pet_id' => $dog_pet_id));
return $query->fetchAll();
}
控制器
/**
* Show user's PRIVATE profile
*/
public function showProfile()
{
'pets' => PetModel::getPet(), // get's the pet's information from the database
'petImages' => PetModel::displayPetImages() // is the function shown above
));
}
景观
foreach ($this->pets as $pet) { ?>
<?php foreach ($this->petImages as $petImage) { ?>
<img src="<?= Config::get('URL') . Config::get('PATH_IMAGES_PUBLIC') . Session::get('user_id') . '/' . 'pets/' . $pet->dog_pet_id . '/' . displayPetImages($pet->dog_pet_id) . '.jpg' ?>" />;
}
}
输出
警告:缺少 PetModel 的参数 1::d isplayPetImages()
注意:未定义的变量:dog_pet_id
/**
* Display the pet images, which have been uploaded by the user
*/
public static function displayPetImages($dog_pet_id)
{
$database = DatabaseFactory::getFactory()->getConnection();
$query = $database->prepare("SELECT * FROM images WHERE user_id = :user_id AND dog_pet_id = :dog_pet_id");
$query->execute(array(':user_id' => $_SESSION['user_id'], ':dog_pet_id' => $dog_pet_id));
// return only image name
}
将宠物 ID 传递给函数并在查询中使用它。
同时更改函数的输出。 它应该只返回图像名称。
并像这样直接调用此函数
foreach ($this->pets as $pet) {
foreach ($this->petImages as $petImage) { ?>
//$_SESSION['dog_pet_id'] = $pet->dog_pet_id; <----- This is always overwritten!
<img src="<?= /path/to/the/image/ . $pet->dog_pet_id . '/' . displayPetImages($pet->dog_pet_id) ?>" />
}
}
由于您指定的原因,该会话操作不起作用:当您的视图运行该循环时,它会覆盖变量。
我会为每只宠物提供一个单独的 HTML 表单,以及一个单独的上传按钮;
<?php foreach ($this->pets as $pet) { ?>
<form action="/wherever" method="POST">
<input type="hidden" name="pet_id" value="<?= $pet->pet_id ?>" />
<?php foreach ($this->petImages as $petImage) { ?>
<img src="<?= /path/to/the/image/.$pet->dog_pet_id.'/'.$petImage->image_name.'.jpg' ?>" />
<?php } ?>
<input type="submit" value="Save" />
</form>
<?php } ?>
然后,根据他们单击的"保存"按钮,它将具有不同的pet_id
值供您比较。
这看起来应该适合您提交所有宠物 ID 的数组。
foreach( $this->pets as $pet ):
echo "<form action='" . Config::get('URL') . 'pet/uploadPetImages_action/' . $pet->dog_pet_id . "' method='post' enctype='multipart/form-data'>";
?>
<?php foreach( $this->petImages as $petImage ): ?>
<input type="hidden" name="pets[<?php echo $pet->dog_pet_id; ?>]" value="pets[<?php echo $pet->dog_pet_id; ?>]" />
<img src="<?php echo '/path/to/the/image/' . $pet->dog_pet_id . '/' . $petImage->image_name . '.jpg'; ?>" />
<?php endforeach; ?>
echo "<input type='file' name='images[]' multiple required />";
echo "<input type='hidden' name='MAX_FILE_SIZE' value='5242880' />";
echo "<input type='submit' value='Upload Images' />";
echo "</form>";
<?php endforeach; ?>