How to Add Full External URL prefix in PHP - php

I have written a PHP on a page displaying info from an XML feed to another website. On the feed I have a product image that we would like to display, but I need to add the external website address as a prefix in order for this to work.
Code I have on the feed is:
$xml = simplexml_load_file('feed/myfeed.xml');
foreach ($xml->item as $item) {
$short_description = substr($item->descshort, 0, 200); ?>
<div class="row">
<div class="lhs col-md-9">
<h3><?php echo $item->name ?></h3>
<p class="pri">£<?php echo $item->regprice ?></p>
<p id="shown">
<?php echo $short_description; ?> ... Read more</p>
</div>
<div class="rhs col-md-3"> //The following is the bit that I am struggling on
<img src="<?php echo 'http://mywebsite.com/myimagefolder/mysubimagefolder/' . $item->imgsm; ?>" alt="<?php echo $item->name ?>" height="150" />
</div>
</div>
But all this gives me is a warning message that I have just tried to access http://mywebsite.com/myimagefolder/mysubimagefolder/
So basically how could I add http://mywebsite.com/myimagefolder/mysubimagefolder/ on the beginning and the filename after it?
Its probably very simple, but not to me. Please could anyone help me?
Many thanks in advance.

I have just tried:
<img src="http://mywebsite.com/myfolder/mysubfolder/<?php echo $url, $item->imgsml ?>" alt="<?php echo $item->name ?>" height="150" />
And this seems to work throughout. And this seems to work throughout. Thanks #schenk, your answer was spot on.

Related

Change form to multi image image instead of single image PHP

My website currently lets me upload 1 photo at a time for each of my listed vehicles. This process takes some time, and it would be convenient to be able to drop all photos into 1 field and have them upload properly. Any help is great!
<?php
if(isset($slider_image)){
if(!empty($slider_image)){
foreach($slider_image as $s){
?>
<div class="col-md-4 hide_me_<?php echo $s->id; ?>" style="margin-bottom:10px;">
<a href="<?php echo site_url('uploads/'.$s->image); ?>" class="lightbox">
<img src="<?php echo site_url('uploads/'.$s->image); ?>" alt="<?php echo $s->title; ?>" class="img-media ">
</a>
Remove Image
</div>
<?php
}
}
}
?>
Thanks,

Retrieving many images from mysql automatically by an echo and enlarging them on click

I have a PhP shopping cart for autoparts and I need to be able to enlarge the retrieved image upon clicking on it. The problem is I can't manually assign an ID to every picture because I'm echoing them from the database.
I've tried assigning one ID/class to all the pictures that get retrieved and enlarge them on click using the JS function but can't get it working.
$items_array = $db_controller->runQuery("SELECT * FROM mystuff ORDER BY ID ASC");
if (!empty($items_array)) {
foreach($items_array as $key=>$value){
?>
<div class="DisplayCartItems">
<form method="post" action="?action=add&mscode=<?php echo $product_array[$key]["mscode"]; ?>">
<img src="<?php echo $product_array[$key]["msimage"]; ?>" class="imgcartdisplay" align="alignnone"/> /* Right here I need some help */
<div class="lif"><strong>Category:</strong> <?php echo $product_array[$key]["mscategory"]; ?></div>
<div class="lif"><strong>Category №:</strong> <?php echo $product_array[$key]["mscatnum"]; ?></div>
<div class="lif"><strong>Stock №:</strong> <?php echo $product_array[$key]["msnomer"]; ?></div>
<div class="lif"><strong>Mark:</strong> <?php echo $product_array[$key]["msmark"]; ?></div>
<div class="lif"><strong>Model:</strong> <?php echo $product_array[$key]["msmodel"]; ?></div>
<div class="lif"><strong>Year:</strong> <?php echo $product_array[$key]["msyear"]; ?></div>
<div class="lif"><strong>Price:</strong> <?php echo "$".$product_array[$key]["msprice"]; ?></div>
<div style="text-align:center"><input type="number" name="oap" placeholder="Offer a price:"></div>
<br class="">
<div class="cartan">
Quantity: <input type="text" class="product-quantity" name="quantity" value="1" size="2" />
<br class="">
<input type="submit" onclick="AddCartAlert()" value="Add to cart" /></div>
</form>
</div>
<?php
}
}
?>```
I think if I can somehow assign an ID or class name for every picture that get displayed from the MySql and onclick change it's width/height, that'd do the job.
If I have correctly understood:
you should replace this:
<img src="<?php echo $product_array[$key]["msimage"]; ?>" class="imgcartdisplay" align="alignnone"/> /* Right here I need some help */
with:
<a target="_blank" href="<?php echo $product_array[$key]["msimage"]; ?>"><img src="<?php echo $product_array[$key]["msimage"]; ?>" class="imgcartdisplay" align="alignnone"/></a>
for opening the image in another tab.
Or... if you mean something like this https://lokeshdhakar.com/projects/lightbox2/#examples you have to use javascript (for example: Image #1)
In your case something like this: (of course you need to add lightbox script: https://lokeshdhakar.com/projects/lightbox2/#getting-started)
<a data-lightbox="image-1" data-title="My caption" href="<?php echo $product_array[$key]["msimage"]; ?>"><img src="<?php echo $product_array[$key]["msimage"]; ?>" class="imgcartdisplay" align="alignnone"/></a>

Getting images from RSS using PHP

I want to display an RSS feed on a web page. Currently I'm only getting the title and description but I want to get images also.
<?php
$html = "";
$url = "http://rss.cnn.com/rss/edition.rss";
$xml = simplexml_load_file($url);
foreach ($xml->channel->item as $itm) {
$title=$itm->title;
$link=$itm->link;
$pubDate=$itm->pubDate;
$description=$itm->description;
$img=$itm->media;
}
?>
<div class="container">
<div class="row">
<div class="col-md-6">
<?php echo $title; ?><br>
<span class="text text-info"><?php echo $pubDate; ?></span><br>
<p align="right" class="text text-info"><?php echo $description; ?></p>
<img src="<?php echo $img; ?>"></img>
</div>
</div>
</div>
<?php } ?>
You've finished your foreach loop twice. That's almost certainly not what you want to happen, or you wouldn't have used the foreach loop.
Try removing the first } (before the bulk of the HTML) and also replacing <img src="<?php echo $img; ?>"></img> with <img src="<?php echo $img; ?>" />.

How can I limit a string when parsing XML in PHP

I am writing a custom landing page in PHP parsing an XML product feed. The product descriptions are very long, as it goes into technical information and specifications, so need to be condensed to read the first 200 characters. hopefully with a read more link once 200 characters has been reached.
The code I have so far is:
<?php
$xml = simplexml_load_file('feed.xml');
foreach ($xml->item as $item) { ?>
<div class="row">
<div class="lhs">
<h3><?php echo $item->brand.' '.$item->title ?></h3>
<p class="pri">£<?php echo $item->price ?></p>
<p><?php echo $item->description; ?></p>
</div>
<div class="rhs">
<img src="<?php echo $item->image_link ?>" alt="<?php echo $item->title ?>" height="150" />
</div>
</div>
<?php
}
?>
Please could anyone advise what else I need to add? I can follow basic patterns in PHP, I just need some guidance.
Many thanks in advance.
Notice the $short_description line.
Edited with some basic js example of "Show more"
<?php
$xml = simplexml_load_file('feed.xml');
foreach ($xml->item as $item) {
$short_description = substr($item->description, 0, 200);
?>
<div class="row">
<div class="lhs">
<h3><?php echo $item->brand . ' ' . $item->title ?></h3>
<p class="pri">£<?php echo $item->price ?></p>
<p id="shown"><?php echo $short_description; ?>... Show more</p>
<p id="hidden"><?php echo $item->description; ?></p>
</div>
<div class="rhs">
<img src="<?php echo $item->image_link ?>" alt="<?php echo $item->title ?>" height="150" />
</div>
</div>
<?php
}
?>

Live jQuery Thumbnails with PHP

I am trying to implement the jQuery live thumbnails with my PHP page. On the page I am retrieving event information along with a comma separated list of thumbnail values. The plugin works a bit differently that that and I am having difficulty adapting my script to work. The plugin is located here http://tutorialzine.com/2012/12/album-photo-previews-jquery/. In their example they use an array while I have a comma separated list of thumbnails. The code I have so far is here...
<?php foreach ($events as $event) { ?>
<div class="left13 section_home" style="margin-bottom:25px;">
<h2><?php echo $event['event_title']; ?></h2>
<div align="center">
<?php foreach ($event['thumbnails'] as $thumbnail) { ?>
<a href="#" data-images="<?php echo str_replace(',', '|', $event['thumbnails']); ?>" class="album">
<img src="<?php echo ($event['thumbnails'] != '') ? base_url() . 'media/photos/thumbnail/' . $event['thumbnails'] : base_url() . 'images/no_photo_thumbnail.png'; ?>" alt="" title="" /><span class="preloader"></span></a>
<p><?php echo ($event['event_description'] != '') ? substr($event['event_description'], 0, 200) : 'No description yet...'; ?></p>
<span class="swirl_left"><span class="swirl_right">View This Event</span></span>
<?php } ?>
</div>
</div>
<?php } ?>
How would I be able to make this script work by using a comma separated list of thumbnail values instead of the array used in the tutorial? Thanks!
I figured it out and the answer is quite simple. First, my data coming from the database included a comma separated list of thumbnails. I just used the proper functions to insert that data into the script. The only problem I have now is that I need to add the path to the beginning of each value in the comma separated list. Is there a PHP function to do that? Below is my almost finished code.
<div class="content">
<?php foreach ($events as $event) {
// Create an array out of the comma separated list of thumbnails
$thumbnails = explode(',', $event['thumbnails']); ?>
<div class="left13 section_home" style="margin-bottom:25px;">
<h2><?php echo $event['event_title']; ?></h2>
<div align="center">
<img src="<?php echo base_url() . 'media/photos/thumbnail/' . $thumbnails[0]; ?>" alt="" title="" /><span class="preloader"></span>
<p><?php echo ($event['event_description'] != '') ? substr($event['event_description'], 0, 200) : 'No description yet...'; ?></p>
<span class="swirl_left"><span class="swirl_right">View This Event</span></span>
</div>
</div>
<?php } ?>
</div>
To finish this up I need to add the following to the beginning of each item in $event['thumbnails']...
<?php echo base_url(); ?>media/photos/thumbnail/
Then it should work perfectly.

Categories