跳过整行,如果一个单元格在MySQL表中使用PHP foreach为Null

时间:2017-07-16 03:40:43

标签: php mysql

这是我的代码:

$cast_name_list = explode(',',$result[$x]["cast_name"]);
$cast_profile_path = explode(',',$result[$x]["cast_profile_path"]);
$cast_character = explode(',',$result[$x]["cast_character"]);

foreach( $cast_name_list as $index => $cast_name ) 
{
echo 'name = '.$cast_name.'<br/> ';
echo ' character name = '.$cast_character[$index].' <br/>';
echo ' profile path = '.$cast_profile_path[$index].' <br/><br/><br/>';
}

这就是我在MySQL表中的数据如何:

cast_name       cast_character    cast_profile_path
Robert             Iron man       example.com/sdfsd.jpg
Rahul               Mohit         example.com/aadfsd.jpg
Tom                 Josh             Null
Tripp               James        example.com/zzz.jpg

我想要的是:,如果表格中有NULL个单元格,则整行不会回显。

即:Tom Josh Null

但是,这就是我的代码回应

name = robert
character name = Iron Man
profile path = example.com/sdfsd.jpg

name = Rahul
character name = Mohit
profile path = example.com/aadfsd.jpg


name = Tom
character name = Josh
profile path = example.com/zzz.jpg


name = Tripp
character name = James
profile path = Notice: Undefined offset: 8 

预期输出:

name = robert
character name = Iron Man
profile path = example.com/sdfsd.jpg

name = Rahul
character name = Mohit
profile path = example.com/aadfsd.jpg


name = Tripp
character name = James
profile path = Notice: example.com/zzz.jpg

修改:由于第3行中的一个值为null,因此$cast_profile_path数组仅包含4个值,而$cast_name&amp; $cast_character包含5个值。

1 个答案:

答案 0 :(得分:0)

试试这个:

var contentData = [
            {
                tops: [
                            { image: "images/top1.jpg" },
                            { image: "images/top2.jpg" },
                            { image: "images/top3.jpg" },
                            { image: "images/top4.jpg" } ]
            }
    ];

$scope.objectArray = contentData;
$scope.objectCount = $scope.objectArray[0].length;

供参考,见:

注意不确定我是否理解正确,您是否在表格列中存储以逗号分隔的值?