SEVIZ Posted May 8, 2009 Share Posted May 8, 2009 Hello all. Here is my code in current form. <?php mysql_connect("----") or die(mysql_error()); mysql_select_db("xxxxxxxx") or die(mysql_error()); if (!empty($_POST["recip"])) { $q = "select `num` from sprint WHERE `dept` = '".mysql_real_escape_string($_POST["recip"])."'"; $ret = mysql_query($q); $emails = array(); while ($row = mysql_fetch_assoc($ret)) { $emails[] = $row['num'].'@messaging.sprintpcs.com'; } if ($emails) { $ToEmail = implode(', ', $emails); $EmailSubject = $_POST["subject"]."\r\n"; $mailheader = "From: ".$_POST["email"]."\r\n"; $MESSAGE_BODY = nl2br($_POST["message"]); // $MESSAGE_BODY .= ($_POST["auth"]); mail($ToEmail,$EmailSubject, $MESSAGE_BODY,$mailheader) or die ("Failure"); } ?> <font color="red"><b>Your Text has been sent. The message sent was:</b></font><br /><br /> <font face="tahoma" size="4"><?php echo $_POST["message"]; ?></font> <br /><br /> - <b><a href="text_tool3.php">GO BACK</a></b> <?php } else { ?> <form action="text_tool3.php" method="post"> <table width="100%" border="0" cellspacing="2" cellpadding="0"> <tr> <td valign="top"> Send To:<br /> <select name="recip"> <option value=\"all\">All North and South</option> <option value=\"sl\">Sups and Leads Only</option> <option value=\"north\">North Only</option> <option value=\"south\">South Only</option> <option value=\"sales\">All Sales</option> </select> </td> </tr> <tr> <td>Message:<br /><textarea name=message wrap=physical cols="20" rows="6" id="comment" maxlength="143"></textarea><br /><input readonly type=text name=remLen size=3 maxlength=3 value="143" /> characters left </td> </tr> <tr> <td> <!-- Reply to email (If applicable):<br /> --><input name="email" type="hidden" id="email" size="20" value="[email protected]"></td> </tr> <tr> <td valign="top"><font color="red" size="2"><b>Do not hit enter for a new line. This will give you less characters to use due to the text limits.</b></font><br /><input type="submit" name="Submit" value="Send"></td> </tr> </table> </form> </body> </html> <?php }; ?> At the top you can see I do a query that with "recip" i query "dept". This was from an earlier incarnation of the script. How can I do a few if statements at that spot? You can see in my code I am using a dropdown for choices. Each one has a seperate value. What I want is that in the top, IF recip = ls, then do THIS query. IF recip=su, then do THIS query. Basically depending on the selection in the dropdown I need it to do a different query at that top line. One query may be using one row, another might use a different one. How could I do this with the above? I am assuming this is very simply but I cannot figure out how to do multiple if statements in one spot for a query. Thanks for any help! Link to comment https://forums.phpfreaks.com/topic/157320-if-statement-for-query/ Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Use switch instead. Link to comment https://forums.phpfreaks.com/topic/157320-if-statement-for-query/#findComment-829189 Share on other sites More sharing options...
GingerRobot Posted May 8, 2009 Share Posted May 8, 2009 Well if the queries are actually very similar, it might not be necessary. Are you just searching different tables? If so, just set the value of the options to be the name of the table you wish to query - you can then just insert the value of the drop-down into your query (after escaping it, of course) Link to comment https://forums.phpfreaks.com/topic/157320-if-statement-for-query/#findComment-829291 Share on other sites More sharing options...
SEVIZ Posted May 8, 2009 Author Share Posted May 8, 2009 Thanks for the reply. The queries are different. I am querying all one table but different rows depending on the choice. And in a couple cases I am searching multiple rows. So sadly, the queries are a bit different. So using switch() makes more sense? I'll have to read more to figure out how to do that. Thanks Link to comment https://forums.phpfreaks.com/topic/157320-if-statement-for-query/#findComment-829356 Share on other sites More sharing options...
GingerRobot Posted May 8, 2009 Share Posted May 8, 2009 So using switch() makes more sense? I'll have to read more to figure out how to do that. Thanks It's more a matter of personal taste, but yes - if you're just checking the value of a variable against multiple cases, switch is usually nicer. Link to comment https://forums.phpfreaks.com/topic/157320-if-statement-for-query/#findComment-829357 Share on other sites More sharing options...
SEVIZ Posted May 8, 2009 Author Share Posted May 8, 2009 Can anyone give me an idea of how to change this code: if (!empty($_POST["recip"])) { $q = "select `num` from sprint WHERE `dept` = '".mysql_real_escape_string($_POST["recip"])."'"; $ret = mysql_query($q); $emails = array(); while ($row = mysql_fetch_assoc($ret)) { $emails[] = $row['num'].'@messaging.sprintpcs.com'; } if ($emails) { $ToEmail = implode(', ', $emails); $EmailSubject = $_POST["subject"]."\r\n"; $mailheader = "From: ".$_POST["email"]."\r\n"; $MESSAGE_BODY = nl2br($_POST["message"]); // $MESSAGE_BODY .= ($_POST["auth"]); mail($ToEmail,$EmailSubject, $MESSAGE_BODY,$mailheader) or die ("Failure"); } ?> To use this switch code? Do I just remove this part if (!empty($_POST["recip"])) { $q = "select `num` from sprint WHERE `dept` = '".mysql_real_escape_string($_POST["recip"])."'"; And replace with switch ($_POST["recip"])) { case lt: echo "QUERY HERE"; break; case tech: echo "QUERY HERE"; break; case all: echo "QUERY HERE"; break; default: echo "QUERY HERE"; } I am very new to all of this so every step is a learning experience. Thank you for any help. Link to comment https://forums.phpfreaks.com/topic/157320-if-statement-for-query/#findComment-829383 Share on other sites More sharing options...
SEVIZ Posted May 8, 2009 Author Share Posted May 8, 2009 Or in more detail: switch (!empty($_POST["recip"])) { case sl: echo "select `num` from sprint WHERE `dept` = 'sups AND leads'"; break; case north: echo "select `num` from sprint WHERE `loc` = 'north'"; break; case south: echo "select `num` from sprint WHERE `loc` = 'south'"; break; default: echo "select `num` from sprint WHERE `all` = 'all'"; } Am I on the right track? Link to comment https://forums.phpfreaks.com/topic/157320-if-statement-for-query/#findComment-829408 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Why on earth would you echo a SQL? ??? Link to comment https://forums.phpfreaks.com/topic/157320-if-statement-for-query/#findComment-829411 Share on other sites More sharing options...
GingerRobot Posted May 8, 2009 Share Posted May 8, 2009 Why on earth would you echo a SQL? ??? To test? Makes perfect sense to me. Edit: almost there SEVIZ, but you'll have to test the empty case separately and add quotes around those strings. E.g. if(!empty($_POST["recip"])){ switch ($_POST["recip"]) { case 'sl': echo "select `num` from sprint WHERE `dept` = 'sups AND leads'"; break; case 'north': echo "select `num` from sprint WHERE `loc` = 'north'"; break; case 'south': echo "select `num` from sprint WHERE `loc` = 'south'"; break; default: echo "select `num` from sprint WHERE `all` = 'all'"; } }else{ //empty } Link to comment https://forums.phpfreaks.com/topic/157320-if-statement-for-query/#findComment-829436 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Yeah, I know. But there are *no* variables in those SQL. I would print out a number rather than a SQL if you're uncertain which SQL is being used. Link to comment https://forums.phpfreaks.com/topic/157320-if-statement-for-query/#findComment-829443 Share on other sites More sharing options...
GingerRobot Posted May 8, 2009 Share Posted May 8, 2009 Yeah, I know. But there are *no* variables in those SQL. I would print out a number rather than a SQL if you're uncertain which SQL is being used. But the goal isn't to test the SQL per se, it's to test the switch. Link to comment https://forums.phpfreaks.com/topic/157320-if-statement-for-query/#findComment-829449 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 OH!! SEVIZ, put quotes around the cases because you're comparing strings, not constants. Link to comment https://forums.phpfreaks.com/topic/157320-if-statement-for-query/#findComment-829452 Share on other sites More sharing options...
SEVIZ Posted May 8, 2009 Author Share Posted May 8, 2009 Thanks for the help guys. Yes, right now I am testing to make sure the strings are coming out right before I move to testing the sql part. I am extremely new to all of this and I am trying to learn how this works as opposed to just tossing it in there and getting a result I do not understand. Link to comment https://forums.phpfreaks.com/topic/157320-if-statement-for-query/#findComment-829604 Share on other sites More sharing options...
SEVIZ Posted May 8, 2009 Author Share Posted May 8, 2009 Is default needed for a switch? Can I just have my separate cases with no default? Link to comment https://forums.phpfreaks.com/topic/157320-if-statement-for-query/#findComment-829799 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Is default needed for a switch? Can I just have my separate cases with no default? You don't have to put default. Link to comment https://forums.phpfreaks.com/topic/157320-if-statement-for-query/#findComment-829812 Share on other sites More sharing options...
SEVIZ Posted May 9, 2009 Author Share Posted May 9, 2009 Cool thanks. I worked on this last night and it seems I am getting very close. Thanks again for the help. It is appreciated! Link to comment https://forums.phpfreaks.com/topic/157320-if-statement-for-query/#findComment-830240 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.