所以现在,我在我的网站主页上有一个“画廊”系统。看一看:
<?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>';
?>我的表应该是这样的:
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/site.com和site.com/stars。我希望我的site.com/nature包含一个仅用于明星照片的图库,而我的site.com/stars则是一个仅用于明星照片的图库,而我的主页包含所有图像,包括自然和明星照片。但我不想通过创建额外的表来手动更新/nature或/stars。相反,我只想使用一个巨大的表,其中包含我网站上的所有图像,但这样我就可以指定我的记录是否也显示(/nature、/stars等),而不仅仅是主页(它包含所有图像)。
我假设我需要另一个列(显然)来指定我希望我的记录出现在哪些其他文件夹中,或者可能需要一些条件语句来确定我的记录也应该出现在哪个子文件夹中,而不仅仅是我的主页。不幸的是,我是一个无名小卒,所以我在问是否有人能帮上忙。谢谢!
发布于 2011-03-26 08:09:20
是的,你是对的。快速、简单的方法是创建另一个列。
但是,您真正应该做的是创建另一个表,名为sections或类似的表。它应该看起来像这样:
----------------
| id | name |
----------------
| 1 | stars |
| 2 | nature |
...放入主表中的新列将指向该表的id列。
------------------------------------------------------------------
| thumbnailID | link | pic | alt | sectionID |
------------------------------------------------------------------
| 1 | ... | raretree | this is ... | 2 |
...最后一列sectionID引用了表sections中的第二项。
接下来,创建一个名为/section/的文件夹,并在其中创建一个名为section.php的新页面。假设您想要查看如下所示的部分:
http://example.com/section/section.php?section=nature
您可以使用$_GET['section']获取该部分,并执行如下的SQL JOIN:
SELECT * FROM `sections` JOIN `images` ON `images`.`sectionID` = `sections`.`id` WHERE `sections`.`name` = {section}将{section}的值替换为$_GET['section']
您甚至可以更改它,以便像这样访问部分:
http://example.com/section/nature
和mod_rewrite在一起。
http://www.snipe.net/2009/02/practical-mod_rewrite/
http://en.wikipedia.org/wiki/Cardinality_(data_modeling
http://www.webdesign.org/web-programming/php/mysql-join-tutorial.14876.html
https://stackoverflow.com/questions/5439337
复制相似问题