Jump to content

[SOLVED] complex SELECT and...


Grant Holmes

Recommended Posts

I have a successful query that is pulling date(s).

 

I then compare them to display today, or a range using a calendar script (below what is posted). All that is working. However, I now want to modify the SELECT to not only pull that data, but ONLY when another field is "active" by the field having a "1" (its an TINYINT field). If the field contains a "0" (zero), I don't want the record. I've tried modifying the code below several ways and am not making progress.

 

So the other SELECT would be [ where Active='1' ]

 

I'm not a "programmer", but am trying to learn the logic. Your help is appreciated.

====My Current SELECT====

include("dbinfo.inc.php");
$tDay = date("m-d");
$fromDay = $_REQUEST["f_date"];
$toDay = $_REQUEST["t_date"];
mysql_connect(mysql,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
if (!empty($_REQUEST["t_date"])) {
  $query='SELECT *,DATE_FORMAT(Cbirthdate,"%m-%d") AS rbirthdate FROM birthdays where DATE_FORMAT(Cbirthdate,"%m-%d") >= "'.$fromDay.'" AND DATE_FORMAT(Cbirthdate,"%m-%d") <= "'.$toDay.'"';
} else if (!empty($_REQUEST["showall"])) {
  $query='SELECT *,DATE_FORMAT(Cbirthdate,"%m-%d") AS rbirthdate FROM birthdays';
} else if (!empty($_REQUEST["id"])) {
  $query='SELECT *,DATE_FORMAT(Cbirthdate,"%m-%d") AS rbirthdate FROM birthdays where id='.$_REQUEST["id"];  
} else {
  $query='SELECT *,DATE_FORMAT(Cbirthdate,"%m-%d") AS rbirthdate FROM birthdays where DATE_FORMAT(Cbirthdate,"%m-%d") = "'.$tDay.'"';
}

 

modified per request. Thanks!

Link to comment
Share on other sites

Zanus, I think I understood the logic that you gave, I have no idea where to insert it in my code.

 

When I look at the code, I see four SELECT statements. Where do I put the [ AND `active` = 1 ]  and do I do it in each of the four statements?

 

Or there are three WHERE statements. Do I only insert in those? (and where do I plop it in)

 

That is my issue.

Link to comment
Share on other sites

Sorry. So in one of my statements:

  $query='SELECT *,DATE_FORMAT(Cbirthdate,"%m-%d") AS rbirthdate FROM birthdays where DATE_FORMAT(Cbirthdate,"%m-%d") >= "'.$fromDay.'" AND DATE_FORMAT(Cbirthdate,"%m-%d") <= "'.$toDay.'"';

 

I think the most basic issue is WHERE to insert it. I've done other statements and used "AND", but in this one, there are so many quotes (single and double) and greater/than and () that I'm not sure. This is where I think I'm failing.

 

So, using "XX " can you point to where I would put it? like:

  $query='SELECT *,DATE_FORMAT(Cbirthdate,"%m-%d") AS rbirthdate FROM birthdays where DATE_FORMAT(Cbirthdate,"%m-%d") >= "'.$fromDay.'" XX AND DATE_FORMAT(Cbirthdate,"%m-%d") <= "'.$toDay.'"';

 

Thanks again

 

Link to comment
Share on other sites

I used a guess and place the "new" SELECT before the date and it works. Thanks for helping me with thinking this through.

 

include("dbinfo.inc.php");
$tDay = date("m-d");
$fromDay = $_REQUEST["f_date"];
$toDay = $_REQUEST["t_date"];
mysql_connect(mysql,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
if (!empty($_REQUEST["t_date"])) {
  $query='SELECT *,DATE_FORMAT(Cbirthdate,"%m-%d") AS rbirthdate FROM birthdays where `Active` = 1 AND DATE_FORMAT(Cbirthdate,"%m-%d") >= "'.$fromDay.'" AND DATE_FORMAT(Cbirthdate,"%m-%d") <= "'.$toDay.'"';
} else if (!empty($_REQUEST["showall"])) {
  $query='SELECT *,DATE_FORMAT(Cbirthdate,"%m-%d") AS rbirthdate FROM birthdays';
} else if (!empty($_REQUEST["id"])) {
  $query='SELECT *,DATE_FORMAT(Cbirthdate,"%m-%d") AS rbirthdate FROM birthdays where `Active` = 1 AND id='.$_REQUEST["id"];  
} else {
  $query='SELECT *,DATE_FORMAT(Cbirthdate,"%m-%d") AS rbirthdate FROM birthdays where `Active` = 1 AND DATE_FORMAT(Cbirthdate,"%m-%d") = "'.$tDay.'"';
}

Link to comment
Share on other sites

Well, I thought I had it fixed. I use this code in two places. In a "backend" page where the user can make the record active/not. The code works there as posted last above.

 

However, when I use it to display publicly, it returns nothing.

 

sigh  :'(    At this point, I'm not sure what help to ask for.

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.