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
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>

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);



Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ;)

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.