I'am currently parsing in a twitter tweets by geocode and displaying them using php. But at the moment unable to access the URL to the tweet and have instead opted to show the users twitter profile.
The URL I'm trying to access is within the link tag
<link type="text/html" href="http://twitter.com/pmhigham/statuses/166271863331368961" rel="alternate" >
Its location within the XML file is Feed->Entry->Link.
I have thought about using a regular expression but don't know how to go about using this.
Here is my code.
<h3><?php
if (isset($column1Heading)) {
echo $column1Heading;
}
?></h3>
<p>
<strong>Tweets within 10 miles radius of London Eye</strong></br>
<?php
$feed = simplexml_load_file ('http://search.twitter.com/search.atom?geocode=51.5069999695%2C-0.142489999533%2C10.0mi%22london%22&lang=en&rpp=5');
if ($feed){foreach ($feed->entry as $item) {
echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
}
}
else echo "Cannot find Twitter feed!"
?>
</br>
<strong>London Hashtag</strong>
</br>
<?php
$feed = simplexml_load_file ('http://search.twitter.com/search.atom?q=%23london&lang=en&rpp=5');
if ($feed){foreach ($feed->entry as $item) {
echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
}
}
else echo "Cannot find Twitter feed!"
?>
</p>
</li>
<li class="col2">
<h3><?php
if (isset($column2Heading)) {
echo $column2Heading;
}
?></h3>
<p>
<?php
$feed = simplexml_load_file ('http://search.twitter.com/search.atom?geocode=40.744544%2C-74.027593%2C10.0mi%22manhattan%2C+ny%22&lang=en&rpp=5');
if ($feed){foreach ($feed->entry as $item) {
echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
}
}
else echo "Cannot find Twitter feed!"
?>
<strong>NYC Hashtag</strong>
</br>
<?php
$feed = simplexml_load_file ('http://search.twitter.com/search.atom?q=%23nyc&lang=en&rpp=5');
if ($feed){foreach ($feed->entry as $item) {
echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
}
}
else echo "Cannot find Twitter feed!"
?>
</p>
</li>
<li class="col3">
<h3><?php
if (isset($column3Heading)) {
echo $column3Heading;
}
?></h3>
<p>
<?php
$feed = simplexml_load_file ('http://search.twitter.com/search.atom?lang=en&geocode=48.861%2C2.336%2C5.0mi%22paris%2C+fr%22&rpp=5&lang=en');
if ($feed){foreach ($feed->entry as $item) {
echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
}
}
else echo "Cannot find Twitter feed!"
?>
</p>
</li>
</ul>
<div class="clear"></div>
</div>
<?php require_once 'footer.php'; ?>
If I understand you correctly:
You can access the attribute with the built in attributes() method of SimpleXML.
$item->link->attributes()->href
Otherwise the regular expression should look like this
$string = '<link type="text/html" href="http://twitter.com/pmhigham/statuses/166271863331368961" rel="alternate" >';
preg_match('/href\=\"([^\"]+)\"/i', $string, $matches);
Related
I have a checkout system, within my Wordpress that shows me the following message:
Notice: Undefined index: answers in
/srv/www/vhosts/wordpress/wp-content/themes/../checkout/fragment-header.php
on line 3
Notice: Trying to access array offset on value of type null in
/srv/www/vhosts/wordpress/wp-content/themes/../checkout/fragment-header.php
on line 3
The php code of that file is the following:
<?php
$valorAccesorio = $args['quote']['answers']['vehicleGncValue'] ?? '0';
$cuotaMensual = $args['quote']['answers']['planPremioMensual'] +
(isset($args['quote']['answers']['apPremioMensual']) ? $args['quote']['answers']['apPremioMensual'] : 0);
?>
<header id="header">
<div class="wrap menu-wrap">
<?php
$class = (theme_get_custom_logo()) ? 'image' : 'text';
echo '<h1 class="site-title ' . $class . '">';
echo '<a href="' . esc_url(home_url('/')) . '" rel="home">';
if (theme_get_custom_logo()) echo '<img src="' . theme_get_custom_logo() . '" class="site-logo" alt="' . htmlspecialchars(get_bloginfo('name')) . '" />';
echo '<strong>' . get_bloginfo('name') . '</strong>';
echo '</a>';
echo '</h1>';
?>
<div class="right-section">
<?php if ($args['quote']['product'] == 'seguro-de-motos' || $args['quote']['product'] == 'seguro-de-autos-y-pick-ups') : ?>
<h1>
Estás contratando un plan
<span><?php echo $args['quote']['answers']['planCobertura'] ?></span>,
para tu
<span>
<?php echo $args['quote']['answers']['vehicleBrand'] ?>
<?php if (isset($args['quote']['answers']['vehicleModel'])) {
echo $args['quote']['answers']['vehicleModel'];
} else {
echo $args['quote']['answers']['vehicleVersion'];
} ?>
<?php echo $args['quote']['answers']['vehicleYear'] ?>
</span>
</h1>
<p>
Suma asegurada: $<?php echo number_format(($args['quote']['answers']['vehicleValue'] + $valorAccesorio), 2, ',', '.') ?>
|
Cuota mensual: $<?php echo number_format(($cuotaMensual), 2, ',', '.') ?>
</p>
<?php endif; ?>
</div>
From what I understand I am generating an array with null value, but I do not understand how to generate it by another way
Some help? Thanks!
Here is the problem that while looping the php in while loop in w3-row-padding of w3 responsive layout . The layout breaks
Here is the source code
<?php
$r=0;
while($r<ceil($fetch_row_count/4))
{ ?>
<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">
<?php
while($row=mysqli_fetch_array($res))
{
?>
<div class="w3-quarter">
<img src="admin/uploads/<?php echo $row['image']; ?>" alt="noodles" style="width:50%">
<h3><?php echo $row['title']; ?></h3>
<p><?php echo $row['description']; ?> </p>
</div>
<?php
}
$r++;
}
?>
</div>
Thanks for reply and comments in advance
That bottom div was not being added for each of your padded containers.
The way the code is written you are adding a padded container and then adding your w3-quarter div for each of your result sets and then repeating that a bunch of times with what looks like to me the same set of data in each one.
What you are probably trying to accomplish is just making one padded div and then echo out your result set with the w3-quarter divs inside of it.
Here is your original way with the bottom div corrected:
<?php
$r=0;
while($r<ceil($fetch_row_count/4)) {
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
}
$r++;
echo
'</div>';
}
?>
Here is the way I think you are trying to do it: (Just guessing)
<?php
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
$r = 0;
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
$r++;
//I would not actually try to control how many results you add like this.
//I would get the results from a better formatted query.
if($r < ceil($fetch_row_count/4)){
break;
}
}
echo
'</div>';
?>
I have a page I am building in WordPress which includes tweets by the Twitter API and then a standard get_posts loop.
At the moment they are displaying one after the other but I wish to create a masonry style grid which includes both.
Is there a way to output both arrays in between each other by date so the items are broken up instead of it outputting tweets first then the posts?
Here is my code.
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
echo '<div class="funny-grid">';
echo '<div class="grid-sizer"></div>';
foreach($string as $items)
{
$string = $items['text'];
$regex = "#(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)#";
echo '<div class="grid-item tweet ">';
echo '<i class="fa fa-lg fa-twitter inverse" aria-hidden="true"></i>';
echo '<div class="tweet-text">' . preg_replace($regex, ' ', $string) . '</div>' ."<br />";
echo '<div class="tweet-user">' . '#' . '' . $items['user']['screen_name'] . '' . '</div>';
echo '<div class="tweet-date">' . $items['created_at'] . '</div>' . "<br />";
echo '</div>';
}
//counts the amount of items in the array.
/*echo "Number of items:" . count($string);*/
?>
<!--<div style="clear: both;"></div>-->
<?php
query_posts('post_type=funny_wall &posts_per_page=8');
if(have_posts()):while(have_Posts()):the_post();
$img = wp_get_attachment_url(get_post_thumbnail_id($post->ID), "full");?>
<div class="grid-item image">
<div class="as">
<img src="<?php echo $img ; ?>">
<a href="<?php the_permalink();?>">
</a>
</div>
</div>
<?php
endwhile;
endif;
wp_reset_query();
?>
</div>
</div>
Instead of echoing the results directly, you could put them into an array, and then use shuffle() to randomise before outputting it in one go.
e.g.:
tweets
$grid_items[] = '<div class="grid-item tweet ">
<i class="fa fa-lg fa-twitter inverse" aria-hidden="true"></i>
<div class="tweet-text">' . preg_replace($regex, ' ', $string) . '</div>' .'<br />
<div class="tweet-user">' . '#' . '' . $items['user']['screen_name'] . '' . '</div>
<div class="tweet-date">' . $items['created_at'] . '</div>' . '<br />
</div>';
posts:
$grid_items[] = "<div class='grid-item image'>
<div class='as'>
<img src=".$img.">
<a href=".the_permalink().">
</a>
</div>
</div>";
then use:
shuffle($grid_items);
foreach($grid_items as $grid_item){
echo $grid_item;
}
when you want to output them.
http://php.net/manual/en/function.shuffle.php
I am trying to scrape some content using simple_html_dom without luck.
I am trying to grab the title, image path and the link and display it.
The HTML structure is:
<div class="article_item clearfix">
<h2 class="title">My amazing Title</h2>
<p class="date">September 22 2014</p>
<p class="image_left">
<a href="http://www.demodomain/articleid=1">
<img src="http://www.demodomain/photos/cef78533cd5.jpg" alt="My amazing post ">
</a>
</p>
<p>This is a demo description<strong>of this amazing</strong> article</p>
<p class="more">Read more...</p>
</div>
My code so far:
foreach($html->find('article_item') as $article) {
$item['title'] = $article->find('.title, a', 0)->plaintext;
$item['thumb'] = $article->find('.image_left img', 0)->src;
$item['details'] = $article->find('p', 0)->plaintext;
$item['url'] = $article->find('.more, a', 0)->plaintext;
echo 'Title: ' . $item['title'];
echo "</br>";
echo "image url: " . $item['thumb'];
echo "</br>";
echo "Description: " . $item['details'];
echo "</br>";
echo "Read More Url: " . $item['url'];
}
// Clear dom object
$html->clear();
unset($html);
You didn't state whats not working but consider this example:
foreach($html->find('div.article_item') as $div) {
// ^ point to div tag with class name article_item
$title = $div->find('h2.title a ', 0)->innertext;
// ^ target the h2 tag with class title with child anchor
// just same as accessing dom with jquery
$thumb = $div->find('p.image_left img ', 0)->src;
$details = $div->children(3)->plaintext;
// $url = $div->find('p.more', 0)->plaintext;
$url = $div->find('p.more a', 0)->href;
echo $title . '<br/>';
echo $thumb . '<br/>';
echo $details . '<br/>';
echo $url . '<br/>';
}
Basically, this is just the same as selecting selectors.
can you try like this
$item['title'] = $article->find('h2.title')->plaintext;
$item['thumb'] = $article->find('p.image_left')->find('img')->src;
I'm currently using the following PHP foreach code below. When I view the source code, it's a giant block of list items.
My question is, how can I edit this so each list item in the source code is on it's own line for easier debugging?
foreach($xml->Event as $event) {
echo '<li><a href="', $event->link, '">';
echo '<strong>', $event->title, '</strong>';
echo '<span>', $event->beginDate, ' at ', $event->beginTime, '</span>';
echo $event->location;
echo '</a></li>';
}
Like this:
foreach($xml->Event as $event) {
echo '<li><a href="', $event->link, '">';
echo '<strong>', $event->title, '</strong>';
echo '<span>', $event->beginDate, ' at ', $event->beginTime, '</span>';
echo $event->location;
echo '</a></li>' . "\n";
}
Just add . "\n" to the last line in your loop.
Use new line '\n' or PHP_EOL at end of each line
Try modifying the final line to include a line break:
echo '</a></li>' . PHP_EOL;
Append all of your strings with either "\n" (make sure you use double-quotes), or PHP_EOL.
echo '</a></li>' . "\n";
// or
echo '</a></li>' . PHP_EOL;
Use \n.
foreach($xml->Event as $event) {
echo '<li><a href="', $event->link, '">' . "\n";
echo '<strong>', $event->title, '</strong>' . "\n";
echo '<span>', $event->beginDate, ' at ', $event->beginTime, '</span>' . "\n";
echo $event->location . "\n";
echo '</a></li> . "\n"';
}
I would write the code like this:
<?php foreach($xml->Event as $event) { ?>
<li>
<a href="<?php echo $event->link; ?>">
<strong><?php echo $event->title; ?></strong>
<span><?php echo $event->beginDate; ?> at <?php echo $event->beginTime; ?></span>
<?php echo $event->location; ?>
</a>
</li>
<?php } ?>
Add a '\n' at the end of each line you want to break.
foreach($xml->Event as $event) {
echo '<li><a href="', $event->link, '">\n';
echo '<strong>', $event->title, '</strong>\n';
echo '<span>', $event->beginDate, ' at ', $event->beginTime, '</span>\n';
echo $event->location;
echo '</a></li>' . "\n";
}