我从保存在数据库中的形式json阵列。我生成从array.The形式的报告有"Do you require the section?"一节。如果用户选择"No"节,我需要跳过部分,直到节结束。

在json阵列,部分开始于

'{"type":"section","label":"Do you require this section?","req":0,"Element_Values":"No"}'

和该部分的端部是在

'{"type":"section-end","label":"","req":0,"Element_Values":""}'

如果Element_Values=No然后跳过数据直到类型section-end,否则什么也不做。

对于我做的做{} while循环,它仍然不工作:

 if($jsonArray[$jsonIndex]['type']=="section")
 {
      if($jsonArray[$jsonIndex]['Element_Values']=="")
      {
           $html.='<tr><td style="color:grey;">'
                           .'Unanswered'
                           .'</td></tr>';
      }
      else if($jsonArray[$jsonIndex]['Element_Values']=="No")
      {
           do 
           {
               continue;
           }while($jsonArray[$jsonIndex]['type']=="section-end");                
      }

  }

我完整的代码下面给出:

<?php
$jsonIndex=0;
$jsonArray='[{"type":"header","label":"Inspection","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"WHAT TYPE OF HOME IS THIS","Element_Values":"\"SINGLE DUPLEX\"","req":1,"choices":[{"label":"SINGLE","sel":0,"notification":0,"subOptions":[]},{"label":"SINGLE DUPLEX","sel":1,"notification":0,"subOptions":[]},{"label":"SINGLE GRANNY FLAT","sel":0,"notification":0,"subOptions":[]},{"label":"DOUBLE","sel":0,"notification":0,"subOptions":[]},{"label":"DOUBLE DUPLEX","sel":0,"notification":0,"subOptions":[]},{"label":"DOUBLE GRANNY FLAT","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"description","label":"IF YOU HAVE SELECTED DUPLEX or GRANNY FLAT THE CHECKLIST IS AT THE BOTTOM FOR THE SECOND UNIT","req":0,"Element_Values":"-"},'
        . '{"type":"sub-header","label":"DOCUMENTS","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Do you have the frame and truss details including bracing, floor joist, wall frame and truss layouts","Element_Values":"\"Yes\"","req":0,"choices":[{"label":"Yes","sel":1,"notification":0,"subOptions":[]},{"label":"No","sel":0,"notification":0,"subOptions":[{"InstructionLabel":"You cannot complete this inspection "}]}]},'
        . '{"type":"sub-header","label":"SERVICES","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Stackwork and a\/c ducts have been framed if required","Element_Values":"\"YES\"","req":0,"choices":[{"label":"YES","sel":1,"notification":0,"subOptions":[]},{"label":"NO","sel":0,"notification":0,"subOptions":[]},{"label":"N\/A","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"dropdown","label":"All drainage points in correct position","Element_Values":"\"YES\"","req":0,"choices":[{"label":"YES","sel":1,"notification":0,"subOptions":[]},{"label":"NO","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"dropdown","label":"Is power connected on site","Element_Values":"\"YES\"","req":0,"choices":[{"label":"YES","sel":1,"notification":0,"subOptions":[]},{"label":"NO","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"sub-header","label":"DUPLEX \/ GRANNY FLAT","req":0,"Element_Values":"-"},'
        . '{"type":"section","label":"Do you require this section?","req":0,"Element_Values":"No"},'
        . '{"type":"dropdown","label":"What side viewing from the street is the home you are about to inspect","Element_Values":"\"\"","req":1,"choices":[{"label":"LEFT","sel":0,"notification":0,"subOptions":[]},{"label":"RIGHT","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"sub-header","label":"DOCUMENTS","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Do you have the frame and truss details including bracing, floor joist, wall frame and truss layouts","Element_Values":"\"\"","req":1,"choices":[{"label":"Yes","sel":0,"notification":0,"subOptions":[]},{"label":"No","sel":0,"notification":0,"subOptions":[{"InstructionLabel":"You cannot complete this inspection\n"}]}]},'
        . '{"type":"sub-header","label":"BRACING - ROOF & WALL","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Is all Wall & Roof Bracing as per approved plan and correctly installed","Element_Values":"\"\"","req":1,"choices":[{"label":"Yes","sel":0,"notification":0,"subOptions":[]},{"label":"No","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"dropdown","label":"Sheer blocks installed as required","Element_Values":"\"\"","req":1,"choices":[{"label":"Yes","sel":0,"notification":0,"subOptions":[]},{"label":"No","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"sub-header","label":"DOWNPIPES","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Are all downpipes installed and connected to system","Element_Values":"\"\"","req":1,"choices":[{"label":"YES","sel":0,"notification":0,"subOptions":[{"DropLabel":"Where downpipes are relocated are they 300mm below bottom course of brickwork","DropOptions":["YES","NO"],"DropSel":"","reqDrop":"0"}]},{"label":"NO","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"section-end","label":"","req":0,"Element_Values":""},'
        . '{"type":"sub-header","label":"SIGN OFF","req":0,"Element_Values":"-"},'
        . '{"type":"text","label":"Signature ","req":1,"Element_Values":"fd bvf"},'
        . '{"type":"dropdown","label":"Job is ready to proceed to Mandatory Pre Linings Inspection","Element_Values":"\"PROCEED\"","req":1,"choices":[{"label":"PROCEED","sel":1,"notification":0,"subOptions":[]},{"label":"DO NOT PROCEED","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"dropdown","label":"Who will be the gyprocker for this job","Element_Values":"\"vgfnari\"","req":1,"choices":[{"label":"FBI-Fornari","sel":1,"notification":0,"subOptions":[]},{"label":"MOMENTUM","sel":0,"notification":0,"subOptions":[]}]}]';
$jsonArray=json_decode($jsonArray,true);
$html="";
if(!empty($jsonArray))
{  
    for ($i=0;$i <count($jsonArray);$i++) 
    {

        if($jsonArray[$jsonIndex]['type']=="header")
        {
            $html.='<div class="header"><h3><b>'.$jsonArray[$jsonIndex]['label']. '</b></h3><hr/></div>'; 
        }
        else if($jsonArray[$jsonIndex]['type']=="sub-header")
        {
            $html.='<div class="sub-header"><h5><b>'.$jsonArray[$jsonIndex]['label'].'</b></h5></div>';
        }
        else if($jsonArray[$jsonIndex]['type']=="description")
        {
            $html.='<table style="border:1px solid grey;border-radius:5px;-moz-border-radius: 5px;padding:10px;cell-padding:20px;width:100%;min-height:100px;">'
                        . '<tbody>'
                        . '<tr>'
                        . '<td style="text-align: left;">'.$jsonArray[$jsonIndex]['label']
                        . '</td>'
                        . '</tr>'
                        . '</tbody>'
                        . '</table>';
        }

        else 
        {
            if($jsonArray[$jsonIndex]['type']=="section")
            {
                if($jsonArray[$jsonIndex]['Element_Values']=="")
                {
                    $html.='<tr><td style="color:grey;">'
                           .'Unanswered'
                           .'</td></tr>';
                }
                else if($jsonArray[$jsonIndex]['Element_Values']=="No")
                {
                    do 
                    {
                        continue;
                    }while($jsonArray[$jsonIndex]['type']=="section-end");                
                }

            }
            if($jsonArray[$jsonIndex]['type']=="signature")
            {
                    $label="Signature";
            }
            else if($jsonArray[$jsonIndex]['type']=="location")
            {
                    $label="Location";
            }
            else 
            {
                    $label=$jsonArray[$jsonIndex]['label'];
            }

            $html.='<table nobr="true" style="border:1px solid grey;border-radius:5px;-moz-border-radius: 5px;padding:5px 10px;cell-padding:10px;width:100%;">'
                       . '<tbody>'
                       . '<tr>';
            $html.='<td><label style="color:grey;font-size:12px">'.$label.'</label></td></tr>';

            if($jsonArray[$jsonIndex]['Element_Values']=="")
            {
                      $html.='<tr><td style="color:grey;">'
                               .'Unanswered'
                               .'</td></tr>';
            }
            else 
            {
                    //$html.='<tr><td>'.$jsonArray[$jsonIndex]['Element_Values'].'</td></tr>';
                    $html.='<tr><td>'.str_replace('"','', $jsonArray[$jsonIndex]['Element_Values']).'</td></tr>';
            }
            $html.='</tbody>'
         .'</table>';
        }

        $jsonIndex++;
    }    
    echo $html;
}

?>

更新后的代码:

<?php
$jsonIndex=0;
$jsonArray='[{"type":"header","label":"Inspection","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"WHAT TYPE OF HOME IS THIS","Element_Values":"\"SINGLE DUPLEX\"","req":1,"choices":[{"label":"SINGLE","sel":0,"notification":0,"subOptions":[]},{"label":"SINGLE DUPLEX","sel":1,"notification":0,"subOptions":[]},{"label":"SINGLE GRANNY FLAT","sel":0,"notification":0,"subOptions":[]},{"label":"DOUBLE","sel":0,"notification":0,"subOptions":[]},{"label":"DOUBLE DUPLEX","sel":0,"notification":0,"subOptions":[]},{"label":"DOUBLE GRANNY FLAT","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"description","label":"IF YOU HAVE SELECTED DUPLEX or GRANNY FLAT THE CHECKLIST IS AT THE BOTTOM FOR THE SECOND UNIT","req":0,"Element_Values":"-"},'
        . '{"type":"sub-header","label":"DOCUMENTS","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Do you have the frame and truss details including bracing, floor joist, wall frame and truss layouts","Element_Values":"\"Yes\"","req":0,"choices":[{"label":"Yes","sel":1,"notification":0,"subOptions":[]},{"label":"No","sel":0,"notification":0,"subOptions":[{"InstructionLabel":"You cannot complete this inspection "}]}]},'
        . '{"type":"sub-header","label":"SERVICES","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Stackwork and a\/c ducts have been framed if required","Element_Values":"\"YES\"","req":0,"choices":[{"label":"YES","sel":1,"notification":0,"subOptions":[]},{"label":"NO","sel":0,"notification":0,"subOptions":[]},{"label":"N\/A","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"dropdown","label":"All drainage points in correct position","Element_Values":"\"YES\"","req":0,"choices":[{"label":"YES","sel":1,"notification":0,"subOptions":[]},{"label":"NO","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"dropdown","label":"Is power connected on site","Element_Values":"\"YES\"","req":0,"choices":[{"label":"YES","sel":1,"notification":0,"subOptions":[]},{"label":"NO","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"sub-header","label":"DUPLEX \/ GRANNY FLAT","req":0,"Element_Values":"-"},'
        . '{"type":"section","label":"Do you require this section?","req":0,"Element_Values":"No"},'
        . '{"type":"dropdown","label":"What side viewing from the street is the home you are about to inspect","Element_Values":"\"\"","req":1,"choices":[{"label":"LEFT","sel":0,"notification":0,"subOptions":[]},{"label":"RIGHT","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"sub-header","label":"DOCUMENTS","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Do you have the frame and truss details including bracing, floor joist, wall frame and truss layouts","Element_Values":"\"\"","req":1,"choices":[{"label":"Yes","sel":0,"notification":0,"subOptions":[]},{"label":"No","sel":0,"notification":0,"subOptions":[{"InstructionLabel":"You cannot complete this inspection\n"}]}]},'
        . '{"type":"sub-header","label":"BRACING - ROOF & WALL","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Is all Wall & Roof Bracing as per approved plan and correctly installed","Element_Values":"\"\"","req":1,"choices":[{"label":"Yes","sel":0,"notification":0,"subOptions":[]},{"label":"No","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"dropdown","label":"Sheer blocks installed as required","Element_Values":"\"\"","req":1,"choices":[{"label":"Yes","sel":0,"notification":0,"subOptions":[]},{"label":"No","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"sub-header","label":"DOWNPIPES","req":0,"Element_Values":"-"},'
        . '{"type":"dropdown","label":"Are all downpipes installed and connected to system","Element_Values":"\"\"","req":1,"choices":[{"label":"YES","sel":0,"notification":0,"subOptions":[{"DropLabel":"Where downpipes are relocated are they 300mm below bottom course of brickwork","DropOptions":["YES","NO"],"DropSel":"","reqDrop":"0"}]},{"label":"NO","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"section-end","label":"","req":0,"Element_Values":""},'
        . '{"type":"sub-header","label":"SIGN OFF","req":0,"Element_Values":"-"},'
        . '{"type":"text","label":"Signature ","req":1,"Element_Values":"fd bvf"},'
        . '{"type":"dropdown","label":"Job is ready to proceed to Mandatory Pre Linings Inspection","Element_Values":"\"PROCEED\"","req":1,"choices":[{"label":"PROCEED","sel":1,"notification":0,"subOptions":[]},{"label":"DO NOT PROCEED","sel":0,"notification":0,"subOptions":[]}]},'
        . '{"type":"dropdown","label":"Who will be the gyprocker for this job","Element_Values":"\"vgfnari\"","req":1,"choices":[{"label":"FBI-Fornari","sel":1,"notification":0,"subOptions":[]},{"label":"MOMENTUM","sel":0,"notification":0,"subOptions":[]}]}]';
$jsonArray=json_decode($jsonArray,true);
$html="";
if(!empty($jsonArray))
{  
    for ($i=0;$i <count($jsonArray);$i++) 
    {

        if($jsonArray[$jsonIndex]['type']=="header")
        {
            $html.='<div class="header"><h3><b>'.$jsonArray[$jsonIndex]['label']. '</b></h3><hr/></div>'; 
        }
        else if($jsonArray[$jsonIndex]['type']=="sub-header")
        {
            $html.='<div class="sub-header"><h5><b>'.$jsonArray[$jsonIndex]['label'].'</b></h5></div>';
        }
        else if($jsonArray[$jsonIndex]['type']=="description")
        {
            $html.='<table style="border:1px solid grey;border-radius:5px;-moz-border-radius: 5px;padding:10px;cell-padding:20px;width:100%;min-height:100px;">'
                        . '<tbody>'
                        . '<tr>'
                        . '<td style="text-align: left;">'.$jsonArray[$jsonIndex]['label']
                        . '</td>'
                        . '</tr>'
                        . '</tbody>'
                        . '</table>';
        }

        else 
        {
            if($jsonArray[$jsonIndex]['type']=="section")
            {
                if($jsonArray[$jsonIndex]['Element_Values']=="")
                {
                    $html.='<tr><td style="color:grey;">'
                           .'Unanswered'
                           .'</td></tr>';
                }
                else if($jsonArray[$jsonIndex]['Element_Values']=="No")
                {
                    do 
                    {
                        $jsonIndex++;
                    }while($jsonArray[$jsonIndex]['type']!="section-end");                
                }

            }
            if($jsonArray[$jsonIndex]['type']=="signature")
            {
                    $label="Signature";
            }
            else if($jsonArray[$jsonIndex]['type']=="location")
            {
                    $label="Location";
            }
            else 
            {
                    $label=$jsonArray[$jsonIndex]['label'];
            }

            $html.='<table nobr="true" style="border:1px solid grey;border-radius:5px;-moz-border-radius: 5px;padding:5px 10px;cell-padding:10px;width:100%;">'
                       . '<tbody>'
                       . '<tr>';
            $html.='<td><label style="color:grey;font-size:12px">'.$label.'</label></td></tr>';

            if($jsonArray[$jsonIndex]['Element_Values']=="")
            {
                      $html.='<tr><td style="color:grey;">'
                               .'Unanswered'
                               .'</td></tr>';
            }
            else 
            {
                    //$html.='<tr><td>'.$jsonArray[$jsonIndex]['Element_Values'].'</td></tr>';
                    $html.='<tr><td>'.str_replace('"','', $jsonArray[$jsonIndex]['Element_Values']).'</td></tr>';
            }
            $html.='</tbody>'
         .'</table>';
        }

        //$jsonIndex++;
    }    
    echo $html;
}
?>
分析解答

这个

do 
{
    continue;
}while($jsonArray[$jsonIndex]['type']=="section-end");

需要是

do 
{
  $jsonIndex++;
} while ($jsonArray[$jsonIndex]['type'] != "section-end");

需要循环的逻辑是相反你所得到的。它使用!=的==代替。此外,否则最终会成为一个无限循环是非常重要的是提高$jsonIndex值。

也改变...

for ($i=0;$i <count($jsonArray);$i++)

for ($jsonIndex=0;$jsonIndex <count($jsonArray);$jsonIndex++)