tags:

views:

75

answers:

5

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

+1  A: 
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";
} 
Paulo Scardine
@paulo: I added Default case but it is not executing
Someone
If you experience this, I think either some case condition was triggered before or your case is never being executed.
Paulo Scardine
A: 

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;
Ruel
@Ruel: I added Default But no use
Someone
+1  A: 

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;
    }
}
jab11
@jab11:How do i write that runsections().You are exactly right iam ordering the results by section .Say I have only 6 sections Thats it .how do write the Code I mean How do i execute if the section is not present
Someone
@Someone: see my edit. basically your switch() part in a function so you don't have to duplicate it in your code. for(...) runsections() will cycle through sections which were missing in your results and call that switch with them.
jab11
@Jab:No Iam Not able to print the section with out the section no not being present from the database or Result set
Someone
@Someone: ok I fixed something, I suppose your $var=''; is really before your foreach(), with $lastsection set to number of your sections it should call runsections() for each section regardless if it is in result set.
jab11
@Jab: I followed Your same logic But my each section is repeating for 6 times
Someone
A: 

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.";
    }
}
Klinky
A: 

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.

Kevin Stich