1

My Code wont work. It just prints out the default

Here is the php

$updatesQuery = "SELECT * FROM updates WHERE Isnote = 0";
        $rs  = mysql_query($updatesQuery) or
        die("SQL: $usersQuery)<br />".mysql_error());
        while($row = mysql_fetch_array($rs)) {
            switch ($i){
                case $row[CatID]=1:
                    $i = "kunder";
                    break;
                case $row[CatID]=2:
                    $i = "bokningar";
                    break;
                case $row[CatID]=3:
                    $i = "offerter";
                    break;
                case $row[CatID]=4:
                    $i = "leverantorer";
                    break;
                case $row[CatID]=5:
                    $i = "kalender";
                    break;
                default:
                    $i = "no work";
                    break;
            }
            echo $i;                
        }

The sql query is valid. But my output from this is just the default value. "no work".

What have I written wrong?

1
  • what is the value of $i dude... Commented Jul 27, 2012 at 12:07

4 Answers 4

7

Switch on your variable, in this case $row['CatID']:

switch ($row['CatID']){
    case 1:
        $i = "kunder";
        break;
    case 2:
        $i = "bokningar";
        break;
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

This is what the poster was almost certainly trying to achieve.
2

It looks like $i is never set, so the switch() statement defaults to the (surprisingly) default clause.

Change your code to something like this;

        switch ($row['CatID']){
            case 1:
                $yourVar = "kunder";
                break;
        }   

A switch statement looks at a current variable. If that variable happens to be a column from a database, you need to evaluate that particular variable.

Your code would have worked if you had assigned your variable $i the data inside $row['catID'] before the switch.

1 Comment

+1 And I assume that the OP has a bad understanding of how a switch works. $row[CatID]=1 is a variable assignment that doesn't make much sense…
1

Just a guess

switch ($row['CatID']) {
  case 1:
    $i = "kunder";
    break;
  // And so on
}
  • $i is never defined
  • $row[CatID] should trigger a notice, because you forgot the '
  • $row[CatID]=2 sets the value, which doesn't make much sense in context

Comments

0
switch($row['CatID'])

Then use case 1:, case 2: etc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.