我正在尝试浏览一个对象。我的初始化问题只出现在第一次迭代中。所以一切都很好,但是我只有一次迭代而他们是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)
答案 0 :(得分:1)
这是因为你在循环的开始时已经$port_item = ""
。
foreach ($rowsItemA as $rowsItem ){
$port_item = "";// declaring as null here(remove this.)
因此,在每个循环中,您的值都会重新初始化,并且您只获得1个元素(数组的最后一个元素)