gpt4 book ai didi

php - 真正的大菜鸟,如何使用 mysql 以便我可以轻松地编辑多个页面?

转载 作者:搜寻专家 更新时间:2023-10-30 21:44:54 25 4
gpt4 key购买 nike

所以现在,我的网站主页上有一个“画廊”系统。看一看:

<?php
$objConnect = mysql_connect("mydb.db","hello","mypass") or die(mysql_error());
$objDB = mysql_select_db("mydb");
$pic2 = "SELECT * FROM gallery";
if (!isset($_GET['Page'])) $_GET['Page']='0';
$pic1 = mysql_query($pic2);
$Num_Rows = mysql_num_rows($pic1);
$Per_Page = 16; // Per Page
$Page = $_GET["Page"];
if(!$_GET["Page"])
{$Page=1;}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{$Num_Pages =1;}
else if(($Num_Rows % $Per_Page)==0)
{$Num_Pages =($Num_Rows/$Per_Page) ;}
else
{$Num_Pages =($Num_Rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;}
$pic2 .=" order by GalleryID ASC LIMIT $Page_Start , $Per_Page";
$pic1 = mysql_query($pic2);
$cell = 0;
$link2 = "SELECT * FROM gallery";
$link1 = mysql_query($link2);
$link = mysql_fetch_array($link1);
$alt2 = "SELECT * FROM gallery";
$alt1 = mysql_query($alt2);
$alt = mysql_fetch_array($alt1);
echo '<div id="tablediv"><table border="0" cellpadding="17" cellspacing="0" class="table"><tr>';
while($pic = mysql_fetch_array($pic1))
{if($cell % 4 == 0) {
echo '</tr><tr>';}
if($cell == 2) {
echo '<td>reserved cell, ignore this</td>';
} elseif ($cell == 3) {
echo '<td>reserved cell, ignore this</td>';
} else {
echo '
<td><a href="/' . $link["link"] . '.php"><div class="image"><img src="https://s3.amazonaws.com/images/' . $pic["pic"] . '" alt="' . $alt["alt"] . ' /></div></a></td>'; }
$cell++;
}
echo '</tr></table></div>';
?>

我的 table 应该是这样的:

CREATE TABLE `images` (
`thumbnailID` int(11) NOT NULL auto_increment,
`link` varchar(100) NOT NULL,
`pic` varchar(100) NOT NULL,
`alt` varchar(100) NOT NULL,
PRIMARY KEY (`thumbnailID`)
) ENGINE=MyISAM ;

INSERT INTO `images` VALUES ('', 'stars/beezlebub', 'beezlebub', 'this is beezlebub');
INSERT INTO `images` VALUES ('', 'nature/raretree', 'raretree', 'this is a rare tree');
INSERT INTO `images` VALUES ('', 'nature/lions', 'lions', 'these are lions');
INSERT INTO `images` VALUES ('', 'nature/tigers', 'tigers', 'these are tigers');
etc. (you get the point)

总之……如您所见,有了这个系统,每当我插入一条新记录时,它都会自动更新我的画廊。现在我的问题是,当我插入一条新记录时,我该如何做到这一点,它不仅会影响我的主页画廊,还会影响我网站其他部分的画廊。不确定我的意思?这是一个例子:

假设我的站点名为 site.com。我还有 site.com 的子文件夹,其中包括 site.com/nature 和 site.com/stars。我希望我的 site.com/nature 包含一个仅包含明星照片的画廊,而我的 site.com/stars 是一个仅包含明星照片的画廊,而我的主页包含所有图像,包括自然和星星照片。但我不想通过创建额外的表来手动更新/nature 或/stars。相反,我只想使用一个巨大的表格,其中包含我网站上的所有图像,但要制作它以便我可以指定我是否希望我的记录也显示(/nature、/stars 等)而不仅仅是家(哪个包含所有图像)。

我想我需要另一列(显然)来指定我希望我的记录出现在哪些其他文件夹中,或者可能需要一些条件语句来确定我的记录应该出现在哪个子文件夹中,而不仅仅是我的主页。不幸的是,我是一个菜鸟,所以我想问是否有人可以伸出援手。谢谢!

最佳答案

在大多数情况下拥有多个表很重要,这样数据就不会重复,这称为规范化,在您的示例中您想要表:

  1. tblImages
  2. tbl画廊

  3. 图片

    • 缩略图 ID、链接、图片、alt、时间、图库 ID
  4. tbl画廊

    • 画廊ID,画廊名称

它们包含彼此的链接,这是键(tblGalleries 中的主键和 tblImages 的外键)。

我的偏好是在 MySQL 中创建一个“存储过程”或简单的“过程”,在 MySQL 中它看起来像这样:

CREATE PROCEDURE spImage_Update @link varchar(50), @picture varchar(50), @alt varchar(50), @galleryID int

AS BEGIN

SET NOCOUNT ON;

INSERT INTO tblImages VALUES (@link, @picture, @alt, @galleryID)

END

  • 通过设置图库 ID,您可以说出您希望它位于哪个图库。

当你想选择所有图像时,这样写你的查询...

SELECT * FROM tblImages

当您想要一个特定的图库时,只需将您的查询作为...

'SELECT * FROM tblImages WHERE GalleryID = 1' or 'SELECT * FROM tblImages WHERE GalleryID = @GalleryID'

  • 在 SP 中。

你到底卡在哪里了。

关于php - 真正的大菜鸟,如何使用 mysql 以便我可以轻松地编辑多个页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5439337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com