多个SELECT查询在同一页面上显示多个表中的数据

时间:2012-11-29 16:22:51

标签: php mysql select drop-down-menu

我决定创建两个单独的表 - 评论和功能 - 来保存不同类型的数据,每个表都使用连接到同一个父表。

我决定使用此方法,因为Features表例如不需要Review表使用的几行,例如Rating,Cover Art等,但我无法弄清楚如何阻止它们显示当我想要展示一个特征时。

现在我想我应该只从一个表中拉出来并使用ifs和elseifs等来改变数据的显示方式,因为当我在导航部分创建一个下拉菜单时,人们可以看到所有索引页面来自评论表的音乐,硬件,软件,书评等等以及来自功能表的所有访谈,新闻,意见等的索引我遇到了比使用我希望可以修复的更大的编码问题和if和其他人一样,虽然我无法理解这些是如何工作的。

以下是我目前为止的两个查询,下面是每个类型在页面上显示的示例。

mysql_select_db($database_em, $em);
$query_getReview =
"SELECT 
reviews.title,
reviews.cover_art,
reviews.blog_entry,
reviews.rating,
reviews.published,
reviews.updated,
artists.artists_name,
contributors.contributors_name,
contributors.contributors_photo,
contributors.contributors_popup,
categories_name
FROM
reviews
JOIN artists ON artists.id = reviews.artistid
JOIN contributors ON contributors.id = reviews.contributorid
JOIN categories ON categories.id = reviews.categoryid
ORDER BY reviews.updated DESC LIMIT 3";
$getReview = mysql_query($query_getReview, $em) or die(mysql_error());
$row_getReview = mysql_fetch_assoc($getReview);
$totalRows_getReview = mysql_num_rows($getReview);

mysql_select_db($database_em, $em);
$query_getFeature =
"SELECT 
features.features_words,
features.published,
features.updated,
artists.artists_name,
contributors.contributors_name,
contributors.contributors_photo,
contributors.contributors_popup,
categories_name
FROM
features
JOIN artists ON artists.id = features.artistid
JOIN contributors ON contributors.id = features.contributorid
JOIN categories ON categories.id = features.categoryid
ORDER BY features.updated DESC LIMIT 1";
$getFeature= mysql_query($query_getFeature, $em) or die(mysql_error());
$row_getFeature = mysql_fetch_assoc($getFeature);
$totalRows_getFeature = mysql_num_rows($getFeature);

以下是我在页面上展示的示例:

<?php do { ?>
<table id="center_table" width="650" border="0" cellpadding="0" cellspacing="0">
<tr>
    <td width="650" height="30" colspan="3" bgcolor="#FFFFFF"><a class="category" href="/" target="_top"><?php echo $row_getReview['categories_name']; ?></span></td><!-- category -->
</tr>
Etc.
</table>
<?php } while ($row_getReview = mysql_fetch_assoc($getReview)); ?>


<?php do { ?>
<table id="center_table" width="650" border="0" cellpadding="0" cellspacing="0">
<tr>
    <td width="650" height="30" colspan="3" bgcolor="#FFFFFF"><a       class="category" href="/" target="_top"><?php echo $row_getFeature['categories_name']; ?></span></td><!-- category -->
</tr>
<tr>
    <td  width="60" height="60" rowspan="2" bgcolor="#FFFFFF">&nbsp;</td><!-- product photo -->
    <td width="590" height="30" colspan="2" bgcolor="#FFFFFF"><span class="artist"><?php echo $row_getFeature['artists_name']; ?></span><span class="title">Interview</td><!-- artist & title -->
</tr>

</table>
<?php } while ($row_getFeature = mysql_fetch_assoc($getFeature)); ?>

毋庸置疑,这个解决方案似乎是错误的路线,但至少可行。

0 个答案:

没有答案
相关问题