PHP - Using Switch With Array Of Cases
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. Does anyone know of any easier way of doing something like this?
Code: [Select] <?php require('config.php'); require(INCLUDE_ROOT.'/classes/Category.php'); $viewPages = array( 'category' => array('classfile' => 'Category.php', 'classname' => 'Category', 'functions' => array('create', 'delete', 'modify', 'merge')), 'questions' => array('classfile' => 'Question.php', 'classname' => 'Question', 'functions' => array('create', 'delete', 'modify', 'votegood', 'votebad')) ); $currentPage = $_GET['action']; switch($currentPage){ foreach($viewPages as $action => $settings){ case $action: require(INCLUDE_ROOT.'/classes/'.$ettings['classfile']); $this->$class = new $settings['classname']; $function = $_REQUEST['do']; $this->$class->$function(); loadTemplate($this->viewFile, $this->messages); break; } ?> Similar TutorialsHello, i am trying to make a calendar for different users which are described in the database. depending on the link to earn a calendar for the user. here's the code: Code: [Select] $sql=" SELECT user , fullname FROM users "; $resource=mysql_query($sql) or die(mysql_error()); while($array=mysql_fetch_assoc($resource)) { $users[]=$array['user']; } if (isset($_GET['user'])) { $user = $_GET['user']; setcookie('user', $user, time()+(3600*24*365)); } elseif (isset($_COOKIE['user'])) { $user = $_COOKIE['user']; } else { $user = 'bg'; } if (!(in_array($user, array_keys($users)))) { die("Грешка: няма езиков файл!"); } require_once "{$user}.php"; function switch_users_options() { global $text, $users, $user; $retval = $text['switch']; $get = $_GET; foreach ($users as $abbrv => $name) { if ($abbrv !== $user) { $get['user'] = $abbrv; $url = $_SERVER['PHP_SELF'] . '?' . http_build_query($get); $retval .= " <a href=\"{$url}\"> {$name}</a><br>"; } } return $retval; } I've taken it from one code to change the language .. the question is that it displayed on new line - users from database but the links are wrong : index.php?user=0, user=1 .. user=2 not as it should be index.php?user=user1, user=test ... please for assistance. Hello, I am having a small issue with a case switch array that uses a range of data. I am using it to set the background of table cells depending on the data. I.E. 0-0.9, 1-1.9 etc with each one being a different color. Issue I am having is it doesn't change. It sets it based on the first array entry. What might I be missing? This is the first time I have used a case switch with a range.
-Thanks
$magScale = ''; switch($magnitude) { case myInterval >= 0 && myInterval <= 0.9: $magScale = 'rgb(195, 218, 236)'; break; case myInterval >= 1 && myInterval <= 1.9: $magScale = 'rgb(210, 238, 197)'; break; case myInterval >= 2 && myInterval <= 2.9: $magScale = 'rgb(244, 240, 202)'; break; case myInterval >= 3 && myInterval <= 3.9: $magScale = 'rgb(244, 223, 202)'; break; case myInterval >= 4 && myInterval <= 4.9: $magScale = 'rgb(240, 199, 205)'; break; case myInterval >= 5 && myInterval <= 10: $magScale = 'rgb(212, 195, 236)'; break; } Not sure how to approach this ... I have a form where someone will select a city from a dropdown (over 80 cities currently, will be expanding to more). Each city falls in one of three categories (i.e. small, medium, large). After the user submits the form I need to determine what city the user selected and the size (small, medium, or large). Should I store the size in an array for each city name? That sounds wrong to me; or is it? I could use a switch statement I guess to check if 'New York' was selected and then set the city size to large, but that seems like excessive code also. Am I close? If you could point me in the right direction so I could read up on it, that would be awesome. Thanks! Does anyone have an example of when htmlentities() would be used over htmlspecialchars()? Morning, I'm working on a web site using a switch to separate different calls from a MySQL database. My style looks like this: <?php switch ($_GET['page']) { case 'news': echo ("NEWS HERE"); } break; case 'schedule'; echo ("SCHEDULE HERE"); } break; default: echo ("MAIN PAGE HERE"); break; } What I am trying to do, is specify that if the page is news, there can be an additional variable for id. I attempted before and after the break; for the news case, to make a new switch, but it isn't taking. I've looked around online, but can't find any information about this. Can anyone offer a hand? Code: [Select] <?php switch($_GET['action']){ case 'delete': break; case 'edit': case 'write': if($action=='edit'){ $form_action = 'edit_ok'; echo 'Edit Page'; }else{ $form_action = 'write_ok'; echo 'Write Page'; } ?> <?php $id=$_GET['id']; $result = mysql_query("SELECT * FROM project_data WHERE id='$id'"); $row = mysql_fetch_array($result); ?> <table border = "1"> <form action="switch.php?id=<?php echo $row['ID']?>&action=[color=purple][b]<?php echo $action ?>[/b][/color]" method="post"> <tr> <td>Date Of Birth</td> <td><input type="text" name="dateofbirth" value="<?php echo $row['Date_Of_Birth']?>"/></td> </tr> <tr> <td>Gender</td> <td><input type="radio" name="gender" value="male" <?php if($row['Gender']=='male'){echo 'checked';}?>/> Male<input type="radio" name="gender" value="female" <?php if($row['Gender']=='female'){echo 'checked';}?>/> Female</td> </tr> <tr> <td>Title</td> <td><select name="title"> <option value="">Please Select</option> <option value="Mr" <?php if($row['Title']=='Mr'){echo 'selected';}?>>Mr</option> <option value="Ms" <?php if($row['Title']=='Ms'){echo 'selected';}?>>Ms</option> <option value="Mrs" <?php if($row['Title']=='Mrs'){echo 'selected';}?>>Mrs</option> <option value="Miss" <?php if($row['Title']=='Miss'){echo 'selected';}?>>Miss</option> <option value="Other" <?php if($row['Title']=='Other'){echo 'selected';}?>>Other</option> </select></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="firstname" value="<?php echo $row['First_Name']?>"/></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastname" value="<?php echo $row['Last_Name']?>" /></td> </tr> <tr> <td>Address Line 1</td> <td><input type="text" name="address1" value="<?php echo $row['Address_Line_1']?>" /></td> </tr> <tr> <td>Address Line 2</td> <td><input type="text" name="address2" value="<?php echo $row['Address_Line_2']?>" /></td> </tr> <tr> <td>City</td> <td><input type="text" name="postcode" value="<?php echo $row['City']?>" /></td> </tr> <tr> <td>Postcode</td> <td><input type="text" name="postcode" value="<?php echo $row['Postcode']?>" /></td> </tr> <tr> <td>Contact No</td> <td><input type="text" name="contactno" value="<?php echo $row['Contact_No']?>" /></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" value="<?php echo $row['Email']?>"/></td> </tr> <tr> <td>Additional Info</td> <td><textarea rows="10" cols="30" name="note"><?php echo $row['Additional_Comment']?></textarea></td> </tr> <tr> <td><input type="submit" value="Update" /></td> <td><a href='switch.php'>Main Page</a></td> </tr> </table> </form> <?php break; //case 'write'; ?> <!--<table border = "1"> <form action="switch.php?action=write_ok" method="post"> <table> <tr> <td>Date Of Birth</td> <td><input type="text" name="dateofbirth" /></td> </tr> <tr> <td>Gender</td> <td><input type="radio" name="gender" value="male" />Male<input type="radio" name="gender" value="female" /> Female</td> </tr> <tr> <td>Title</td> <td><select name="title"> <option value="" selected="selcted">Please Select</option> <option value="Mr">Mr</option> <option value="Mrs">Mrs</option> <option value="Miss">Miss</option> <option value="Ms">Ms</option> <option value="Other">Other</option> </select></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="firstname" /></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastname" /></td> </tr> <tr> <td>Address Line 1</td> <td><input type="text" name="address1" /></td> </tr> <tr> <td>Address Line 2</td> <td><input type="text" name="address2" /></td> </tr> <tr> <td>City</td> <td><input type="text" name="city" /></td> </tr> <tr> <td>Postcode</td> <td><input type="text" name="postcode" /></td> </tr> <tr> <td>Contact No</td> <td><input type="text" name="contactno" /></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" /></td> </tr> <tr> <td>Additional Info</td> <td><textarea rows="15" cols="30" name="note">Please Enter Any Additional Information</textarea></td> </tr> <tr> <td><input type="submit" value="Submit"></td> <td><a href='switch.php'>Back</a></td> </tr> </table> </form>--> <?php //break; I have this code, which is basically a form for inputting and another for editing. As you can see I have made one part a comment as I want just the edit part to control the input and edit, which is shown at the very beginning with write case, and the seperate edit case, all I need to complete it is the part that I have highlighted in purple/bold. Does anybody have any help? Code: [Select] <!--PHP Update--> <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("project1", $con); if ($_GET['action']=='delete_ok'){ }elseif ($_GET['action']=='edit_ok'){ } switch($_GET['action']){ case 'write_ok'; $sql="INSERT INTO project_data (Date_Of_Birth, Gender, Title, First_Name, Last_Name, Address_Line_1, Address_Line_2, City, Postcode, Contact_No, Email, Additional_Comment) VALUES ('".$_POST[dateofbirth]."','".$_POST[gender]."','".$_POST[title]."','".$_POST[firstname]."','".$_POST[lastname]."','".$_POST[address1]."','".$_POST[address2]."','".$_POST[city]."','".$_POST[postcode]."','".$_POST[contactno]."','".$_POST[email]."','".$_POST[note]."')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }else{ echo '<script>alert("Data Has Been Successfully Updated");</script>'; echo '<meta http-equiv="Refresh" content="0;URL=switch.php">'; } break; case 'edit_ok'; if ((!isset($_GET['id']) || trim($_GET['id']) == '')) { die('Missing record ID!'); } $id= $_GET['id']; mysql_query("UPDATE project_data SET Date_Of_Birth='".$_POST[dateofbirth]."',Gender='".$_POST[gender]."',Title='".$_POST[title]."',First_Name='".$_POST[firstname]."',Last_Name='".$_POST[surname]."',Address_Line_1='".$_POST[address1]."',Address_Line_2='".$_POST[address2]."',City='".$_POST[city]."',Postcode='".$_POST[postcode]."',Contact_No='".$_POST[contactno]."',Email='".$_POST[email]."',Additional_Comment='".$_POST[note]."' WHERE ID='".$_GET['id']. "'") or die ("Error in query: $query. " . mysql_error()); echo '<script>alert("Data Has Been Successfully Updated");</script>'; echo '<meta http-equiv="Refresh" content="0;URL=switch.php">'; } break; case 'delete_ok': #########Delete OK Start################# $id= $_GET['id']; $result = mysql_query("DELETE FROM project_data WHERE ID = '$id'") or die ("Error in query: $query. " . mysql_error()); echo '<script>alert("Data Has Been Successfully Updated");</script>'; echo '<meta http-equiv="Refresh" content="0;URL=switch.php">'; } #########Delete OK End ################# break; } ?> <!--HTML--> <?php switch($_GET['action']){ case 'delete'; break; case 'edit'; echo 'Edit page'; ?> <?php $id=$_GET['id']; $result = mysql_query("SELECT * FROM project_data WHERE id='$id'"); $row = mysql_fetch_array($result); ?> <table border = "1"> <form action="switch.php?id=<?php echo $row['ID']?> action=edit_ok" method="post"> <tr> <td>Date Of Birth</td> <td><input type="text" name="dateofbirth" value="<?php echo $row['Date_Of_Birth']?>"/></td> </tr> <tr> <td>Gender</td> <td><input type="radio" name="gender" value="male" <?php if($row['Gender']=='male'){echo 'checked';}?>/> Male<input type="radio" name="gender" value="female" <?php if($row['Gender']=='female'){echo 'checked';}?>/> Female</td> </tr> <tr> <td>Title</td> <td><select name="title"> <option value="">Please Select</option> <option value="Mr" <?php if($row['Title']=='Mr'){echo 'selected';}?>>Mr</option> <option value="Ms" <?php if($row['Title']=='Ms'){echo 'selected';}?>>Ms</option> <option value="Mrs" <?php if($row['Title']=='Mrs'){echo 'selected';}?>>Mrs</option> <option value="Miss" <?php if($row['Title']=='Miss'){echo 'selected';}?>>Miss</option> <option value="Other" <?php if($row['Title']=='Other'){echo 'selected';}?>>Other</option> </select></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="firstname" value="<?php echo $row['First_Name']?>"/></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastname" value="<?php echo $row['Last_Name']?>" /></td> </tr> <tr> <td>Address Line 1</td> <td><input type="text" name="address1" value="<?php echo $row['Address_Line_1']?>" /></td> </tr> <tr> <td>Address Line 2</td> <td><input type="text" name="address2" value="<?php echo $row['Address_Line_2']?>" /></td> </tr> <tr> <td>City</td> <td><input type="text" name="postcode" value="<?php echo $row['City']?>" /></td> </tr> <tr> <td>Postcode</td> <td><input type="text" name="postcode" value="<?php echo $row['Postcode']?>" /></td> </tr> <tr> <td>Contact No</td> <td><input type="text" name="contactno" value="<?php echo $row['Contact_No']?>" /></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" value="<?php echo $row['Email']?>"/></td> </tr> <tr> <td>Additional Info</td> <td><textarea rows="10" cols="30" name="note"><?php echo $row['Additional_Comment']?></textarea></td> </tr> <tr> <td><input type="submit" value="Edit" /></td> <td><a href='switch.php'>Back</a></td> </tr> </table> </form> <?php break; case 'write'; ?> <form action="switch.php?action=write_ok" method="post"> <table> <tr> <td>Date Of Birth</td> <td><input type="text" name="dateofbirth" /></td> </tr> <tr> <td>Gender</td> <td><input type="radio" name="gender" value="male" />Male<input type="radio" name="gender" value="female" /> Female</td> </tr> <tr> <td>Title</td> <td><select name="title"> <option value="" selected="selcted">Please Select</option> <option value="Mr">Mr</option> <option value="Mrs">Mrs</option> <option value="Miss">Miss</option> <option value="Ms">Ms</option> <option value="Other">Other</option> </select></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="firstname" /></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastname" /></td> </tr> <tr> <td>Address Line 1</td> <td><input type="text" name="address1" /></td> </tr> <tr> <td>Address Line 2</td> <td><input type="text" name="address2" /></td> </tr> <tr> <td>City</td> <td><input type="text" name="city" /></td> </tr> <tr> <td>Postcode</td> <td><input type="text" name="postcode" /></td> </tr> <tr> <td>Contact No</td> <td><input type="text" name="contactno" /></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" /></td> </tr> <tr> <td>Additional Info</td> <td><textarea rows="15" cols="30" name="note">Please Enter Any Additional Information</textarea></td> </tr> <tr> <td><input type="submit" value="Submit"></td> <td><a href='switch.php'>Back</a></td> </tr> </table> </form> <?php break; default: echo "<table border='1'> <tr> <th>ID</th> <th>Date Of Birth</th> <th>Gender</th> <th>Title</th> <th>First Name</th> <th>Last Name</th> <th>Address Line 1</th> <th>Address Line 2</th> <th>City</th> <th>Postcode</th> <th>Contact No</th> <th>Email</th> <th>Additional Info</th> <th>Action</th> </tr>"; $result = mysql_query("SELECT * FROM project_data"); while($row = mysql_fetch_array($result)){ echo "<tr>"; echo "<td>" . $row['ID'] . "</td>"; echo "<td>" . $row['Date_Of_Birth'] . "</td>"; echo "<td>" . $row['Gender'] . "</td>"; echo "<td>" . $row['Title'] . "</td>"; echo "<td>" . $row['First_Name'] . "</td>"; echo "<td>" . $row['Last_Name'] . "</td>"; echo "<td>" . $row['Address_Line_1'] . "</td>"; echo "<td>" . $row['Address_Line_2'] . "</td>"; echo "<td>" . $row['City'] . "</td>"; echo "<td>" . $row['Postcode'] . "</td>"; echo "<td>" . $row['Contact_No'] . "</td>"; echo "<td>" . $row['Email'] . "</td>"; echo "<td>" . $row['Additional_Comment'] . "</td>"; echo "<td><a href='switch.php?action=edit&id=" . $row['ID']."'>Edit</a>  <a href='switch.php?action=delete&id=" . $row['ID']."'>Delete</a></td>"; echo "</tr>"; } echo "</table>"; echo "<a href='switch.php?action=write'>Insert</a>"; } mysql_close($con); ?> Here is the completed code, but I still have a problem the page is coming up with an error: ( ! ) Fatal error: Cannot break/continue 1 level in C:\wamp\www\p1\switch.php on line 63 Call Stack # Time Memory Function Location 1 0.0009 413960 {main}( ) ..\switch.php:0 I cannot seem to figure it out, Can anyone help? Thanks Here is my dilemma. I'm trying to get the "class name" to return instead of the number that is associated with it. I wrote a switch so that each of the 16 cases would be associated with what the class name is. However, how do I make it output into my statement? Here is what I have so far: function getClassByNumber($x){ switch($x){ case 1: return "Alchemist"; case 2: return "Assassin"; case 3: return "Dark Arts"; case 4: return "Dark Paladin"; case 5: return "Entertainer"; case 6: return "Hunter"; case 7: return "Mage"; case 8: return "Monk"; case 9: return "Paladin"; case 10: return "Pirate"; case 11: return "Priest"; case 12: return "Psion"; case 13: return "Scholar"; case 14: return "Thief"; case 15: return "Warlock"; case 16: return "Warrior"; } } $class = getClassByNumber($username->class); echo You are now a $class. Username is defined as seeking the table I want. Class is the field where the number is stored. Thanks! i know theres a way but i just forget. i want to do a switch function. Code: [Select] switch ($_GET['page']) { case 0: include 'page'; break; case 1: include 'page1'; break; case 2: include 'page2'; break; default: include 'page'; } ?> just like that but if there is no $_GET page i just want it to do the default. instead of doing if isset GET page do switch else default page. i just want it to be simpler by doing something like switch(isset$_GET etc. As far as it goes for me I have the switch() working, but not to what im trying to do. This is for a "media" page where I have 3 different categories "Music", "Videos" and "Photos". I'm trying to get it so when you goto the "Media" page it will show just the 3 categories, that section works but it is also showing the sub categories for all them categories. So for what im trying to do is when viewing the "media" page, it will display the 3 categories, then when selecting the category it will display the "sub categories". and then when you select the "sub category" it will display the contents of that "sub category" should i be trying to do this a different way? or do i have something wrong here? lol here is my switch() code Code: [Select] <?php $cat = (isset($_GET["m_cat"])) ? (intval($_GET["m_cat"])) : 0; switch($_GET["b"]=="true") { default : if("/ffy/media.php"==$_SERVER['PHP_SELF']) { echo "Select a Category"; } case "false": if($_GET["m_cat"]==$cat) { echo "<font size='3'>"; $dcat = mysql_query("SELECT * FROM media_sub_cat WHERE m_cat=$cat") or die("mySQL Query Failed: " .mysql_error()); while($catrow = mysql_fetch_array($dcat)) { list($id, $m_sub_cat, $m_cat) = $catrow; echo ": <font class='myFont'><a href='media.php?b=true&cat=".$m_cat."&subcat=".$m_sub_cat."'>".$m_sub_cat."</a></font> "; } echo ":</font>"; echo "Work Damb You"; } break; case "true": if($_GET["cat"]==$cat && $_GET["subcat"]==$m_sub_cat) { echo "Weird..."; } break; } ?> Hi all, I am creating a switch page which will be acting as more of an index page for the different php files I have in my project. I have seven different pages and, I am a bit stuck as to what the order is and where the codes should go. I have got this far as is shown below: Code: [Select] <!--PHP Update--> <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("project1", $con); if ($_GET['action']=='delete_ok' and or){ }elseif ($_GET['action']=='edit_ok'){ } switch($_GET['action']){ case 'delete_ok': #########Delete OK Start################# //echo '<meta http-equiv="Refresh" content="0;URL=switch.php">'; #########Delete OK End ################# break; case 'edit_ok': break; } ?> <!--HTML--> <?php switch($_GET['action']){ case 'delete'; break; case 'edit'; echo 'Edit page'; ?> <form action="switch.php?action=edit_ok" method="post"> <?php break; default: echo "<table border='1'> <tr> <th>ID</th> <th>Date Of Birth</th> <th>Gender</th> <th>Title</th> <th>First Name</th> <th>Last Name</th> <th>Address Line 1</th> <th>Address Line 2</th> <th>City</th> <th>Postcode</th> <th>Contact No</th> <th>Email</th> <th>Additional Info</th> <th>Action</th> </tr>"; $result = mysql_query("SELECT * FROM project_data"); while($row = mysql_fetch_array($result)){ echo "<tr>"; echo "<td>" . $row['ID'] . "</td>"; echo "<td>" . $row['Date_Of_Birth'] . "</td>"; echo "<td>" . $row['Gender'] . "</td>"; echo "<td>" . $row['Title'] . "</td>"; echo "<td>" . $row['First_Name'] . "</td>"; echo "<td>" . $row['Last_Name'] . "</td>"; echo "<td>" . $row['Address_Line_1'] . "</td>"; echo "<td>" . $row['Address_Line_2'] . "</td>"; echo "<td>" . $row['City'] . "</td>"; echo "<td>" . $row['Postcode'] . "</td>"; echo "<td>" . $row['Contact_No'] . "</td>"; echo "<td>" . $row['Email'] . "</td>"; echo "<td>" . $row['Additional_Comment'] . "</td>"; echo "<td><a href='switch.php?action=edit&id=" . $row['ID']."'>Edit</a>  <a href='delete_write.php?id=" . $row['ID']."'>Delete</a></td>"; echo "</tr>"; } echo "</table>"; echo "<a href='write.php'>Insert</a>"; } mysql_close($con); ?> the file names a delete_write.php- deletes the data display.php- displays the data edit_write.php- editing the info edit_write_ok.php- edits on the database and shows on display.php write.php- this is the input page write_input.php- this inputs the data from write.php into the main database any help would be much appreciated. MOD EDIT: code tags added. Hi, I am trying to use switch to select from different options, but i only am able to get the == value displayed and never the != value: switch ($judgement){ case $a == $b: echo Same <br>"; break; case $c != $d: echo "Different <br>"; break; } What am i doing wrong please? Is there anything in php that let's me SWAP 2 arrays? like let's say I have 3,2,1 in a array, is there a way I can swap the 2 and 1 if a statement is true? Hello everyone from a php newbie, I have a option selections box and need to select an action depending on the results. Its for an insert script for items into a Database and depending on the chosen option it add the picture to the DB table The selection options are <select name="extension" style="font-size:18px"><option value="pdf">PDF</option><option value="zip">ZIP</option><option value="powerpoint">PowerPoint</option></select> the Swtich is switch $_POST['extention']{ case 'zip': echo $insert_image = $_POST['extension'].'winzip.gif'; break; case 'powerpoint': echo $insert_image = $_POST['extension'].'ppt.gif'; break; default: echo $insert_image = $_POST['extension'].'products/pdf.gif'; } Would someone please tell me if this is correct syntax? Or have i got it horribly wrong! Thank you for your a help Charlie So, I'm working on making my code a little prettier. Previously, if I wanted to do 3 different things, I would create 3 different php pages for those three different things. For Example: <form action="user_add.php"> <form action="user_edit.php"> <form action="user_delete.php"> This would usually be 6 different pages. (3 "gui" pages and 3 php pages that performed specific actions) So, I've decided to create 1 specific page called "actions.php" Using a switch.case variable named $request This will allow me to achieve the following: <form action="actions.php?request=userAdd"> <form action="actions.php?request=userEdit"> <form action="actions.php?request=userDelete"> My actions page looks like this: Code: [Select] <?php include("connectDB.php"); $request=$_GET['request']; switch ($request) { case "userDelete": echo "delete"; break; case "userEdit": echo "edit"; break; case "userAdd": echo "add"; break; } ?> Now, assume I access the page that allows me to add a new user. The form action would look like this: <form action=actions.php?request=userAdd> When I click on the submit button, I would see the words "add" on the page. Perfect, good job, yay me. Now, assume I add the following code block into the "userAdd" case Code: [Select] <?php ... case "userAdd": // Get values from form $username=$_POST['username']; $password=$_POST['password']; $email=$_POST['email']; // Insert data into mysql $sql="INSERT INTO $tbl_users(username, password, email)VALUES('$username', '$password', '$email')"; $result=mysql_query($sql); // if successfully insert data into database, displays message "Successful". if($result) { echo "Successful"; } else { echo "ERROR"; } echo $previous_page; break; ?>This works perfectly. Good job, yay me! Now assume I change the case block to look like this: Code: [Select] <?php ... case "userAdd": adduser(); break; } function adduser() { // Get values from form $username=$_POST['username']; $password=$_POST['password']; $email=$_POST['email']; // Insert data into mysql $sql="INSERT INTO $tbl_users(username, password, email)VALUES('$username', '$password', '$email')"; $result=mysql_query($sql); // if successfully insert data into database, displays message "Successful". if($result) { echo "Successful"; } else { echo "ERROR"; } echo $previous_page; } ?>This doesn't work. WHY? If I change the adduser() function to echo "hello world"; I would see "hello world", but when I attempt to copy and paste the block of code (that works in the switch case statement) into the function, it stops working. (It prints nothing) Now, I don't even know if this is the correct place to put this. Last time I had a mysql database error and my thread was moved here, even though I knew it wasn't a php error. So, if this is the wrong section, I apologize - but I assume it falls under both a mysql and php problem. Hello again having trouble to switch pages All it dose just refrashe page <a href=/?p=cinema&pid=503&act=video>Test</a> Quote $keiciam = isset($_GET['act']) ? $_GET['act']:''; switch($keiciam) { case 'video': eval("\$cinema_player = \"".$TSUE['TSUE_Template']->LoadTemplate('cinema_player')."\";"); break; default: } Hello, How can i switch between the 2 lines below in my function. At the moment the bottom line is ignored: /* Determine if user is logged in */ $this->logged_in = $this->checkLogin(); /* Determine if client is logged in */ $this->logged_in_Client = $this->checkLoginClient(); Code: [Select] function startSession(){ global $database; //The database connection session_start(); //Tell PHP to start the session /* Determine if user is logged in */ $this->logged_in = $this->checkLogin(); /* Determine if client is logged in */ $this->logged_in_Client = $this->checkLoginClient(); if($this->logged_in){ $database->addActiveUser($this->username, $this->time); } if($this->logged_in_Client){ $database->addActiveClient($this->clientname, $this->time); } /* Set referrer page */ if(isset($_SESSION['url'])){ $this->referrer = $_SESSION['url']; }else{ $this->referrer = "/"; } /* Set current url */ $this->url = $_SESSION['url'] = $_SERVER['REQUEST_URI']; } Can you use a Switch() statement to effectively check for codes like this... Code: [Select] PASSWORD_USER_NOT_LOGGED_IN I have about 5 codes that need to be checked in order to display different outcome messages... Would it be better to use an IF-THEN-ELSEIF instead? Debbie can i do something like this? Code: [Select] <?php switch ($i) { case 0: echo "i equals 0";?> ////////////// html stuff //////// <?php break; case 1: echo "i equals 1"; ?> ///////////////////html stuff////////////// <?php break; case 2: echo "i equals 2"; ?> ////////////html stuff/////////////// <?php break; } ?> |