Show today's birthday - php

I want to use the script i placed underneath, but it should show me who's having birthday today. i have added a birthday in my sql table ion this format: 1985-06-03
<html>
<head>
<title>Last 10 Results</title>
</head>
<body>
<table>
<thead>
<tr>
<td>Id</td>
<td>Name</td>
</tr>
</thead>
<tbody>
<?php
$connect = mysql_connect("localhost","root", "root");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("apploymentdevs");
$results = mysql_query("SELECT * FROM demo LIMIT 10");
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['Id']?></td>
<td><?php echo $row['Name']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
DB Table structure:
ID INT11
FirstName Varchar
LastName Varchar
Department Varchar
Birthday Date (yyyy-mm-dd)

Since, you will need to exclude the year, you can use the MONTH and DAY SQL functions like so:
SELECT * FROM table WHERE DAY(birthday) = DAY(CURDATE()) AND MONTH(birthday) = MONTH(CURDATE());

In your query, format each date to be MM-DD.
SELECT *
FROM demo
WHERE DATE_FORMAT(birthday, "%c-%d") = DATE_FORMAT(NOW(), "%c-%d")
LIMIT 10
This will bring back results where the MM-DD of both NOW() and the birthday value are equal.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

<?php
$connect = mysql_connect("localhost","root", "root");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("apploymentdevs");
$today_date = date('d');
$today_month = date('m');
$results = mysql_query("SELECT * FROM `table_name` where DATE('dob_column') = $today_date && MONTH(`dob_column`) = $today_month");
if(mysql_num_rows($results) > 0){
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['Id']?></td>
<td><?php echo $row['Name']?></td>
</tr>
<?php
}
}else{
echo "No one birthday on today enter code here";
}
?>

Can you try something like this:
$results = mysql_query("SELECT * FROM demo WHERE MONTH(`table_column`) = '".date('m')."' AND YEAR(`table_column`) = '".date('Y')."'");

Related

PHP table leaderboard

I'm trying to make a leaderboard of my favorite movies.
<table>
<tr>
<td>Rank</td>
<td>Score</td>
<td>Year</td>
</tr>
<?php
$con=mysqli_connect("localhost", "", "", "movies");
$sql = ("SELECT score, year FROM mymovies ORDER BY score DESC");
$result = mysqli_query($con, $sql);
$rank = 1;
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<td>{$rank}</td>
<td>{$row['score']}</td>
<td>{$row['number_of_times']}</td>";
$rank++;
}
}
?>
</table>
The problem is that it shows the data in one line instead of showing it underneath each other like this:
Rank Score Year
1 25 1999
2 23 1987
3 20 2005
Probably because you need to include a <tr> tag ..
<table>
<tr>
<td>Rank</td>
<td>Score</td>
<td>Year</td>
</tr>
<?php
$con=mysqli_connect("localhost", "", "", "movies");
$sql = ("SELECT score, year FROM mymovies ORDER BY score DESC");
$result = mysqli_query($con, $sql);
$rank = 1;
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>{$rank}</td>
<td>{$row['score']}</td>
<td>{$row['number_of_times']}</td>
</tr>";
$rank++;
}
}
?>
</table>

PHP calendar with dates --> distinct weeks and merging rows

I am currently creating a calendar where you can see all users that are saved in the database for that day and the possibility to add an user per date.
I want to work with dates because I think it is easier to work with in the future.
So what I am doing: I save a date into the database combined to an userid.
While I am fetching the data from database into a table I am converting the dates to days and weeks and years. Just to see which day is at 2015/10/05.
So I get $day = Monday, $week = 41, $year = 2015
In the table I want to put the users to the matching day.
The calendar looks like this:
I am calling an if statement:
if ($day == 'Mon') {
// SELECT * FROM users WHERE datum=$date
// Fetch and echo the users name into that day
}
The database looks like this:
The weeks showed here in the database, maybe they can be used for a proper display? But I don't know I tried several things.
I am using this code for putting the database rows into a table:
$sql = "SELECT distinct(week),datum, van , tot FROM roosternew ORDER BY datum ASC";
$run_sql = mysqli_query($conn , $sql);
?>
<table class="table">
<thead>
<tr class="primary">
<th width="5%"><center>Week</center></th>
<th width="10%">Maandag</th>
<th width="10%">Dinsdag</th>
<th width="10%">Woensdag</th>
<th width="10%">Donderdag</th>
<th width="10%">Vrijdag</th>
<th width="10%">Zaterdag</th>
<th width="10%">Zondag</th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_assoc($run_sql)) {
echo $row['datum'].'test';
$datum = $row['datum'];
$van = $row['van'];
$tot = $row['tot'];
$tijd = $van.' - '.$tot;
$date = new DateTime($datum);
$week = $date->format("W");
$dag = $date->format("D");
$jaar = $date->format("Y");
$newdate_ = new DateTime($datum);
$newdate = $newdate_->format('Y-m-d');
$week_start = new DateTime();
$week_start->setISODate($jaar,$week);
$thisdate_ = $week_start->format('d-m');
//Create plus button
$plus_button = ' <i class="fa fa-plus-square"></i><br/>';
//Plus button
$thisdate = '<div class="datetable"><div class="datetable">'.$thisdate_.'</div></div><br/>';
?>
<tr>
<td class="primary"><center><strong><?= $week; ?></strong><br/><?php echo $thisdate.'<h4><small>'.$jaar.'</small></h4>'; ?></center></td>
<td>
<?php
createPlusButton(0, $jaar,$week);
if ($dag == 'Mon') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(1, $jaar,$week);
if ($dag == 'Tue') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(2, $jaar,$week);
if ($dag == 'Wed') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(3, $jaar,$week);
if ($dag == 'Thu') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(4, $jaar,$week);
if ($dag == 'Fri') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(5, $jaar,$week);
if ($dag == 'Sat') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(6, $jaar,$week);
if ($dag == 'Sun') {
getScheduleAdmin($newdate);
}
?>
</td>
</tr>
<? } ?>
</tbody>
</table>
The function getScheduleAdmin($newdate):
function getScheduleAdmin($newdate) {
require('connect.php');
$sql2 = "SELECT * FROM roosternew WHERE datum='$newdate'";
$run_sql2 = mysqli_query($conn , $sql2);
while ($row2 = mysqli_fetch_assoc($run_sql2)) {
$id_rooster_users = $row2['idusers'];
$sql= mysqli_query($conn,"SELECT * FROM users WHERE idusers='$id_rooster_users'");
$row = mysqli_fetch_array($sql);
$name = $row['name'];
$color = $row['color'];
$sql3 = "SELECT van,tot FROM roosternew WHERE idusers= '$id_rooster_users'";
$sql3_run = mysqli_query($conn,$sql3);
$sql3_array = mysqli_fetch_array($sql3_run);
$van_user = $sql3_array['van'];
$tot_user = $sql3_array['tot'];
$tijd_user = $van_user.' - '.$tot_user;
echo ''.$name.'<br/>'.'('.$tijd_user.')';
echo '<br/>';
}
}
The problem is described in the first image (the calendar), it will show me more than once the week and they won't merge.
I dont know how to merge the weeks so that it will be :
Something like that.
And not like this (situation right now):
if anyone knows a better way of doing this, please tell me.

Showing data correctly in html table with php

Hello this table pulls data from a database.
<h2>Weekly appointment list</h2>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Week Day</th>
<th>Customers</th>
<th>Selected service</th>
<th>Time</th>
</tr>
</thead>
<tbody>
<tr>
<td>Monday</td>
<?php
$date = date('Y-m-d', strtotime("this Monday"));
$sql = "SELECT * FROM appointment WHERE weekday = '$date'";
$query = mysqli_query($db, $sql);
$numRows = mysqli_num_rows($query);
if ($numRows > 0) {
while ($row = mysqli_fetch_array($query)) { ?>
<td><?php echo $row['user_name'] ?></td>
<td><?php echo $row['service'] ?></td>
<td><?php echo $row['time'] ?></td>
<?php }
}
?>
</tr>
</tbody>
But the problem is when i have two users from echo $row['user_name'] //user x, user y the table rows break and show something like this: image link. See the the table row is broken. I want to show this way:expected table structure. All customers are shown on the particular day row in customers column. How to fix my code or the way of representation. Thanks in advance.
change your sql query to group concat username,service and time.
$sql ="SELECT group_concat(user_name) as user_name,group_concat(service) as service ,group_concat(time) as time FROM appointment WHERE weekday = '$date'";
This query will return one row with all the user and service information in one row.
you want to combine something like this
With something like this:
$res = mysql_query(/**/);
$rows = array();
while($row = mysql_fetch_assoc($res)){
array_push($rows, $row);
}
Let me know if you struggle.

PHP - displaying last 3 SQL rows in a table (not working)

I'm trying to develop a user profile system on my website, with the previous 3 posts made by the user. I can get it to select the previous 3 records, but it will only display one of them. Am I just being stupid because I'm trying to code at 2am?
<?php
$q = "SELECT * FROM blog_cmt WHERE uid=".$profile_uid." ORDER BY id DESC LIMIT 3";
$r = $db->query($q);
$c = $r->num_rows;
?>
<table class="table">
<?php
while($post = $r->fetch_assoc()) {
$pid = $post['pid'];
$q = "SELECT * FROM blog WHERE pid=".$pid;
$r = $db->query($q);
$blog = $r->fetch_assoc();
$title = $blog['title'];
$date = timeSince(strtotime($post['date']));
?>
<tr>
<td>
Commented on <?php echo $title; ?>
</td>
<td>
<?php echo $date; ?> ago
</td>
</tr>
<?php
}
?>
</table>
<?php
$profile_post_qry = "SELECT * FROM blog_cmt WHERE uid=".$profile_uid." ORDER BY id DESC LIMIT 3";
$profile_posts = $db->query($profile_post_qry);
$profile_post_count = $profile_posts->num_rows;
?>
<table class="table">
<?php
while($post = $profile_posts->fetch_assoc()) {
$pid = $post['pid'];
$blog_qry = "SELECT * FROM blog WHERE pid=".$pid;
$blog_info = $db->query($blog_qry);
$blog = $blog_info->fetch_assoc();
$title = $blog['title'];
$date = timeSince(strtotime($post['date']));
?>
<tr>
<td>
Commented on <?php echo $title; ?>
</td>
<td>
<?php echo $date; ?> ago
</td>
</tr>
<?php

MySQL/PHP News System

I have a PHP/MySQL News system which displayes the newest news article on the home page and a full list on a news page.
The newest article bit works but, my problem is that whenever i try to echo all the news article on the news page it either repeats the same or outputs one and nothing else.
**MySQL Information**
id INT AUTO_INCREMENT,
author VARCHAR(xxx),
title VARCHAR(xxx),
message TEXT,
date TEXT,
time TEXT,
PRIMARY KEY(id)
This is the insertion page (news_center.php)
<form action='/?module=admin&n=news_center_ac' method='post'>
<table align="center" width="68%">
<tr>
<td>Title</td>
<td><input style="width:100%;" type='text' name='news_title' /></td>
</tr>
<tr>
<td height="57">Message</td>
<td><input style="width:100%; height:100%;" type='text' name='news_message' /></td>
</tr>
<tr>
<td colspan='2'><input type='submit' /></td>
</tr>
</table>
This is news_center_ac.php
<?php
$conn = mysql_connect(*Connection Information*) or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$author == $_SESSION['name'];
if(!empty($_POST['news_title']) || !empty($_POST['news_message']))
{
if(!empty($_POST['news_title']) && !empty($_POST['news_message']))
{
$date = date( 'jS F Y' );
$time = date( 'H:i' );
$query = "INSERT INTO news (id, author, title, content, date, time) VALUES('', '".$author."', '".$_POST['news_title']."', '".$_POST['news_message']."', '".$date."', '".$time."')" or die(mysql_error());
$insert = mysql_query($query) or die(mysql_error());
echo '<p>Successful News Update “'.$_POST['news_title'].'”';
}
else
{
echo '<p>Please fill in all fields</p>';
}
}
?>
This is the Output on the news page (/news/index.php)
<?php
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");
$output = mysql_fetch_array($news);
?>
*CONTENT*
<?php
foreach ($output as $value) {
echo "<p> “" .$output['content'];
echo "”";
echo "Posted:" .$output['date'];
echo " " .$output['time'];
}
?>
I just want it to output each news article in turn i can sort out the formatting later once it works.
You are misusing mysql_fetch_array(). It needs to be called in a loop, as it only returns one row at a time.
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");
EDIT Added htmlentities() calls to convert html special characters
while ($row = mysql_fetch_array($news)) {
echo "<p> “" . htmlentities($row['content']);
echo "”";
echo "Posted:" . htmlentities($row['date']);
echo " " . htmlentities($row['time']);
}
Rewrite it this way:
<?php
while($output = mysql_fetch_array($news)) {
echo "<p> “" .$output['content'];
echo "”";
echo "Posted:" .$output['date'];
echo " " .$output['time'];
}
?>
When you call output first, it is only returning one value, this will loop through all.
try /news/index.php
<?php
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");
while($output = mysql_fetch_assoc($news)) {
echo "<p> “" .$output['content'];
echo "”";
echo "Posted:" .$output['date'];
echo " " .$output['time'];
}
?>
Shouldn't the last foreach loop use $value, ie
<?php
foreach ($output as $value) {
echo "<p> “" .$value['content'];
echo "”";
echo "Posted:" .$value['date'];
echo " " .$value['time'];
}
?>

Categories