xml string to php page - php

a photographer here with next to none programming experience. I am working on my website and got far enough (yes, I did a lot of searches) but cannot find the answer. Anybody can point me to the right direction?
I want to generate a code for my galleries. I have several different galleries/pages, and I would like to store all src, alt in one xml file, then bring it to a specific page.
SO far I have this
<?xml version="1.0"?>
<catalog>
<img id="pf01">
<src>foodphotography/01</src>
<alt>food1</alt>
</img>
<img id="pf02">
<src>foodphotography/01</src>
<alt>food2</alt>
</img>
<pf id="pf03">
<src>foodphotography/05</src>
<alt>food2</alt>
</pf>
</catalog>
and the php code
<?php
// Loading the XML file
$xml = simplexml_load_file("img.xml");
foreach($xml->children() as $img)
{
echo "<div class='swiper-slide'>\n";
echo "<img src='/images/".$img->src."' alt='".$img->alt."'>\n";
echo "</div>\n";
}
?>
I am trying to display on one page only <img> and on the other <pf>. Right now it displays all 3 images. How do I pick from the xml file only specific images? It looks like I cannot have more than one catalog.
Any better way to do this?
Thank you

There's lots of way you could filter on this, but since you're interested in having them on different pages, then you might just use xpath to filter the children. So to just show the img use the xpath /document/img
<?php
// Loading the XML file
$xml = simplexml_load_file("img.xml");
// swap children for xpath
foreach($xml->xpath('/catalog/img') as $img)
{
echo "<div class='swiper-slide'>\n";
echo "<img src='/images/".$img->src."' alt='".$img->alt."'>\n";
echo "</div>\n";
}
And on your other page, use the xpath /catalog/pf

Related

Display Image from PHP to XML

I am trying to call an image in my PHP to an XML file. I have tried to do this many different ways, but have not had much luck. More specifically, I am trying to call img file variable "Image1" to display. I have also sought tutoring, but the tutor was also somehow stumped on this, so any help is appreciated.
Prompt: (a) Create a XML file which contains the description of at least 3 product names, prices, and image file names.
(b) Load the XML file using Php and generate the shopping items: image, name, price and allow order quantity. Hint: Replace the image file names, item names, and prices with the Php codes. The Php codes supply the Php variables which were filled by reading the XML file at the beginning of the page. Then use "echo" verb to show the content of the session variable which contains the file name, item name, and price. The page should have the file extension .php.
//My XML
<?xml version="1.0" encoding="UTF-8"?>
<items>
<object>
<Name>CSU Womens T-Shirt</Name>
<Material>3% Polyester / 97% Cotton</Material>
<Price>$23.99</Price>
<image1>C:\Users\erica\OneDrive\Documents\IST450HW\IMGs\csupic.jpg</image1>
</object>
<object>
<Name>CSU Unisex Hat</Name>
<Material>2% Spandex / 98% Cotton</Material>
<Price>$10.99</Price>
</object>
<object>
<Name>CSU Men's T-Shirt</Name>
<Material>5% Polyester / 95% Cotton</Material>
<Price>$19.99</Price>
</object>
</items>
//My PHP
<?php
$xml = simplexml_load_file("Items.xml");
foreach ($xml as $key => $value) {
foreach ($value as $key => $value) {
echo $key.": ".$value."</br>";
}
}
$result = $xml->$items->$object->$image1;
echo '<img src="'.$result.'" height="100"; "width="100" ;>';
?>
$xml will be an object, im not sure why you are looping through it with a foreach, so ill ignore that bit.
To access the element which you're after you need to do it like:
$result = $xml->object[0]->image1;
echo '<img src="'.$result.'" height="100" width="100"/>';
But your browser won't see that path (or it wont work once you put it elsewhere), so place the image in an accessible location e.g: ./imgs and then change your xml.
For example:
<image1>./imgs/csupic.jpg</image1>
Then the resulting img tag will look like:
<img src="./imgs/csupic.jpg" height="100" width="100"/>
The web server will then be able to serve the file without issues.
Try put image file csupic.jpg in the same dir of php file and if not work change echo line to:
echo '<img src="/'.$result.'" height="100"; "width="100" ;>';

How do I generate different HTML for the last uploaded file using PHP?

I am creating an image gallery in fancybox but am trying to automate image uploading.
Fancybox galleries are a collection of <a> tags, but the first <a> tag listed also has an <img> tag to provide a thumbnail to open like so:
<img src="image1thumb.jpg">
Currently I would have to manually add <a> tags for every new image I upload which would be tedious. I would rather add the links to a mySQL database which will then be picked up by a piece of PHP.
In plain English the code would read as follows :
For the last uploaded file, show <a href.......><img src=.......></a> and then for all other images show <a href........></a> only.
I already have the code for the last uploaded file which displays as $lastupload, but am struggling with a foreach loop. So far I have:
foreach ($lastupload) {
echo '<a class="fancybox" rel="'.$r["rel"].'" href="'.$r["imagelink"].'" title="'.$r["comment"].'"><img src="thumb.jpg"></a>';
}
else {
echo '<a class="fancybox" rel="'.$r["rel"].'" href="'.$r["imagelink"].'" title="'.$r["comment"].'"></a>
}
Now, I know that this code is basically useless. I know that I need more in the initial foreach brackets, and that else shouldn't be a part of this loop (it should be foreach/ifelse), but I don't know how to proceed further. All of the examples I've looked introduce counts which I'm not sure fits into what I'm trying to do. Plus, no example I've looked at so far shows how to generate a list of links from a mySQL database.
Any help you can give will go a long way.
Thanks
Have you looked at how the foreach() loop actually works? You don't need a count for this particular loop. Or you could loop until the end of your results (see below).
http://php.net/manual/en/control-structures.foreach.php
As for extracting links from your database, look at mysqli_fetch_assoc(). There are some examples of while() loops to run through your query.
http://php.net/manual/en/mysqli-result.fetch-assoc.php
Look at the above examples in the PHP.net manual, this is a really good source of information.
I'm not sure I fully understand your question. I don't know what $r is in your example, but from what I can tell you want to do something like this:
$i = 0;
foreach ($r as $row) {
if ($i == 0) {
echo '<a class="fancybox" rel="'.$row["rel"].'" href="'.$row["imagelink"].'" title="'.$row["comment"].'"><img src="'.$lastupload.'"></a>';
}
else {
echo '<a class="fancybox" rel="'.$row["rel"].'" href="'.$row["imagelink"].'" title="'.$row["comment"].'"></a>
}
$i++;
}
The answer was not to look for the last uploaded file, just treat the first result of the query differently.
Here is the full code for what I got:
<?php
$foo_query->setFetchMode(PDO::FETCH_ASSOC);
$is_first=false;
while($r=$foo_query->fetch()) {
if($is_first) {
echo '<a...blah blah blah><img src="thumb.jpg"></a>';
} else {
echo '<a...more blah></a>';
}
}
?>

Flickr api title

Hello everyone I am trying to make a gallery on my website and I am pulling the images/sets from flickr. I am able to load all the sets with this bit of code:
$flickr = simplexml_load_file('http://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key='.$api.'&user_id='.$user_id.'');
foreach($flickr->photosets->photoset as $ps) {
echo '<img src="http://farm'.$ps['farm'].'.staticflickr.com/'.$ps['server'].'/'.$ps['primary'].'_'.$ps['secret'].'_q.jpg"><br />';
}
With this it will return a list of all the set's main images. However I also would like to add the title above it but the XML output of the title is in $flickr->photosets->photoset->title making it hard for me to get the title above every picture. Is there a easy way to get the title inside the foreach loop for the images but that the title also aligns correctly with the image?
the xml flickr outputs looks like:
<photosets page="1" pages="1" perpage="30" total="2" cancreate="1">
<photoset id="72157626216528324" primary="5504567858" secret="017804c585" server="5174" farm="6" photos="22" videos="0" count_views="137" count_comments="0" can_comment="1" date_create="1299514498" date_update="1300335009">
<title>Avis Blanche</title>
<description>My Grandma's Recipe File.</description>
</photoset>
</photosets>
If it's with the given XML, you can obtain it inside of the foreach loop with $ps->title.

Fetch URL from XML with jQuery and PHP - Jquery Slideshow

Im using the jQuery Nivo Slider, its pretty simple to setup:
<link rel="stylesheet" href="nivo-slider.css" type="text/css" media="screen" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="jquery.nivo.slider.pack.js" type="text/javascript"></script>
<!-- Then somewhere in the <body> section -->
<div id="slider">
<img src="images/slide1.jpg" alt="" />
<img src="images/slide2.jpg" alt="" title="#htmlcaption" />
<img src="images/slide3.jpg" alt="" title="This is an example of a caption" />
<img src="images/slide4.jpg" alt="" />
</div>
<div id="htmlcaption" class="nivo-html-caption">
<strong>This</strong> is an example of a <em>HTML</em> caption with a link.
</div>
However I want to be able to define the Images and captions in a seperate file, perhaps an XML file or text file. I'd like to store the images in a fairly readable fashion so that virtually anyone could update the file if need be.
So question is how can I generate HTML in the same structure as used above by generated automatically via PHP from a file?
I need to generate a list of images with some options:
I supposse the options are:
IMG src
href
alt
title (The title relates to the caption)
caption (The actual caption text)
Option in the PHP to order the images based on an ID
Only the IMG src, alt and title would be compulsary with the remaining fields optional.
I imagine XML is going to be the easiest way to define these options, but I have no idea how to go about creating my own XML. I've already written down the following but I need to know how to correctly define this as XML. E.g. Is there a namespace or something I need to add to the top of the file to create valid XML?
<images>
<image>
<id>1</id>
<source>images/slide1.jpg</source>
<alt>This is an image</alt>
<title>caption1</title>
<href>http://url.com</href>
<image>
<image>
<id>3</id>
<source>images/slide3.jpg</source>
<alt>This is an image</alt>
<title>caption3</title>
<href>http://url.com</href>
<caption>This is an example of a HTML caption.</caption>
<image>
<image>
<id>2</id>
<source>images/slide2.jpg</source>
<alt>This is an image</alt>
<title>caption2</title>
<href>http://url.com</href>
<image>
</images>
So thats potentially the XML defined to store the slider details.
Now I need to read the XML to generate the HTML:
All the images have to be stored between the <div id="slider"> images </div> and the captions have to be defined outside of this .
So I need to fetch the images first in the corret order based on the ID <id>. Something like (Not real code).
foreach image as images {
$id = <id>;
$src = <src>;
$title = <title>;
$alt = <alt>;
$href = <href>;
orderby $id;
<!--HERE I NEED TO DO SOME KIND OF if HERE SO THAT ONLY THOSE ELEMENTS
THAT ARE POPULATED IN THE XML ARE USED
-->
echo "<a href='$href'><img src='$src' alt='$alt' title='$title' /></a>";
}
The above would loop 3 times based on the above XML, and output the full HTML ready for a caption.
So to generate the captions I need to create this:
foreach images as image {
if $title = * {
echo "<div id='$title' class='nivo-html-caption'>
This is an example of a HTML caption.
</div>"
}
}
I beleive the Title give to the <img> is the relative to the ID given to the nivo-html-caption ID.
I realise I've written a lot here but just done have the understanding of XML to put the file together in a valid way.
How can I fetch the file using PHP?
How can I loop through the XML to generate the Images in HTML and put them in the correct order?
How can I generate the HTML captions seperatly?
Any help greatly appreciated!
Personally, I'd use JSON to store this information as its very simplified. Using php or perl, you just loop through the array, and this information can be pushed to the users browser if need be.
All of this can be found on David Powers's book OOP php, these examples are his in the book fyi:
How can I fetch the file using PHP?
$xml = simplexml_load_file($location);
How can I loop through the XML to generate the Images in HTML and put them in the correct order?
example of xml structure:
<book isbn13="978-1-43020-991-1">
<title>PHP Object Oriented Solutions</title>
<author>David Powers</author>
<publisher>friends of ED</publisher>
<description>Introduces the key concepts . . . </description>
</book>
ex:
$xml = simplexml_load_file('inventory.xml');
echo $xml->book[0]->title;
And it should show you, PHP Object Oriented Solutions
ex:
$xml = simplexml_load_file('inventory.xml');
foreach ($xml->book as $book) {
echo $book->title . '<br />';
}
This loops through each books if you have more than one .
How can I generate the HTML captions seperatly?
I assume you can have a separate foreach loop that is used for grabbing the captions from the xml.
Yeah, I think this is enough for you to google tutorial on simplexml functions and stuff. Good luck.
Oh, it can make your xml file simpler, less level of nested tags, by embedding html into the xml:
<url> <![CDATA[ URL ]]> </url>
So with that perhaps it'll make parsing your xml file a lot easier.

DOM Manipulation with PHP

I would like to make a simple but non trivial manipulation of DOM Elements with PHP but I am lost.
Assume a page like Wikipedia where you have paragraphs and titles (<p>, <h2>). They are siblings. I would like to take both elements, in sequential order.
I have tried GetElementbyName but then you have no possibility to organize information.
I have tried DOMXPath->query() but I found it really confusing.
Just parsing something like:
<html>
<head></head>
<body>
<h2>Title1</h2>
<p>Paragraph1</p>
<p>Paragraph2</p>
<h2>Title2</h2>
<p>Paragraph3</p>
</body>
</html>
into:
Title1
Paragraph1
Paragraph2
Title2
Paragraph3
With a few bits of HTML code I do not need between all.
Thank you. I hope question does not look like homework.
I think DOMXPath->query() is the right approach. This XPath expression will return all nodes that are either a <h2> or a <p> on the same level (since you said they were siblings).
/html/body/*[name() = 'p' or name() = 'h2']
The nodes will be returned as a node list in the right order (document order). You can then construct a foreach loop over the result.
I have uased a few times simple html dom by S.C.Chen.
Perfect class for access dom elements.
Example:
// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');
// Find all images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
// Find all links
foreach($html->find('a') as $element)
echo $element->href . '<br>';
Check it out here. simplehtmldom
May help with future projects
Try having a look at this library and corresponding project:
Simple HTML DOM
This allows you to open up an online webpage or a html page from filesystem and access its items via class names, tag names and IDs. If you are familiar with jQuery and its syntax you need no time in getting used to this library.

Categories