Possible To Include Conditional IF Statements Inside Switch Case Declarations?
Is it possible to include conditional IF statements inside switch case declarations?
For example... Instead of doing...
View Complete Forum Thread with Replies
Related Forum Messages:
Conditional Statements And Loops
Not long ago I was asked to give PHP lections to some private IT school. I'm on second lection now, and will be teaching my :) students conditional statements (if..else and switch) and loops (while and for). That was my story, now the question. Can you give me an advise on some geeky use of those statements to stir up some enthusiasm in students.
View Replies !
Question On Conditional Statements
I'm a perl programmer and am trying to learn PHP. So far I have figured out most of the differences, but have not been able to find out how to do the following: When running through a loop, how can you test two separate conditions against the same $element of an array. For example, this is how I thought it would be done (similar to Perl), but it did not work: if (eregi("apple", $line) and (eregi("orange", $line) { do whatever; } Basically testing each line of an array to see if both the words "apple" and "orange" are present. Also, is there any equivalent to the following from Perl while running through a loop: next if (whatever conditions); last if (whatever conditions);
View Replies !
Avoiding Endless Conditional Statements
Here is my problem. I want to make a webapp that will basically take the work out of finding what tool works for what situation. There are 5 factors that go into tool selection. 1)Material Group, 2)Insert Radius, 3)Insert Type and Size, and 2 others that I can't remember off-hand. There are probably about 1000 different scenarios that I have to code for..(there are several blank table cells and it's not setup as you might imagine, there is a lot of overlapping), and I desperately want to avoid making a gigantic conditional statement. I'm afraid that I really have to provide a picture of what I'm doing here, so I will do that. Here is the link. You only see 3 of the 5 factors on this page, and the other 2 have to do with whether this table or one of many other tables (not pictured) will be used. So, now that I've confused everyone. That's my table. What's a good way to go about programming that sucker in so that people don't have to do all the "work" involved to figure out what tools they need. They should just be able to choose selections from 5 dropdown boxes and get one tool back. A tool is something like "10.655.600".
View Replies !
Mysql Real Escape String() In Conditional Statements
I created the following bit of code that allows me to pass a MySQL conditional statement to a function. I am trying to figure out where and how would I go about incorporating the mysql_real_escape_string() function? Is there a way to call the mysql_real_escape_string() in the function itself? Code:
View Replies !
Case Statements
I am fairly new to php and I was wondering if there was something similar to case statements in C++. It would be a lot easier than writing about 20 if statements.
View Replies !
Switch My Case
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... $numrows=mysql_num_rows($result); switch ($numrows) { case $numrows==0: $traff='Do it again...' break; case $numrows==1: $traff='Great!!' break; case $numrows<=25: $traff='Should be here somewhere...' break; case $numrows<=50: $traff='a little too many...' break; case $numrows>51: $traff='Whoa!!!' break; default: $traff='OK!'} 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 Replies !
Using The Case Switch
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 break; case "2" use this $var2; //came from this page default: print "error"; }
View Replies !
Php Case Switch
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 Replies !
Switch Case
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 Replies !
Switch And Case Problem
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 <?php echo " <html><head><title>Aventis</title></head> <body>"; require "./config.inc.php"; $db_connect = mysql_connect ($host,$user,$passwd); mysql_select_db($database, $db_connect); $failure=0; 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"; } //default: //echo "Wrong username"; //break; }} 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 " </body> </html>"; ?>
View Replies !
Using If Statement With Switch / Case
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? switch($current_state){ case 'forcetrade': $next_state = 'forceplace' break; case 'forceplace': $next_state = 'attacking' break; }
View Replies !
Switch Case And Array
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']; break; case 'Two': echo $section['Two'].$addSomething; break; case 'var_Three': echo $section['var_Three'].$more; break; default 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.
View Replies !
Case Switch Best Practice
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 Replies !
Header() Ignored In Switch/case Statement
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 Replies !
Importing StyleSheet Using Switch/Case
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 Replies !
Switch Case Image Maps
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 Replies !
Switch Statement - First Case Always Gets Executed
here's part of my script: switch ($member) { case (('justus') ¦¦ ('Justus')): echo "bla"; break; case (('tsavo') ¦¦ ('Tsavo')): echo "blah"; break; } and here's my problem: no matter what $member equals, the first case always gets executed. what am i doing wrong?
View Replies !
IF Statements Inside Arrays
Is it possible to put an IF statement inside an array? I want to display a member's IP adderess but only to admins/moderators and need an if statement to do it. I have the address displayed in a template file in which i have arrays replacing words with strings. I don't want to have any php in the template file so would the following work? 'IP_ADDRESS' => if($perm == 1){ echo "$ip_address"; }, Would that work? If not, I know there are other ways to do it, but I'm not sure what they are. How else could I do this?
View Replies !
If-else Or Switch Inside A Vprintf()
The code below pulls all the data from the database correctly but I want to change what is displayed. Usually I would just use an if-else or a switch but the field i want to change is inside a vprintf(). Here's the code:
View Replies !
Using Foreach Inside Switch
On a form there's a list of countries for the user to choose from. On the processing script, how can I check to make sure that users have indeed choosen from the list I have provided? I tried the following but it gave me a "parse error, unexpected T_FOREACH, expecting T_CASE or T_DEFAULT or '}'" on the line with the foreach statement. How should I code this security check? Code:
View Replies !
Need 200+ SELECT Statements Inside A Foreach Loop
I have to query 4 tables in order to get the final result. But using left join to query all the tables together is just too slow. If I perform the queries seperately, I will need 200+ SELECT statements inside a foreach loop. Both approaches take about 10 seconds and 5 seconds respectively. Is there anyway to optimize the SQL query?
View Replies !
PHP Conditional Include
I just set up a perl script that outputs a single html file. Works great, but I need to be able to customize that one file for different sections of the site. Specifically I need different navigation and heading on the page. I'm not a programmer, but this sounds like I want a conditional include to show what I want when I want, right? If so, can anyone give me any idea how to get started with something like this? I figured I'd try with PHP because it sounds easier than fooling with perl. Also, doesn't a conditional statement generally work from a variable or something else in the page that changes? If so, what do I do when the page stays the same and doesn't include variables?
View Replies !
Include In Conditional
Why does this not work? if (!$artnr) { include "./smalltable.php"; } else { include "./bigtable.php"; } Is this the best php group for this sort of question?
View Replies !
Sessions + Conditional Include
I am having a little problem in trying to figure out why my session state is not expiring after I close the browser window. Having been used to the ASP way of doing things I am trying to get my head around this slightly different method of handling sessions in PHP.... What happens is that I log on to the site, do various things and then close it when I am finished. I return the next day to find that the 'logged in' menu is displayed first which shouldnt happen until you have logged in again. I assumed that the session was been stored as a cookie but wasnt been released after the session terminates by default, but found I was wrong when I deleted all my internet files and went back to the site. My code for the first page is as follows..
View Replies !
Content Switch Inside A Content Switch
I have my index.php file which is the layout for the site, and includes a section that im using a: <? switch($act) { case 'rand': include('includes/random.inc.php'); break; case 'submit': include('includes/submit.inc.php'); break; case 'admin': include('admin/admin.php'); break; case 'adminlog': include('admin/adminlogin.php'); break; default: include('includes/new10.inc.php'); break; } ?> In the admin.php page, it is a form, which the action is adminlogin.php Inside a part of the adminlogin.php page, i have another switch, that again i want to use the same template as index.php, and insert itself in the same place. adminlogin.php <?php session_start(); include "../includes/config.inc.php"; mysql_connect ($dbhost, $dbusername, $dbuserpass); mysql_select_db($dbname) or die(mysql_error()); $query="SELECT * from $logtable where username='$username' and password='$password'"; $result= mysql_query($query) or die("Could not execute query: $q." . mysql_error()); if (mysql_num_rows($result) == 0) { echo "<div align=center><b>Oops! Your login is wrong. Please click back and try again.</b></div>"; } else { if ($_SESSION['login_username'] = $username) { switch($act) { case 'edit': include('edittuts.php'); break; default: include('edittuts.php'); break; } } else{ header("Location: ../index.php?act=admin"); } } ?> But this isn't working correctly. I want to be able to get to all of these things through an index.php?act=adminlog&act=edit Im thinking the code i am currently using should work, but it doesn't. Any idea how to do this but make it work?
View Replies !
Include Statements
I am currently using a perl script to process form data. I don't want to lose the functionality I have with this script but need to output the data to a mysql db. Someone mentioned that I should use includes to process the form with a php script. Can one form be processed by two scripts at the same time. If so is this a bad idea? It sounds like it would solve my problem if it would work. The problem is that I have done nothing more than setup a fre script that requires no coding. I don't even know how to implement an include statement.
View Replies !
Include Switch
I am trying to use the code to change pages from a menu link. It does accomplish that except link 1 will include page2 and page3 information. Page2 includes page3 information. Finally page3 only contains page3 information. I need each link to only display the called page information. What I am doing is I have a 3 column built. Index.php has includes for the header.php, left_menu.php, right_menu.php, footer.php, and the body.php I am creating a menu to different pages within the website and my goal is to have each link only change the content of body.php. So if I click on a link to page1, page1 content will load in the body area. Click on page2 and page2 content will load into the body area w/o the page1 content being there. hope this makes sense. If you know a better way to accomplish this with out the use of frames that could work as well. <html> <head> <title>My Page</title> </head> <body> <!-- Page Header --> <?php include("header.php"); ?> <!-- End Page Header --> <a href="index.php?page=page1">Click for page 1</a><br /> <a href="index.php?page=page2">Click for page 2</a><br /> <a href="index.php?page=page3">Click for page 3</a><br /><br /> <!-- Page Content --> <?php switch($_GET['page']){ case "page1": include("page3.php"); case "page2": include("page2.php"); case "page3": include("page1.php"); } ?> <!-- End Page Content --> </body> </html>
View Replies !
Include() In Echo Statements
I have a problem in that I have no clue on how to incorporate an include()statementinside an echo. The include is: PHP Code: include $inc . 'link.php' the echo statement where I want to include it is simliar to this: PHP Code: echo '<td>' . /* it goes in here */ . '</td>' Everytime I do this I get PHP errors when accessing the page. My alternative was to define a variable which contained the include statement and then just add the variable in the echo. Like this: PHP Code: $link = include $inc . 'link.php' echo '<td>' . $link . '</td>' However the strange thing is that the included file is parsed on the line that the variable is defined and not where the variable is placed (in the actual file the variable is defined outside the tables so the included content appears not where it should). How can I solve this?
View Replies !
Include In While-switch-loop
I have a system used for multiple sites. Most functions are standard, but some sites have some extra functions. I want the extra functions in a file by itself, so the files containing the standard functions can be the same in all sites. I tried while (..) { switch ($op) case "std1": ... break case "std2": ... break @include "custom_functions.php"; default: .... break; } custom_functions.php: <? case "custom1": ... break; ?> But custom_functions.php are never included. It appears that the include takes place at run-time. Is there some way to include at compile-time? Or other neat way to accomplish what I want: To have custom bits in separate file.
View Replies !
Include() And Short Echo Statements
I seem to have some very strange behaviour from the include() command. Script 1 contains: include("http://www.myurl.com/dir/script2.php"); and script 2 contains: echo "12345678"; If I do that, I get the error message: Warning: main() [function.main]: stream does not support seeking in http://www.myurl.com/dir/script2.php on line 078l(,@¨Ê The garbage at the end seems pretty random except the first few chars relate to the end of the echo statement. However once I get to 14+ chars in the echo statement or if I reference the include via /home/login/public_html... it works fine. Is this a known thing with PHP or is it more likely the PHP is corrupted somewhere?
View Replies !
Template Class - Include Statements
I have managed to build a small template class that can so far parse template variables. Now, to move one step further, I have begun to put in a template include part (includes separate templates within a template file). Here's what I have so far (only the include parts are shown): Code:
View Replies !
I Moved My Software To A New Server And Now The Include Statements Won't Work At All
I wrote a PHP content management system and it worked fine on old servers. I just moved it to a new server on Rackspace. I'm trying to debug. At the top of the first file (index.php) I need to include a file. The file won't be included. In the code below, $success remains false. I've checked a million times and the file is there. error_reporting(E_ALL); $success = include("ppKernel/McControllerForAll.php"); if (!$success) $success = include("ppExtras/McControllerForAll.php"); if (!$success) $success = include("McControllerForAll.php"); if (!$success) $success = @include("../McControllerForAll.php"); if (!$success) $success = @include("../../McControllerForAll.php"); if (!$success) $success = @include("../../../McControllerForAll.php"); if (!$success) $success = @include("../../../../McControllerForAll.php");
View Replies !
Convert An IF ELSE Statement To A SWITCH CASE Statement?
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 Replies !
Getting The URL Inside An Include
I have an include that appears in my php page that has a form, and upon submitting the form I want it to take people to thepage.php?id=whatever for example. However because the <? echo($_SERVER['REQUEST_URI']);?> . is in my include file I get includefile.php?id=whatever. Is there a way I can request the url of the page that the include is part of, and not the location of the include itself? One way would be to preset the URL in a seperate variable, but there's gotta be a way to avoid that.
View Replies !
Pharsing Arrays Inside A PHP With Include
need to keep a array from being seen but needs to be pharsed so that it is included from within another php. <? php include "/dir/hidden.php?array1=Tadaa" ; ?> What happens is the include thinks the whole file name to include in everything that is inside the quotes and the array's are not pharsed and do not work. How would I go about making this work using the above example?
View Replies !
Problems W/ Using An Include Inside A Table
I have a form for my customers to register for events that my company is sponsoring. Inside the form is a table with various text fields, etc for my customers to fill in. Some of our events are free, some are not. I built a separate page with the payment fields on it, and had hoped to include it on the registration form when the url passed a parameter of "?charge=yes". I had inserted an if statement in a cell in the table to call the include file. The if statement works. When my urls is used like above, the include file shows, but it actually shows up on top of my other form, making the page largely unreadable.
View Replies !
Code Inside Include File
I'm new to php and i have an include file with some php inside that kills the rest of the page content for some reason. I have this code in my webpage: <?php session_start(); session_name('polv_id'); $page_title = 'User Login' include('./header.php'); include('./pageheader.php'); <--- This is the problem include('./pagecontbody.php'); include('./pageleftcol.php'); include('./pagerightcol.php'); ?>
View Replies !
Rollover Images Inside Include File
I have a navigation that uses javascript for image rollovers and I would like to place this navigation in an include file and include it on all of the site pages. I have the javascript functions in an external file located in a folder called "js". I have the included header file in a folder called "inc". Code:
View Replies !
Cannot Get The Values Specified Inside Of Another File Using Include Or Require_once
I cannot reference the values in side of config.php $dbhost,$dbuser ,$dbpasswd ,$dbname why? <?php require_once('includes/db.php'); //db connection functions require_once('config.php'); //db connection parameters class DBRegion{ var $db; function DBRegion(){ } function open(){ $this -db = new sql_db($dbhost , $dbuser , $dbpasswd , $dbname , false); if( !$this -db -db_connect_id ) { die( "Could not connect to the database" ); } } function close(){ $this -db -sql_close(); } function updateName( $id, $name ){ $sql = "update web_coupon_region set name = '$name' where id = $id "; $result = $this -db -sql_query( $sql ) or die( "Coun't proceed with the following sql: ".$sql ); } //UPDATE `web_coupon_region` SET `mdate` = 񟭆-08-16 05:05:55' WHERE `id` = Ƈ' LIMIT 1 ; function updateModDate( $id, $date ) { //$this -db = new sql_db($dbhost , $dbuser , $dbpasswd , $dbname , false); $sql = "update web_coupon_region set mdate = '$date' where id = '$id'"; $result = $this -db -sql_query( $sql ) ; if(!$result ){ $error = $this -db -sql_error(); die( "Coun't proceed with the following sql: ".$error['message'] ); } } } ?>
View Replies !
Is It Possible To Include A Photo Inside A GD Graphic Image?
I have been experimenting with php GD Graphics. I have managed to create a new jpeg image and insert dynamic text from a mysql database into the image. I have now come up against a problem and I have been unable to find an answer on the internet. Is it possible to create a new jpeg php gd image and then insert an existing jpeg photo into that image?
View Replies !
Classes And Var Declarations
I've read many places on the net that when using classes in PHP, all member variables must be declared via the 'var' keyword. I know that doing this creates all these variables when the object is first created. My question is what is the point of this and is it bad if I don't declare all my variables at the beginning? Right now when I have a variable I want to save in the object globally I just assign $this->variable whenever I need it. I am serializing/unserializing my object in $_SESSION between page calls. Everything is working fine, and this is actually better for me, because I do not want all my variables to be created at the start, but instead as I go based on different function results (I do not necessarily want every possible variable in my class to be set on each run). So to summarize, are there any problems with simply assigning $this->variable whenever I need, instead of explicitly declaring all variables with 'var' at the beginning of my class? I have found many sites telling me I have to declare them, but most don't explain why, and I have not had any luck finding any documentation on potential drawbacks with what I am doing.
View Replies !
Splitting Class/function Declarations & Definitions
Is it possible to split-up a class declaration and its' functions definitions? By this I mean the same idea as in C++ with header and source files. Having to define my functions within its class declaration is clumsy and untidy, made worse by larger classes and functions. PHP Code:
View Replies !
Class Variable Declarations: Public/private/protected
<?php class myClass { var $myVar; .. .. } ?> In php.ini I have error_reporting = E_ALL | E_STRICT The above code is causing the following error/notification in my Apache logs:- "PHP Strict Standards: var: Deprecated. Please use the public/private/protected modifiers." Searching in the most current .chm help finds very little, indeed the class definition examples still use var. I understand the reason for the warning, what I don't have is a scope definition for public, private and protected nor an explicit definition of how to use the modifiers.
View Replies !
Checking The DB For Either Lower Case Or Upper Case In A Query ?
I have people submitting a form twice, one with lower case letters and one with capital letters. Of course I have it checking to see if email is in DB, and this is their way of submitting twice. Is there a way to prevent this by checking the DB for either lower case or upper case in a query ? Or maybe they will even do both lower and upper case at same time. Example : SOMENAME@SOMESITE.COM somename@somesite.com SoMeNAME@soMeSiTE.CoM Many variations they try and succeed.
View Replies !
Case Sensitive, Case Sensitivity, $_GET
IF, inside the address-bar, the link appears as: index.php?CITY=Raleigh+NC or index.php?City=Raleigh+NC $_GET['city'] displays behaves like a stubborn mule and refuses to get the get and returns empty (""). Is there a way to turn off the case sensitivity ?
View Replies !
|