How do I show todays top search list? - php

I owns a website which is doing lots of searches per day. these search results are stored in my MySQL databse. what I want is I want retrieve today's top searches from MySQL limit to 20 and display in my website. how do I do this with PHP & MySQL??
Thanks
Mathew

You will need to do something looked like this:
$today = date("F j, Y"); // NOTICE, you have to format your date like you have # database field
$q = " SELECT search_string_field, count(search_string_field) as count
FROM search_results_table
WHERE search_date_field = '$today'
ORDER BY count DESC
GROUP BY search_string_field
LIMIT 20";
$results = mysql_query($q);
and fetch the results after executing query

You'll need to store the time for each search. With that you can query the database (using date ranges and limit) for the popular searches within a time period or you can do it with PHP.

What metric do you have for storing searches. Do you store every search term, or update a number if the term was used more than once?
If you have a number of terms searched
$query = 'SELECT * FROM searches ORDER BY searched DESC LIMIT 20';
But I think you need to give more info... what does your table look like?

Related

Limit the total number of entries displayed in the Datatable

I have a history table with 3700000+ entries. I am using server-side processing in the datatable to initially display 25 records. It is taking lots of time to initially load the datatable even though the query fetches only 25 records.
I am using MySQL database. Now I want to limit the total number of entries from which the data should be processed. I want only past 15 days entries to be considered.
Is there a way by which I can load the table quickly?
There are a number of ways you can limit how much data you get back from a SQL Query.
Using WHERE will allow you to only select entries after a certain date.
$sql = "SELECT * FROM history WHERE date > ".strtotime("-15 days");
The above query assumes that in your history database you have a date that is a unix based timestamp. Another way that you could limit the amount of data that is returned is by using the LIMIT function.
$sql = "SELECT * FROM history ORDER BY id DESC LIMIT 15";
This query assumes that you only have one entry per day, and will limit it to only 15 entries to be displayed.
Along with this, if your history table has a lot of data that doesn't need to be used, then you can only select the columns that you need, doing something like this:
$sql = "SELECT id, time, name FROM history";
SELECT * FROM $table_name WHERE created_date>= DATE(NOW() - INTERVAL 15 DAY)

PHP - Keep randomized query results for 24 hours

I have a simple query that displays random results, however I'd like to be able to keep those same results for a 24 hour period per visitor so it doesn't randomize on every page refresh. Here's the query:
<?php
$rs1= mysql_query("
SELECT * FROM Table_1
ORDER BY RAND()
LIMIT 4
");
while ($row1= mysql_fetch_array($rs1)) { ?>
I gather I'll need to use the date somehow. I did figure out how to get a random number to show for 24 hours:
<?php
date_default_timezone_set('America/Los_Angeles');
mt_srand(date('Ymd'));
$number = mt_rand(50, 5000);
mt_srand(); //reset for other calls
echo $number;
?>
But I'm not sure how to make that work with my original query. Any ideas? Or should I be creating a cookie rather than trying to do it with only php? How would I make a cookie work with it?
This is a bit CPU intensive if you have to calculate over lot of rows, but do not uses cookies nor any special data to keep selection.
select * from table order by md5(concat(id,curdate())) limit 4
Will show same order on every query on the same day. If want to be diferent based on the user doing the query but still the same on the day, you can add his id to the concat too.
select * from table order by md5(concat(id,userID,curdate())) limit 4
Should be say, that results are no more random and can be predicted by someone that takes the job. You can add a seed too, to mitigate some way this effect.
Pass the date seed that you are using into the query. MySQL's rand() function also takes a seed which would make your random results the same.
http://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_rand
Here is an example using the mysqli extension since mysql is deprecated.
<?php
date_default_timezone_set('America/Los_Angeles');
mt_srand(date('Ymd'));
$number = mt_rand(50, 5000);
$mysqli = new mysqli("localhost", "user", "password", "database");
$stmt = $mysqli->prepare("SELECT * FROM Table_1 ORDER BY RAND(?) LIMIT 4");
$stmt->bind_param('i', $number);
$stmt->execute();
You could also just do it all in the query
SELECT * FROM Table_1 ORDER BY RAND(CAST(CONCAT(YEAR(), MONTH(), DAY()) AS UNSIGNED)) LIMIT 4
Or whatever seed you want to get from the date.
Save/cache the result to a file (using serialize()) and use the contents of this file to generate your page. Then refresh this file every 24 hours as needed.

Add values from same date

I just got started learning PHP so if there's already an answer to this question, please point me to it as I wasn't able to find one.
I have a mysqli database with 2 entries: date_added & menu_price. The user inputs (through a webpage interface) the menu_price (double) while the date is automatically set in this format: Y-m-d. Note that multiple entries for menu_price can be set for the same date.
My question: I'm trying to pull out a daily report which should add all prices for every day and only display the date once.
Example: let's say I had two entries for 2015-04-10: 10 & 20 and I had 3 entries for 2015-04-11: 10, 20 & 30. So my report for 2015-04-10 should say 30 and for 2015-04-11 should say 60.
I managed to extract unique date entries (I'm not sure it's useful or not) the way described below but I'm not sure how to proceed.
// Extract unique date entries
$newDate = "SELECT DISTINCT (date_added) AS date_added
FROM main
ORDER BY date_added DESC";
$dateResult = $conn->query($newDate);
// Create an array with unique dates
$results = array();
while ($dateArray = $dateResult->fetch_assoc())
{
$results[] = $dateArray["date_added"];
};
This requires aggregate functions. Use SUM() and GROUP BY in your query:
$newDate = "SELECT date_added,
SUM(menu_price )
FROM main
GROUP BY date_added
ORDER BY date_added DESC";

MYSQL Query - Sort by Date in PHP

I'm trying to set up a query that will search a MYSQL database and only pull in the rows from the database who's expiry_date is after todays date.
I would also like to be able to work out how many days or weeks there are remaining from todays date to the expiry date of the rows in the database that match the above query.
I think that in order to get the current date I would have to set up a variable of $date = time(); which I will then later be able to use to compare against the expiry_date column in the database. However I am now stumped as what to do to achieve the required result. I'm not exactly a PHP noob but I'm not an expert either, so please go easy on me ;)
Thanks in advance!
If the Column you want to check is a DATE(TIME), try
$sql="SELECT column FROM table WHERE expiry_date > CURDATE()";
If you saved the UNIX timestamp, you can simply use
$sql="SELECT column FROM table WHERE expiry_date > '".time()."'";
If you use the first with "NOW()" or the second, you'll proably get results for the current day.
If this is not acceptable, try "mktime(0, 0, 0)" instead of time();
Use this query
$query = "select timestampdiff(days,'$exipry_date','$now')";

how to show one record per day order by id?

I have this little script that shows one wisdom each day.
so I have three columns.
Id wisdom timestamp
1 wisdon 1 4/1/2012
2 wisdon 2 4/1/2012
3 wisdon 3 4/2/2012
and I want to fetch array of one wisdom for each day
I looked around your website, but unfortunately I didn't find something similar to what I want.
also I got this code
$sql = mysql_query("SELECT DISTINCT id FROM day_table group by timestamp");
but this also not working.
any ideas?
is it possible to make a counter of 24 hours update wisdom date?
please give me some help.
You can make another table that is called wisdom_of_day
The table would have the following columns, id, wisdom_id, date
Basically each day you can randomly select a wisdom from your wisdom table and insert it into the wisdom day table. You can also add a constraint to your date column so it is distinct. It is important that it is a date column and not a timestamp since you don't care about time.
Then you can retrieve the wisdom of the day by querying based on the date.
It's possible I read your question wrong and you just want to select one wisdom for each day, but you want to show multiple days and you want to get the data from your table.
If so, the reason your query is not working is because you are grouping by a timestamp which includes the date and time. You need to group it by date for it to group like you want.
Here is a query that will group by the day correctly. This will only work if you have a timestamp field and are not storing a unix timstamp on an int column.
select id, wisdom, date(timestamp) date_only from day_table group by date_only order by date_only asc;
Hmm, I noticed that your timestamp values are in some kind of date format, maybe as a string? If so the above query probably won't work.
First compute number of days since 1970
SELECT DATEDIFF(CURDATE(), '1970-01-01')
Then insert this number inside RAND, for example:
SELECT * FROM table ORDER BY RAND(15767) LIMIT 1;
Rand with number as argument is deterministic.
Full query:
SELECT * FROM table ORDER BY RAND((SELECT DATEDIFF(CURDATE(), '1970-01-01'))) LIMIT 1;

Categories