Jump to content

Archived

This topic is now archived and is closed to further replies.

spare

Switch statement--right usage or error

Recommended Posts

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.

[code]
$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>");
}[/code]

Share this post


Link to post
Share on other sites
Use "==" not "=" to compare. "=" assigns, "==" compares.

Share this post


Link to post
Share on other sites
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:
[code]<?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;
}
?>[/code]

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

Ken

Share this post


Link to post
Share on other sites
True, "==" is obsolete in this context.

Share this post


Link to post
Share on other sites

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 <>.

Share this post


Link to post
Share on other sites
Is $country_codequery a number or a string? If it is a string, try
[code]<?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;
}
?>[/code]

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

Ken

Share this post


Link to post
Share on other sites
i have a feeling that $country_codequery is actually a query resource id... or a query result.

Share this post


Link to post
Share on other sites
[quote author=ryanlwh link=topic=101585.msg403421#msg403421 date=1153860655]
i have a feeling that $country_codequery is actually a query resource id... or a query result.
[/quote]

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:
[code]<?php
$bookNumberpart1 = ($country_codequery >= 30 && $country_codequery <= 32)? $dataArray[0]['info'.$infonum]:$dataArray[0]['info00'];
?>[/code]

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

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

Share this post


Link to post
Share on other sites
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'

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Here's a clue:
[quote]
--print_r($country_codequery)--
'30'
[/quote]
It looks like the value of [color=red]$country_codequery[/color] is [b][color=blue]'30'[/color][/b]. The single quotes are included in the value. Therefore the switch statement should look like:
[code]<?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;
}
?>[/code]

Ken

Share this post


Link to post
Share on other sites
[quote author=kenrbnsn link=topic=101585.msg404531#msg404531 date=1154006875]
It looks like the value of [color=red]$country_codequery[/color] is [b][color=blue]'30'[/color][/b]. The single quotes are included in the value.
[/quote]

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

Share this post


Link to post
Share on other sites
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.

[quote author=448191 link=topic=101585.msg404785#msg404785 date=1154035982]
You have to be kidding me. If true, that's just plain wrong!
[/quote]

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.

Share this post


Link to post
Share on other sites
Can you post the code that stores the value that eventually ends up as $country_codequery? Because somehow you're storing the value of [b]'30'[/b] instead of a plain [color=red][b]30[/b][/color].

Ken

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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. 

Share this post


Link to post
Share on other sites
Ohter than that, it's just plain wrong.  ;D

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Lighten up.

I'm not even going to explain the humour in my previous post, if you can't see it, or at least see it is meant humoursly, you're taking life way to seriously. 

If being a coder means having no sense of humor than I guess I'll never be one.


P.S. Adding quotes to strings when not nessesary is just plain wrong. Shoot me.  ;D

Share this post


Link to post
Share on other sites
Hi,

The country code is entered into the DB with phpMyAdmin.  It is put directly into the country code field using the edit function.  That is why I don't understand the '--' since it is entered without quotes as 30, 31, etc.  The field type is a char(3) could that cause the single quotes?

I'm not a programmer, I just DIY whatever needs doing (car, desktop/laptop, websites, etc) and learn along the way.  I'll be repairing a digital camera in the next few weeks :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.