I have Drupal 7 View. It contains 2249 results to be displayed on a graph. I am using the highcharts module to display that data. I have created a running total of the integer data with a global math expression field. I wanted to keep the expression's total on the graph but limit the amount of results as they are overlapping each other and it looks really bad.
I am using aggregation to combine days that are similar but it seems at this point date granularity is not available with the date module in views.
Is there a way to do this views php filter and use a bit of php to do the math so it would work ?
I have a Global:PHP filter that has this in it but it's not doing anything right maybe someone has a tip to get it working properly -
if( $row->id % 10 == 0 ) return TRUE;
thanks
Related
I'm trying to make pagination work on my site dynamically and I'm not sure how to change regular pagination to accomplish what I need to do and I was wondering if a PHP guru could help out!
Basically, once a week a report runs and populates records for the last week in my database. Also, at the end of the month, the report runs and pulls in the last month of data.
I would like to build my PHP page so that all items in the same month (~4-5 weeklies and 1 monthly) show up on the same page, and that each pagination page is a different month's data.
Currently the weeks are stored as two fields, start and end date, in the format: 2018-mm-dd.
How could I have it so that page 1 is all 2018-03 records, page 2 is all 2018-02 records and page 3 is all 2018-01 records? I also would like this to be automated so each newest month becomes thE new page 1.
Does this make sense? Is this possible? Any assistance would be greatly appreciated!! Thank you very much!!!
I am currently working on a simple booking system and I need to select some ranges and save them to a mysql database.
The problem I am facing is deciding if it's better to save a range, or to save each day separately.
There will be around 500 properties, and each will have from 2 to 5 months booked.
So the client will insert his property and will chose some dates that will be unavailable. The same will happen when someone books a property.
I was thinking of having a separate table for unavailable dates only, so if a property is booked from 10 may to 20 may, instead of having one record (2016-06-10 => 2016-06-20) I will have 10 records, one for each booked day.
I think this is easier to work with when searching between dates, but I am not sure.
Will the performance be noticeable worse ?
Should I save the ranges or single days ?
Thank you
I would advise that all "events" go into one table and they all have a start and end datetime. Use of indexes on these fields is of course recommended.
The reasons are that when you are looking for bookings and available events - you are not selecting from two different tables (or joining them). And storing a full range is much better for the code as you can easily perform the checks within a SQL query and all php code to handle events works as standard for both. If you only store one event type differently to another you'll find loads of "if's" in your code and find it harder to write the SQL.
I run many booking systems at present and have made mistakes in this area before so I know this is good advice - and also a good question.
This is too much for a comment,So I will leave this as an answer
So the table's primary key would be the property_id and the Date of a particular month.
I don't recommend it.Because think of a scenario when u going to apply this logic to 5 or 10 years system,the performance will be worse.You will get approximately 30*12*1= 360 raws for 1 year.Implement a logic to calculate the duration of a booking and add it to table against the user.
I have a model Task(id, start_date, end_date, description). I use Paginator like
$this->Paginator->settings = array(
'Task'=>array(
'contain'=>$contain,
'limit'=> $limit,
'conditions'=>$conditions,
'order'=>'Task.start_date ASC',
'page'=> $page,
));
What I'm after is to be able to know the range of start_date covered by each page of the paged set. Instead of page numbers (i.e. in view generated by $this->Paginator->numbers()) I'd like to create links like "2 weeks ago" and "Today" that jump to the page containing the first Task with start_date > NOW()-14Days, for example.
I fully understand I could alter my $conditions and set a range on the start_date, but I want the whole set.
Open to other ideas on how to achieve the same result, or any pointers in the right direction.
You need to calculate the records before and after the given start date of your condition and then divide it by the number of records per page to get the page it is on.
However, this is not very user friendly, instead I would do this:
Pass your start date and range in the URL and based on the passed values build your query that you then use within your pagination settings.
/something/index?range=last-two-weeks
And check for the range value and do a switch for that value to set the right conditions. This will directly filter only the records the user is really interested in by a clear to understand URL.
I ended up running my search twice, once paginated, once not. I then went through the unpaged results
$pgStarts = array();
$pgCounter = 1;
foreach($tasks as $k => $task){
if(($k % $limit) == 0){
$pgStarts[$pgCounter] = $task['Task']['start_time'];
$pgCounter++;
}
}
Once I had the array of start dates for each page I could easily change the way the paging numbers were labeled. I was even able to break down the key event day by hour (most tasks are on this day).
I'm happy with the result, as I think it gives the paging more context. I'll continue to look for a better way of doing it, but this will do for now.
I would like to override the _wp_mysql_week() wordpress function located in wp-includes/functions.php since the actual _wp_mysql_week() is not complying with the ISO 8601 and asking the mode 1 to the WEEK() MYSQL function instead of the mode 3.
I'd like to put a filter or something in the theme functions.php but couldn't find how.
Thx for any help
The way this function is rigged up in WP 4.1, if you specify "Monday" as the first day of the week in the options table, you'll get ISO8601:1988 week numbers (MySQL Mode 1). Otherwise you'll get mode 0.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week
You can also intervene with a filter after the query is constructed but before it's passed over to MySQL. In your filter you can muck around with the text of the query to change
WEEK( something, 0 )
to
WEEK( something, 1 )
to get your ISO 8601:1988 behavior. This explains query filtering. http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_clauses
Notice that the _wp_mysql_week function helpfully generates its output with spaces where I have shown pound signs here.
WEEK(#something,#1#)
If your week starts on some day besides Sunday or Monday, the generated query code looks like this:
WEEK(#DATE_SUB(#something,#INTERVAL#3#DAY#),#0#)
So, you can probably use a regex in your filter to search for
WEEK( .+? 0 )
then change the digit to 1. But of course regexs have hazards.
You could also request that a new filter be added specifically to the output of _wp_mysql_week in order to allow user code to control it. https://core.trac.wordpress.org/ The WordPress team is generally receptive to such requests.
I mean what the most efficient way to get information about the quantity of your page's items and make sql query with LIMIT that you need. or I should get all items and then crop array with php functions?
now I do 2 queries: first to count all items and second to get items that I need with LIMIT.
OK, I'll be more concrete. For example I need to show a question on my page and 20 answers to this question. At the bottom there shold be page control: links to the next, prev page and so on. I want to show proper number of links (number of answers/20) and when I go to any link I want to recieve proper answers (for example 41 to 60 on the 3d page). So what's the best way to get number of items (answers) to show proper number of links and to get proper answers for each link?
I guess your'e trying to say you want to know how many items/answers there is in the query but only read up to 20 items at at time, for pagination.
Firstly: You really should look for a pagination package; lots and lots of people have had the same problem before and there probably exists both free/opensource and proprietary solutions for your programming language and framework. (If you say what language you are using I'm sure someone can reccomend a solution for you.)
Anyway, I know I like to know how things work, so this is how it usually does:
As far as I know the pagination code calculates the pages by doing one query using select count(*) from tblX where something divide this number with the items-per-page number and use ceiling (e.g. 4.1 => 5).
For listing the results per page a new query is required; don't worry the count query is terribly much faster than getting every result discarding the ones you don't need DO NOT DO THAT (that's the recipie for becoming the top story on this page). Something like select * from tblX where something limit Y offset Z where Y is the number of items per page, and Z is the the (requested_page - 1)*Y; page 1 will have offset 0, page 2 have offset 20 (if thats what Y are) etc..
But do not try to implement this manually, it's unneccesary, tedious and error prone, much better to use your time customizing a readymade solution.
I'm assuming you want a count of the number of rows you'll be reading so as to do some pagination or similar? I don't understand your need for the LIMIT in the context of your question. However, if you just want a count of how many rows have been found, use one of the following.
You select the count of all rows such as:
select count(*) as counted, name, address
from contact
Or found rows:
SELECT SQL_CALC_FOUND_ROWS, name, address
from contact
This may be mysql specific I'm not sure.
Update:
For pagination you would do something like the following - (Psuedocode)
$rows = array($result)
$num_rows = sql_calc_found_rows
$per_page = 20
$pages = ceil($num_rows / $per_page)
page
$rows_this_page = array()
$rows_this_page = get_values($rows, (min index)$page_number * $per_page - $per_page, (max index)$page_number * $per_page - 1)