Jump to content

How to display upcoming birthdays?


Scorptique

Recommended Posts

Hello again guys, I need some help here.

 

I have a list of contact details, which includes their birthdays. If user clicks the link "Sort by birthday", the url will become contacts.php?category=cl_dob. So I use the GET method and put it in $category. How do I sort the contacts by the upcoming birthdays? Means the contact with the nearest birthday will be at the top.

 

 

I tried this but it didnt work. Help guys.

 

if ($category == 'cl_dob')

{

$sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob from contact_list where username = '$username'  AND DATE_FORMAT('cl_dob', '%m-%d') > DATE_FORMAT(NOW(), '%m-%d')  ORDER BY cl_dob DESC ";

}

 

(I didnt use the <code></code> thingy because there was sth wrong with it)

 

 

Link to comment
https://forums.phpfreaks.com/topic/135800-how-to-display-upcoming-birthdays/
Share on other sites

You can't use an alias to order, try this;

 

$sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob_formatted from contact_list where username = '$username'  AND DATE_FORMAT('cl_dob', '%m-%d') > DATE_FORMAT(NOW(), '%m-%d')  ORDER BY cl_dob DESC ";

 

Note, to get the formatted date use $row['cl_dob_formatted']

 

Also the code tags are [ code ] [ /code ] not <code> </code>

Well, I dont think that's a good idea because it is 300 lines long! I just paste the few I think are important.

 

 

   

Sort by <a href="main3.php?card_id=<?php echo $card; ?>&sort=cl_fname">name</a> | 
    <a href="main3.php?card_id=<?php echo $card; ?>&sort=cl_dob">Incoming birthdays</a> | 
    <a href="main3.php?card_id=<?php echo $card; ?>&sort=cl_email">Email</a>


$category= $_GET['sort'];
if (empty($category))
{
	$category = 'cl_fname';
}


$conn = mysql_connect("localhost", "root", null);
mysql_select_db("ecarddb", $conn);
if ($category == 'cl_fname'){
$sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob from contact_list where username = '$username' order by cl_fname";}
if ($category == 'cl_email'){
$sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob from contact_list where username = '$username' order by cl_email";}
if ($category == 'cl_dob'){
$sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob_formatted from contact_list where username = '$username'  AND DATE_FORMAT('cl_dob', '%m-%d') > DATE_FORMAT(NOW(), '%m-%d')  ORDER BY cl_dob DESC ";}

$result = mysql_query($sql, $conn) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
	$cl_email = $row['cl_email'];
	?>
        
        <table style="word-wrap:break-word;">
  		<tr>
    	<td height="21">
	<?php echo '<td width="100">'. $row['cl_fname'].', '. $row['cl_lname'].'</td>';
	echo '<td width="85">'.$row['cl_dob'].'</td>';
	echo '<td width="170">'.$row['cl_email'].'</td>'; ?></td>
		</tr>
	</table> <?php
}
mysql_close($conn);

Note, to get the formatted date use $row['cl_dob_formatted']

 

Sort by <a href="main3.php?card_id=<?php echo $card; ?>&sort=cl_fname">name</a> |
    <a href="main3.php?card_id=<?php echo $card; ?>&sort=cl_dob">Incoming birthdays</a> |
    <a href="main3.php?card_id=<?php echo $card; ?>&sort=cl_email">Email</a>
   

$category= $_GET['sort'];
   if (empty($category))
   {
      $category = 'cl_fname';
   }

   
   $conn = mysql_connect("localhost", "root", null);
   mysql_select_db("ecarddb", $conn);
   if ($category == 'cl_fname'){
   $sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob_formatted from contact_list where username = '$username' order by cl_fname";}
   if ($category == 'cl_email'){
   $sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob_formatted from contact_list where username = '$username' order by cl_email";}
   if ($category == 'cl_dob'){
   $sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob_formatted from contact_list where username = '$username'  AND DATE_FORMAT('cl_dob', '%m-%d') > DATE_FORMAT(NOW(), '%m-%d')  ORDER BY cl_dob DESC ";}

   $result = mysql_query($sql, $conn) or die(mysql_error());
   while($row = mysql_fetch_array($result))
   {
      $cl_email = $row['cl_email'];
      ?>
       
        <table style="word-wrap:break-word;">
        <tr>
       <td height="21">
      <?php echo '<td width="100">'. $row['cl_fname'].', '. $row['cl_lname'].'</td>';
      echo '<td width="85">'.$row['cl_dob_formatted'].'</td>';
      echo '<td width="170">'.$row['cl_email'].'</td>'; ?></td>
       </tr>
      </table> <?php
   }
   mysql_close($conn);

Sort by <a href="main3.php?card_id=<?php echo $card; ?>&sort=cl_fname">name</a> |
    <a href="main3.php?card_id=<?php echo $card; ?>&sort=cl_dob">Incoming birthdays</a> |
    <a href="main3.php?card_id=<?php echo $card; ?>&sort=cl_email">Email</a>
   

$category= $_GET['sort'];
   if (empty($category))
   {
      $category = 'cl_fname';
   }

   
   $conn = mysql_connect("localhost", "root", null);
   mysql_select_db("ecarddb", $conn);
   if ($category == 'cl_fname'){
   $sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob_formatted from contact_list where username = '$username' order by cl_fname";}
   if ($category == 'cl_email'){
   $sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob_formatted from contact_list where username = '$username' order by cl_email";}
   if ($category == 'cl_dob'){
   $sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob_formatted from contact_list where username = '$username'  AND DATE_FORMAT('cl_dob', '%m-%d') > DATE_FORMAT(NOW(), '%m-%d')  ORDER BY cl_dob DESC ";}

echo $sql;

die();

   $result = mysql_query($sql, $conn) or die(mysql_error());
   while($row = mysql_fetch_array($result))
   {
      $cl_email = $row['cl_email'];
      ?>
       
        <table style="word-wrap:break-word;">
        <tr>
       <td height="21">
      <?php echo '<td width="100">'. $row['cl_fname'].', '. $row['cl_lname'].'</td>';
      echo '<td width="85">'.$row['cl_dob_formatted'].'</td>';
      echo '<td width="170">'.$row['cl_email'].'</td>'; ?></td>
       </tr>
      </table> <?php
   }
   mysql_close($conn);

 

That will echo the query so we can see whats going on

Hm..it just echos out the same statement.

 

"select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob_formatted from contact_list where username = 'raven' AND DATE_FORMAT('cl_dob', '%m-%d') > DATE_FORMAT(NOW(), '%m-%d') ORDER BY cl_dob DESC"

I'm a bit lost, so I'm going to try a few other things...

 

Sort by <a href="main3.php?card_id=<?php echo $card; ?>&sort=cl_fname">name</a> |
    <a href="main3.php?card_id=<?php echo $card; ?>&sort=cl_dob">Incoming birthdays</a> |
    <a href="main3.php?card_id=<?php echo $card; ?>&sort=cl_email">Email</a>
   

$category= $_GET['sort'];
   if (empty($category))
   {
      $category = 'cl_fname';
   }

   
   $conn = mysql_connect("localhost", "root", null);
   mysql_select_db("ecarddb", $conn);
   if ($category == 'cl_fname'){
   $sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob_formatted from contact_list where username = '$username' order by cl_fname";}
   if ($category == 'cl_email'){
   $sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob_formatted from contact_list where username = '$username' order by cl_email";}
   if ($category == 'cl_dob'){
   $sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob_formatted from contact_list where username = '$username'  AND DATE_FORMAT('cl_dob', '%m-%d') > DATE_FORMAT(NOW(), '%m-%d')  ORDER BY cl_dob DESC ";}

   $result = mysql_query($sql, $conn) or die(mysql_error());
  echo mysql_num_rows($result);
die();
   while($row = mysql_fetch_array($result))
   {
      $cl_email = $row['cl_email'];
      ?>
       
        <table style="word-wrap:break-word;">
        <tr>
       <td height="21">
      <?php echo '<td width="100">'. $row['cl_fname'].', '. $row['cl_lname'].'</td>';
      echo '<td width="85">'.$row['cl_dob_formatted'].'</td>';
      echo '<td width="170">'.$row['cl_email'].'</td>'; ?></td>
       </tr>
      </table> <?php
   }
   mysql_close($conn);


 

This will echo the number of rows you're getting back from the database

That's why you only get one row, because your asking for all the rows from the database where username='$username' (that is only one row)

 

try this;

 

Sort by <a href="main3.php?card_id=<?php echo $card; ?>&sort=cl_fname">name</a> |
    <a href="main3.php?card_id=<?php echo $card; ?>&sort=cl_dob">Incoming birthdays</a> |
    <a href="main3.php?card_id=<?php echo $card; ?>&sort=cl_email">Email</a>
   

$category= $_GET['sort'];
   if (empty($category))
   {
      $category = 'cl_fname';
   }

   
   $conn = mysql_connect("localhost", "root", null);
   mysql_select_db("ecarddb", $conn);
   if ($category == 'cl_fname'){
   $sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob_formatted from contact_list order by cl_fname";}
   if ($category == 'cl_email'){
   $sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob_formatted from contact_list order by cl_email";}
   if ($category == 'cl_dob'){
   $sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob_formatted from contact_list where DATE_FORMAT('cl_dob', '%m-%d') > DATE_FORMAT(NOW(), '%m-%d')  ORDER BY cl_dob DESC ";}

   $result = mysql_query($sql, $conn) or die(mysql_error());

   while($row = mysql_fetch_array($result))
   {
      $cl_email = $row['cl_email'];
      ?>
       
        <table style="word-wrap:break-word;">
        <tr>
       <td height="21">
      <?php echo '<td width="100">'. $row['cl_fname'].', '. $row['cl_lname'].'</td>';
      echo '<td width="85">'.$row['cl_dob_formatted'].'</td>';
      echo '<td width="170">'.$row['cl_email'].'</td>'; ?></td>
       </tr>
      </table> <?php
   }
   mysql_close($conn);



OMG. Gevans! I found this code somewhere online...

 

$sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob,
(TO_DAYS((cl_dob + INTERVAL (YEAR(CURRENT_DATE)-YEAR(cl_dob) + (RIGHT(CURRENT_DATE,5)>
RIGHT(cl_dob,5))) YEAR)) - TO_DAYS(CURRENT_DATE)) as daysAway from contact_list where
username = '$username' order by daysAway DESC";

 

And it works! But...i dont really understand (TO_DAYS((cl_dob + INTERVAL (YEAR(CURRENT_DATE)-YEAR(cl_dob) + (RIGHT(CURRENT_DATE,5)>RIGHT(cl_dob,5))) YEAR)) - TO_DAYS(CURRENT_DATE)). if you understand, can you explain to me? or should I post this in the mysql forum instead.

Glad you got it working!! your best way to find out what all the seperate parts are doing is to visit mysql.com.

 

There you'll be able to find all those functions and find out what they do.

 

Alternatively as you said you could post it in the mysql forum ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.