473,698 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with 'Switch'

I have using a 'switch' to retrieve the name of the website section through
sending a 'section' integer...

function title($section)
{
switch ($section)
{
case 0:
$output = "Introducti on";
case 1:
$output = "login";
case 2:
$output = "choose_dat e";
case 3:
$output = "am_pm";
case 4:
$output = "hour";
case 5:
$output = "choose_tim e";
case 6:
$output = "slots";
case 7:
$output = "add_notes" ;
case 8:
$output = "confirm";
case 9:
$output = "booked";
default:
$output = "Introducti on";
}
return $output;
}

However, no matter what number I sent to the command I am getting the
default case out (Introduction).

This seems like a very simple command, where am I going wrong???

Many thanks

Jamie
Jul 17 '05 #1
5 1670
Jamie Wright wrote:
I have using a 'switch' to retrieve the name of the website section through
sending a 'section' integer...

function title($section)
{
switch ($section)
{
case 0:
$output = "Introducti on";
case 1:
$output = "login"; (snip) default:
$output = "Introducti on";
}
return $output;
}

However, no matter what number I sent to the command I am getting the
default case out (Introduction).

This seems like a very simple command, where am I going wrong???


switch's cases fall through to next case, so that you can do

switch($a) {
case 0:
case 1:
whatever();
break;
case 2:
case 3:
somethingelse() ;
break;
default:
nomatch();
break;
}

Here, whatever() will be called when $a is 0 or 1; somethingelse() will
be called when $a is 2 or 3; and nomatch() will be called at all other
times.

In your script, when $section is 0, it will set $output to
"Introducti on", then to "login", then ..., and finally to "Introducti on"
again.
You need break statements in each case to stop the script from going on
to next case statements.
--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #2

"Pedro Graca" <he****@hotpop. com> wrote in message
news:sl******** ***********@ID-203069.user.uni-berlin.de...
Jamie Wright wrote:
I have using a 'switch' to retrieve the name of the website section through sending a 'section' integer...

function title($section)
{
switch ($section)
{
case 0:
$output = "Introducti on";
case 1:
$output = "login";

(snip)
default:
$output = "Introducti on";
}
return $output;
}

However, no matter what number I sent to the command I am getting the
default case out (Introduction).

This seems like a very simple command, where am I going wrong???


switch's cases fall through to next case, so that you can do

switch($a) {
case 0:
case 1:
whatever();
break;
case 2:
case 3:
somethingelse() ;
break;
default:
nomatch();
break;
}

Here, whatever() will be called when $a is 0 or 1; somethingelse() will
be called when $a is 2 or 3; and nomatch() will be called at all other
times.

In your script, when $section is 0, it will set $output to
"Introducti on", then to "login", then ..., and finally to "Introducti on"
again.
You need break statements in each case to stop the script from going on
to next case statements.
--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |


Brilliant. Thanks

Jamie
Jul 17 '05 #3
To paraphrase Detective Deckard, how can a page not know what it is?

"Jamie Wright" <an**@anon.co m> wrote in message
news:cc******** **@news8.svr.po l.co.uk...
I have using a 'switch' to retrieve the name of the website section through sending a 'section' integer...

function title($section)
{
switch ($section)
{
case 0:
$output = "Introducti on";
case 1:
$output = "login";
case 2:
$output = "choose_dat e";
case 3:
$output = "am_pm";
case 4:
$output = "hour";
case 5:
$output = "choose_tim e";
case 6:
$output = "slots";
case 7:
$output = "add_notes" ;
case 8:
$output = "confirm";
case 9:
$output = "booked";
default:
$output = "Introducti on";
}
return $output;
}

However, no matter what number I sent to the command I am getting the
default case out (Introduction).

This seems like a very simple command, where am I going wrong???

Many thanks

Jamie

Jul 17 '05 #4

"Jamie Wright" <an**@anon.co m> wrote in message
news:cc******** **@news8.svr.po l.co.uk...

"Pedro Graca" <he****@hotpop. com> wrote in message
news:sl******** ***********@ID-203069.user.uni-berlin.de...
Jamie Wright wrote:
I have using a 'switch' to retrieve the name of the website section through sending a 'section' integer...

function title($section)
{
switch ($section)
{
case 0:
$output = "Introducti on";
case 1:
$output = "login";

(snip)
default:
$output = "Introducti on";
}
return $output;
}

However, no matter what number I sent to the command I am getting the
default case out (Introduction).

This seems like a very simple command, where am I going wrong???


switch's cases fall through to next case, so that you can do

switch($a) {
case 0:
case 1:
whatever();
break;
case 2:
case 3:
somethingelse() ;
break;
default:
nomatch();
break;
}

Here, whatever() will be called when $a is 0 or 1; somethingelse() will
be called when $a is 2 or 3; and nomatch() will be called at all other
times.

In your script, when $section is 0, it will set $output to
"Introducti on", then to "login", then ..., and finally to "Introducti on"
again.
You need break statements in each case to stop the script from going on
to next case statements.
--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |


Brilliant. Thanks

Jamie

You already got the switch/break answer, but if all you want to do is assign
a string constant, you could also have used an array filled with the 10
strings you need and use the $a as the index.

$arr = array("Introduc tion","login"," choose_date","a m_pm","hour"); // add
the other 5 as well of course...
$output = $arr [$a];

Less typing, and I think more elegant as well. I haven't tried which
solution is faster at runtime. My guess would be the array version too.

HTH
Pjotr
HTH
Pjotr
Jul 17 '05 #5
"Pjotr Wedersteers" <x3****@westert erp.com> wrote in message
news:40******** *************** @dreader10.news .xs4all.nl...
-snip-
$arr = array("Introduc tion","login"," choose_date","a m_pm","hour"); // add
the other 5 as well of course...
$output = $arr [$a];

Less typing, and I think more elegant as well. I haven't tried which
solution is faster at runtime.


And vulnerable to attack

$a = 9999;
echo $arr[$a];

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3315
by: Steve Loft | last post by:
I'm trying to use a switch construct to compare the result of a form with a list of known values and display a different page according to the submitted string. The problem is that some of the answers I need to check for are strings of the character zero of varying length: E.g. <?php switch (trim(strtolower($_POST))):
3
2474
by: ritchie | last post by:
Hi all! Still working on this program! Just to recap, I am writing a program to sort an array with four different sort algorythms. I am having a little trouble at the moment though! Now, I am trying to calculate, with each sort, how many times during the sort the array elements are compared and swapped.
2
1737
by: George | last post by:
I have marked the areas with comments as to what I would like to do and what the code does. The two problem areas are in the key pressing and mousing clicking areas. thanks for the help. This code outputs a set of lines on the screen in different colors /*****************************­*************** ** problem2 .c ** ** --------------- **
27
2299
by: John Salerno | last post by:
Ok, here's a problem I've sort of assigned to myself for fun, but it's turning out to be quite a pain to wrap my mind around. It's from a puzzle game. It will help if you look at this image: http://www.johnjsal.devisland.net/switches.jpg Here's the situation: Each of the four rows in the diagram is considered a single 'panel'. Each panel has eight 'switches', which are composed of two columns each, and these columns have a total of 20...
4
2028
by: ^MisterJingo^ | last post by:
Hi all, I have a web page which has a single dropdownlist containing 3 items. Below the dropdownlist are two listboxes. Depending on the option selected from the dropdownlist, the left most listbox populates itself from a DB table, assigning the correct id to each item as a value. In between the listboxes are two buttons. On clicking an 'add' button, I am trying to grab the id of the item selected (the goal being to place this id and...
8
1933
by: tony | last post by:
Hello! I have below a for loop and a switch in the for loop. I have also a enum called colBlowStep with some values. I have also an array called m_columnBlowStep with some strings. All items in the array m_columnBlowStep is string because I have used ToString on each enum item as you can see. I want to use enum in the case statment in the switch. I know I can use string but I rather want to use enum.
0
1167
by: Rudy | last post by:
Hello all! I am having problem wih filewatch, I think. Basicly I have a trigger that creates a file. Filewatch looks for a modified date on the file, than when it see's it, it should pop up a message box. The problem it pop's up 3 or 4 message boxes. I just need it once.Here some code from app 1 that will execute the store procedure. Private Sub ON_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
2
1568
by: huzzaa | last post by:
I am trying to get the user to input 1-10 and that will select a a number from an array and place it into a variable. here is the code. do { cout << "Enter the first row from column 1 to be summed (1-10)"; cout << endl << endl;
2
2880
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of various module that I found it useful. Here is the 1st problem I encounter: I had a function to edit a event row form the database which is fine with me, than I pass on the code to a function that save(update) the data to the database.
1
1847
by: inferi9 | last post by:
hi, I am writing the Sudoku game code and it is just from 1 to 3 (NOT to 9 like the ususal one) when i did right it give me the win message but when i did it wrong it also give me the win message, the first chart is to tell you the places and the second id the one to fill (you will see it after compile) and after finishing you have to enter the number -1 to see if you win or lose, here is the code.AND I CAN NOT USE ARRAYS #include<iostream>...
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8610
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.