PHPTAL Nested Repeat - php

I am having an problem trying to achieve a nested repeat in PHPTAL:
<tr tal:repeat="business analysis_result">
<td>${business/trading_name}</td>
<tal:block tal:repeat="selected_key selected_keys">
<td>HOW??????</td> <---problem
</tal:block>
</tr>
basically I want to have the <td> of inner repeat to get the value of $business[$selected_key], I have looked at the phptal manual which doesn't really give you a demo on how to do this.

Try
<tr tal:repeat="business analysis_result">
<td>${business/trading_name}</td>
<tal:block tal:repeat="selected_key business/selected_keys">
<td tal:content="selected_key"/>
</tal:block>
</tr>
Imagine if you were using php instead of some new language you have to learn! Imagine how easy it would be!

<td tal:content="php:business[selected_key]"/>
or the "TAL way":
<td tal:content="business/${selected_key}"/>

Related

echo html as text with PHP variable

I need to display a large amount of html as text(a table) which contains PHP variables. Now I would like to know if there is a command that echos the HTML as text, but keeps the PHP.
I know I can change the > with >. I'm just wondering if there is a better way to do this.
<table style="width:400px">
<tr>
<td><b style="font-size:27px;">$_POST['name']</b></td>
<td rowspan="2"> IMG</td>
</tr>
<tr>
<td><b style="font-size:23px;font-weight:400">$_POST['function']</b></td>
</tr>
</table>
This is only a part of it of course.
TL;DR: I want to echo the above text(as text, not code) but the variables from PHP need to execute as code.
Pushing it through the htmlentities function when you output the data should work. The function will convert whatever string you put in into one that looks exactly the same when rendered in HTML, which seems to be what you want.
Use htmlspecialchars and add curly braces around PHP variables:
$string = "<table style=\"width:400px\">
<tr>
<td><b style=\"font-size:27px;\">{$_POST['name']}</b></td>
<td rowspan=\"2\"> IMG</td>
</tr>
<tr>
<td><b style=\"font-size:23px;font-weight:400\">{$_POST['function']}</b></td>
</tr>
</table>";
echo htmlspecialchars($string);

PHP regular expression to extract html values

I want to extract values from the code below.
<tbody>
<tr>
<td><div class="file_pdf">note1</div></td>
<td class="textright">110 KB</td>
<td class="textright">106</td>
</tr>
<tr>
<td><div class="file_pdf">note2.pdf</div></td>
<td class="textright">44 KB</td>
<td class="textright">104</td>
</tr>
</tbody>
I want to extract 'note1', 'note2' strings and 1628 and 1629 numbers.
i treid
preg_match_all('~(\'\)\">(.*?)<\/a>)~', $getinside, $matches);
but its result is not what I am looking for..
is there any simple RegEx to extract them? Thanks!
It should work for you:
preg_match_all("~downloadFile\('(\d+)'\)\">([^<]*)</a>~", $getinside, $matches);
Remember: If your html is very large/complex and you also need to parse more other things from there, then regex is not a better option to do this.

Layout problems (php,html)

I made some images which i need, currently i have :
$output .= "
<tr>
<td align='center'><b>{$name}</b></td> </tr>
<tr>
<td align='center'><img src='$avatar'/></td> </tr>
<td align='center'><b>Points</b>: {$points}</td> </tr>
<tr>
<td align='center'><b>Status</b>:<b><font color='green'> {$misc['text_status']}</font></b></td> </tr>
<tr>
<td align='center'><a href='".$link."'><font color='red'><b>See more statistics</b></font></a><hr></td></tr>
";
with script i get this(ie image) http://img717.imageshack.us/img717/7885/thisdn.png .
Now i need to output something like this http://img703.imageshack.us/img703/171/thatq.png (ie image) without mysql. May i have some examples of yours?
move <tr> before your for loop and </tr> after you for loop
or better one, place individual tables with float:left for each member
You could try to wrap every profile information (photo, points etc) in a div and not use tables at all. Then float these div-s to the right or left. Note that every div must have the same class.

How Can I Get Data From HTML Source Code with PHP and RegEx?

I have got HTML source code, and i must get some information text in the HTML. I can not use DOM, because the document isn't well-formed.
Maybe, the source could change later, I can not be aware of this situation. So, the solution of this problem must be advisible for most situation.
Im getting source with curl, and i will edit it with preg_match_all function and regular expressions.
Source :
...
<TR Class="Head1">
<TD width="15%"><font size="12">Name</font></TD>
<TD>: </TD>
<TD align="center"><font color="red">Alex</font></TD>
<TD width="25%"><b>Job</b></TD>
<TD>: </B></TD>
<TD align="center" width="25%"><font color="red">Doctor</font></TD>
</TR>
...
...
<TR Class="Head2">
<TD width="15%" align="left">Age</B></TD>
<TD>: </TD>
<TD align="center"><font color="red">32</font></TD>
<TD width="15%"><font size="10">data</TD></font>
<TD> </B></TD>
<TD width="40%"> </TD>
</TR>
...
As we have seen, the source is not well-formed. In fact, terrible! But there is nothing I can do.
The source is longer than this.
How can I get the data from the source? I can delete all of HTML codes, but how can i know sequence of data? What can I do with preg_match_all and regex? What else can I do?
Im waiting for your help.
If you can use the DOM this is far better than regexes. Take a look a PHP Tidy - it's designed to manage badly formed HTML.
You can use DOMDocument to load badly formed HTML:
$doc = new DOMDocument();
#$doc->loadHTML('<TR Class="Head2">
<TD width="15%" align="left">Age</B></TD>
<TD>: </TD>
<TD align="center"><font color="red">32</font></TD>
<TD width="15%"><font size="10">data</TD></font>
<TD> </B></TD>
<TD width="40%"> </TD>
</TR>');
$tds = #$doc->getElementsByTagName('td');
foreach ($tds as $td) {
echo $td->textContent, "\n";
}
I'm suppressing warnings in the above code for brevity.
Output:
Age
:
32
data
<!-- space -->
<!-- space -->
Using regex to parse HTML can be a futile effort as HTML is not a regular language.
Don't use RegEx. The link is funny but not informative, so the long and short of it is that HTML markup is not a regular language, hence cannot be parsed simply using regular expressions.
You could use RegEx to parse individual 'tokens' ( a single open tag; a single attribute name or value...) as part of a recursive parsing algorithm, but you cannot use a magic RegEx to parse HTML all on its own.
Or you could use a parser.
Since the markup isn't valid, maybe you could use TagSoup or PHP:Tidy.
$regex = <<<EOF
<TR Class="Head2">\s+<TD width="15%" align="left">Age</B></TD>\s+<TD>: </TD>\s+<TD align="center"><font color="red">(\d+)</font></TD>\s+<TD width="15%"><font size="10">(\w+)</TD></font>\s+<TD> </B></TD>\s+<TD width="40%"> </TD>\s+</TR>
EOF;
preg_match_all($regex, $text, $result);
var_dump($result)

Zebra Striping with PHPTAL?

I'm trying out PHPTAL and I want to render a table with zebra stripes. I'm looping through a simple php assoc array ($_SERVER).
Note that I don't want to use jQuery or anything like that, I'm trying to learn PHPTAL usage!
Currently I have it working like this (too verbose for my liking):
<tr tal:repeat="item server">
<td tal:condition="repeat/item/odd" tal:content="repeat/item/key" class="odd">item key</td>
<td tal:condition="repeat/item/even" tal:content="repeat/item/key" class="even">item key</td>
<td tal:condition="repeat/item/odd" tal:content="item" class="odd">item value</td>
<td tal:condition="repeat/item/even" tal:content="item" class="even">item value</td>
</tr>
Basically I want some kind of conditional assignment on the fly, but I'm unsure of the syntax.
You could create expression modifier by writing phptal_tales_evenodd() function (see phptal_tales() in manual):
<td tal:attributes="class evenodd:repeat/item/odd">
Well, it seems like I have my own answer, though I still think this is rather ugly:
<tr tal:repeat="item server">
<td tal:content="repeat/item/key" tal:attributes="class php: repeat.item.odd ? 'odd' : 'even'">item key</td>
<td tal:content="item" tal:attributes="class php: repeat.item.odd ? 'odd' : 'even'">item value</td>
</tr>
Anyone got anything more graceful looking for PHPTAL?

Categories