PHP - Simplified If ... Ifelse?
Is there a more simplified way of doing a query conditional based on 2 possible options (or more?). If I remember right, there's an operator in javascript that does this simplification.
right now I have: Code: [Select] if (isset($_POST['print_change']) && ($_POST['print_change'] = "no")){ $query_print = "UPDATE tbl_registration SET reg_hardcopied = '0' WHERE reg_id = '$id_printed'"; mysql_query($query_print); } elseif (isset($_POST['print_change']) && ($_POST['print_change'] = "yes")){ $query_print = "UPDATE tbl_registration SET reg_hardcopied = '1' WHERE reg_id = '$id_printed'"; mysql_query($query_print); } Similar TutorialsSimple one, I think. I just want to know if this is how I would do this code. on page one a javascript toggles between two divs depending on the option made in a select box. Div one is a radio button valued "dc resident" and the other is a text box where they enter some text. Then on page two, whichever one is entered becomes the variable to insert to my database. Code: [Select] if (isset($_POST['how1']){$how1 = mysql_escape_string($_POST['how1']);} elseif (isset($_POST['resident']){$how1 = mysql_escape_string($_POST['resident']); }; I think it's right, but something in my gut tells me its not, or that maybe there's a simpler way to do this? I am looking to identify a particular company records from a table that holds multiple records of the same company but with different criteria such as source of payment and row count to delete a specific record from two table and update a third.
The ifelse statememts don't working. There are 4 conditions. First is if the paid source = "MT1000". Second is if the paid source = "2411". The third condition is if the Row Count >= 2, and the forth condition is that the Row Count == 1. Even though on a particular query the row count is 2, the condition returned is Row Count = 1. If I remove one record and switch the order of the last two conditions, the row count is 1 but returns the Row Count <= 2.
$result = mysql_query( "SELECT COUNT(1) FROM townSponsor_OBC_tools WHERE ts_obc_username = '$ts_obc_username' AND ts_obc_userID = '$ts_obc_userID'" ) or die("SELECT Error: ".mysql_error()); $num_rows = mysql_result($result, 0, 0); echo 'Row Count: = '; echo $num_rows; if ( $ts_obc_paidSourcee == 'MTM1000' ) { echo "This Paid source is MTM1000 and Nothing is done"; } elseif ( $ts_obc_paidSourcee == '2411' ) { echo "This Paid source is 2411 and Nothing is done"; } elseif ( $num_rows == 1 || $ts_obc_paidSourcee <> 'MTM1000' || $ts_obc_paidSourcee <> '2411') { echo "Row Count is = 1 and paid source isn not MTM1000 or 2411"; mysql_query("DELETE FROM townSponsor_OBC_tools WHERE ts_obc_m_orgIDSponsor = '$ts_obc_m_orgIDSponsor' AND ts_obc_username = '$ts_obc_username' AND ts_obc_userID = '$ts_obc_userID'"); mysql_query("DELETE FROM townSponsor_members WHERE tsm_m_username = '$ts_obc_username' AND tsm_m_userID = '$ts_obc_userID' AND tsm_assocID = '$ts_obc_m_orgIDSponsor'"); mysql_query("UPDATE users SET zabp_paid = 'NEW_S' WHERE username = '$ts_obc_username' AND id = '$ts_obc_userID'"); } elseif ( $num_rows >= 2 || $ts_obc_paidSourcee <> 'MTM1000' || $ts_obc_paidSourcee <> '2411') { echo "Row Count is >= 2 and paid source isn not MTM1000 or 2411"; mysql_query("DELETE FROM townSponsor_OBC_toolsTest WHERE ts_obc_m_orgIDSponsor = '$ts_obc_m_orgIDSponsor' AND ts_obc_username = '$ts_obc_username' AND ts_obc_userID = '$ts_obc_userID'"); mysql_query("DELETE FROM townSponsor_members WHERE tsm_m_username = '$ts_obc_username' AND tsm_m_userID = '$ts_obc_userID' AND tsm_assocID = '$ts_obc_m_orgIDSponsor'"); }Thank you in advance for any insights or suggestions. This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=310389.0 So, I am not very experienced with regexes, so I wanna ask if this code can be simplified with preg_match? It is code from my framework on what I am working for adding routes. To explain what is "default" and "MAP", here is example usage. And, callable is called via call_user_func_array if that helps.
// mapping routes $fw->route('home: GET|POST /', 'home'); // provide ReST interface by mapping HTTP requests to class method $fw->route('MAP /rest', 'some_class'); // default route (404 page) $fw->route('default', 'error');And this is route method. public function route($pattern, $callable) { $pattern = strtr($pattern,array(' '=>'')); if ($pattern == 'default') { $this->default_route = $callable; return $this; } $arr = explode('/', $pattern, 2); $method = $arr[0]; $route = '/'.$arr[1]; if (strpos($arr[0], ':') !== false) { $arr = explode(':', $arr[0], 2); $name = $arr[0]; $method = $arr[1]; } if ($method == 'MAP') { foreach ((explode('|', self::Methods)) as $method) { $this->route((isset($name)?$name.':':null).$method.$route, $callable.'->'.strtolower($method)); } return $this; } $this->routes[] = array($method, $route, $callable, isset($name)?$name:null); return $this; } |