我有两张桌子:
courses_cat
(这个有"标题"字段)courses
(这个有一个"父"字段,我想用到" group",这个" parent"是相等的到"标题" 所以我需要以图形方式""表1中表2的内容。
这是我的代码(两个表分开):
<?php
$query = $db->prepare('SELECT title, body, showeb, datapost FROM courses_cat WHERE showeb="si"');
$query->execute();
$query->store_result();
$query->bind_result($title, $body, $showeb, $datapost);
while ($query->fetch())
echo " <div class='pblock'><h2>$title</h2><pre>$body</pre></div> ";
?>
<?php
$query2 = $db->prepare('SELECT parent, title, body, showeb, datapost FROM courses WHERE showeb="si"');
$query2->execute();
$query2->store_result();
$query2->bind_result($parent, $title, $body, $showeb, $datapost);
while ($query2->fetch())
echo " <div id='courseblock'><a href='#'><span class='top'>$title</span><span class='content'>$shortbody</span></a></div>";
?>
答案 0 :(得分:2)
我不确定我100%理解最终结果应该是什么,但我相信你可以将你的两个查询合并为一个;正确地循环这可能会解决您的问题:
SELECT cat.title as CatTitle, cat.body as CatBody, courses.title as CourseTitle, courses.body as CourseBody -- and any other fields you need like that
FROM courses_cat cat, courses
WHERE cat.parent = courses.title
GROUP BY cat.title
答案 1 :(得分:0)
我尝试使用Digital Chris发布的代码,它的工作原理(部分),为了更好地解释,下面的代码(见下面编辑过的代码)“分组确定类别并把项目放在里面”但我需要得到更多的一个课程(div“courseblock”)到每个类别(div“pblock”),这个代码只打印一个...(我认为它“打破了循环”,可能需要将“课程”的数据加入到他们的“类别“)
在这个链接可以看到我的意思的图形示例:
http://liberartestudio.com/download/aaa.jpg
(类别块是那些白人,课程是红色的)
这里是页面的链接:
http://lvdsys.liberartestudio.com/services_courses.php
<?php
$query = $db->prepare('
SELECT courses_cat.title AS CatTitle, courses_cat.body AS CatBody, courses_cat.datapost AS CatData, courses.parent, courses.title AS CourseTitle, courses.shortbody AS CourseShortbody, courses.datapost AS CourseData
FROM courses_cat, courses
WHERE courses_cat.title = courses.parent
GROUP BY parent
ORDER BY CatData AND CourseData
');
$query->execute();
$query->store_result();
$query->bind_result($CatTitle, $CatBody, $CatData, $parent, $CourseTitle, $CourseShortbody, $CourseData);
while ($query->fetch())
echo "
<div class='pblock'>
<h2>$CatTitle</h2><pre>$CatBody</pre>
<div id='courseblock'><a href='#'><span class='top'>$CourseTitle</span><span class='content'>$CourseShortbody</span></a>
</div>
</div>
"; ?>
答案 2 :(得分:0)
这里是我需要的工作代码(简单版本)...
<?php
$cat = mysqli_query($db,"SELECT title AS catTitle, body AS catBody FROM courses_cat");
$result = mysqli_query($db,"SELECT parent, title, shortbody FROM courses");
//array x categories
$category = array(); while ($row = mysqli_fetch_assoc($cat)) {$category[] = $row;}
//array x courses
$course = array(); while ($row = mysqli_fetch_assoc($result)) {$course[] = $row;}
// here my divs
$cat_div="";
foreach ($category as $row) {
$cat_div = $row['catTitle'];
echo "<div class='pblock'>";
echo "<h2>".$row['catTitle']."</h2><pre>".$row['catBody']."</pre>";
foreach ($course as $row) {
if ($cat_div == $row['parent']) {
echo "<div id='courseblock'><a href='#'><span class='top'>".$row['title']."</span><span> class='content'>".$row['shortbody']."</span></a></div>";}}
echo "</div>";}
$cat->free_result();
$result->free_result();
mysqli_close($db);
?>