donglisi8644 2010-10-04 14:32
浏览 19
已采纳

我如何得到这个或纠正这个PHP逻辑工作

I am creating a report with the data which calls the stored procedure and that procedure returns various sections (1,2,3,4,5,6) with the data in each section.Now the sections may contain or may not contain the data.This is how i have wriiten my logic

 foreach($this->$dbresults as $row){
$var1 ='';
 If($var1!=$row['section']){
switch($row['section']){
case '1':echo "some thing data";
     break;
case '2':echo "some thing data";
     break;
case '3':echo "some thing data";
     break;
case '4':echo "some thing data";
     break;
case '5':echo "some thing data";
     break;
case '6':echo "some thing data";
     break;
}
  } 

$var1=$row['section']
}

So here My problem if any one of the section is not present then that section case cannot be executed .I mean How do i execute the section even if the section is not returned from the database

  • 写回答

5条回答 默认 最新

  • dongzhang1839 2010-10-04 14:48
    关注

    I guess you're already ordering your results by section. If your sections are really 1-n, you could put your switch() code into some runsections function and do this:

    $var1=0; $lastsection=16;
    foreach($this->dbresults as $row) {
      If($var1!=$row['section']){
        for($num=$var1+1; $num<$row['section']; $num++) runsections($num);
        runsections($row['section']);
      }
      $var1=$row['section'];
    }
    for($num=$var1+1;$num<=$lastsection;$num++) runsections($num);
    

    if your sections aren't sequential numbers you could create an array and check if they've all been executed

    $sections=array('a'=>0,'b'=>0,'c'=>0,'d'=>0,'e'=>0);
    If($var1!=$row['section']){
        unset($sections[$row['section']]);
        runsection($row['section']);
    }
    ...
    }
    foreach($sections as $num) {
        runsection($num);
    }
    

    edit: so the runsections() function would look like this:

    function runsections($section) {
        switch($section){
        case '1':echo "some thing data";
             break;
        case '2':echo "some thing data";
             break;
        case '3':echo "some thing data";
             break;
        case '4':echo "some thing data";
             break;
        case '5':echo "some thing data";
             break;
        case '6':echo "some thing data";
             break;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?