0

我有两个导航,一个在标题中,另一个在同一页面中,现在我想要做的是当我单击子类别时能够从表产品中提取所有数据从子类别中的特定产品. 我现在所拥有的是,当我单击标题页上的菜单时,该菜单包含在每个页面中,我列出了产品,但产品页面上的菜单会产生一个错误,即 foreach 为空。当我使用其他功能提取所有子类别时,它工作正常,但我只想提取属于该特定主类别的子类别。下面的代码会更有意义。

页眉:

<?php 
error_reporting(E_ALL);
ini_set('display_errors','1');?>
<?php require_once("initi.php");?>
<html>
<head>
<title></title>
 <link rel="stylesheet" href="css/reset.css" />
 <link rel="stylesheet" href="css/main.css" />
 <link rel="stylesheet" href="css/footer.css" />
<script type="text/javascript" src="/ghaniya.com/javascript/jqu.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".logbtn").click(function(){ 
      $("#login2").hide("show");
      $("#login1").slideToggle("slow");
      $(this).toggleClass("active");

});

$(".logbtn2").click(function(){ 
      $("#login1").hide("show");
      $("#login2").slideToggle("slow");
      $(this).toggleClass("active");      
});
});
</script>
<style type="text/css">

div#login1{
background-color:#efebee;
margin:0 auto;
width:100%;
display:none;
height:100px;
}

div#login2{
background-color:#f5dce4;
margin:0 auto;
width:100%;
display:none;
height:100px;
}
.size{width:960px; }
.flotl{float:left; font-size:11px; text-decoration:none; height:30px; margin-left:20px;}
div#centerl{ width:960px; margin:0 auto; }
</style>
</head>
<body>
<div id="head">

<div class="size center">
<img src="img/logosmall.png">
<ul id="mainLink">
<li><a class="logbtn">Women</a></li><li><a class="logbtn2" href="#">man</a></li><li><a href="#">Beauty</a></li><li><a href="">Gifts</a></li><li><a href="">Perfumes</a></li>
</ul>

<div id="loginBox">
<h3>REGISTER | SIGN IN</h3>
</div>

<div id="search">
<input type="submit"/><input type="text" value="Search"/>
</div>
<div id="bag"></div>
</div>
</div>


<div id="login1" >
<div id="centerl">
<ul class="flotl" >
<?php
$cata = Catagory::find_all(); 
foreach($cata as $catag){?>
      <li><a href="/ghaniya.com/product.php?catid=<?php echo $catag->catagory_id;?>"><?php echo $catag->name; ?></a></li>
<?php }?>
</ul> 
</div>
</div>

产品页面:

<?php require_once("includes/head.php"); ?>
<?php
  $cata_id = "";  
  $subcat ="";
  if(isset($_GET['catid']))
    {
         $cata_id = $_GET['catid'];
         $product = Product::find_by_cata_id($cata_id);
    }
    elseif(isset($_GET['subcat']))
    {
    $subcat =$_GET['subcat'];
    $subcat = SubCata::find_by_cata_id(1);
    print_r($subcat);
    }
?>
<div class="cBoth"></div>
<div id="contentEveryWhere" class="center">
<div id="cata">
<div id="leftNoticeBoard"></div>
<ul id="catagories">
<h1>CLOTHING</h1>
<?php foreach($subcat as $sub){ ?>
<li><a href="product.php?subcat=<?php echo $sub->subcata_id; ?>"><?php echo $sub->name; ?></a></li>
<?php } ?>
</ul>
</div>
<div id="products">
<div id="catatitle"> 
<ul>
<li>Home ></li><li>Women ></li><li>Coat<li>
</ul>
<h1>Coat</h1>
<p class="floatl">Sort items by Price: High | Low View as: <img src="#" alt="icon png"/> |<img src="#" alt="icon png"/></p>
</div>
<ul id="product">
<?php $product = Product::find_by_cata_id($cata_id);?>
<?php foreach($product as $pro){?>
<?php $image = Image::find_by_product_id( $pro->product_id);?>
          <li>
               <?php if(isset($image->filename)){ ?>
               <img src="/ghaniya.com/img/products/<?php echo $image->filename;?>"/>
               <?php } ?>

              <h2><?php echo $pro->name;?></h2>
              <h3> <?php echo $pro->product_desc;?></h3>
              <h4><?php echo $pro->price;?></h4>
          </li>
<?php } ?>
</ul><!--end of product list-->
</div> <!--end of products div-->


</div><!--end of contentEveryWhere div-->
<div class="cBoth"></div>
<?php require_once("includes/footer.php"); ?>

警告:在第 25 行的 /Users/mughery/Website/ghaniya.com/product.php 中为 foreach() 提供的参数无效

4

3 回答 3

0

使用 foreach 时,第一个参数必须是数组。

您可以将代码更改为:

<?php
   if( is_array( $subcat ) )
     foreach($subcat as $sub){ 
?>
于 2012-09-04T11:01:12.730 回答
0

4行:

<?php
 $subcat = array();
?>
于 2012-09-04T11:02:40.810 回答
0

问题很简单:

if(isset($_GET['catid']))
{
    $cata_id = $_GET['catid'];
    $product = Product::find_by_cata_id($cata_id);
}
elseif(isset($_GET['subcat']))
{
    $subcat =$_GET['subcat'];
    $subcat = SubCata::find_by_cata_id(1);
}

这意味着您仅设置$subcatif 且仅当$_GET['catid']未设置 $_GET['subcat']is 时。在所有其他情况下$subcat是一个空字符串,而不是一个数组。只需添加如下一行:

$subcat = (is_array($subcat) ? $subcat : SubCata::find_by_cata_id(1));
//or: 
$subcat = (is_array($subcat) ? $subcat : array());

你会很高兴的

另外,这个:

$subcat =$_GET['subcat'];
$subcat = SubCata::find_by_cata_id(1);

对我来说真的没有意义,我认为你需要类似的东西:

elseif(isset($_GET['subcat']))
{
    $subcat =SubCata::find_by_cata_id($_GET['subcat']);
}
$subcat = (is_array($subcat) ? $subcat : SubCata::find_by_cata_id(1));

如果没有,无论 subcat 参数的实际值如何,您都将获取 id 1 的所有数据...

于 2012-09-04T11:03:17.907 回答