开发者

How do i get this or correct this PHP logic Working

开发者 https://www.devze.com 2023-01-18 20:57 出处:网络
I am creating a report with the data which calls the storedprocedure and that procedure returns various sections (1,2,3,4,5,6) with the data in each section.Nowthe sections may contain or may

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 t开发者_运维问答hing 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


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;
    }
}


switch($x){
    case '1':
        echo "some thing 1";
        break;
    case '2':
        echo "some thing 2";
       break;
    case 'N':
        echo "some thing N";
       break;
    default:
        echo "some thing else";
} 


After your last case, insert:

default: echo "some error";

The break is optional since it's the last case in the switch statement. Also, The single quotes are also optional if you're looking for numeric options.

case 1: echo "something";
        break;


Maybe I am not quite understanding exactly what you want. The following code should work in the following conditions:

You always want to display the 6 sections with either DB data or non-DB data.

You do not need to display the same section multiple times.

$sections = range(1, 6);
foreach($sections as $sectionNum) {
    $sectionNum = (string) $sectionNum;
    $foundSection = false;
    foreach($this->dbresults as $row) {
        if ($row['section'] == $sectionNum) {
            echo "section #$sectionNum has DB data: " . $row['data'];
            $foundSection = true;
            break;
        }
    }
    if (!$foundSection) {
            echo "section #$sectionNum does not have DB data.";
    }
}


Here's what I've got:

foreach($this->dbresults as $row){
    if(isset($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;
            default:echo "some thing data";
                break;
        }
    } else {
        //Do something since no section data was stored
    }
}

I added a default case, fixed the small php errors ($this->$dbresults is changed, using isset instead of !='') and added an else to your if to do something if the section isn't found.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号