Jump to content


Photo

Switch statement--right usage or error


  • Please log in to reply
21 replies to this topic

#1 spare

spare
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 23 July 2006 - 08:43 PM

Hello,

I am trying to get a switch statement to work but I am getting only the first case no matter the variable's value.  I know that the information is being passed from the database to the page because when I echo the variable the value is correct. 

I would appreciate it if someone could help me or tell me if a switch statement is the wrong way to go about this.

Regards


The information appearing on the page varies depending on the country code of the person logged in.

$country_codequery = $db->mySQLsafe($VisitorData[0]['country_code']);

echo "Country code $country_codequery";
switch ($country_codequery)
{
case ($country_codequery = 30):
  $bookNumberpart1 =($dataArray[0]['info30']);
  break;
case ($country_codequery = 31):
  $bookNumberpart1 =($dataArray[0]['info31']);
  break;
case ($country_codequery = 32):
  $bookNumberpart1 =($dataArray[0]['info32']);
  break;

default:
  $bookNumberpart1 =($dataArray[0]['info00']);
  break;
}


$bookNumber = bookNumber($bookNumberpart1[0],$dataArray[0]['book_number']);

if(bookNumber($bookNumberpart1[0], $dataArray[0]['book_number'])==FALSE) {
		$book_view->assign("TXT_INFO",codeFormat($bookNumberpart1[0]));
	} else {
		$book_view->assign("TXT_INFO","<span class='txtOldInfo'>".codeFormat($bookNumberpart1[0])."</span>");
	}


#2 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 23 July 2006 - 08:50 PM

Use "==" not "=" to compare. "=" assigns, "==" compares.

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 July 2006 - 08:56 PM

Actually in the case statements you put the value you're comparing the variable to, so in your case (no pun intended), the code would look like:
<?php
switch ($country_codequery)
{
case 30:
  $bookNumberpart1 =$dataArray[0]['info30'];
  break;
case 31:
  $bookNumberpart1 =$dataArray[0]['info31'];
  break;
case 32:
  $bookNumberpart1 =$dataArray[0]['info32'];
  break;
default:
  $bookNumberpart1 =$dataArray[0]['info00'];
  break;
}
?>

The other way you can do this without a switch statement:
<?php
$infonum = ($country_codequery >= 30 && $country_codequery <= 32)?$country_codequery:'00';
$bookNumberpart1 = $dataArray[0]['info'.$infonum];
?>

Ken

#4 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 23 July 2006 - 08:58 PM

True, "==" is obsolete in this context.

#5 spare

spare
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 25 July 2006 - 08:24 PM


Hello,

I appreciate all of the help. 

I have tried using just "case 30:" and "($country_codequery = 30)" with and without the "==" still I am not getting the correct response.  I only get the default or nothing.  I added an echo "Book number $bookNumberpart1";  after the switch to see what the value is but it is blank.  The  echo "Country code $country_codequery"; is still coming up with the right answer. 

I only had a few hours this evening to try it.  I'm going to play around with it tomorrow and I will see what happens.

As for
<?php
$infonum = ($country_codequery >= 30 && $country_codequery <= 32)?$country_codequery:'00';
$bookNumberpart1 = $dataArray[0]['info'.$infonum];
?>
it interests me.  If I were to keep adding other country codes, would I use =, ==, or must I use <>.

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 25 July 2006 - 08:48 PM

Is $country_codequery a number or a string? If it is a string, try
<?php
switch ($country_codequery)
{
case '30':
  $bookNumberpart1 =$dataArray[0]['info30'];
  break;
case '31':
  $bookNumberpart1 =$dataArray[0]['info31'];
  break;
case '32':
  $bookNumberpart1 =$dataArray[0]['info32'];
  break;
default:
  $bookNumberpart1 =$dataArray[0]['info00'];
  break;
}
?>

It shouldn't matter, but you never know until you try it.

Ken

#7 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 25 July 2006 - 08:50 PM

i have a feeling that $country_codequery is actually a query resource id... or a query result.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#8 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 26 July 2006 - 09:31 AM

i have a feeling that $country_codequery is actually a query resource id... or a query result.


Wouldn't echoing it print "resource id #n", or "array()" if that were true?

Anyway, spare, I think the code you posted is a better solution than switching. Your piece of code has a little surplus logic.

Alternative:
<?php
$bookNumberpart1 = ($country_codequery >= 30 && $country_codequery <= 32)? $dataArray[0]['info'.$infonum]:$dataArray[0]['info00'];
?>

I don't think that will fix your problem though. Switching should work fine. I think the problem is in your $dataArray.

<?php
echo '<pre>';
print_r($dataArray);
echo '</pre>';
?>
To show us it's contents

#9 spare

spare
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 26 July 2006 - 09:12 PM

Hi,

Still no luck.  Even with the single quotes '30'.  When I ask it to echo the $bookNumberpart1 it comes up blank.

Yes, $country_codequery is a query result--see below. 

That bit of code was actually Ken's suggestion.

Here are print_r for several queries and the results.

--print_r($dataArray)--
(
    [0] => Array
        (
            [bookId] => 38
            [bookCode] => TOT812
            [quantity] => 1
            [booktitle] => Test Book
            [description] => Description
            [description1] => -
            [description2] => -
            [description3] => -
            [description4] => -
            [description5] => -
            [description6] => -
            [description7] => -
            [description8] => -
            [description9] => -
            [description10] => -
            [description11] => -
            [description12] => -
            [image] => Test1.jpg
            [info] => 1.00
            [info0] => -please login-
            [info30] => 300
            [info31] => 311
            [info32] => 322
            [book_number] => 000
            [stock_level] => 0
            [useStockLevel] => 0
            [noBooks] => 4

--print_r($concodequery)--
SELECT * FROM Poesie_login WHERE countrycode = '30'

--print_r($country_codequery)--
'30'


#10 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 27 July 2006 - 08:36 AM

Please show us the mySQLsafe() method.

You say you only get "the first result", but from all you've shown us I can only see that that is correct.

I don't see what you're trying to do.

what exactly does $db->mySQLsafe($VisitorData[0]['country_code']); return, a resultset?

Where does this $dataArray come from? It seems more like a resultset then $country_querycode does.

In short, I don't get it.


#11 sonal4php

sonal4php
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 27 July 2006 - 09:12 AM

give a try without default switch case
it once happened with me
i need to scroll back to see, what have i done for that
just a thought, try it w/o default
i know that it's not a solution, but a thought...
will get back again



#12 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 July 2006 - 01:27 PM

Here's a clue:

--print_r($country_codequery)--
'30'

It looks like the value of $country_codequery is '30'. The single quotes are included in the value. Therefore the switch statement should look like:
<?php
switch ($country_codequery)
{
case "'30'":
  $bookNumberpart1 =$dataArray[0]['info30'];
  break;
case "'31'":
  $bookNumberpart1 =$dataArray[0]['info31'];
  break;
case "'32'":
  $bookNumberpart1 =$dataArray[0]['info32'];
  break;
default:
  $bookNumberpart1 =$dataArray[0]['info00'];
  break;
}
?>

Ken

#13 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 27 July 2006 - 09:33 PM

It looks like the value of $country_codequery is '30'. The single quotes are included in the value.


You have to be kidding me. If true, that's just plain wrong!

#14 spare

spare
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 27 July 2006 - 10:40 PM

It works!!!!  It works!!!!  It works!!!!
Thank you!!! Thank you!!!  Thank you !!!

It was only the difference in adding the " as well as the ' it would work with either alone only with both.

You have to be kidding me. If true, that's just plain wrong!


Yes,  448191, it is just plain wrong. :)

I had tried it without the default case but it used the first case as default.

I understand the logic but it is just TOO logical for me
;) I would have never thought of it.

#15 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 July 2006 - 11:28 PM

Can you post the code that stores the value that eventually ends up as $country_codequery? Because somehow you're storing the value of '30' instead of a plain 30.

Ken

#16 spare

spare
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 28 July 2006 - 09:56 PM

Hi,

There isn't any code for adding the country code information.  The country code is entered directly in the database by site admin--just as 30 not '30'. Then it is passed around a few times before being called up on the page that required the switch. 

I'm just so glad that it works.  I don't really care about the "'--'" .  The country code information is never view by anyone other than site admin. 

Do you think that the extra quotes slow things down or will do so in the future?


#17 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 28 July 2006 - 10:09 PM

If there's no code, how does the information get into the DB? The only thing the extra quotes would slow down is a new person who picks up your code and trys to figure out what's going on.

Ken

#18 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 28 July 2006 - 10:41 PM

That may not slow it down but you won't believe this until it's too late, but 2 months down the road when you realize you need to do mathematical equations with it you will regret that you might want to take ken's advice and post that part of the code, so we can fix that issue, that will cause you headaches later on. 

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#19 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 29 July 2006 - 05:53 PM

Ohter than that, it's just plain wrong.  ;D

#20 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 July 2006 - 06:58 PM

Why are you insisting that it's plain wrong? You can have strings defined anyway you want and as long as they are used consistantly, there is no problem with it.

I wouldn't create strings like that and neither would most programmers.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users