2013-09-30 89 views
0

有一個forech搜索,我只想得到結果,其中$ postType < 2。我如何修改我的代碼?php foreach只顯示一個x結果的結果在我的一行中

<?php 
if ($resultSearch){ 
foreach ($resultSearch as $row){ 
$idPost=$row['idPost']; 
$postType=$row['type']; 
$postTypeName=getTypeName($postType); 
$postTitle=$row['title']; 
$category=$row['category'];//real category name 
$fcategory=$row['fcategory'];//frienfly name category 
$idCategoryParent=$row['idCategoryParent']; 
$fCategoryParent=$row['parent']; 
$postImage=$row['image']; 
$insertDate=setDate($row['insertDate']); 
if ($row["hasImages"]==1){ 
$postImage=getPostImages($idPost,$insertDate,true,true); 
} 
else $postImage=getPostImages(true,true,true,true);//there's no image 
$postUrl=itemURL($idPost,$fcategory,$postTypeName,$postTitle,$fCategoryParent);?> 

回答

0
foreach ($resultSearch as $row) if ($row['type']<2) { 
1

你的意思是這樣的:

if ($resultSearch){ 
foreach ($resultSearch as $row){ 
$idPost=$row['idPost']; 
$postType=$row['type']; 
if($postType<2) 
{ 
    //do what you want.. 
} 
.... 
1
<?php 
if ($resultSearch) 
{ 
    foreach ($resultSearch as $row) 
    { 
     $idPost=$row['idPost']; 
     $postType=$row['type']; 
     $postTypeName=getTypeName($postType); 
     $postTitle=$row['title']; 
     $category=$row['category'];//real category name 
     $fcategory=$row['fcategory'];//frienfly name category 
     $idCategoryParent=$row['idCategoryParent']; 
     $fCategoryParent=$row['parent']; 
     $postImage=$row['image']; 
     $insertDate=setDate($row['insertDate']); 
     if ($postType < 2) 
     { 
     if ($row["hasImages"]==1) 
     { 
      $postImage=getPostImages($idPost,$insertDate,true,true); 
     } 
     else 
     { 
      $postImage=getPostImages(true,true,true,true);//there's no image 
     } 
     $postUrl=itemURL($idPost,$fcategory,$postTypeName,$postTitle,$fCategoryParent); 
     } 
    } 
} 
?>