0

I have called a database which has 4 "rows" of data in the field CNT. (2,1,3,1) I want to total these and when I hit a Max number, kick out to another php page. If the count is below the Max there is another header("Location...) command below.

It will not kick out - can you give me any suggestions

    $Count = 0;
    $Max = 5;
    While ($row = mysql_fetch_assoc($result)) { 
        $Count = $Count + $row["Cnt"];
        If ($Count > $Max) { 
            header("Location: pareAbilities.asp?Who=".$Who."&Logcode=".$LogCode."&A=".$row['A']."&B=".$row['B']."&CNT=".$Count );
        } 
    }

2 Answers2

1

use exit() after Header

If ($Count > $Max) { 
   header("Location: pareAbilities.asp?Who=".$Who."&Logcode=".$LogCode."&A=".$row['A']."&B=".$row['B']."&CNT=".$Count );
   exit();
} 

PHP - Should I call exit() after calling Location: header?

Community
  • 1
  • 1
Ritesh Kumar Gupta
  • 5,055
  • 7
  • 45
  • 71
0

Without knowing what else your doing in the loop.. the following code would be way faster and easier to read.

  • Used SQL SUM function to sum up the CNT column
  • http_build_query to build your redirect location
  • sprintf tidys up your concatenation
  • Added die to stop the rest of the page from possible processing

Whats important for your answer is the die. Also make sure you have no other output being sent to the browser before this is executing. header can only be sent when there has been no output yet. If you cannot stop the output so easily, try looking into output buffering: ob_start ob_flush

// SUM(CNT) as sum_cnt automagically does the calculation
$query = mysql_query("SELECT SUM(CNT) as sum_cnt, A, B, CNT FROM table");
$result = mysql_fetch_assoc($query);

if($result['sum_cnt'] > $Max) {
    $querystring = http_build_query( // builds the query string for you
            array(
                'Who' => $Who,
                'Logcode' => $LogCode,
                'A' => $result['A'],
                'B' => $result['B'],
                'CNT' => $result['sum_cnt'],
            )
    );
    header( // sprintf is very nice to keep long concat strings tidy
            sprintf(
                    "Location: pareAbilities.asp?%s", 
                    $querystring
            )
    );
    die(); // Added die here to stop other code from executing
}
phpisuber01
  • 7,585
  • 3
  • 22
  • 26