Basically I have a website where user vote on projects posted by users or "Support" everytime a user clicks the support button, it goes into a table and stores 3 things, a normal auto increment identifier, the user_id that 'supported' it and the project id they supported. Now everyday we want to display 'Top projects' on the homepage. This will my guess be done with a php script and a cron job. I can't really think of how to query the database properly to determine X amount of top projects. (Most likely 5 or 10) Iv'e thought about this for a while, but I can't think of an answer. Any ideas/answers help alot! Thanks.
Well, your going to have to add a time stamp of one sort or another to that database. So you can query by the day from midnight to 11:59p. I'd say datetime format ie: YYYY-MM-DD HH:MM:SS (its in 24 hour format)
From that if handled correctly you could do something like
<?php
$todayMidnight = date('Y-m-d'). ' 00:00:00';
$todayLastMin = date('Y-m-d'). ' 23:59:59';
$result = mysql_query("select * from the_table where the_Time BETWEEN $todayMidnight AND $todayLastMin LIMIT 10 order by the_count");
?>
Suppose, you have an SQL database table called votesTable with a field numberOfVotes.
Without a cron-job or such, you might run this MySQL-query
SELECT *
FROM votesTable
ORDER BY votesTable.numberOfVotes DESC
LIMIT 10
to retrieve the to top 10 vote entries.
This YouTube video PHP Tutorial: Simple Rating / Voting System [part 01] may help (tutorial author's page).
Related
So I'm a software Development student and for my web class I created a project that uses among other things Php and SQL; In this project, users can create posts and other users can comment on them.
The thing is I want posts to only be available for a certain period of time.
Then I have an SQL table named 'Posts' and they have a column named 'Status' (you know, if the status it's 0 they're not available and else they are.)
When a user creates a post I make SQL:
INSERT INTO posts *All the post data*, I set the Status to 1 and make a TIMESTAMP to register the date of creation of the post. I want that a week after the date registered in the Timestamp changes the status column to 0 but I don't want it to be with a page request (I need it to be automatic) and I want the user to be notified via email or something.
Can it be made with some python CGI that checks the date, updates the Status and sends the email or is there a better/easier way to do it?
Thanks a lot for your help :)
You dont need the status 0/1 AND the timestamp column, if all you want to do is show a post for a set period of time.
Just use the timestamp column and amend the queries that fetch the posts to only show those posts that are < 7 days old (or any period you decide)
EG
SELECT * from posts where timestamp_col < DATE_SUB(NOW(), INTERVAL 7 DAY)
or something similiar that meets your needs
Turns out the best way to solve this was using Cron Jobs.
I run a PHP script every day and I modify the posts which are exactly 7 days old, using
UPDATE Posts SET Status = 0 WHERE DATE(timestamp_col) = DATE_SUB(NOW(), INTERVAL 7 DAY)
And then I iterate through the affected rows emailing the users.
Say I have a table like this :
Say I want to select a column from id = 4 and date=2014/2/14
I must say mysql like this:
$db = new mysqli("localhost",'root','','myDatabase');
$sql = "SELECT * FROM myTable WHERE id=`4`";
$result= $db->query($sql);
while($row = $result->fetch_object()){
$time = $row->"2014/2/14";
echo $time;
}
And this will result like this:
9-10AM
So far it's OK with no problem
But say I want to select 6 coumun after 2014/2/14 too!!
every time I select an id an a date like 2014/2/14 , I want to show 6 day after 2014/2/14
I mean I expect result to be like this(continuing my example):
bold
9-10AM
8-10AM
9-10AM
6-7PM
for god sake, please don bit around the bushes
see:
how to do this?
Since a 75K trolling user (who now covered his tracks by deleting all comments) thinks it is wise to first teach you about complicated things like SQL injection and how to prevent this here we go. So better first read through these links you won't learn anything since it expects you to know the basics but we do not seem to care here in this community.
http://www.us-cert.gov/sites/default/files/publications/Practical-SQLi-Identification.pdf
http://www.cc.gatech.edu/fac/Alex.Orso/papers/halfond.viegas.orso.ISSSE06.pdf
http://web.archive.org/web/20070928163708/http://www.ngssoftware.com/papers/advanced_sql_injection.pdf
https://media.blackhat.com/us-13/US-13-Salgado-SQLi-Optimization-and-Obfuscation-Techniques-WP.pdf
While we are learning already about injection it might be wise to know how to optimize your database too since you are talking about large numbers.
http://www.mysqlperformanceblog.com/files/presentations/UC2005-Advanced-MySQL-Performance-Optimization.pdf
And a comprehensive guide to working with date and time since you want to work with date and time.
http://oreilly.com/catalog/mysqlian/chapter/ch06.pdf
(believe me, i would have written all these guides down here for future reference but that would take to much time, so forgive me that i just post the links).
Now you are probably a view years older and know pretty much about MySQL, yet i leave my original answer here for others that might bump into the same problem. You have to look into normalization of your database. Mysql can look for time and dates if they are records inside a table. Not if they are column headers.
name your columns like: ID - date - time
Now you can search for a certain time with BETWEEN like this:
WHERE date BETWEEN (yourdate) AND (yourdate + INTERVAL 7 DAY)
This will result in picking all records from the date you select plus 7 days.
-edit-
As for your comment, name your tables like ID - doctor - date - time
Now if you want to see all doctors availability in the comming week between 9:00 and 12:00 do:
$doctorQuery = mysql_query("SELECT doctor FROM theTable WHERE date BETWEEN (NOW()) AND (NOW() + INTERVAL 7 DAY) AND date TIME 9:00:00 AND 12:00:00");
while ($result = mysql_fetch_assoc($doctorQuery))
{
echo $result['doctor']; //you can output it here already.
$resultarray[] = $result; //or store it for later in a 3 dimensional array.
}
foreach ($resultArray = $singleRow)
{
echo $singleRow['doctor']; //And output it whenever you want.
}
Forgive me, i'm little rusty and this all is not tested. But this should sent you in the right direction. Please for your own sake, believe us and do not create collumns with dates as headers. This will cause problems later on, one reason is, in two years from now you would have added 730 columns to your table. Of course you could use the dates from my table to output them as headers on your website to make it humanly readable.
-Another edit-
As for you latest comment you probably need a table structure like this:
table for doctors and there info:
[doctorID]-[doctorName]-[doctorInfo]-[LinkToPicture]
Here you fill in a single row for every doctor you have.
Table for there work times:
[timeID]-[doctorID]-[date]-[workhours]
doctorID is a link key, or foreign key which tells the table to which doctor this row belongs. Here fill in the date and time the doctor works. You can insert as many records as you want for each doctor.
I am working on a PHP and MySQL based application in which I am processing mysql data tables for one week data at a time. All my PHP scripts will run in a particular sequence and process the data in all tables (upto 15 tables) for given week.
Presently I have written the date filter in WHERE clause and application is working fine.
IS there any way by which I can set the week's date range at one place and all the queries are fired in all the tables with given date range.
I want this bcoz my application processes are growing and its hard to manage 20+ pages and 50+ queries written in it.
I am using command line PHP.
Please suggest the technique if any.
Thanks
You could use $_SESSION...
$sql = $db->query("SELECT * FROM table WHERE date BETWEEN '". $_SESSION['range']['from'] ."' AND '". $_SESSION['range']['to'] ."'");
... which will persist for the browser session.
SELECT stuff from your_table
WHERE your_date_field > DATE_ADD(CURDATE(), INTERVAL - 7 DAY);
Run that once a week to get last 7 days, then hack it around to get the exact results you want.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
Is there a way to add articles (data) in mysql/php but make them
auto-publish during the day when I´m not available.
So lets say, if I have a news site but I´ll be busy tomorrow the whole day so I could pre-write articles the day before with timestamp and they would appear when I want
Is this possible?
How would the script be like:
SELECT FROM articles WHEN TIME is 2011-12-01 12:15
Thanks
As simple as:
SELECT * FROM articles WHERE timestamp <= NOW()
Though I never worked with them to me the easiest solutions seems to be Cronjobs combined with an extra waiting table and a script linking both.
You pre-write your article and store them in table together with the time stamp you want to publish them.
Your cron will invoke a script every 2,3, 5 hours (twice a day, whatever).
This script checks the time stamps in the table against the actual time and if it is about time to realise the article it will do so (or hand the information to the realise script).
I'm working on a "community". And of course I would like to be able to tell if a user is online or offline.
I've created so that when you log in a row in my table UPDATE's to 1 (default is 0) and then they're online. And when they log out they're offline. But if they don't press the Log out button, they will be online until they press that button.
So what I would like to create is:
After 5 minutes of inactivity the row in my database should UPDATE to 0.
What I'm looking for is how to do this the easiest way. Should I make an mysql_query which UPDATE's the row to 1 every time a page is loaded. Or is there another way to do it?
Instead of using a boolean "Online" field, use a DateTime. When a user makes a request to the page, update the DateTime to NOW(). When you are gathering your list of current users online, your WHERE clause would be something like WHERE lastSeen > DATE_SUB(NOW(), INTERVAL 5 Minutes)
Update: To retrieve individual online status.
select if(lastSeen > date_sub(now(), interval 15 minutes), 1, 0) as status from table where userid=$userid
This tutorial is quite handy: Who Is Online Widget With PHP, MySQL & jQuery
Well, if you don't want to set up a cron job, that would execute some code every 5 minutes, you have no options. But, actually, I think the following approach would be much more efficient:
Change your 1/0 column to timestamp
On each user request update that timestamp to current DateTime.
When checking for active users, check if that timestamp is less than 5 minutes from now
This way you'll be having actual data on users and no recurring queries - just one additional update per request
If you will update the row only on page load, then some of information would be incorrect.
Let's assume that user have opened page and is writing really long text or something. He is doing it for half an hour now. And your database ny now is already updated and he is counted as offline user.
I would write javascript that pings you back each 5 minutes, if opened tab is active.
This ping updates database field 'last_activity' to NOW(). And to count online users, or check if user is online you'll need to compare 'last_activity' to NOW() minus five minutes.
Simpliest ways (IMHO):
You can count sessions in session_save_path() dir.
you can store last visit timestamp in DB, and count rows with (timestamp > current_timestamp - somedelay).