如何制作图像标题数组

时间:2014-03-29 08:25:04

标签: php arrays input textbox gallery

我使用以下代码从MySQL数据库填充图像。我希望每个图像都有image_title,它将成为图像下方的文本框。我知道如何填充它但是如何命名,所以当用户更改标题时,它只会针对特定图像进行更改?

<?php
    $mysql_command = "CALL sp_populate_member_article_media(:param_article_guid, :param_member_id)";
    $mysql_query = $mysql_connection->prepare($mysql_command);
    $mysql_query->bindParam(':param_article_guid', $_GET["aid"], PDO::PARAM_STR);
    $mysql_query->bindParam(':param_member_id', $_SESSION["volow_member_id"], PDO::PARAM_STR);
    $mysql_query->execute();

    while($mysql_row = $mysql_query->fetch())
    {
        list($width, $height) = getimagesize($_SESSION["volow_absolute_path"] . "/gallery/members/" . $_SESSION["volow_member_guid"] . "/" . $_SESSION["volow_channel_guid"] . "/" . $_GET["aid"] . "/". $mysql_row['file_name'] . ".png");

        if ($width > 650) { $width = 650; };
        if ($height > 271) { $height = 271; };
?>
<tr><td align="right" valign="top"><img src="<?php echo $_SESSION["volow_domain_name"]; ?>gallery/members/<?php echo $_SESSION["volow_member_guid"]; ?>/<?php echo $_SESSION["volow_channel_guid"]; ?>/<?php echo $_GET["aid"]; ?>/<?php echo $mysql_row['file_name']; ?>.png" width="<?php echo $width; ?>" height="<?php echo $height; ?>" /></td></tr>
<tr><td><hr></td></tr>
<?php } ?>

1 个答案:

答案 0 :(得分:0)

您需要为每个图像的输入文件指定一个特定名称。为此目的,每个图像都需要一个唯一的标识符。

假设每个图像都有file_id字段,您可以按如下方式生成表单元素:

<input type="text" name="title<?=$mysql_row['file_id']?>" />

然后在处理表单提交时,您需要迭代$_POST数组以找出提交的图像ID:

foreach ($_POST as $name => $value) {
  if (substr($name, 0, 5) == 'title') {
     $id = (int) substr($name, 5);
     $title = $value;
     $sql = "UPDATE imagetable SET title = " . mysqli_real_escape_string($title) . " WHERE id = " . $id;
     ...
     // run sql
     ...
  }
}