Search the Community
Showing results for tags 'switch'.
-
class ufo { const MP_SIZE_LARGE = 600; const MP_SIZE_MEDIUM = 400; const MP_SIZE_SMALL = 250; const LARGE = 'LARGE'; const MEDIUM = 'MEDIUM'; const SMALL = 'SMALL'; public $mp_size; public function desired_width() { switch ( $this->mp_size ) { case ufo::LARGE : return ufo::MP_SIZE_LARGE; case ufo::MEDIUM : return ufo::MP_SIZE_MEDIUM; case ufo::SMALL : return ufo::MP_SIZE_SMALL; default: return 0; } } } So check out the class and function above. What should "desired width" return if mp_size is 0? It makes no sense that the value is anything but 0. Instead, it returns 600. In fact, it will return 400 if I put the "medium" case first. Why would it do that? The function returns expected values if I put strval() around the mp_size. Can someone explain that? This makes absolutely no sense to me. I can't see how the order of the cases could possibly matter, and I also can't see how any case would be "true" when 0 doesn't equal "LARGE".
-
Hello, I don't know if this is possible. I want to make a switch between pages, lets say i have more than 100 pages and each page have a unique name.php. So what i want to do is when page equal to random-page.php SELECT * FROM `jobs` WHERE title LIKE '%news%' And for example how it should be, but i don't know what is exactly wrong here and how to fix it. $sql_q; $path=$_SERVER['PHP_SELF']; $page=basename($path); switch("$page") { case 'index.php': $sql_q = 'SELECT * FROM `jobs` WHERE `title` LIKE '%news%' LIMIT $p_num, $per_page'; break; case 'jobs.php': $sql_q = 'SELECT * FROM `jobs` WHERE `title` LIKE '%jobs%' LIMIT $p_num, $per_page'; break; case 'region.php': $sql_q = 'SELECT * FROM `jobs` WHERE `title` LIKE '%region%' LIMIT $p_num, $per_page'; break; } And here: $getquery = mysql_query("$sql_q"); Is that possible somehow ? Thanks.
-
Is there a nicer, cleaner more efficient and less code way of achieving this? I have an array of rows from the database and 2 of the columns could have either of 2 values which give me 4 possibilities. I want to loop through each of them and assign the row to one of 4 arrays so they are all separated out. This is what i have so far. Haven't tested it yet but the theory makes sense. if(!empty($rows)) { foreach($rows as $key => $row) { switch($row['type']) { case 'post': switch($row['archived']) { case 0: $array1[$key] = $row; break; case 1: $array2[$key] = $row; break; default: } break; case 'event': switch($row['archived']) { case 0: $array3[$key] = $row; break; case 1: $array4[$key] = $row; break; default: } break; default: } } }
-
<html> <head> <!--title>Hello</title--> <link rel="stylesheet" href="uvcs.css" type="text/css" /> </head> <body> <div id=everything> <div id=header> This is the header </div> <div id=clear></div> <div id=navmenu> <div id=navmenu> <ul> <li> <a href="index.php?cmd=home">HOME</a> </li> <li> <!--submenu--> <a>VIEW</a> <ul> <li> <a href="index.php?cmd=student">STUDENT PROFILE</a> </li> <li> <a href="index.php?cmd=employee">EMPLOYEE</a> <!--subsubmenu--> <!--ul> <li> <a href=#>subsubmenu1</a> <a href=#>subsubmenu2</a> </li> </ul--> </li> </ul> </li> <li> <a href="index.php?cmd=courses">COURSES</a> </li> <li> <a href="index.php?cmd=aboutus">ABOUT UVCS</a> </li> <li> <a href="index.php?cmd=itsupport">IT SUPPORT</a> </li> <li> <a href="index.php?cmd=login">LOGIN</a> </li> </ul> </div> </div> <div id=clear></div> <div id=maincontent> <?php include("selector.php"); ?> </div> <div id=sidebar> side bar </div> <div id=clear></div> <div id=footer> Footer </div> </div> </body> </html> <?php $cmd =$_GET['cmd']; switch($cmd) { case 'home': include_once "home.php"; break; case 'student': include_once "student.php"; break; case 'employee': include_once "employee.php"; break; case 'courses': include_once "courses.php"; break; case 'aboutus': include_once "aboutus.php"; break; case 'itsupport': include_once "itsupport.php"; break; case 'login': include_once "login.php"; break; default: include_once "home.php"; break; } ?> These are my codes that supposedly would show home.php (which contains only the text "you are viewing home.php"), when you open the index.php. But for some reason I would get the line Notice: Undefined index: cmd in C:\xampp\htdocs\UVCSPortal\selector.php on line 2 you are viewing home.php I thought with these codes with would go directly to default case. But instead it reads the <?php include("selector.php"); ?> in index.php twice, I think. I tried using isset, but couldn't understand it.
-
Hi, I'm making a code for determining the astrological sign of anyone. The code receives a date in this format "YYYY-MM-DD" and strips that string in year, month and day. So I've wrotten this function: //determinar signo public function Determinar(){ switch ($this->month){ case 01: if ($this->day <= 19){ $this->signo = "Capricornio"; } else { $this->signo = "Acuario"; } break; case 02: if ($this->day <= 18){ $this->signo = "Acuario"; } else { $this->signo = "Piscis"; } break; case 03: if ($this->day <= 20){ $this->signo = "Piscis"; } else { $this->signo = "Aries"; } break; case 04: if ($this->day <= 20){ $this->signo = "Aries"; } else { $this->signo = "Tauro"; } break; case 05: if ($this->day <= 20){ $this->signo = "Tauro"; } else { $this->signo = "Geminis"; } break; case 06: if ($this->day <= 20){ $this->signo = "Geminis"; } else { $this->signo = "Cancer"; } break; case 07: if ($this->day <= 20){ $this->signo = "Cancer"; } else { $this->signo = "Leo"; } break; case 08: if ($this->day <= 21){ $this->signo = "Leo"; } else { $this->signo = "Virgo"; } break; case 09: if ($this->day <= 22){ $this->signo = "Virgo"; } else { $this->signo = "Libra"; } break; case 10: if ($this->day <= 22){ $this->signo = "Libra"; } else { $this->signo = "Escorpio"; } break; case 11: if ($this->day <= 22){ $this->signo = "Escorpio"; } else { $this->signo = "Sagitario"; } break; case 12: if ($this->day <= 20){ $this->signo = "Sagitario"; } else { $this->signo = "Capricornio"; } break; } } In this switch, cases 01 to 07 and 10 to 12 are working correctly. But cases 08 and 09 doesn't works. I can't find the problem/error, syntax seems to be correct
-
Hi, I have the code below that using for a simple login system (security not a concern at all) This code works but it now has around ten if statements (not included them all for clarity), would this be best written as a switch statement? if so can you help me code it. I can write basic switch statements but I don't know how to match two cases. The login and the password. If a switch statement isn't the best option can you suggest what is and perhaps give me an example? Thanks guys! The code: <?php session_start(); $is_ajax = $_REQUEST['is_ajax']; if(isset($is_ajax) && $is_ajax) { $username = $_REQUEST['username']; $password = $_REQUEST['password']; if($username == 'richard' && $password == '123') { $_SESSION['isLoggedIn'] = true; echo ("login-richard"); } if($username == 'john' && $password == '456') { $_SESSION['isLoggedIn'] = true; echo ("login-john"); } } ?>
-
I have a switch that uses a rand() to find the respective cases in between a number range, like so: switch($number = rand(1,10)) { case ($number > 1 && $number <= 5): doFunction(); break; case ($number > 5): doFunction(); break; } how would I use this in an array format? I find using switches to be clunky and limiting. I want to be able to do like: $array = array( 1-10 => 'value', 11-20 => 'value' ) Is this possible?