how to make mod_topbanner show 2 images to 2 different links - php

I'm, trying to get the Joomla topbanner to show 2 different images leading to 2 different links, so I changed the following code,
from this:
// get a parameter from the module's configuration
$imgname = $params->get('imgname');
$imgwidth = $params->get('imgwidth', '');
$imgheight = $params->get('imgheight', '90');
$imgtarget = $params->get('imgtarget', '');
$imgpath = JURI::base().'images/stories/';
$html = "<a href='".$imgtarget."' target='_blank'>";
$html .= "<img src='".$imgpath.$imgname."' border='0' alt='' width='".$imgwidth."' height='".$imgheight."'>";
$html .= "</a>";
echo $html;
to this:
// get a parameter from the module's configuration
$imgname = $params->get('imgname');
$imgwidth = $params->get('imgwidth', '');
$imgheight = $params->get('imgheight', '90');
$imgtarget = $params->get('imgtarget', '');
$imgpath = JURI::base().'images/stories/';
//split up image and targets
list($image1,$image2) = explode(',',$imgname);
list($target1,$target2) = explode(',',$imgtarget);
//steps
//divide image into 2 using any image editing tool
//upload to server
//set up images separated by comma in admin
//add second target to admin separated by comma
$html = "<a href='".$target1."' target='_blank'>";
$html .= "<img src='".$imgpath.$image1."' border='0' alt='' width='".$imgwidth."' height='".$imgheight."'>";
$html .= "</a>";
$html = "<a href='".$target2."' target='_blank'>";
$html .= "<img src='".$imgpath.$image2."' border='0' alt='' width='".$imgwidth."' height='".$imgheight."'>";
$html .= "</a>";
echo $html;
I edited it in the back end with a delimiter but it is only reading the second image and link.
Any help please?

The issue is with this line:
$html = "<a href='".$target2."' target='_blank'>";
You're not cocatenating $html, you're reassigning it. All the old information is lost. Simply do this:
$html .= "<a href='".$target2."' target='_blank'>";

Related

Removing Images from RSS Feed Description when pulling feed to another site

I am almost completely on my own trying to solve this problem, know next to nothing about PHP and am in desperate need of help. A Wordpress news site redesign has images included in the description tag of the feed, we already had a way set up to pull those images so now the feed on other sites is seeing double. I want to turn off the image being pulled in the description tag and I'm having trouble figuring out how to accomplish that.
I have tried a few things, like strip_tag, shuffling code/HTML, etc., but I suspect most of my problem is not knowing where or how to implement them.
if ($rss = simplexml_load_file($feed)){ // (replace relative path with variable: $rssPath ).
$count = 0;
foreach($rss->channel->item as $item){
if ($count < $items) {
/*Set variables from RSS*/
$title = $item->title;
$desc = $item->description;
$content = htmlspecialchars_decode($item->children('content', true)->encoded);
preg_match('/(<img[^>]+>)/i', $content,
$content_image);
//if(strstr($content, 'img')){
// $content_trunc =
substr($content,0,strpos($content,'</p>', 550)) . "...";
//}
//else {
//$content_trunc = substr($content,0,strpos($content,'</p>', 400)) .
"...";
//}
$content_image = str_replace('class="',
'class="media-object ', $content_image);
$link = $item->link;
$pubdate = $item->pubDate;
$pubMonth = substr($pubdate, 8, 3);
$pubDay = substr($pubdate, 5, 2);
/*Display items*/
$output= "<div class='row'>\n<div class='col-md-3'>";
$output = $output .
" <a href='". $link ."'>"
. $content_image[0] .
" </div>
<div class='col-md-9'><span class='news-headline' style='vertical-align:top;'>" . $title . "</span>
</a>" . "\n
<p class='news-blurb'>" . $desc . "</p>
</div>\n</div>\n <hr />";
echo $output;
$count++;
}
}
This is the description tag, in short:
<description><![CDATA[<img width="150" height="150" src="http://placehold.it/150.150" class="attachment-thumbnail size-thumbnail wp-post-image" alt="" />news article description here [...]]]></description>
Please help! This is a language I want to learn, but I'm a long way from it yet. All I know is that I need to filter content out of $desc. but I don't know how.
Try preg_replace("/<img[^>]+\>/i", "", $desc); after $desc = $item->description; line.

Get all img and pdf with php

I'm trying to get all images from folder with php and link them with another folder where I have pdf files. The names of both are the same.
My code looks like this:
$dir_jpg = "../wp-content/uploads/newspaper/jpg/";
$dir_pdf = "../wp-content/uploads/newspaper/pdf/";
$images = glob($dir_jpg."*.*");
$pdfs = glob($dir_pdf."*.*");
$array = array_combine($images, $pdfs);
foreach($array as $image => $pdf){
echo '<ul>';
echo '<li>';
echo '<img src="'.$image.'" height="100" width="100" />';
echo '</li>';
echo '</ul>';
}
I did var_dump to check the array, but it was empty. What could be the reason my code is not working?
Update
I store my files in a wordpress folder system, but I didn't upload them through
wordpress media, so there is no records about them in database. I want to avoid it, and upload those files through ftp account and list them with a php code.
Update 2 On chat with #Gabor Klement we have got to this
$wp_root = wp_upload_dir()['baseurl'];
$dir_jpg = $wp_root."/newspaper/jpg/";
$dir_pdf = $wp_root."/newspaper/pdf/";
$images = glob($dir_jpg."*.*");
$pdfs = array();
$imgNum = count($images);
$list = '<ul>';
for ($i = 0; $i < $imgNum; $i++) {
$filename = substr(basename($images[$i]), 0, -4);
$pdfs[$i] = $dir_pdf.$filename.".pdf";
if (file_exists($dir_pdf.$filename.".pdf")) {
$list .= '<li><img src="'.$images[$i].'" height="100" width="100" /></li>';
}
}
$list .= '</ul>';
echo $list;
but for some reason wordpress is not showing those files. Funy part is that <img src="<?php echo $dir_jpg ?>/july2012.jpg" /> placed before the $list works
Update 3
the only thing that is passing paths to $images is wp_upload_dir()['basedir']; but then the wordpress creates a path like this domain.com/home/user/domains/domain/public_html/wp-content/uploads/newspaper/jpg/december2012.jpg and the image is not found.
I found a solution for my problem. I had to use wp_upload_dir()['basedir'] to pass the path to glob, and then wp_upload_dir()['baseurl']to pass the link to src.
Maybe it not the best solution, but it works.
$base_dir = trailingslashit(wp_upload_dir()['basedir']);
$base_url = wp_upload_dir()['baseurl'];
$dir_jpg = '/newspaper/jpg/';
$dir_pdf = '/newspaper/pdf/';
$images = glob($base_dir.$dir_jpg.'*.*');
foreach($images as $image) {
$url = $base_url.$dir_jpg.basename($image);
$filename = substr(basename($image), 0, -4);
$pdfs = $base_url.$dir_pdf.$filename.".pdf";
printf('<img src="%s" alt="'.$filename.'.pdf"><div class="newspaper-hover"></div>', esc_url($url));
}

How to Parse XML's Media:Content with PHP?

I've found a great tutorial on how to accomplish most of the work at:
https://www.developphp.com/video/PHP/simpleXML-Tutorial-Learn-to-Parse-XML-Files-and-RSS-Feeds
but I can't understand how to extract media:content images from the feeds. I've read as much info as i can find, but i'm still stuck.
ie: How to get media:content with SimpleXML
this suggests using:
foreach ($xml->channel->item as $news){
$ns_media = $news->children('http://search.yahoo.com/mrss/');
echo $ns_media->content; // displays "<media:content>"}
but i can't get it to work.
Here's my script and feed i'm trying to parse:
<?php
$html = "";
$url = "http://rssfeeds.webmd.com/rss/rss.aspx?RSSSource=RSS_PUBLIC";
$xml = simplexml_load_file($url);
for($i = 0; $i < 10; $i++){
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$description = $xml->channel->item[$i]->description;
$pubDate = $xml->channel->item[$i]->pubDate;
$html .= "<a href='$link'><h3>$title</h3></a>";
$html .= "$description";
$html .= "<br />$pubDate<hr />";
}
echo $html;
?>
I don't know where to add this code into the script to make it work. Honestly, i've browsed for hours, but couldn't find working script that would parse media:content.
Can someone help with this?
========================
UPDATE:
Thanx to fusion3k, i got the final code working:
<?php
$html = "";
$url = "http://rssfeeds.webmd.com/rss/rss.aspx?RSSSource=RSS_PUBLIC";
$xml = simplexml_load_file($url);
for($i = 0; $i < 5; $i++){
$image = $xml->channel->item[$i]->children('media', True)->content->attributes();
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$description = $xml->channel->item[$i]->description;
$pubDate = $xml->channel->item[$i]->pubDate;
$html .= "<img src='$image' alt='$title'>";
$html .= "<a href='$link'><h3>$title</h3></a>";
$html .= "$description";
$html .= "<br />$pubDate<hr />";
}
echo $html;
?>
Basically all i needed was this simple line:
$image = $xml->channel->item[$i]->children('media', True)->content->attributes();
Can't believe it was so hard for non techie to find this info online after reading dozens of posts and articles. Well, hope this will serve well for other folks like me :)
To get 'url' attribute, use ->attribute() syntax:
$ns_media = $news->children('http://search.yahoo.com/mrss/');
/* Echoes 'url' attribute: */
echo $ns_media->content->attributes()['url'];
// in php < 5.5: $attr = $ns_media->content->attributes(); echo $attr['url'];
/* Catches 'url' attribute: */
$url = $ns_media->content->attributes()['url']->__toString();
// in php < 5.5: $attr = $ns_media->content->attributes(); $url = $attr['url']->__toString();
Namespaces explanation:
The ->children() arguments is not the URL of your XML, it is a Namespace URI.
XML namespaces are used for providing uniquely named elements and attributes in an XML document:
<xxx> Standard XML tag
<yyy:zzz> Namespaced tag
└┬┘ └┬┘
│ └──── Element Name
└──────── Element Prefix (Namespace Identifier)
So, in your case, <media:content> is the “content” element of Namespace “media”. Namespaced elements must be have an associated Namespace URI, as attribute of a parent node or — most commonly — of the root element: this attribute has the form xmlns:yyy="NamespaceURI" (in your case xmlns:media="http://search.yahoo.com/mrss/" as attribute of root node <rss>).
Ultimately, the above $news->children( 'http://search.yahoo.com/mrss/' ) means “retrieve all children elements with http://search.yahoo.com/mrss/ as Namespace URI; an alternative — most intelligible — syntax is: $news->children( 'media', True ) (True means “regarded as a prefix”).
Returning to the code in example, the generic syntax to retrieve all first item's children with prefix media is:
$xml = simplexml_load_file( 'http://rssfeeds.webmd.com/rss/rss.aspx?RSSSource=RSS_PUBLIC' );
$xml->channel->item[0]->children( 'http://search.yahoo.com/mrss/' );
or (identical result):
$xml = simplexml_load_file( 'http://rssfeeds.webmd.com/rss/rss.aspx?RSSSource=RSS_PUBLIC' );
$xml->channel->item[0]->children( 'media', True );
Your new code:
If you want to show the <media:content url> thumbnail for each element in your page, modify the original code in this way:
(...)
$pubDate = $xml->channel->item[$i]->pubDate;
$image = $xml->channel->item[$i]->children( 'media', True )->content->attributes()['url'];
// in php < 5.5:
// $attr = $xml->channel->item[$i]->children( 'media', True )->content->attributes();
// $image = $attr['url'];
$html .= "<a href='$link'><h3>$title</h3></a>";
$html .= "<img src='$image' alt='$title'>";
(...)
Simple example for newbs like me:
$url = "https://www.youtube.com/feeds/videos.xml?channel_id=UCwNPPl_oX8oUtKVMLxL13jg";
$rss = simplexml_load_file($url);
foreach($rss->entry as $item) {
$time = $item->published;
$time = date('Y-m-d \ H:i', strtotime($time));
$media_group = $item->children( 'media', true );
$title = $media_group->group->title;
$description = $media_group->group->description;
$views = $media_group->group->community->statistics->attributes()['views'];
}
echo $time . ' :: ' . $title . '<br>' . $description . '<br>' . $views . '<br>';

i have an article driven from a db and it contains image tag, i need a regex to grab that image and put it into anchor tags

Some text before <img style="float: left;" src="../images/265218_imgthw.jpg" alt="" width="81" height="88"> some text after
Say i have something like that wrap inside some text coming from db. I need it to be changed to:
Some text before some text after
Any help please.
I tried breaking the code using the img tag with explode. It works fine but need to reduce the code using regEx. This is how i do it:
$string = "<img";
$explored = explode($string,$desc);
$desc = "";
foreach ($explored as $key => $value) {
static $counter = 0;
if(strpos($explored[$key], '/>')){
$desc .= "<a href='".$path.$image[$counter]."' class='lightview'><img".$explored[$key];
$counter++;
} else $desc .= $explored[$key]."<a href='".$path.$image[$key]."' class='lightview'><img";
}
$string = "/>";
$explored = explode($string,$desc);
$desc = "";
foreach ($explored as $key => $value) {
if(strpos($explored[$key], '<img'))
{
$desc .= $explored[$key]."/></a>";
}
else $desc .= $explored[$key];
}
You can do it using SimpleHTMLDOM pretend that you have a variable named as $t
// '$t' is the element to be wrapped with anchor
$tempHtml = str_get_html('<a>' . $t->outertext . '</a>');
$link = $tempHtml->find('a', 0);
// '$src' is the url of the link
$link->href = $image['src'];

Search server for image and display it

The user on my site can currently search a mysql db via PHP, and the results are displayed on the same page inside a DIV, thanks to ajax...
What I need now is to display an image that is associated with the mysql results...
Say the result has ID=250, then I want a piece of code to search in a folder for an image with the name 250.jpg on the server...
And then display this image in a table column using php script...
Here is my php script that displays the results as well as where I want the picture to appear...
Please help me...
$qry_result = mysql_query($query) or die(mysql_error());
}
// Build Result String
$display_table = "<table align='center'>";
// Insert a new row in the table for each result
while($row = mysql_fetch_array($qry_result)){
$display_table .= "<tr>";
$display_table .= "<td class='ad_container' colspan='4'></td>";
$display_table .= "</tr>";
$display_table .= "<tr>";
$display_table .= "<td width='110' rowspan='2'>----- IMAGE HERE -----</td>";
$display_table .= "<td width='377' height='15'>$row[headline]</td>";
$display_table .= "<td width='67' rowspan='2'>$row[insert_date]</td>";
$display_table .= "</tr>";
$display_table .= "<tr>";
$display_table .= "<td height='15'>$row[price]:-</td>";
$display_table .= "</tr>";
}
$display_table .= "</table>";
echo $display_table;
This solution checks to ensure the image actually exists prior to display:
// absolute path to image directory
$path = '/var/www/vhosts/path/to/dir';
// basepath image directory
$basepath = '/path/to/dir';
// assuming you store JPEGs
$image = $row['id'] . '.jpg';
// start column output
$display_table .= '<td width="110" rowspan="2">';
// make sure image exists
if (file_exists($path . $image)) {
$display_table .= '<img src="' . $basepath . $image . '" />';
}
// end column output
$display_table .= '</td>';
I think something like this would work:
$display_table .= "<td width='110' rowspan='2'><img src='/image/folder/" . $row["id"] . ".jpg'/></td>";

Categories