PHP - Switch.. If Statements.. Oy.
Hello, I'm currently working on just a quick little frontend for VirtualBox and I'm just running into a little bit of a problem that I can't find my way around. Now this is my first project in php. I'm familiar with programming concepts (well except this one apparently ) and by familiar were talking I like to tinker with small languages.. no formal training.
Anyway, here we go. So heres the main page. Pretty simple, you type the argument that goes with the following command and check the box next to the command you'd like to execute. Code: [Select] <HTML> <HEAD> <TITLE>LCCP ver1</TITLE> </HEAD> <BODY> <?php include "header.php"; ?> <form method="post" action="lccp_ver1.php"> <p>cmd="createvm"<br> Enter the name of the Virtual Guest you'd like to create: <input type="text" name="id"> <input type="checkbox" name="createvm"> </p> <p>cmd="showvrtinfo"<br> Enter the name of the Virtual Machine you'd like to view: <input type="text" name="id2"> <input type="checkbox" name="showvminfo"> </p> <p>cmd="unregistervm"<br> Enter the name of the Virtual Guest you'd like to delete: <input type="text" name="id3"> <input type="checkbox" name="unregistervm"> </p> <p>cmd="startvm"<br> Enter the name of the Virtual Machine you'd like to start: <input type="text" name="id4"> <input type="checkbox" name="startvm"> </p> <p>cmd="controlvm"<br> Enter the name of the Virtual Machine you'd like to control: <input type="text" name="id5"> <input type="checkbox" name="controlvm"> </p> <p>Then.. would you like to...</p> <p>arg="pause" <input type="checkbox" name="controlvm_pause"></p> <p>arg="resume" <input type="checkbox" name="controlvm_resume"></p> <p>arg="reset" <input type="checkbox" name="controlvm_reset"></p> <p>arg="poweroff" <input type="checkbox" name="controlvm_poweroff"></p> <p>arg="savestate" <input type="checkbox" name="controlvm_savestate"></p> <input type="submit" name="Submit" value="Submit"> </form> </BODY> </HTML> And then here is the execution page Code: [Select] <HTML> <HEAD> <TITLE>LCCP command <?php function ServerLoad() { $stats = exec('uptime'); preg_match('/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/', $stats, $regs); return ($regs[1].', '.$regs[2].', '.$regs[3]); } echo ServerLoad(); ?> </TITLE> </HEAD> <BODY> <?php include "header.php"; ?> <?php function createvm($id) { $output = shell_exec("VBoxManage createvm --name " . $id . " --register"); return $output; } function showvminfo($id) { $output = shell_exec("VBoxManage showvminfo " . $id); return $output; } function unregistervm($id) { $output = shell_exec("VBoxManage unregistervm ". $id ." --delete"); return $output; } function startvm($id) { $output = shell_exec("VBoxManage startvm ". $id ." --type headless"); return $output; } function controlvm($id,$arg) { $output = shell_exec("VBoxManage controlvm ". $id ." ". $arg); return $output; } function print_output($output) { echo' <pre>'. $output . ' </pre>'; } //******************exec main********************** if (isset($_POST['createvm'])) { // createvm($_POST['id2']); print_output(createvm($_POST['id'])); } if (isset($_POST['showvminfo'])) { // showvminfo($_POST['id']); print_output(showvminfo($_POST['id2'])); } if (isset($_POST['unregistervm'])) { // unregistervm($_POST['id3'); print_output(unregistervm($_POST['id3'])); } if (isset($_POST['startvm'])) { // startvm($_POST['id4']); print_output(startvm($_POST['id4'])); } if ((isset($_POST['controlvm'])) AND (isset($_POST['controlvm_pause'))) { //controlvm($_POST['id5']," pause"); print_output(controlvm($_POST['id5']," pause")); } if ((isset($_POST['controlvm'])) AND (isset($_POST['controlvm_resume'))) { //controlvm($_POST['id5']," resume"); print_output(controlvm($_POST['id5']," resume")); } if ((isset($_POST['controlvm'])) AND (isset($_POST['controlvm_reset'))) { //controlvm($_POST['id5']," reset"); print_output(controlvm($_POST['id5']," reset")); } if ((isset($_POST['controlvm'])) AND (isset($_POST['controlvm_poweroff'))) { //controlvm($_POST['id5']," poweroff"); print_output(controlvm($_POST['id5']," poweroff")); } if ((isset($_POST['controlvm'])) AND (isset($_POST['controlvm_savestate'))) { //controlvm($_POST['id5']," savestate"); print_output(controlvm($_POST['id5']," savestate")); } //**********working on*************** //teleport? ?> </BODY> </HTML> Now my question is with the if statements. Well, number one I guess is there a more efficient way of doing this that I'm overlooking? I looked into switches but I don't know how I'd pull that off with checkboxes without some complicated loop and somehow converting the checkboxes "yes" or "nos" into numbers or something. If statements just seemed like less of a headache but I may be finding otherwise haha. Number two here is how do I get the "controlvm" command to work? When I comment out that last indented lines of if statements the program runs perfect (and thats with the controlvm function as well. so.. the problem isn't there). Basically whats the appropriate way to do this. I'm stuck and my limited program knowledge and Google searching is starting to run thin. Thanks in advance for the help! Similar TutorialsI'm using a switch with multiple nested switches. I tested the switch and nested switches by setting up the structure, and using basic text for the cases. For example, instead of one of the nested switch cases needing to = $_GET['id'], I just entered two cases, each equaling 1 and 2, then the default case. Once I begin to enter my SQL coding to connect to the databases, only certain information prints. For example; on my switch that is $_GET['page'], I have a case for 'news'. Inside this case, I have a nested switch called $_GET['id'], which will display information if the page=nes&id=whatever. That works, but when I go do the page=news, nothing displays. My coding is omitting displaying the default information if there is no $_GET['id']. Attached, I am going to have my .php file. The reason for not posting it here is because my coding is rather messy. Please help! 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? 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! 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 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? The below is constantly loading the password page no matter what is enter in the password input on the form (on another page). I have checked the $login variable by echoing it and it is carrying the value from the field. <?php $off="yes"; $login=$_POST['login']; if($off=="yes" && (!isset($_SESSION['login']) || $_SESSION['login']!="pass")){ if($login=="" || $_SESSION['login']!="pass"){ header('Location: ./index2.html'); exit(); } elseif(!isset($_SESSION['login'])){ session_start(); $_SESSION['login']=$login; header('Location: ./index.php'); exit(); } } elseif($off=="no" || $_SESSION['login']=="pass"){ content goes here. } ?> Hello, I am learning PDO SQL statements, I have mananged to connect using PDO: Code: [Select] try { $this->link = $dbh = new PDO('mysql:host='.$this->dbhost.';dbname='.$this->dbname.'', $this->dbuser, $this->dbpass); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); } I am not getting any error, so I guess that is a good start. I then tried the PDO SQL statement out. My old mySQL query is commented out in the Code: [Select] if (isset($_SESSION['id']) && isset($_SESSION['password'])) { $_SESSION['id'] = ( isset( $_SESSION['id'] ) ) ? $_SESSION['id'] : FALSE; $_SESSION['password'] = ( isset( $_SESSION['password'] ) ) ? $_SESSION['password'] : FALSE; //$logged = mysql_query("SELECT * FROM `db_members` WHERE `id`='".$_SESSION['id']."' AND `password` = '".$_SESSION['password']."'"); //$logged = mysql_fetch_array( $logged ); // the new pdo statement $ff = $dbh->prepare('SELECT * FROM db_members WHERE id = '.$_SESSION['id'].' AND password = '.$_SESSION['password'].''); $ff->execute(); $logged = $ff->fetchAll(); echo $logged['username']; } I am trying to assign the session to logged variable. So all I am asking is go into db_members and check the id and password that is the same as session id and password and collect the rows data such as username. My login script and everything works perfectly, even session id and password is valid when echo'd but I cannot assign it to the logged variable like my old sql statements. Code: [Select] $logged = mysql_query("SELECT * FROM `db_members` WHERE `id`='".$_SESSION['id']."' AND `password` = '".$_SESSION['password']."'"); $logged = mysql_fetch_array( $logged ); I used other PDO statements and it works perfectly but I just don't understand why this is not working.... can I please get some help if you have any solution to this? Hey Guys, I am creating an array and I have a need to put a if statement inside it. Is this even possible? Here is where I am at so far, its just not working. function load_permissions($gid) { $data = array(); $this->db->select('`groups`.`name` AS `permission_name`, `permissions`.``'); $query = $this->db->get('`groups`,`permissions`'); if ($query->num_rows() > 0){ foreach ($query->result_array() as $row){ $data[] = array( "name" => $row['permission_name'], "read" => if($row['level'] >= 1) { img("img/true.gif") } else { img("img/false.gif") }, ); } } $query->free_result(); return $data; Thanks, Peter Hi Guys, Newbie trying to understand PHP conditionals. In the below Code: [Select] [[*id]] is a global variable which equates to my current page id so it could have the value of 1 for example. I tested it using echo and the correct values pass to it in the cms I'm using. What I cant achieve is for the conditionals below to work correctly so perhaps you can see a syntax error or show me away of doing the same using an array or something perhaps? THANKS Code: [Select] <?php $id='[[*id]]' if ($id=="1" || $id=="2") echo "we are in 1 or 2"; elseif ($id=="3" || $id=="4") echo "we are in 3 or 4"; else echo "not in 1 to 4"; ?> Hey guys, is there any way to have a LIKE condition on an IF statement? Something like: Code: [Select] if $GET_['WTV'] LIKE %LOVE% { $category=romance Any clues? Thanks in advance. I am trying to write an if statement that checks to make sure the information put in the text box is an imbedded file. Does anyone know how to do that? How to write 2 "if" statements together? if($userResult==$myResult) and ($userResult<0) { Anything wrong with this because I got a parse error ONLY after adding to create this. <?php $visible = array('Yes', 'No'); foreach($visible as $visible): $visible2 = array('yes', 'no'); foreach($visible2 as $visible2): ?> 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 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: } 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, 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. Hello Everyone,
I am working on a little project, which is basically creating a lottery draft order for our fantasy football league (I know uber cool right? lol).
I'm basically trying to mimic the old NBA draft lottery by doing something like the following:
Say our league has 12 teams and I want to weight each players chance of getting that 1st pick in the draft?
Well I would give the worst player 12 chances out of 78, 2nd to last 11 chances out of 78 etc.
So I basically created an array with values 1-78 to be my overall chances and some other arrays to assign numbers to each team member i.e. 1-12 to last place, 13-23 for send to last etc.
and I was planning to do a while loop and array_diff to narrow down the chances. here is what i have thus far:
while($x <= 12){ $num = $adjusted[mt_rand(0,count($adjusted)-1)]; Switch($num){ case in_array($num,$place12): echo $x + ' ' + 'Player12'; $adjusted = array_diff($adjusted,$place12); $x++; break; case in_array($num,$place11): echo $x + ' ' + 'Player11'; $adjusted = array_diff($adjusted,$place11); $x++; break; case in_array($num,$place10): echo $x + ' ' + 'Player10'; $adjusted = array_diff($adjusted,$place10); $x++; break; case in_array($num,$place9): echo $x + ' ' + 'Player9'; $adjusted = array_diff($adjusted,$place9); $x++; break; case in_array($num,$place8): echo $x + ' ' + 'Player8'; $adjusted = array_diff($adjusted,$place8); $x++; break; case in_array($num,$place7): echo $x + ' ' + 'Player7'; $adjusted = array_diff($adjusted,$place7); $x++; break; case in_array($num,$place6): echo $x + ' ' + 'Player6'; $adjusted = array_diff($adjusted,$place6); $x++; break; case in_array($num,$place5): echo $x + ' ' + 'Player5'; $adjusted = array_diff($adjusted,$place5); $x++; break; case in_array($num,$place4): echo $x + ' ' + 'Player4'; $adjusted = array_diff($adjusted,$place4); $x++; break; case in_array($num,$place3): echo $x + ' ' + 'Player3'; $adjusted = array_diff($adjusted,$place3); $x++; break; case in_array($num,$place2): echo $x + ' ' + 'Player2'; $adjusted = array_diff($adjusted,$place2); $x++; break; case in_array($num,$place1): echo $x + ' ' + 'Player1'; $adjusted = array_diff($adjusted,$place1); $x++; break; default: $adjusted = $lottery; break; } }Just starting out on the project so it's still pretty basic. Just trying to work on functionality right now. I am getting an undefined offset error which i assume is related to the way i'm doing and storing the mt_rand value. any thoughts on how i might fix it? 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; } ?> |