我在循环中获取产品..总共有四个循环。
<div class="content-page">
<div class="container">
<?php
$main_cats=mysql_query("SELECT * FROM TBLCATS inner join TBLCATDETAIL on TBLCATDETAIL.catid=TBLCATS.recid where TBLCATS.parent='0' and TBLCATS.Hidden='No' and TBLCATDETAIL.langid=$LangID ORDER BY TBLCATS.rank") or die("Error getting records For Main Categories");
////FIRST LOOP GETTING MAIN CATEGORIES/////
while($row_main_cat=mysql_fetch_array($main_cats)){
?>
<div class="category-featured">
<nav class="navbar nav-menu bgcolor show-brand">
<div class="container">
<div class="navbar-brand"><a href="#"><?php print $row_main_cat['catname'];?></a></div>
<span class="toggle-menu"></span>
<div class="collapse navbar-collapse">
<!-- tabing menu start -->
<ul class="nav navbar-nav">
<?php
$second_level=dbQuery("SELECT * FROM TBLCATS inner join TBLCATDETAIL on TBLCATDETAIL.catid=TBLCATS.recid where TBLCATS.parent='$row_main_cat[recid]' and TBLCATS.Hidden='No' and TBLCATDETAIL.langid='$LangID' ORDER BY TBLCATS.rank limit 4") or die("Error getting records For Sub Categories");
$counter_1=1;
////second LOOP GETTING SUB-CATEGORIES/////FOR TAB-HEADS
while($row_second_level=mysql_fetch_array($second_level)){?><li><a data-toggle="tab" href="#tab-k<?php print $row_second_level['recid'];?>-<?php print $counter_1;?>"><?php print $row_second_level['catname'];?></a></li><?php $counter_1++;}?>
</ul>
<!-- tabing menu close -->
</div>
</div>
</nav>
<div class="product-featured clearfix">
<div class="product-featured-content">
<div class="product-featured-list">
<div class="tab-container autoheight">
<!-- tab product START-->
<?php
$second_level_2=dbQuery("SELECT * FROM TBLCATS inner join TBLCATDETAIL on TBLCATDETAIL.catid=TBLCATS.recid where TBLCATS.parent='$row_main_cat[recid]' and TBLCATS.Hidden='No' and TBLCATDETAIL.langid='$LangID' ORDER BY TBLCATS.rank limit 4") or die("Error getting records For Sub Categories");
$counter_2=1;
////second LOOP GETTING SUB-CATEGORIES/////FOR TAB-PANEL
while($row_second_level_2=mysql_fetch_array($second_level_2)){?>
<!-- tab product -->
<div class="tab-panel" id="tab-k<?php print $row_second_level_2['recid'];?>-<?php print $counter_2;?>" dir="ltr">
<ul class="product-list owl-carousel" data-dots="false" data-loop="true" data-nav = "true" data-margin = "0" data-autoplayTimeout="1000" data-autoplayHoverPause = "true" data-responsive='{"0":{"items":1},"600":{"items":3},"1000":{"items":4}}'>
<?php
//GETTING ALL THE THIRD LEVEL CATEGORIES AND FETCH PRODUCST FROM ALL THIRD LEVEL CATEGORIES.
$third_level=dbQuery("SELECT * FROM TBLCATS inner join TBLCATDETAIL on TBLCATDETAIL.catid=TBLCATS.recid where TBLCATS.parent='$row_second_level_2[recid]' and TBLCATS.Hidden='No' and TBLCATDETAIL.langid='$LangID' order by TBLCATS.rank") or die("Error getting records");
$psqlp="";
/// HERE I GET PERFECT NUMBER OF THIRD LEVEL CATEGOIES
print mysql_num_rows($third_level);
while($row_third_level=mysql_fetch_array($third_level))
{
$psqlp.=" or TBLPRODS.catid='$row_third_level[recid]'";
}
//HERE IS A PRODUCT QUERY WHERE I PUT ALL THE THIRD LEVEL CATEGORIES ID BUTT IT FETCH ALL THE OTHER NON RELATED PRODUCTS
$kkacprod=dbQuery("SELECT * FROM TBLPRODS inner join TBLPDET where TBLPRODS.recid=TBLPDET.pid and TBLPRODS.Hidden='No' and TBLPRODS.langid='$LangID' $psqlp ORDER by rand() limit 20") or die("Error loading Products");while($krspffs=dbFetchArray($kkacprod)){?>
<!-- HERE I GET WRONG CAT ID... FOR EXAMPLE MAIN CATEGORY IS MOBILE AND SECOND CATEGORY IS SAMSUNG AND THIRD IS GALAXY BUT HERE I GET OTHER FOR EXAMPLE PRINTERS. -->
<li><?php print $krspffs['recid'];?></li>
<?php }?>
</ul>
</div>
<?php $kcnt2++;}?>
<!-- tab product END -->
</div>
</div>
</div>
</div>
</div>
<!-- end featured category jewelry-->
<?php }?>
</div>
</div>
&#13;
答案 0 :(得分:0)
问题在于:
$psqlp.=" or TBLPRODS.catid='$row_third_level[recid]'";
因为这构造了一个查询
SELECT ...
WHERE TBLPRODS.recid=TBLPDET.pid
AND ...
AND ...
OR TBLPRODS.catid = ...
OR TBLPRODS.catid = ...
这不是你想要的。要仅获取这些类别中的产品,请使用
WHERE ... AND TBLPRODS.catid IN (1,2,...)
这是
的另一种形式WHERE ... AND ( TBLPRODS.catid = 1 OR TBLPRODS.catid = 2 OR ... )
我已经冒昧地改变了您的代码,因此它更易于管理,我希望它能为您提供一些替代方法的提示。我已经考虑了一些function
,并使用了PDO,并使用另一种方法使用子查询查找该类别中的产品。查询底部。享受。
<div class="content-page">
<div class="container">
<?php
foreach ( get_categories( $db, $lang_id, 0 ) as $cat )
show_main_category( $db, $lang_id, $cat );
?>
</div>
</div>
<?php
function show_main_catgory( $db, $cat )
{
$nav = null;
$tab = null;
foreach ( get_categories( $db, $cat->recid, $lang_id, "LIMIT 4" ) as $i => $subcat )
{
$html_id = "tab-k{$subcat->recid}-$i";
$nav .= "<li><a data-toggle='tab' href='#$html_id'>$subcat->catname</a></li>";
$tab .= "
<div class='tab-panel' id='$html_id' dir='ltr'>
<ul class='product-list owl-carousel'>"
. implode( "\n",
array_map( function($prod) { return "<li>$p->recid</li>"; },
get_products_in_child_cats_of( $db, $subcat->recid, $lang_id )
) )
. "
</ul></div>";
}
echo <<<HTML
<div class="category-featured">
<nav class="navbar nav-menu bgcolor show-brand">
<div class="container">
<div class="navbar-brand"><a href="#">$cat->catname</a></div>
<span class="toggle-menu"></span>
<div class="collapse navbar-collapse">
<!-- tabing menu start -->
<ul class="nav navbar-nav">
$nav
</ul>
<!-- tabing menu close -->
</div>
</div>
</nav>
<div class="product-featured clearfix">
<div class="product-featured-content">
<div class="product-featured-list">
<div class="tab-container autoheight">
<!-- tab product START-->
$tab
<!-- tab product END -->
</div>
</div>
</div>
</div>
</div>
HTML;
}
function db_query( $db, $sql, $args = [] ) {
$sth = $db->prepare( $sql );
$sth->execute( $args );
return $sth->fetchAll( \PDO::FETCH_ASSOC );
}
function get_categories( $db, $parent_id, $lang_id, $limit = null )
{
return db_query( "
SELECT * FROM TBLCATS
INNER JOIN TBLCATDETAIL ON TBLCATDETAIL.catid=TBLCATS.recid
WHERE TBLCATS.parent = ?
AND TBLCATS.Hidden='No'
AND TBLCATDETAIL.langid = ?
ORDER BY TBLCATS.rank
$limit
",
[ $parent_id, $lang_id ]
);
}
function get_products_in_child_cats( $db, $parent_id, $lang_id )
{
return db_query( $db, "
SELECT * FROM TBLPRODS
INNER JOIN TBLPDET
WHERE TBLPRODS.recid=TBLPDET.pid
AND TBLPRODS.Hidden='No'
AND TBLPRODS.langid = ?
AND TBLPRODS.catid IN (
SELECT recid
FROM TBLCATS
WHERE parent = ?
)
ORDER by rand()
LIMIT 20
", [ $lang_id, $parent_id ]
);
}
注意:$db
应该初始化为
$db = new PDO( "mysql:dbname=$dbname", $dbuser, $dbpass, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
] );