Switch Case And Array

Nov 26, 2006

I have an array and I need to be able to switch case based on the array values. I've tried the following but something is wrong and it's not working:

$section = array(
'varOne'     => 'This is variable One',
'Two'     => 'Work with variable Two',
'var_Three' => 'Post var three',
'myDefault' => 'This is my default');

switch($section) {
case 'postProject':
   echo section['varOne'];
case 'Two':
   echo $section['Two'].$addSomething;
case 'var_Three':
   echo $section['var_Three'].$more;
   echo $section['myDefault'];

What am I missing? Basicly I'd like to match case by array's KEY adn output areray's VALUE... Honestly I'm not on a short hand with arrays.


Switch Statement > Include Multiple Case Matches In A Single Case?

Apr 8, 2011

I'd like to pass multiple values in a single switch case. I realize its not possible they way I'm trying to do it. Is there another way, short of placing each case on its on line?

case 'test1', 'test2':[code]....

View 5 Replies View Related

Switch My Case

Jan 23, 2001

I haven't used switch before, so I thought I'd give it a go. But I ran into some trouble. The case is like this: I have a search results page where, depending on the no of hits, different texts should be printed. Perfect for switch I thought, but I need to use a different range for each case.

Some code:
// connect and query code not included...

switch ($numrows) {
case $numrows==0:
$traff='Do it again...'
case $numrows==1:
case $numrows<=25:
$traff='Should be here somewhere...'
case $numrows<=50:
$traff='a little too many...'
case $numrows>51:

echo "$numrows $traff"; So I always know how many hits I get and this works except when I get no hits. The value of $numrows shows 0, but $traff shows 'Great!!'Why? Or am I doing this wrong?

View 7 Replies View Related

Using The Case Switch

Jun 3, 2001

What i'm looking for is an example of what a link coming from some page to a form page holding a variable that i can use in a switch function after the form is submitted would look like. The action on the form takes me to a script where i will need this variable.

switch ($var)
case "1"
use this $var1; //came from this page
case "2"
use this $var2; //came from this page
print "error";

View 6 Replies View Related

Switch Case

Jun 30, 2007

I have a switch statement and its probably not possible to do what I want to do but I thought I'd ask. I have 2 similar cases (date and datetime) so if its date or datetime I want to show the date, then after that only put the time if its datetime. Now if I wanted the time to go first.

View 7 Replies View Related

Switch Case - How Does Value Get Placed

Nov 11, 2009

I'm looking at trying out using switch case as a way to turn a division either on or off or with an include a file thing. The information I've seen so far is basic. That is, the discussion is on it's primary use. Using a standard link, how does one embed the variable into the link to pass the value on to the action? [URL]... When google is clicked on, the google division shows. When yahoo is clicked the yahoo division is shown.Where and how does the value get placed?

View 1 Replies View Related

Php Case Switch

Aug 15, 2007

I'm implemening an update profile section on my site and I'd like to use switch/case method. e.g. page.com?update=success or page.com?update=failure, when I use the $_GET['update'] it works fine, however what of someone changes the url manually to .com?update=somethingelse ?

How can I stop people getting access to other pages/cases manually.

View 2 Replies View Related

Switch Case More Than 1 Value In The Case?

Nov 12, 2010

I have the next situation. I have the variable, which can has the next values ('Weekly', 'Monthly', 'Quarterly', 'Annual'). And I have another int variable, which can has values from 1 to 10. Now I do the next things:

switch ($var2) {
case 1:
$var3 = 'Weekly';
case 2:


View 2 Replies View Related

Problem With Switch/case, Want It Instead Of If

Jun 7, 2003

I have this code that fetches a nationID from my table.
Depending on what ID it is... it displays a country flag.

View 4 Replies View Related

Switch And Case Problem

Aug 22, 2002

Can anyone give me a helping hand with this code

I want the script to verify a username and password using a switch and case statement.

if the username and password is matched I want to move on to the next section of the site. If there is no match I want it to ask for the username and password again.

The trouble is Im not getting the desired results with the switch and case

echo "
require "./config.inc.php";
$db_connect = mysql_connect ($host,$user,$passwd);
mysql_select_db($database, $db_connect);
if ( $rep_name )
switch ($page) {
case "login":
$result=mysql_query("select * from rep where rep_name='$rep_name'",$db_connect) or die ("cant do it");
while ($row=mysql_fetch_array($result)) {
if ($row["rep_passwd"] != $rep_passwd) {
$failure = 1; break;
echo "successful login";
//echo "Wrong username";
if ($failure == 1)
{ echo "Wrong details"; }
else { echo"
<form method='post' action='$PHP_SELF?page=login'>
Username: <input type='text' name='rep_name'><br>
Password: <input type='password' name='rep_passwd'><br>
<input type='submit' value='Submit'>
echo "

View 4 Replies View Related

Switch / Case Errors

Jun 11, 2002

For some reason I can never get the "Switch / Case" functions to work for me. Can some one tell me whats wrong with this: PHP Code:

View 6 Replies View Related

Using If Statement With Switch / Case

Feb 28, 2007

Is it possible to add an if statement inside of switch case? Below is a small piece of a pretty long switch statement that has about 12 different cases. But I only want it to change/switch cases if certain conditions are met for each particular case.

Can I just add an if statement within the case?

case 'forcetrade': $next_state = 'forceplace'
case 'forceplace': $next_state = 'attacking'

View 5 Replies View Related

Case Switch Best Practice

Aug 15, 2006

I don't know what to call what I want to do, so I'd appreciate suggestions for keywords so that I can study up on it. if it's easy your input of course. I want to have a table of contents plus 10 articles in one php file. Depending on which article link the visitor clicks on, the page will refresh and that section of the php code will show the appropriate article on the page. I've gotten as far as this has something to do with blah.php?article2. I've also found that case switch does this, but that looked very messy for something that seems so simple.

View 4 Replies View Related

Add Two Values For One Case Using Switch?

Dec 8, 2010

im trying to create a website using switch commands to navigate with the contents displayed in a table via echo content. everything works fine. but one of the pages consists of multiple pages. The address looks like this website/index.php?content=home etc. but i want to make it like this for the page consisting multiple pages website/index.php?content=home&page=1

my index code:

case "home":
$content = file_get_contents('home.php');...............

but when i do it like this the echo command shows me the code of the home.php but not the one of the page i wanna load inside of it.

View 4 Replies View Related

Switch Case / Add Regexp?

Apr 6, 2011

how can fix it or add regexp ?

case substr($rrr['url'],-4)=='.jpg' || '.png' || '.gif' || '.tif' || '.tiff':

View 4 Replies View Related

Use Switch Statement And Case Instead Of If And Else

Aug 7, 2010

I am trying to understand how to use switch and case instead of if and else. I have this code that works


I get this error... Parse error: parse error, expecting `'('' in C:wampwww estmaintain.php on line 129. WHich in this case matches up with the 'case empty:' line I have two questions... First how do I change the above code so that it works like the if else statement?

View 5 Replies View Related

Switch Case With Three Parameters?

Aug 25, 2009

Is it possible to use three parameters in switch-case, like this:

switch($var1, $var2, $var3){
case true, false, false:
echo "Hello";

If not, should I just use if-else or is there a better solution?

View 7 Replies View Related

Using Variable In Another Switch Case?

Oct 23, 2010

Yes i have this line in my first "upload" case of the switch:

$file_name = $USER . "-".rand(1, 888).".jpg";

Then in my switch case "crop" (that takes care of the image you crop), I wish to replace the uploaded one with the new one, simply by uploading with same name. Now i cant use $file_name in the case "crop" so how can I do this?

View 2 Replies View Related

Two Values In The Case Of Switch?

Sep 5, 2009

If "car" or "ferrari" as an input, it should print "car or ferrari". How can I achieve it?

$car ='333';

View 5 Replies View Related

Switch, Same Value For Multiple Case?

Sep 2, 2010

switch ($i) {
case A:
$letter = 'first';

Is there any way to shorten first three cases? They have the same values inside.

View 2 Replies View Related

Header() Ignored In Switch/case Statement

Apr 21, 2003

Can anyone tell me why in the 'unreg' part of this switch/case statement, the header() is being completely ignored? I am including the code for for the switch/case, plus the code on the page referenced in the header() redirect. PHP Code:

View 5 Replies View Related

Automaticly Create A SWITCH Case

Sep 16, 2003

I tried to automaticly create a SWITCH Case with all files in a specific directory. PHP Code:

View 5 Replies View Related

Switch And Case With Special Characters...

Feb 1, 2002

i have a switch and case section. the switch parameter is a veriable called $Char and a character it is (i.e. "e" or any other character (not its ascii value)). how can i put the chars $ " in the case command?

View 1 Replies View Related

Importing StyleSheet Using Switch/Case

Aug 16, 2004

I'm using a simple radio group buttons to let the user select the stylesheet they want.
Then the switch/case script imports the desired stylesheet. What is wrong with this script (because it doesn't work)?

Okay I know it is the "@import/blue.css"; but I can't figure out what it should be. Is there a site that explains how to do it?

View 1 Replies View Related

Switch Case Image Maps

Nov 8, 2005

Seems my switch case statement isn't working, and I can't see why it isn't.

The App: The webapp I am making is a webpage that uses imagemaps as a menu system. The menu is basically a table with 6 columns and 5 rows. Each row is a seperate image. When a cell of the "table" is clicked, the options under that tables category are displayed as pictures. I did this by passing a variable and reloading the page when a hotspot on the image map is clicked. When the page reloads a switch case is used to display either a row with a cell selected, or the default unselected row.

Below is the select case that will not work properly. The Map functions all work (they have been tested) and pass the variable.

PHP Code: .....

View 4 Replies View Related

Switch Statement - First Case Always Gets Executed

May 7, 2005

here's part of my script:

switch ($member)
case (('justus') ¦¦ ('Justus')):
echo "bla";

case (('tsavo') ¦¦ ('Tsavo')):
echo "blah";

and here's my problem: no matter what $member equals, the first case always gets executed. what am i doing wrong?

View 1 Replies View Related

Default: Switch To Other Case Option?

May 2, 2011

Say I have something like this:

switch($_GET['func']) {
case 'foo':
case 'orange':
if($_GET['aubergine'] == 'catdog') {

How should I implement do_default_option()? In case you didn't get it, I want do_default_option to skip to the default: part of the switch() statement.

View 2 Replies View Related

Switch With Multiple Values Ver Case?

Aug 21, 2009

How would you do this PHP switch statement? Also note that these are much smaller versions, the 1 I need to create will have a lot more values added to it.

Version 1:
switch ($p) {
case 'home':
case '':
$current_home = 'current';
case 'users.online':

View 8 Replies View Related

Syntax Error And Switch Case?

Jul 13, 2010

I can't find a reason for why I'm getting this error:

Parse error: syntax error, unexpected '{' on line 4

if I should use a switch case in my code instead of what I have at the moment, or leave it as is?

$error = "An error occurred, please press f5 to refresh the page and try again.";


View 1 Replies View Related

Switch Case - Access Control Of Page

Jun 10, 2010

I want to control the access in php website. I have a solution right now with switch case.


But when i have so many pages, it becomes difficult to manage them. Do you have better solutions? Right now, i develop the application using php4. And i want to use php5.

View 4 Replies View Related

Magento - HTML CSS Beginner USE CASE / SWITCH / IF ELSE?

May 27, 2010

My idea was to use CSS to show the TAB over the relevant Navigation menu item based on the URL.However, one URL always changes, so I wanted to somehow use an ifelse statement.I've come up with two methods that I think could work, could any experts tell me what they would think is best and how they would implement it?

Method 1
<div id="nav">
<ul id="mainnav">
<li><a href="index.php" title="Welcome page" <?php if ($page == 'index.php') { ?>class="active"<?php } ?>>Welcome</a></li>

View 4 Replies View Related

Read From Text File And Then Using A Switch Case Not Working Together?

Jan 26, 2011

I'm testing out small bits of code for myself, and I'm trying to understand why this isn't coming out how I would imagine it to...I have my fution which stores colors in an array from a text file, then I return back a random color from my array like so...

function getColor() {
$ball_color = explode("
", file_get_contents('colors.txt')); // Takes file contents and stores them into an array.
return $ball_color[array_rand($ball_color)];

View 3 Replies View Related

Possible To Include Conditional IF Statements Inside Switch Case Declarations?

Mar 21, 2006

Is it possible to include conditional IF statements inside switch case declarations?

For example... Instead of doing...

View 2 Replies View Related

Convert An IF ELSE Statement To A SWITCH CASE Statement?

Nov 25, 2005

How do I convert an IF ELSE statement to a SWITCH CASE statement? The switch case below doesn't work.

$textlen = strlen($descr);

if($textlen>10 && $textlen<50) {
$completeness = 1; $deduct = 1000;
elseif($textlen>50 && $textlen<100) {
$completeness = 2; $deduct = 500;
else {
$completeness = 0; $deduct = 2000;

switch($textlen) {
case >10 && < 50: $completeness = 1; $deduct = 1000; break;
case >50 && < 100: $completeness = 2; $deduct = 500; break;
default: $completeness = 0; $deduct = 2000; break;

View 3 Replies View Related

Shortening A "for / Switch -case"

Aug 4, 2010

A school system has 30 schools. The lowest school code is 298 and the highest is 516. For every school, the same processes will be called. The approach I've taken so far can be seen below. How can I shorten this code?

for ( $i = 298; $i <= 516; $i++ )
switch ( $i )
case 298:
$c_sch = strval ( $i ) ;
// call a function
uf_mem_requester ( $c_sch, $t_mem1, $t_mem2, $t_mem3,.........

View 3 Replies View Related

"Else Case" In The Switch Function To Use?

Feb 10, 2010

A switch statement consists of "cases"...But is there any "else" case for all other cases?Have never found the answer to this...

switch ($var){

View 3 Replies View Related

Switch From A Hard-coded Array To A Dynamic Array Read From A Db Table?

Sep 25, 2009

While attempting to switch from a hard-coded array to a dynamic array read from a db table, I'm running into a few snags. I'm using the following function to create the dropdown menu, and it has worked for everything thus far:

function showDrop($array, $active, $echo=true){
$string = '';
foreach($array as $k => $v){
$s = ($active == $k)? ' selected="selected"' : '';
$string .= '<option value="'.$k.'"'.$s.'>'.$v.'</option>'."
if($echo) echo $string;


This should be an easy fix, but I'm just not seeing it.

View 3 Replies View Related

From Array To Switch... How To?

Jul 17, 2005

I'm trying to get some data from MySql database and to create switch
based on those data, but as an php beginer, I have no clear idea how to
do this. have tryed many options which loked promising, but with limited
success. here are two versions of code:

the 1st one is executing just 1st loaded data, ie: if case is "id_001"
it will print "data for id #001", and will, in case there is nothing in
url like "index.php?action=" print "nothing" as expected. but It will
also print that ugly "nothing" for "id_002", etc...

//just taking something from mysql
$query = "SELECT some_id,some_data FROM modules";
$result = mysql_query ($query) or die ("Query failed ($query)");
list($some_id,$some_data) = mysql_fetch_array($result);

switch ($action)
case "$some_id":
print "$some_data";
print "nothing";

the 2nd one is executing all I want and "much more"..! it will in case
of "id_003" print "yet another something about #003", but will print
"nothing" (default statement) as much times as I have rows in my mysql

//just taking something from mysql
$query = "SELECT some_id,some_data FROM modules";
$result = mysql_query ($query) or die ("Query failed ($query)");
while(list($some_id,$some_data) = mysql_fetch_array($result))
switch ($action) {
case "$some_id":
print "$some_data";
print "nothing";

can anyone help me to find the right way to do this? (I have shortened
code (querys) in this post, actualy, this is suposed to load different

View 4 Replies View Related

Keep Page In GET Array To Use Switch

Jan 13, 2010

I need to direct users to a page based on their response to a form. Here is my code:


I am using index.php as a switch to insert questions into the page like this:


How can I keep the page in the GET array to use the switch and also include the logic from the form to direct the user to the next page depending on their response?

View 10 Replies View Related

Using Switch With Array Of Cases?

Nov 8, 2010

I've been looking for a faster(both to code and to load) so I tried storing all of my cases in an array.  I want to use a foreach statement to load all the information for the appropriate case which includes classfile, class name, and methods within that class.  I guess you can't use a foreach loop inside of a switch function.

Code: [Select]<?php


View 1 Replies View Related

Regex - Regular Expression To Convert String Upper Case To Lower Case?

Jun 17, 2011

In a single PHP file, demonstrate a regular expression to convert "123 Tree Street, Connecticut" into "123_tree_street_connecticut".I have successfully replace spaces and comma with _, but unable to change character case using Regular expression in php. what i have did is

echo preg_replace('/(,s|s)/', '_', '123 Tree Street, Connecticut');

It replaces spaces and commas with _ but not able to change it's case.Can any one guide me to how it is done Using php and regular expression only.

View 4 Replies View Related

Autoload - From Camel Case To Underscore Case Conversion __autoload() Function?

Oct 19, 2009

PHP manual suggests to autoload classes like

function __autoload($class_name){

and this appoach works fine to load class FooClass saved in the file my_dir/FooClass.php like

class FooClass{
//some implementation

how can I make it possible to use _autoload() function and access FooClass saved in the file my_dir/foo_class.php?

[Note: 1. the CamelCase and under_score notations on the files

2. the manual link is : http:[url].....

View 2 Replies View Related

URL Case - Redirect All Urls To Lowercase, But Internal Navigation Is All Mixed Case?

Jun 6, 2011

by default my urls structure is in mixed case, e.g [url]...  and Google for some reasons is indexing two versions of my urls [url].... and [url].... . Now i want to 301 redirect all urls to lowercase, but my internal navigation is all mixed case e.g the category page [url]...has all the products listed in the page with mixed case.I know its bad programming,

View 3 Replies View Related

Change The Case Of An Array

Jan 5, 2006

What is the function to change the case of an array to lower or upper case? That's the values, not the keys. I've searched the manual, and all I can find is the array_change_key_case() but no array_change_case(). Is there a function to do this?

View 2 Replies View Related

Convert Russian Characters From Upper Case To Lower Case?

Apr 25, 2011

'm trying to change the case of russian characters from upper to lower.


View 2 Replies View Related