I created a own viewhelper for my extension, which creates dynamic html code.
I use
protected $escapeOutput = false;
in my extension. The html code for the Fluid viewhelper will be returned inside a string.
<f:image src='".$lPlay->getIfName()."' alt='Bild' height='50' />
But the Fluid viewhelper is not executed. In the source code of the web page I can see
<f:image src='fileadmin/user_upload/AlbumArtSmall.jpg' alt='Bild' height='50' />
What is my mistake or is it not possible to use a viewhelper inside a viewhelper, which creates html code.
Maybe I could call the fluid viewhelper inside my viewhelper directly with php. But I don't know how to do it.
public function render() {
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager');
$lPlayC = $objectManager->get('HGA\\Mairlist\\Controller\\LastPlayController');
$lPlays = $lPlayC->sendAction();
$return = "\n<div class='lPlay'>\n";
$return .= "\t<table class='tx_mairlist' >\n";
$return .= "\t\t<tr class='header'>\n";
$return .= "\t\t\t<th colspan='3'><p class='header'>gerade gespielt</p></th>\n";
$return .= "\t\t</tr>\n";
$iPos = 1;
foreach ($lPlays as $lPlay) {
if ($iPos == 1) {
$class = "onair";
} else {
$class = "gone";
}
$return .= "<tr class='".$class."'>\n";
$date = $lPlay->getDate();
$temp = $date->format('H:i:s');
$return .= "<td class='time'> <p class='time'>".$temp."</p>\n";
$return .= "<p class='space'></br></p>\n";
$return .= "<p class='duration'>".$lPlay->getDuration()."</p>\n";
$return .= "</td>\n";
$return .= "<td> <f:image src='".$lPlay->getIfName()."' alt='Bild' height='50' /></td>\n";
$return .= "<td> <img src='".PATH_site.$lPlay->getIfName()."' alt='Bild' height='50' /></td>\n";
$return .= "<td class='info'><b>".$lPlay->getTitle()."</b></br><p class='artist'>".$lPlay->getArtist()."</p></td>\n";
$return .= "</tr>\n";
$iPos++;
}
$return .= "</table>\n";
$return .= "</div>\n";
error_log("ViewHelper: " . $return, 0);
return $return;
}
You should handle the HTML output in your template. You can add a variable to Fluid in your ViewHelper and render its children:
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager');
$lPlayC = $objectManager->get('HGA\\Mairlist\\Controller\\LastPlayController');
$lPlays = $lPlayC->sendAction();
$this->templateVariableContainer->add('plays', $lPlays);
$output = $this->renderChildren();
$this->templateVariableContainer->remove('plays');
return $output;
In your template you can then use this ViewHelper like:
<ns:myViewHelper>
<div class="lPlay">
<table class="tx_mairlist">
<tr class="header">
<th colspan="3"><p class="header">gerade gespielt</p></th>
</tr>
<f:for each="{plays}" as="play" iteration="iteration">
<tr class="{f:if(condition: iteration.isFirst, then: 'onair', else: 'gone')}">
<td class="time">
<p class="time"><f:format.date format="H:i:s">{play.date}</f:format.date></p>
<p class="space"><br /></p>
<p class="duration">{play.duration}</p>
</td>
<td>
<f:image src="{play.ifName}" alt="Bild" height="50" />
</td>
<td class="info">
<b>{play.title}</b></br>
<p class="artist">{play.artist}</p>
</td>
</tr>
</f:for>
</table>
</div>
</ns:myViewHelper>
Related
I'm a noob in perl, trying to download a IMDB movie poster image via perl script with the help of Mechanize framework. I'm not getting the 'id' attribute for 'td' tags so that I can find the specific place for the image. This is how the HTML of the image portion of IMDB page looks like:
<table id="title-overview-widget-layout" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td id="img_primary" rowspan="2">
<div class="image">
<a href="/media/rm419297536/tt2338151?ref_=tt_ov_i">
<img width="214" height="317" itemprop="image" src="http://ia.media-imdb.com/images/M/MV5BMTYzOTE2NjkxN15BMl5BanBnXkFtZTgwMDgzMTg0MzE#._V1_SY317_CR2,0,214,317_AL_.jpg" title="PK (2014) Poster" alt="PK (2014) Poster">
</a>
</div>
<div class="pro-title-link text-center">
</td>
<td id="overview-top">
</tr>
<tr>
</tbody>
</table>
And here is the perl script I'm trying to download with:
use strict;
use warnings;
use WWW::Mechanize;
use HTML::TokeParser;
#create a new instance of mechanize
my $agent = WWW::Mechanize->new();
#get the page we want.
$agent->get("http://www.imdb.com/title/tt2338151/");
#supply a reference to that page to TokeParser
my $stream = HTML::TokeParser->new(\$agent->{content});
my $c = 0;#to store the count of images and give the images names
#loop through all the td's
while (my $tag1 = $stream->get_tag("td")) {
$tag1->[1]->{id} ||= 'none';
my $asd = $tag1->[1]->{id};
print "$asd\n"; #shows none for all of the td's
if ($asd && $asd eq 'img_primary') {
while(my $tag = $stream->get_tag("div"))
{
# $tag will contain this array => [$tag, $attr, $attrseq, $text]
#get the class of the div tag from attr
my $cls = $tag->[1]{class};
#we're looking for div's with the class gallery-img2
if($cls && $cls eq "image") {
#get the content of the src tag
my $image = $stream->get_tag('img')->[1]{src};
#create a new mechanize to download the image
my $imgDown = WWW::Mechanize->new();
#give the image url and the local path to mechanize
$imgDown->get($image, ":content_file" => ".//image".$c.".jpg");
#update the count
$c++;
}
}
}
}
print "Total images scraped $c\n";
Any help will be much appropriated.
When JavaScript is involved, it's best to use a real browser to visit pages and query their contents.
You can do this with Selenium::Remote::Driver.
i am displaying the daily report submitted by the user now i want to mail the same to email when the send email button is clicked. how can i do that in PHP ?
i used the following code
extract($_REQUEST);
if(isset($send_mail)){
$edata= $_POST['send_msg'];
echo "<script type='text/javascript'>alert('$edata');</script>";
$ka_semail=$_SESSION['user_email'];
$subject = "Report";
$body = $edata;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Info <'.$ka_semail.'>' . "\r\n";
$mchk= mail($ka_semail, $subject, $body, $headers);
if(isset($mchk)){
echo "<script type='text/javascript'>alert('Your data Sent sucessfully');</script>";
header("location:reports.php");
}
}
?>
<body>
<div id="main">
<header>
<div id="logo">
<div id="logo_text">
<a href="index.html">
<div style="background:#2A0000"><img src="images/kreamsoftlogo.png"/></a></div>
</div>
</div>
<?php include'nav.php'; ?>
</header>
<div id="site_content">
<div id="email-data">
<div id="content">
<div class="content" style="min-height:385px; width: 100%" >
<h1>Daily Report </h1>
<?
$current_date = date("d/m/Y");
$current_user = $_SESSION['user_login'];
$find = mysql_query("select * from k_dailyreport where kr_user='$current_user' AND kr_date='$current_date'");
?>
<div class="content_item">
<ul>
<?php
$numrow=mysql_num_rows($find);
if($numrow>0){
$sino = 0;
echo "<table border='0' width='100%'>";
echo "<tr class='head'>";
echo "<th>S.No</th>";
echo "<th>Project</th>";
echo "<th>Process</th>";
echo "<th>Process Date</th>";
echo "<th>Current Duration</th>";
echo "</tr>";
$sno=0;
while($row = mysql_fetch_array($find)){
$sno=$sno +1;
$sino++;
$cls = ($sino%2==0) ? "even" : "odd";
echo "<tr class='t1'>";
echo "<td>$sno</td>";
echo "<td>".$row['kr_project']."</td>";
echo "<td>".$row['kr_process']."</td>";
echo "<td>".$row['kr_rtime']."</td>";
echo "<td>".$row['kr_ctime']."</td>";
}?></td>
<?php
echo "</tr>";
?><form name="test" method="post"enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p style='padding-top: 15px;margin-right: 50px; float:right'><input class='submit' id="sendEmail" type="submit" name='sendmail' value='Send Mail' />
<input type="hidden" name="send_mail" value="send_mail" />
<input type="hidden" name="send_msg" value="<?php
$numrow=mysql_num_rows($find);
if($numrow>0){
$sino = 0;
echo "<table border='0' width='100%'>";
echo "<tr class='head'>";
echo "<th>S.No</th>";
echo "<th>Project</th>";
echo "<th>Process</th>";
echo "<th>Process Date</th>";
echo "<th>Current Duration</th>";
echo "</tr>";
$sno=0;
while($row = mysql_fetch_array($find)){
$sno=$sno +1;
$sino++;
$cls = ($sino%2==0) ? "even" : "odd";
echo "<tr class='t1'>";
echo "<td>$sno</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[3]."</td>";
echo "</tr>";
}echo "</table>";}?>" /></p>
</form>
<?
}else{
?>
<center><img style="text-align:center" src="images/no_record.gif" /></center>
<?
}
echo "</table>";
?>
</li>
</ul>
</div>
</div> </div>
</div>
</div>
i want to mail the entire table with data
Without knowing anything about your code, I can perhaps at least guess as to the structure of what builds this table. Maybe something along the lines of this?:
writeTableHeader();
foreach ($values as $value) {
writeTableRow($value);
}
writeTableFooter();
Maybe even abstracted into its own higher-level function?:
function writeTable($values) {
$result = '';
$result .= buildTableHeader();
foreach ($values as $value) {
$result .= buildTableRow($value);
}
$result .= buildTableFooter();
return $result;
}
Any such function or abstraction can be used to construct the HTML for the email body as well...
// ... previously building the overall email body
$mailBody .= writeTable($values);
// ... maybe some more email body elements as well
mail($to, $subject, $mailBody);
Styling the email will be another story, of course. It's best not to rely on linked resources in an HTML email, those tend not to be requested by mail clients as they're often abused by spammers and the like. You can perhaps include the style sheet(s) as an "embedded resource" in the email, though in most cases I think inline styling is probably best for emails. (Some mail clients might not even load an embedded resource, at least not without first prompting the user, which isn't an ideal user experience.)
Assuming you don't want to duplicate much code, the above method can be abstracted using the Form Template Method refactoring pattern (example here, among others).
I guess the point I'm getting at is that the email doesn't send the web page, but rather the web page and the email are both independent "views" of the underlying data. Common code between them can be abstracted and shared of course, but trying to re-use a webpage itself in an email is over-complicating the situation.
I want to parse the html page.Extract the badge , description , and the badge type using
<div class="row">
<div class="span8">
<table id="badge-list">
<tr>
<td style="width: 25px;"></td>
<td style="width: 200px;" class="badge-cell">
<a class="badge-name" href="/badge/show/3/">
<span class="badge-icon bronze">•</span>
Editor
</a>
<span class="multiplier">x 3892</span></td>
<td class="earned False"> </td>
<td>First edit</td>
</tr>
my perl code is follows,
i am trying to extract a class="badge-name" and other detail using the below code
my $tree = HTML::TreeBuilder->new();
$tree->parse($content);
my ($h1) = $tree->look_down('_tag', 'table', 'id', 'badge-list');
my #tr = $h1->look_down('_tag', 'tr') ;
foreach my $tr (#tr) {
my #tdList = $tr->look_down('_tag','td');
foreach my $td ( #tdList) {
if (my $a = $td->look_down('_tag','a')) {
print $a->as_text , "\n";
my $span = $a->look_down('_tag','span', 'class');
print $span->attr('class');
}
else {
my $text = $td->as_text , "\n";
print "$text\n";
}
}
}
This code is throwing warning Wide character in print at ..
look_down requires pairs of attribute/value parameters.
$a->look_down('_tag','span', 'class')
should be just
$a->look_down('_tag','span')
I would suggest to add "use utf8;" at the start of the script to add support non ASCII symbols in the print. The symbol • is deferentially is wide.
use utf8;
I am new to codeigniter trying out some simple pages. I have successfully connected to the database for inserting and updating. But now I want to populate the fields from the database for updating. That is when the user selects the name from the dropdown the corresponding values should appear in the other fields so that the user can update easily.
I have worked on this to do without ajax or jquery. It worked for me but with some warning message - Use of undefined constant.
I am giving by mvc. Help me clear this.
Controller:
public function update()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Update Type';
$data['product_type'] = $this->editType_model->get_product_type();
$data['row'] = $this->editType_model->get_row();
$this->form_validation->set_rules('productype', 'productype', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('cm/update', $data);
$this->load->view('templates/footer');
}
else
{
$data['title']= 'Updation';
$data['message']= 'Updation succeeded';
$this->editType_model->update_news();
$this->load->view('cm/success', $data);
}
}
Model:
public function get_product_type() {
$data = array();
$this->db->order_by("product_type_name", "desc");
$query = $this->db->get('product_type');
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
public function get_row() {
$data = array();
$id= $_POST[product_type_id];
$query = $this->db->get_where('product_type', array('product_type_id' => $id));
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
public function update_news()
{
$this->load->helper('date');
$Product = $this->input->post('product_type_id');
$data = array(
'product_type_name'=>($_POST['productype']),
'rank'=>($_POST['rank'])
);
$this->db->where('product_type_id',$Product);
return $this->db->update('product_type', $data);
}
View:
<?php $productType = 0;
if(isset($_POST['product_type_id'])){
$productType = $_POST['product_type_id'];
}?>
<div id="Content"><div>
<form action="" method="post" name="f1">
<table ><tr><td>Select Product Type Name:</td>
<td><Select id="product_type_id" name="product_type_id" onChange="document.f1.submit()">
<option value="0">--SELECT</option>
<?php if (count($product_type)) {
foreach ($product_type as $list) { ?>
<option value="<?php echo $list['product_type_id']; ?>"
<?php if($productType==$list['product_type_id']) echo "Selected" ?> >
<?php echo $list['product_type_name']; ?></option>
<?php }} ?></Select></td></tr></table></form>
<?php if($productType){
if (count($row)) {
foreach ($row as $faProductType) { ?>
<div > <form action="" method="post" class="form-Fields" >
<input type="hidden" name="product_type_id" id="product_type_id" value="<?php echo $faProductType['product_type_id']; ?>" >
<table border="0" cellpadding="8" cellspacing="0" >
<tr><td>Product Type <font color="red">*</font></td><td style="width: 10px;"> : </td>
<td><input name="productype" type="text" value="<?php echo $faProductType['product_type_name']; ?>" style=" width:300px;" /></td>
<td><span class="err" id="productTypeDesc1Err"></span></td></tr>
<tr><td >Rank</td><td style="width:10px;"> : </td>
<td ><input type="text" name="rank" id="rank" value="<?php echo $faProductType['rank']; ?>" size="39" /> <span class="err" id="productTypeNameErr"></span></td>
<td ></td></tr><tr><td></td><Td colspan="2" align="center">
<input type="submit" value="Update" class="buttonimg" name='cm/editType/update' style="width:100px"
onClick="return validEditProductType();">
</Td><td></td></tr></table></form></div></div></div>
<?php }}} ?>
you can use Ajax for this purpose
onchange of your select box fill a div with form.
Controller Method
function getUserdata(){
$where['username'] = $this->input->post('username');
$this->load->model('users_model');
$data['user'] = $this->users_model->getUser($where);
$this->load->view('userform',$data);
}
And you model method
function getUser($where){
return $this->db->where($where)->get('usertable')->row()
}
Ajax
$(function(){
$('#selecttion').change(function(){
var username = $(this).val();
$.ajax({
url : '<?php 'echo url to controller method'?>',
type : 'POST' ,
data : 'username='+username,
success : function(data){
$('#dividtofill').html(data);
}
});
});
})
EDIT :
calling ajax is easy
for this include jquery library in your header.
Here is your dropdown
Note : put the above ajax code in your head section in script
<select name="user" id="selecttion">
<option value="John">John</option>
<option value="Sam">Sam</option>
<option value="David">David</option>
</select>
Now when this dropdown value is changed the ajax function defined above will be called.
I have finally cleared the error. I have done this by a simple if isset loop.
Now my get_row() in my model looks like this.
public function get_row() {
$data = array();
$id= 0;
if(isset($_POST['product_type_id'])){
$id = $_POST['product_type_id'];
}
$query = $this->db->get_where('product_type', array('product_type_id' => $id));
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
I'm writing a script to rearrange html content and I'm stuck with 2 problems. I have this html structure, which is movie titles and release years with thumbnails grouped in 5 columns. I want to generate new html files with the movies grouped by decades from 2011 to 1911, e.g. present-2011; 2010-2001; 2000-1991; etc.
<table>
<tr>
<td class="basic" valign="top">
<a href="details/267226.html" title="" id="thumbimage">
<img src="images/267226f.jpg"/>
</a>
<br/>Cowboys & Aliens (2011)
</td>
<td class="basic" valign="top">
<a href="details/267185.html" title="" id="thumbimage">
<img src="images/267185f.jpg"/>
</a>
<br/>The Hangover Part II (2011)
</td>
<td class="basic" valign="top">
<a href="details/267138.html" title="" id="thumbimage">
<img src="images/267138f.jpg"/>
</a>
<br/>Friends With Benefits (2011)
</td>
<td class="basic" valign="top">
<a href="details/266870.html" title="" id="thumbimage">
<img src="images/266870f.jpg"/>
</a>
<br/>Beauty And The Beast (1991)
</td>
<td class="basic" valign="top">
<a href="details/266846.html" title="" id="thumbimage">
<img src="images/266846f.jpg"/>
</a>
<br/>The Fox And The Hound (1981)
</td>
</tr>
......
</table>
The one problem I have no idea how to solve is that after removing movies not matching the decade I'm left with empty 'tr' tags and thumbnail positions and don't know how to rearrange again every row in 5 columns filled with 5 titles. And also how to process each decade with one call of the script. Thanks.
use autodie;
use strict;
use warnings;
use File::Slurp;
use HTML::TreeBuilder;
my $tree = HTML::TreeBuilder->new_from_file( 'test.html' );
for my $h ( $tree->look_down( class => 'basic' ) ) {
edit_links( $h );
my ($year) = ($h->as_text =~ /.*?\((\d+)\).*/);
if ($year > 2010 or $year < 2001) {
$h->detach;
write_file( "decades/2010-2001.html", \$tree->as_HTML('<>&',' ',{}), "\n" );
}
}
sub edit_links {
my $h = shift;
for my $link ( $h->find_by_tag_name( 'a' ) ) {
my $href = '../'.$link->attr( 'href' );
$link->attr( 'href', $href );
}
for my $link ( $h->find_by_tag_name( 'img' ) ) {
my $src = '../'.$link->attr( 'src' );
$link->attr( 'src', $src );
}
}
The approach below should do what you wanted in question. During the HTML file processing, the hash %decade is setup, each key being ending year of decade and value arrayref of appropriate cells.
Second loop traverses the hash and outputs file for each decade, surrounding each 5 cells with <tr> tag.
use strict;
use HTML::TreeBuilder;
use File::Slurp;
use List::MoreUtils qw(part);
my $tree = HTML::TreeBuilder->new_from_file('test.html');
my %decade = ();
for my $h ( $tree->look_down( class => 'basic' ) ) {
edit_links( $h );
my ($year) = ($h->as_text =~ /.*?\((\d+)\).*/);
my $dec = (int($year/10) + 1) * 10;
$decade{$dec} ||= [];
push #{$decade{$dec}}, $h;
}
for my $dec (sort { $b <=> $a } keys %decade) {
my $filename = "decades/" . $dec . "-" . ($dec - 9) . ".html";
my $idx = 0;
my #items = map { $_->as_HTML('<>&',' ',{}) } #{ $decade{$dec} };
my $contents = join('',
'<table>',
(map { "<tr>#$_</tr>" } part { int($idx++ / 5) } #items),
'</table>');
write_file( $filename, $contents);
}
...