Jump to content

if statement for query


SEVIZ

Recommended Posts

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="reply@here.com"></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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.