[Smarty, and maybe doctrine] - doctrine

I'hv got a problem when I am using foreach in smarty,
an array with 2 item was loop in a foreach, but the result is it loop 3 time.
I use doctrine to get a list of review by a user from database
Doctrine_Core::getTable('review')->findByUser($userId);
then I assign it to smarty and loop in foreach:
{foreach from=$reviewList item=review}
<p>User {$review.User.name} said: {$review.content}</p>
{/foreach}
However the result is e.g.:
User Joe said: yoyo
User Mary said: hihi
User said:
Please notice that the extra row doesn't get anything from the array.
I have checked that there is only 2 record in database, and I have count the $reviewList by count($reviewList), the result is also 2.
When I insert one more record to database, the forloop also loop extra one time.
Can anyone tell me why this happen? Thanks a lot!

This should filter the empty line:
{foreach from=$reviewList item=review}{if $review.User.name}
<p>User {$review.User.name} said: {$review.content}</p>
{/if}{/foreach}

Related

if the array were empty in laravel . then how to dispaly the message that their are no record found

#foreach($array as $value)
{{$value}}
#endforeach
$array is my array pass from Laravel controller then i want to display the message record not found if the array were empty.
this is my code example.
Besides 'foreach' loops, we also got 'forelse' loops in laravel blade template, what exactly this 'forelse' loops does? and most importantly should we care about it?
The 'forelse' loops is the better version of 'foreach', so yes, you should care, 'forelse' loops works exactly as 'foreach' except it also check the value is empty or not.
So with 'foreach' normally you check first whether the value is empty or not using 'if' statement, using 'forelse' you don't need to do that, the value is automatically checked.
#forelse ($array as $value)
{{ $value }}
#empty
There are no record found.
#endforelse
You can use #forelseas Shoukat Mirza in his answer and you also could use #if clause:
#if (count($array) > 0)
// foreach loop here
#endif
This is also helpful when you have multiple conditionals to check.

accessing the text value of last nested <tr> element with no id or class hooks

I need to access the value of the 10th <td> element in the last row of a table. I can't use an ID as a hook because only the table has an ID. I've managed to make it work using the code below. Unfortunately, its static. I know I will always need the 10th <td> element, but I won't ever know which row it needs to be. I just know it needs to be the last row in the table. How would I replace "tr[6]" with the actual last <tr> dynamically? (this is probably really easy, but this is literally my first time doing anything with ruby).
page = Nokogiri::HTML(open(url))
test = page.css("tr[6]").map { |row|
row.css("td[10]").text}
puts test
You want to do:
page.at("tr:last td:eq(10)")
If you do not need to do anything else with the page you can actually make this a single line with
test = Nokogiri::HTML(open(url)).search("tr").last.search("td")[10].text
Otherwise (this will work):
page = Nokogiri::HTML(open(url))
test = page.search("tr").last.search("td")[10].text
puts test
Example:(Used a large table from another question on StackOverflow)
Nokogiri::HTML(open("http://en.wikipedia.org/wiki/Richard_Dreyfuss")).search('table')[1].search('tr').last.search('td').children.map{|c| c.text}.join(" ")
#=> "2013 Paranoia Francis Cassidy"
Is there a particular reason you want an Array with 1 element? My example will return a string but you could easily modify it to return an Array.
You can use CSS pseudo class selectors for this:
page.css("table#the-table-id tr:last-of-type td:nth-of-type(10)")
This first selects the <table> with the appropriate id, then selects the last <tr> child of that table, and then selects the 10th <td> of that <tr>. The result is an array of all matching elements, if youexpect there to be only one you could use at_css instead.
If you prefer XPath, you could use this:
page.xpath("//table[#id='the-table-id']/tr[last()]/td[10]")

how can i check if a php associative array's key is equal to a defined variable from phptal condition?

i've this problem my phptal view has a variable called data which is an associative array.
Let's suppose that i've those Keys M01 M02 and M03 not in a particular order, i've to print to the user view all the data not referred by key M02.
How can i do this?
Thanks in advance
Marco
UPDATE: I forgot to say that i cannot edit the code which calls the view.
If order is important, use second array with ordered keys to print:
<div tal:repeat="key php:array('M01','M03')" tal:content="array/$key"/>
Otherwise you can just add a condition:
<div tal:repeat="data array">
<tal:block tal:condition="php:repeat.data.key != 'M02'" tal:content="data"/>
</div>

Smarty change elements in array

I have an array in my template files ($data) witch looks like this
$data.1.name
$data.1.date
$data.1.place
$data.2.name
$data.2.date
$data.2.place
$data.3.name
$data.3.date
$data.3.place
now I would like to check the entire array and remove an item where the date is older then today.
The date check i figured out but i'm stuck at removing the item.
So let's say item 2 is older, the result should look like
$data.1.name
$data.1.date
$data.1.place
$data.3.name
$data.3.date
$data.3.place
Anyone an idea how i do this? If it is at all possible?
{foreach from=$data item=val}
{if $val.date >= $smarty.now}
{$val.name}
{$val.date}
{$val.place}
{/if}
{/foreach}

How to get table data from tables using xpath

What XPATH query could i use to get the values from the 1st and 3rd <td> tag for each row in a html table.
The XPATH query I have used use is
/table/tr/td[1]|td[3].
This only returns the values in the first <td> tag for each row in a table.
EXAMPLE
I would expect to get the values bob,19,jane,11,cameron and 32 from the below table. But am only getting bob,jane,cameron.
<table>
<tr><td>Bob</td><td>Male</td><td>19</td></tr>
<tr><td>Jane</td><td>Feale</td><td>11</td></tr>
<tr><td>Cameron</td><td>Male</td><td>32</td></tr>
</table>
#jakenoble's answer:
/table/tr/td[1]|/table/tr/td[3]
is correct.
An equivalent XPath expression that avoids the | (union) operator and may be more efficient is:
/table/tr/td[position() = 1 or position() = 3]
Try
/table/tr/td[1]|/table/tr/td[3]
I remember doing this in the past and found it rather annoying because it is ugly and long-winded

Resources