Is it possible to query the nthChild with DOMDocument? - dom

$html = '<div id="animal"><p class="dog"> pudal </p> <p class="dog">chiwawa </p> </div>';
$qry = '//div[#class="animal"]/p[#class="dog"]/text();
how to get chiwawa or pudal only

You can try this xpath
//div[#id = 'animal']/p[2]/text()

Related

Use fluid viewhelper inside own viewhelper which creates html code

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>

Skip div within div simple html dom

A page that I want to scrape data from has the following elements
<div class="content">
<p>I want this</p>
<p>I want this</p>
<div class="row">
<p>I do not want this</p>
<p>I do not want this</p>
</div>
</div>
I do not want paragraphs in the <div class="row">
This is what I have tried so far. Doesnt seem to work
foreach ($sample_html->find('div[class=entry-content]') as $content) {
foreach($content->find('div[class=row]') as $row){
foreach ($row->find('p') as $skip) {
$skip->outertext = '';
}
}
foreach($content->find('p') as $paragraph){
//echo $paragraph;
$paragraphs .= '<p>'.$paragraph->innertext.'</p>';
echo $paragraphs;
}
}
Change
$skip->outertext = '';
to
$skip->innertext= '';

How to display related post by categories with next post of current post (don't latest post or random post)

How to display related post by categories with next post of current post (don't latest post or random post). I'm use code related post for twentytwelve theme. but now, author in wentytwelve_entry_meta() is repeat.
pls help me :
<div id="related_posts">
<?php
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach((get_the_category()) as $category) {
$id = $category->cat_ID;
}
global $wpdb;
$query = "
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id = $category_ids[0]
AND $wpdb->posts.id < $post->ID
ORDER BY id DESC limit 3
";
$my_query = $wpdb->get_results($query);
if( $my_query) {
echo '<h3>Related Posts</h3><ul>';
foreach($my_query as $key => $post) {
?>
<li>
<div class="entry-header">
<div class="header-l">
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
<p class="datetime">
<?php twentytwelve_entry_meta(); ?>
</p>
</div>
<?php the_post_thumbnail(); ?>
</div>
<div class="entry-summary">
<?php
$str_content = wp_trim_words($post->post_content);
$str_content = str_replace('[', '<', $str_content);
$str_content = str_replace(']', '>', $str_content);
echo $str_content;
?>
</div>
</li>
<?php
}
echo '</ul>';
}
}?>
WordPress customary is to use WP_Query class to fetch posts from DB, if you can modify your code to use WP_Query it would be easier.
WP_Query Reference
As you are using Custom Query to load your posts from DB, template tags like the_permalink(), the_title_attribute(), the_title() etc will not work properly that is the reason while theme function twentytwelve_entry_meta() fails.
As per codex reference Displaying Posts Using a Custom Select Query
You should try something like this:
global $wpdb;
$query = "
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id = $category_ids[0]
AND $wpdb->posts.id < $post->ID
ORDER BY id DESC limit 3";
$my_query = $wpdb->get_results($query);
if($my_query) {
global $post;
echo '<h3>Related Posts</h3><ul>';
foreach($my_query as $key => $post) {
//use setup postdata, it works only for variable named $post.
setup_postdata($post);
//you can safely use template tags now.
?>
<li>
<div class="entry-header">
<div class="header-l">
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
<p class="datetime">
<?php twentytwelve_entry_meta(); ?>
</p>
</div>
<?php the_post_thumbnail(); ?>
</div>
<div class="entry-summary">
<?php
$str_content = wp_trim_words($post->post_content);
$str_content = str_replace('[', '<', $str_content);
$str_content = str_replace(']', '>', $str_content);
echo $str_content;
?>
</div>
</li>
<?php
}
}
Other interesting post:
What does setup_postdata ($post ) do?

Get the values of checkbox

i have a contact form with some checkobox choices :
The html :
<div class="col-xs-6 col-md-6 form-group checkboxEight">
<div class="checkbox c-checkbox"><label><input type="checkbox" value="Seo" name="service[]"/><span class="fa fa-check"></span>seo</label>
</div>
<div class="checkbox c-checkbox "><label><input type="checkbox" value="ppc" name="service[]"/><span class="fa fa-check"></span>ppc</label>
</div>
<div class="checkbox c-checkbox "><label><input type="checkbox" value="social" name="service[]"/><span class="fa fa-check"></span>social media</label>
</div>
<div class="checkbox c-checkbox "><label><input type="checkbox" value="web" name="service[]"/><span class="fa fa-check"></span>web & interface</label></div>
</div>
php :
foreach($_POST['service'] as $selected)
{
//$service[] = $selected;
$service[] = trim($_POST['value']);
}
and $body
$subject = '[Contact Form] From '.$name;
$body = "Name: $name \n\n Email: $email \n\n Tel: $tel \n\n URL: $url \n\n service: $selected \n\n Comments: $comments";
$headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
For each checkbox that i select i want to receive via email. Βut now what happens is that only sends the last choice and not those who have chosen.
Try the following code:
foreach( $_POST['service'] as $selected ) {
$checked_boxes .= $selected . ' ';
}
$body = "Name: $name \n\n Email: $email \n\n Tel: $tel \n\n URL: $url \n\n service: $checked_boxes \n\n Comments: $comments";

PHP form submits regardless of required/error handling attempts

newbie here
I am currently attempting to build a simple contact form to learn how best to build one. I've tried to make several fields required (name, email, do you like spam radio button). The required html5 element isn't working for me or I am not implementing it correctly based on what i've read here, on google and other places. Likewise the error/require messages don't display if someone attempts to submit a message. Instead the form goes through on submit even if NO content anywhere has been entered. I built this yesterday and have been working on the errors for about 24 hours now and am hoping to find some help here.
I currently am returning no errors, the form sends all information entered as hoped, but it also sends if NO information is entered and that doesn't seem like the best possible form to me. I've tried to build a function to help the required, but that doesn't seem to be working either.
Again, i'm new so if my mistakes are obvious and silly, please help em to understand them, that would be greatly appreciated.
current example can be found here
My code:
<!-- For PoohPot -->
<style TYPE="text/css">
input[type="text"]#website { display: none; }
input#website { display: none; }
.vSmall {font-size: 50%; text-align: center;}
</style>
<script>
$('form').submit(function(){
if ($('input#website').val().length != 0) {
return false;
}
});
</script>
<?php
//template.php
include '_inc/config.php';//holds arrays, et al.
include '_inc/functions.php';//holds functions that call arrays, etc
include '_inc/head.php';//Begin Document
//include '_inc/header.php';//Begin Content
?>
<article class="container box style3">
<header>
<img src="_img/icons/icon_lg-max-o-matic2.png" alt="php icon">
<h2><?=NAME_SITE?></h2>
<p>Spam Survey</p>
</header>
<?php
$to = "chezshire#gmail.com";
$subject="Spam Survey";
//----- end conf ----- //
// define variables and set to empty values
$nameErr = $emailErr = $spamErr = "";
$userName = $userEmail = $userMessage = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["userName"]))
{$nameErr = "* Name is required";}
else
{$name = testInput($_POST["userName"]);}
if (empty($_POST["userEmail"]))
{$emailErr = "* Email is required";}
else
{$email = testInput($_POST["userEmail"]);}
if (empty($_POST["spamErr"]))
{$emailErr = "* You must select an option";}
else
{$email = testInput($_POST["userEmail"]);}
if (empty($_POST["userMessage"]))
{$comment = "";}
else
{$comment = testInput($_POST["userMessage"]);}
}
if(isset($_POST['userName']))
{//data is submitted, show it
//echo $_POST['userName'];
$text = 'From: ' . $_POST['userName'] . PHP_EOL;
//$text .= $_POST['Comments']; //add on to text
$text .= process_post(); // uses proces_post to handle comments and stuff
$from = $_POST['userEmail'];
/*
echo '<pre>';
var_dump($_POST);
echo '</pre>';
*/
//domain spoofing controls
$headers = 'From: noreply#max-o-matic.com' . PHP_EOL . //from where formhandler sits
'Reply-To: ' . $from . PHP_EOL . //where replies are to go to
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $text, $headers); //from becomes headers
echo '<div class="row">
<div class="12u">
<p>Thanxs!</p>
<p>
<br />RESET
</p>
</div>
</div>
</article>
';
/*
echo '<pre>';
var_dump($_POST);
echo '</pre>';
*/
}else{//no data, show form
echo '
<form action="' . THIS_PAGE . '" method="post">
<div class="row half">
<div class="6u">
<input type="text" class="text" name="userName" placeholder="Name?" required autofocus />
<span class="error"><?php echo $nameErr;?></span>
</div>
<div class="6u">
<input type="text" class="text" name="userEmail" placeholder="Email" required />
<span class="error"><?php echo $emailErr;?></span>
</div>
</div>
<div class="row half">
<div class="6u">
Do you like spam?<br />
<input type="radio" name="Please_Spam_Me" value="YES!" required value="1" />Yes<br />
<input type="radio" name="Please_Spam_Me" value="NO!" />No<br />
<span class="error"><?php echo $spamErr;?>
</div>
<div class="6u">
How much Spam is too much?<br />
<input type="checkbox" name="checkbox-group" class="group-required" id="checkbox-group-id" value="6+" >6 or more<br />
<input type="checkbox" name="checkbox-group" value="2+">2 to 5<br />
<input type="checkbox" name="checkbox-group" value="Any">ANY<br /><br />
</div>
</div>
<!--For Winnie the Pooh -->
<input id="website" name="website" type="text" value"Website" />
<div class="row half">
<div class="12u">
<textarea name="userMessage" placeholder="What is your opinion on Spam?"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li>SEND!</li>
<input type="submit" />
</ul>
<p class="vSmall">
* Please note, instead of a reCaptcha I build my own \'honeypot\' from scratch.
</p>
</div>
</div>
</form>
</article>
';
}
function testInput($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// this is a function
function process_post()
{//loop through POST vars and return a single string
$myReturn = ''; //set to initial empty value
foreach($_POST as $varName=> $value)//$_POST is an array
{#loop POST vars to create JS array on the current page - include email
$strippedVarName = str_replace("_"," ",$varName);#remove underscores
if(is_array($_POST[$varName]))
{#checkboxes are arrays, and we need to collapse the array to comma separated string!
$myReturn .= $strippedVarName . ": " . implode(",",$_POST[$varName]) . PHP_EOL;
}else{//not an array, create line
$myReturn .= $strippedVarName . ": " . $value . PHP_EOL;
}
}
return $myReturn;
}
include "_inc/footer.php";//End Content/Document
?>
If you're trying to use HTML5 stuffs then you need the HTML5 doctype. Place this at the start of your output and see what happens:
<!DOCTYPE html>
Also, typo value"Website" should be value="Website"