2015-11-14 117 views
-2

我試圖通過一個對象運行。我在初始階段遇到問題,只能在第一次迭代中運行。所以一切都很好,但是我只能在他們是3時進行迭代。任何想法爲什麼會發生這種情況?對於每個只返回第一個迭代

<?php 
global $wpdb; 
/*Begin*/ 
$itemsTable = $wpdb->prefix . "HFW_portfolio_items"; 
$catTable = $wpdb->prefix . "HFW_portfolio_categories"; 
$tagTable = $wpdb->prefix . "HFW_portfolio_tags"; 
$rowsItemA = $wpdb->get_results("SELECT * FROM"." $itemsTable"."", ARRAY_A); 
exit(var_dump($rowsItemA)); 
$numItems = count($rowsItem); 
$i = 0; 


//exit(var_dump($rowsItem)); 
foreach ($rowsItemA as $rowsItem){ 
    //$id = $rowsItem[id]; 


    $port_item = ""; 
    $id = stripslashes ($rowsItem[id]); 
    //exit(var_dump($id)); 
    $portfolio_category = stripslashes ($rowsItem[portfolio_category]); 
    $sql = "SELECT * FROM"." $catTable"." WHERE id="."$portfolio_category"." LIMIT 1"; 
    $catNameDB = $wpdb->get_results($sql); 
    /*from Above Select for CATEGORY*/ 
    foreach ($catNameDB as $catNameDBs){ 
     $portfolio_category = stripslashes ($catNameDBs->cat_name); 
    }/**/ 
    $portfolio_name = stripslashes ($rowsItem[portfolio_name]); 
    $portfolio_active = stripslashes ($rowsItem[portfolio_active]); 
    $portfolio_tags = stripslashes ($rowsItem[portfolio_tags]); 
    $portfolio_main_image = stripslashes ($rowsItem[main_image]); 
    $portfolio_desc = stripslashes ($rowsItem[portfolio_desc]); 
    $image_2 = stripslashes ($rowsItem[image_2]); 
    $image_3 = stripslashes ($rowsItem[image_3]); 
    $portfolio_website = stripslashes ($rowsItem[portfolio_website]); 
    //exit(var_dump($image_2,$image_3)); 
    if($image_2 !== '' || $image_2 = null) 
     { 
      $image_2 = ",'".$image_2."',"; 
     } 
    else 
     { 
      $image_2 = ''; 
     } 
    if($image_3 !== '' || $image_3 = null) 
     { 
      $image_3 = ",'".$image_3."'"; 
     } 
    else 
     { 
      $image_3 = ''; 
     } 
    $port_item .= " 
    { 
     'title'   : '".$portfolio_name."', 
     'description' : '".$portfolio_desc."', 
     'thumbnail'  : ['".$portfolio_main_image."' ".$image_2." ".$image_3."], 
     'large'   : ['".$portfolio_main_image."' ".$image_2." ".$image_3."], 
     'tags'   : ['".$portfolio_category."'] 
    } 
    "; 
    if(++$i === $numItems) { 
     $port_item .= ""; 
    } 
    else 
     $port_item .=","; 
     //exit(var_dump($i,$numItems)); 
} 
?> 

出口(的var_dump($ rowsItemA));

array (size=3) 
    0 => 
    array (size=10) 
     'id' => string '9' (length=1) 
     'portfolio_name' => string 'Da' (length=26) 
     'main_image' => string 'elicate-dashley-1.png' (length=101) 
     'image_2' => string 'n-and-mn.png' (length=107) 
     'image_3' => string '' (length=0) 
     'portfolio_active' => string '0' (length=1) 
     'portfolio_category' => string '1' (length=1) 
     'portfolio_desc' => string 'test1' (length=246) 
     'portfolio_tags' => string '["1","2","3","4","5","6","10"]' (length=30) 
     'portfolio_website' => string 'http://www.test.com/' (length=26) 
    1 => 
    array (size=10) 
     'id' => string '10' (length=2) 
     'portfolio_name' => string 'Sage' (length=29) 
     'main_image' => string 'purs-er.png' (length=99) 
     'image_2' => string '' (length=0) 
     'image_3' => string '' (length=0) 
     'portfolio_active' => string '0' (length=1) 
     'portfolio_category' => string '1' (length=1) 
     'portfolio_desc' => string 'test 2' (length=249) 
     'portfolio_tags' => string '["1","2","5","6","9","10"]' (length=26) 
     'portfolio_website' => string 'http://www.test.com/test' (length=27) 
    2 => 
    array (size=10) 
     'id' => string '11' (length=2) 
     'portfolio_name' => string 'Scap' (length=20) 
     'main_image' => string 's-day-cap.png' (length=93) 
     'image_2' => string '' (length=0) 
     'image_3' => string '' (length=0) 
     'portfolio_active' => string '0' (length=1) 
     'portfolio_category' => string '1' (length=1) 
     'portfolio_desc' => string 'test 3' (length=155) 
     'portfolio_tags' => string '["1","2","5","6","9","10"]' (length=26) 
     'portfolio_website' => string 'http://www.test.com/test/test' (length=44) 
+1

什麼是'的var_dump($ rowsItemA)'輸出? – vbrmnd

+0

在原始文章中加入 –

+0

您是如何知道自己只獲得一次迭代的? – vbrmnd

回答

1

這是因爲,你已經在循環brginning做出$port_item = ""

foreach ($rowsItemA as $rowsItem){ 


    $port_item = "";// declaring as null here(remove this.) 

所以在每一個循環的價值被重新初始化,而你只得到1元(您的數組的最後一個元素)