EE v2.5.3
I'm trying to accomplish the following:
6 Radio Shows (Mon-Fri)
1 Radio Show (Sun)
Default Setting (Logo)
I'd like to be able to tell EE the following:
If Monday - Friday AND
0600 - 1000
Morning Show
else if
1000 - 1500
Midday Show
etc etc..
else if
Sunday AND
0600 - 1200
Sunday Show
else if
1700-1900
Sunday Night Show
else
Default display of Logo
My (non-working) Example:
<div id="in_studio_now_content" class="container_4">
{if
'{current_time format="%l"}' == Thursday AND
'{current_time format="%H%i"}' >= '1000' AND
'{current_time format="%H%i"}' <= '1700'
}
<div class="showContainer">
<img src="http://placehold.it/75x75" class="container_1" />
<div class="showInfo left">
<h5>The Midday Show</h5>
<p>with Jenn</p>
<p class="timeslot">Weekdays 10:00 - 3:00 pm</p>
<div id="facebookLike">F like 32k</div>
</div>
More about this show ›
</div><!-- /show -->
{/if}
</div><!-- studio content -->
This should sort you
{if
'{current_time format='%l'}' == 'Thursday' &&
'{current_time format='%H%i'}' >= '1000' &&'{current_time format='%H%i'}' <= '1700'}
Note that I've placed Thursday in single quotes and replaced your time formatting with single quotes also.
I'd recommend using a combination of Switchee and IFElse, both from Croxton, to make this parse more quickly since it's a rather complex conditional. How about something like this, for example?
{exp:switchee var="{current_time format='%l'}" parse="inward"}
{case value="Monday|Tuesday|Wednesday|Thursday|Friday"}
{exp:ifelse parse="inward"}
{if '{current_time format="%H%i"}' >= '0600' AND '{current_time format="%H%i"}' <= '0959'}
Morning show
{if:elseif '{current_time format="%H%i"}' >= '1000' AND '{current_time format="%H%i"}' <= '1459'}
Midday Show
{/if}
{/exp:ifelse}
{/case}
{case value="Saturday"}
Do the same sort of thing for Saturday
{/case}
{case value="Sunday"}
Do the same sort of thing for Sunday
{/case}
{/exp:switchee}
Also note here that I've adjusted the times by 1 minutes - otherwise, two conditions in the same set (because you are using equal to or less/greater than) could be true at the same time.
Related
I have the following raw html:
<h3>Job Description</h3>
<p>We are recruiting part time or full time cashier, to be based at our restaurant at Fraser Place, Jalan Perak.</p>
<p>Work Day: Monday to Friday<br> Work Hour: 7am-5pm (Full time), 9am-2pm (Part Time) or 10am-2pm (Part Time)</p>
<p>Full time rate at RM1600-RM1800 per month depends on experience, part time rate RM7-RM8/ hour depends on experience.</p>
<hr>
<h3>Working Location </h3>
I'm trying to get all text under "Job Descrtion" only excluding <hr> tag
I've tried:
for header in soup.find_all('h3'):
para = header.find_next_sibling('p')
but only manage to get first <p> after "Job desription" and also it will not run on <br> tag within <p> tag
You can iterate your header siblings until you match hr.
Example:
example = """<h3>Job Description</h3>
<p>We are recruiting part time or full time cashier, to be based at our
restaurant at Fraser Place, Jalan Perak.</p>
<p>Work Day: Monday to Friday**<br>** Work Hour: 7am-5pm (Full time), 9am-2pm
(Part Time) or 10am-2pm (Part Time)</p>
<p>Full time rate at RM1600-RM1800 per month depends on experience, part time
rate RM7-RM8/ hour depends on experience.</p>
<hr>
<h3>Working Location </h3>"""
soup = BeautifulSoup(example, 'html.parser')
for header in soup.find_all('h3'):
nextNode = header
while True:
nextNode = nextNode.nextSibling
if nextNode is None:
break
if nextNode.name is not None:
if nextNode.name == "hr":
break
print (nextNode.get_text(strip=True))
Outputs:
We are recruiting part time or full time cashier, to be based at our
restaurant at Fraser Place, Jalan Perak.
Work Day: Monday to Friday**** Work Hour: 7am-5pm (Full time), 9am-2pm (Part
Time) or 10am-2pm (Part Time)
Full time rate at RM1600-RM1800 per month depends on experience, part time
rate RM7-RM8/ hour depends on experience.
I have 3 tables
1 Advertiser
2 Adverts
id
Advertiser can have multiple adverts
3 instances
id
ad_id
date
Adverts can have multiple date - date is the column in instances table
$input = Input::get('fullname'); // fullname is the id of adverstiser
// input is the advertiser id
$adverts = DB::table('adverts')->where('a_id',$input)->get();
return view('adverts.index', compact( 'adverts'));
This query will give me all adverts data related to advertiser.
fine with that.
in my adverts.index
#foreach($adverts as $advert)
<div class="caption">
<ul>
<li><i class="fa fa-picture-o"></i> {{$advert->height}}x{{$advert->height}}</li>
<li><i class="fa fa-file"></i> {{$advert->finfo}}</li>
<li><i class="fa fa-eye"></i> Last seen:</li>
<li><i class="fa fa-link"></i> {{$advert->domain}}</li>
<li><i class="fa fa-link"></i> Visit full ad</li>
</ul>
</div>
#endforeach
I would like to get the last seen date from the instance table.
SELECT MAX(date) AS "Last seen date" FROM instances WHERE ad_id =1
ad_id is the primary key id from adverts table
I would like to pass the value of this query to them same view as above
adverts.index
Is the loop must be done in view?
Or something else I am missing.
I would first see if you can get the maximum date for each advert that has at least one corresponding instance. Something like this should do the trick:
$instances = DB::table('instances')
->select(DB::raw('*, MAX(date) as last_seen_date'))
->groupBy('ad_id')
->get();
If you can get this to work first, then you can join it with your advertiser and adverts tables to match a particular advertiser and get the remaining information for each advert. You can also do a leftJoin to ensure that you grab all adverts, even ones that don't have any matching instances.
I would also strongly encourage you to define models, which will make it easier to create reusable query fragments ("scopes") throughout your application.
I am working on migrating a legacy database into my Rails application (3.2.3). The original database comes with quite a few long sql queries for reports. For now, what I would like to do it use the sql queries in the Rails application and then one by one (when time allows) swap the sql queries to 'proper' Rails queries.
I have a clinical model and the controller has the following code:
#clinical_income_by_year = Clinical.find_all_by_sql(SELECT date_format(c.transactiondate,'%Y') as Year,
date_format(c.transactiondate,'%b') as Month,
sum(c.LineBalance) as "Income"
FROM clinical c
WHERE c.Payments = 0 AND c.LineBalance <> 0
AND c.analysiscode <> 213
GROUP BY c.MonthYear;)
However, when I run that code I get a few errors to do with the formatting.
Started GET "/clinicals" for 127.0.0.1 at 2012-04-29 18:00:45 +0100
SyntaxError (/Users/dannymcclelland/Projects/premvet/app/controllers/clinicals_controller.rb:6: syntax error, unexpected tIDENTIFIER, expecting ')'
...rmat(c.transactiondate,'%Y') as Year,
... ^
/Users/dannymcclelland/Projects/premvet/app/controllers/clinicals_controller.rb:7: syntax error, unexpected tIDENTIFIER, expecting keyword_end
...rmat(c.transactiondate,'%b') as Month,
... ^
/Users/dannymcclelland/Projects/premvet/app/controllers/clinicals_controller.rb:8: syntax error, unexpected tIDENTIFIER, expecting keyword_end
... sum(c.LineBalance) as "Income"
... ^
/Users/dannymcclelland/Projects/premvet/app/controllers/clinicals_controller.rb:10: syntax error, unexpected tCONSTANT, expecting keyword_end
... WHERE c.Payments = 0 AND c.LineBalance <> 0
... ^
/Users/dannymcclelland/Projects/premvet/app/controllers/clinicals_controller.rb:10: syntax error, unexpected '>'
...yments = 0 AND c.LineBalance <> 0
... ^
/Users/dannymcclelland/Projects/premvet/app/controllers/clinicals_controller.rb:11: syntax error, unexpected '>'
... AND c.analysiscode <> 213
... ^
Is there something I should be doing to the sql query before importing it into the controller? Although it's possible there is something wrong with the query (It was written quite some time ago), it does work as expected when run directly within the database. It returns an array like this:
----------------------------------------------
| Year | Month | Income |
----------------------------------------------
----------------------------------------------
| 2012 | January | 20,000 |
| 2012 | February | 20,000 |
| 2012 | March | 20,000 |
| 2012 | April | 20,000 |
----------------------------------------------
etc..
Any help, advice or general pointers would be appreciated!
I'm reading through http://guides.rubyonrails.org/active_record_querying.html trying to convert the sql query to a correct Rails query.
So far I have matched the second to last line:
AND c.analysiscode <> 213
with
#clinical_income_by_year = Clinical.where("AnalysisCode != 213")
baby steps!
UPDATE
I've got the filtering sorted now, thanks to the Rails guide site but I'm stuck on the grouping and sum part of the sql query. I have the following so far:
#clinical_income_by_year = Clinical.where("AnalysisCode != 213 AND Payments != 0 AND LineBalance != 0").page(params[:page]).per_page(15)
I'm struggling to build in the following two lines of the sql query:
sum(c.LineBalance) as "Income"
and
GROUP BY c.MonthYear;)
My view code looks like this:
<% #clinical_income_by_year.each do |clinical| %>
<tr>
<td><%= clinical.TransactionDate.strftime("%Y") %></td>
<td><%= clinical.TransactionDate.strftime("%B") %></td>
<td><%= Clinical.sum(:LineBalance) %></td>
</tr>
<% end %>
</table>
<%= will_paginate #clinical_income_by_year %>
The Ruby parser doesn't understand SQL, you need to use a string:
#clinical_income_by_year = Clinical.find_by_sql(%q{ ... })
I'd recommend using %q or %Q (if you need interpolation) for this so that you don't have to worry about embedded quotes so much. You should also move that into a class method in the model to keep your controllers from worrying about things that aren't their business, this will also give you easy access to connection.quote and friends so that you can properly use string interpolation:
find_by_sql(%Q{
select ...
from ...
where x = #{connection.quote(some_string)}
})
Also, the semicolon in your SQL:
GROUP BY c.MonthYear;})
isn't necessary. Some databases will let it through but you should get rid of it anyway.
Depending on your database, the identifiers (table names, column names, ...) should be case insensitive (unless some hateful person quoted them when they were created) so you might be able to use lower case column names to make things fit into Rails better.
Also note that some databases won't like that GROUP BY as you have columns in your SELECT that aren't aggregated or grouped so there is ambiguity about which c.transactiondate to use for each group.
A more "Railsy" version of your query would look something like this:
#c = Clinical.select(%q{date_format(transactiondate, '%Y') as year, date_format(transactiondate, '%b') as month, sum(LineBalance) as income})
.where(:payments => 0)
.where('linebalance <> ?', 0)
.where('analysiscode <> ?', 213)
.group(:monthyear)
Then you could do things like this:
#c.each do |c|
puts c.year
puts c.month
puts c.income
end
to access the results. You could also simplify a little bit by pushing the date mangling into Ruby:
#c = Clinical.select(%q{c.transactiondate, sum(c.LineBalance) as income})
.where(:payments => 0)
.where('linebalance <> ?', 0)
.where('analysiscode <> ?', 213)
.group(:monthyear)
Then pull apart c.transactiondate in Ruby rather than calling c.year and c.month.
I have this set of tables (I'm using postgresql)
User (with id, first_name, ....)
Assignment (with id, name, start_date, finish_date,....)
AssignmentUser(with assignment_id, user_id, flag_status)
The flag_status is a boolean that says if a user is still or not in an assignment.
Let's say user1 applies for assignment1, assignment2, assignment3 as follows:
start_date finish_date flag_status
user1 assignment1 11-11-11 11-12-11 false
user1 assignment2 01-10-11 01-02-12 true
user1 assignment3 01-01-12 01-03-12 true
Let's say I want to search TODAY the closest start_date of an user's assignment.
I've done this in my User model:
def last_date
self.assignments.where("date < ?", Date.today).max.try(:date)
end
and this
def last_status
AssignmentUser.find_by_assignment_id_and_user_id(Assignment.find_by_date(self.last_date), self.id).flag_status if self.last_date.present?
end
And in my view for each user:
User.all.each do |u|
<td> u.first_name </td>
<td> u.last_date </td>
<td> u.last_status </td>
end
It works well but, in my log I see 3 queries for each user (as expected).
So my question is: how can I avoid these multiple queries? (I guess it's more like a SQL question than a Rails one)
#au = AssignmentUsers.find_by_sql(
"SELECT assignment_users.user_id, MAX(assignment_users.date) as last_date,
assignment_id,
assignments.flag_status,
users.first_name
FROM assignment_users
LEFT JOIN assignments
ON assignments.id = assignment_id
LEFT JOIN users
ON users.id = user_id
WHERE date < CURDATE()
GROUP BY user_id;"
)
Then in your view:
#au.each do |u|
<tr>
<td> u.first_name </td>
<td> u.last_date </td>
<td> u.last_status </td>
</tr>
end
PLEASE NOTE: this is iterating over AssignmentUsers, so if there are any users who are without an assignment they will be missed. IF that isn't good enough (i.e. you don't want to pull back a 2nd list - easily done by User.all(:conditions => "id NOT IN (#{#au.collect(&:user_id}) - then this solution isn't suitable and if that's the case, then i think you're already probably doing it the best way, although i would have changed your methods to scopes - if you want me to show you how that would look let me know.
<DIV class=x-grid3-scroller id=ext-gen742>
<DIV class=x-grid3-body id=ext-gen743>
<DIV class=x-grid3-row x-grid3-row-first x-grid3-row-last >
<TABLE class=x-grid3-row-table>
<TBODY>
<TR>
<TD> class=x-grid3-col x-grid3-cell x-grid3-td-0 x-grid3-cell-first
<td> class=x-grid3-col x-grid3-cell x-grid3-td-1
<td> class=x-grid3-col x-grid3-cell x-grid3-td-2
<td> class=x-grid3-col x-grid3-cell x-grid3-td-3
Once I click on any one of these TD values via GUI, it should be selected in the search field in the form.
Could any one help meregarding this.
I'm not sure what your asking, but based off the title I'm going to give this example as an answer:
table
abc def
foo bar
jkl mno
XPath
//tr[td/text()='foo']/td[2]
This would select bar with the following breakdown.
find any row[that has a column, with text that is ‘foo’] with a column [that is the second]
I'm not sure what your asking, but based off the title I'm going to give this example as an answer:
table
abc def
foo bar
jkl mno
XPath
//tr[td/text()='foo']/td[2]
This would select bar with the following breakdown.
find any row[that has a column, with text that is ‘foo’] with a column [that is the second]