在项目中,我正在努力。用户可以在网站的不同部分上传图片。他们可以在他们发布的帖子中上传图片,也可以上传图片以展示他们的宠物(已经有个人资料页面)。所以我想出了一张图像表,如下所示:
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等于pet的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 :: displayPetImages()的参数1 注意:未定义的变量:dog_pet_id
答案 0 :(得分:2)
/**
* 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
}
将pet 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) ?>" />
}
}
答案 1 :(得分:1)
由于您指定的原因,该会话事项无法正常工作:当您的视图运行该循环时,它会覆盖该变量。
我为每只宠物提供单独的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
值供您比较。
答案 2 :(得分:0)
在提交所有宠物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; ?>