spare Posted July 23, 2006 Share Posted July 23, 2006 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.RegardsThe 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] Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/ Share on other sites More sharing options...
448191 Posted July 23, 2006 Share Posted July 23, 2006 Use "==" not "=" to compare. "=" assigns, "==" compares. Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-62499 Share on other sites More sharing options...
kenrbnsn Posted July 23, 2006 Share Posted July 23, 2006 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]<?phpswitch ($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 Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-62502 Share on other sites More sharing options...
448191 Posted July 23, 2006 Share Posted July 23, 2006 True, "==" is obsolete in this context. Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-62504 Share on other sites More sharing options...
spare Posted July 25, 2006 Author Share Posted July 25, 2006 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 <>. Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-63618 Share on other sites More sharing options...
kenrbnsn Posted July 25, 2006 Share Posted July 25, 2006 Is $country_codequery a number or a string? If it is a string, try[code]<?phpswitch ($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 Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-63628 Share on other sites More sharing options...
ryanlwh Posted July 25, 2006 Share Posted July 25, 2006 i have a feeling that $country_codequery is actually a query resource id... or a query result. Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-63629 Share on other sites More sharing options...
448191 Posted July 26, 2006 Share Posted July 26, 2006 [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]<?phpecho '<pre>';print_r($dataArray);echo '</pre>';?>[/code]To show us it's contents Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-63880 Share on other sites More sharing options...
spare Posted July 26, 2006 Author Share Posted July 26, 2006 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' Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-64329 Share on other sites More sharing options...
448191 Posted July 27, 2006 Share Posted July 27, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-64544 Share on other sites More sharing options...
sonal4php Posted July 27, 2006 Share Posted July 27, 2006 give a try without default switch caseit once happened with mei need to scroll back to see, what have i done for thatjust a thought, try it w/o defaulti know that it's not a solution, but a thought...will get back again Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-64553 Share on other sites More sharing options...
kenrbnsn Posted July 27, 2006 Share Posted July 27, 2006 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]<?phpswitch ($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 Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-64634 Share on other sites More sharing options...
448191 Posted July 27, 2006 Share Posted July 27, 2006 [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! Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-64846 Share on other sites More sharing options...
spare Posted July 27, 2006 Author Share Posted July 27, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-64865 Share on other sites More sharing options...
kenrbnsn Posted July 27, 2006 Share Posted July 27, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-64879 Share on other sites More sharing options...
spare Posted July 28, 2006 Author Share Posted July 28, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-65458 Share on other sites More sharing options...
kenrbnsn Posted July 28, 2006 Share Posted July 28, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-65462 Share on other sites More sharing options...
Ninjakreborn Posted July 28, 2006 Share Posted July 28, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-65471 Share on other sites More sharing options...
448191 Posted July 29, 2006 Share Posted July 29, 2006 Ohter than that, it's just plain wrong. ;D Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-65554 Share on other sites More sharing options...
kenrbnsn Posted July 29, 2006 Share Posted July 29, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-65575 Share on other sites More sharing options...
448191 Posted July 29, 2006 Share Posted July 29, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-65593 Share on other sites More sharing options...
spare Posted July 29, 2006 Author Share Posted July 29, 2006 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 :) Quote Link to comment https://forums.phpfreaks.com/topic/15418-switch-statement-right-usage-or-error/#findComment-65633 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.