Im trying to create a table of Jobs on my site, pulling info from an xml feed I have access to... I've looked at various examples online and videos but I can't seem to understand how it works. My xml feed returns the following node structure:
<OutputVacancyAsXml>
<Vacancy>
<VacancyID></VacancyID>
<Job></Job>
<ClosingDate></ClosingDate>
</Vacancy>
</OutputVacancyAsXml>
I've had success with pulling through one item with this code:
<?php
$x = simplexml_load_file('https://www.octopus-hr.co.uk/recruit/OutputVacancyAsXml.aspx?CompanyID=400-73A3BCA1-D952-4BA6-AADB-D8BF3B495DF6');
echo $x->Vacancy[5]->Job;
?>
But converting it to foreach seems to be where I'm struggling. Heres the code I have tried so far with no luck;
<?php
$html = "";
$url = "https://www.octopus-hr.co.uk/recruit/OutputVacancyAsXml.aspx?CompanyID=400-73A3BCA1-D952-4BA6-AADB-D8BF3B495DF6";
$xml = simplexml_load_file($url);
for ($i = 0; $i < 10; $i++) {
$title = $xml->OutputVacancyAsXml->Vacancy[$i]->job;
$html .= "<p>$title</p>";
}
echo $html;
?>
Thanks all :)
Taken from the documentation
Note:
Properties ($movies->movie in previous example) are not arrays. They
are iterable and accessible objects.
With that kept in mind you can simple run over the nodes with foreach
$xml = simplexml_load_file($url);
foreach ($xml->OutputVacancyAsXml->Vacancy as $vacanacy)
{
echo (string)$vacanacy->Job; // Echo out the Job Title
}
Ok looks like I found a solution. Heres the code that worked for me plus it contains a little bit of code that pulls out duplicated (it was displaying each item 4 times!)...
<?php
$x = simplexml_load_file('https://www.octopus-hr.co.uk/recruit/OutputVacancyAsXml.aspx?CompanyID=400-73A3BCA1-D952-4BA6-AADB-D8BF3B495DF6');
$num = count($x->Vacancy);
//echo "num is $num";
$stopduplicates = array();
for ($i = 0; $i < $num; $i++) {
$job = $x->Vacancy[$i]->Job;
$closingdate = $x->Vacancy[$i]->ClosingDate;
// http://stackoverflow.com/questions/416548/forcing-a-simplexml-object-to-a-string-regardless-of-context
$vacancyid = (string) $x->Vacancy[$i]->VacancyID;
if (!in_array($vacancyid, $stopduplicates)) {
echo '
<tr class="job-row">
<td class="job-cell">'.$job.'</td>
<td class="date-cell">'.$closingdate.'</td>
<td class="apply-cell">
Apply Here
</td>
</tr>';
}
$stopduplicates[] = $vacancyid;
} //print_r($stopduplicates);
?>
below is my PHP code, I just can't figure out how to make the "INFO" text appear as a clickable url. I've seen the echo method but not one that works for the below. This method needs to create a link using the data and also set the link as the text used inside the table.
<?php
if(isset($data) && !empty($data)) {
foreach ($data as $row) {
#$count += 1;
$keyword = htmlspecialchars($row['Keyword']);
$suburb = htmlspecialchars($row['Suburb']);
$postcode = htmlspecialchars($row['Postcode']);
$status = htmlspecialchars($row['Status']);
$info = htmlspecialchars($row['Info']);
#$table_row .= "<tr><td>$count</td><td>$keyword</td><td>$suburb</td><td>$postcode</td><td>$status</td><td>$info</td></tr>";
}
echo #$table_row;
}
?>
It's the 'Info' column that i want to pull in as a url.
Cheers,
Curtis
http://www.homegiraffe.com.au
$info = '' . htmlspecialchars($row['Info']) . '';
This is the answer. Thanks to Chris85 for the help!
you can use this
<?php
if(isset($data) && !empty($data)) {
foreach ($data as $row) {
#$count += 1;
$keyword = htmlspecialchars($row['Keyword']);
$suburb = htmlspecialchars($row['Suburb']);
$postcode = htmlspecialchars($row['Postcode']);
$status = htmlspecialchars($row['Status']);
$info = htmlspecialchars($row['Info']);
?>
<tr><td><?php echo $count;?></td><td><?php echo $keyword;?></td><td><?php echo $suburb;?></td><td><?php echo $postcode?></td><td><?php echo $status;?></td><td><?php echo $info;?></td></tr>;
<?php
}
}
?>
and it work very good :)
I'm trying to make a php slideshow and I'm almost done I just need to implement the next and back buttons which I thought were going to be easy, but apparently you can't increment indexes in php?
$sql = "SELECT pic_url FROM pic_info";
$result = $conn->query($sql);
$count = 0;
$dir = "http://dev2.matrix.msu.edu/~matrix.training/Holmberg_Dane/";
$source = "gallery.php";
if ($result->num_rows > 0) {
// output data of each row
$pic_array = array();
while ($row = $result->fetch_assoc()) {
$pic_array[$count] = $row['pic_url'];
$count++;
}
$index = 1;
echo "<img src= ' $dir$pic_array[$index]' />";
echo "<a href= '$dir$pic_array[$index + 1]'>next</a>";
echo "<a href= '$dir$pic_array[$index - 1]'>back</a>";
}
$conn->close();
?>
Try this
<?php
echo '<img src="'.$dir.$pic_array[$index].'">
next
back';
?>
I would suggest to place the url's in an array and loop over them.
$urls = ['website/url/for/image/image.jpg', 'another/url/image.jpg'];
foreach ($urls as $url) {
echo 'a href="http://'.$url.'"> Click </a>';
}
It is definitely more readable that way.
I have a file with 20 pictures of country artists, and a text file with their websites. I'm trying to display this data in a 4 row 5 column table using PHP.
I try to use a foreach loop that iterates for every 5 elements in the array
(to load each row one by one)
foreach(array_chunk($CountryArtists, 5, true) as $Value) {
<table>
<tr><td> $CountryArtists[$Value] $ArtistImages[$Value]</td>
<td> $CountryArtists[$Value] $ArtistImages[$Value]</td>
<td> $CountryArtists[$Value] $ArtistImages[$Value]</td>
<td> $CountryArtists[$Value] $ArtistImages[$Value]</td>
<td> $CountryArtists[$Value] $ArtistImages[$Value]</td></tr>
</table>
Ive been trying to figure out how to load the images into the array, but having no luck. Im starting to think i must put the reference to the file location in the array,but i am not sure.
$colsToDisplay = 5;
$CountryArtists = array("C:\Users\THEFOLDER\images");
$ArtistImages = array("AJackson", "BShelton", "CUnderwood", "DBentley", "DLJones", "DRucker", "JAldean", "JCash", "JJohnson", "JStrait", "KChesney", "LAntebellum", "LDavis", "LRimes", "MLambert", "MMcBride", RTravis", "STwain", TKeith", TMcgraw");
$filename = "C:\Users\THEFOLDER\images";
I'm relatively new to PHP and really just need to know how to load my images and how to make this table show up correctly.
EDIT:
I added echo to the table lines but it just shows echo in the browser output:
" echo " $CountryArtists[$Value] $ArtistImages[$Value]" echo " .$CountryArtists[$Value]. $ArtistImages[$Value]" echo " .$CountryArtists[$Value]. $ArtistImages[$Value]" echo " .$CountryArtists[$Value]. $ArtistImages[$Value]" echo " .$CountryArtists[$Value]. $ArtistImages[$Value]" echo "" } ?>
My code now looks like this:
foreach(array_chunk($CountryArtists, 5, true) as $Value) {
echo "<table>"
echo "<tr><td> $CountryArtists[$Value] $ArtistImages[$Value]</td>"
echo "<td> .$CountryArtists[$Value]. $ArtistImages[$Value]</td>"
echo "<td> .$CountryArtists[$Value]. $ArtistImages[$Value]</td>"
echo "<td> .$CountryArtists[$Value]. $ArtistImages[$Value]</td>"
echo "<td> .$CountryArtists[$Value]. $ArtistImages[$Value]</td></tr>"
echo "</table>"
}
I feel like I'm doing strong wrong, would be so grateful to have it pointed out to me.
FULL FILE
<!DOCTYPE HTML>
<html>
<body>
<?php
$ArtistImages = array("AJackson", "BShelton", "CUnderwood", "DBentley", "DLJones", "DRucker", "JAldean", "JCash", "JJohnson", "JStrait", "KChesney", "LAntebellum", "LDavis", "LRimes", "MLambert", "MMcBride", "RTravis", "STwain", "TKeith", "TMcgraw");
$count = count($ArtistImages);
$cols = 6;
$div = (int) $count / (int)$cols;
$diff = ceil($div);
echo
$fin = $cols * $diff;
$a = 1;
echo '<table>';
for($i = 0; $i < $fin; $i++) {
if($a == 1)
echo "\t<tr>".PHP_EOL;
$artist = (!empty($ArtistImages[$i]))? $ArtistImages[$i]: "";
echo "\t\t".'<td>'.$artist.'</td>'.PHP_EOL;
if($a == $cols) {
echo "\t</tr>".PHP_EOL;
$a=0;
}
$a++;
}
echo '</table>';
?>
</body>
</html>
I think you may be looking for something similar to this algorithm. It adds a row every 5 values. You will want to do a divisor make it ceil() to make it add any required empty cells. You can do this foreach if you do <ul> and <li> and use CSS to make them display like a table. Then you don't need to calculate extra cells.
$i = 1;
echo '<table>';
foreach($array as $value) {
if($i == 1)
echo "<tr>";
echo '<td>'.$value.'</td>';
if($i == 5) {
echo "</tr>";
$i=0;
}
$i++;
}
echo '</table>';
EDIT:
Here is a more practical version based on yours:
$ArtistImages = array("AJackson", "BShelton", "CUnderwood", "DBentley", "DLJones", "DRucker", "JAldean", "JCash", "JJohnson", "JStrait", "KChesney", "LAntebellum", "LDavis", "LRimes", "MLambert", "MMcBride", "RTravis", "STwain", "TKeith", "TMcgraw");
// Count total artists in array
$count = count($ArtistImages);
// Choose how many to display per row
$cols = 6;
// Divide the total by the columns
$div = (int) $count / (int)$cols;
// Round up (incase the number will produce empty cells
$diff = ceil($div);
// Mulitply the final numbers
$fin = $cols * $diff;
// Create an autoincrementer to keep track of next rows
$a = 1;
echo '<table>'.PHP_EOL;
for($i = 0; $i < $fin; $i++) {
if($a == 1)
echo "\t<tr>".PHP_EOL;
// You need to see if this artist is populated. If not, make empty
// If left without this it will have a warning saying not exists
$artist = (!empty($ArtistImages[$i]))? $ArtistImages[$i]: "";
echo "\t\t".'<td>'.$artist.'</td>'.PHP_EOL;
if($a == $cols) {
echo "\t</tr>".PHP_EOL;
$a=0;
}
$a++;
}
echo '</table>';
In the php file you need to echo the data you want.
However if you in the php file you can close php like this.
? >
And write html code.
When you want to display php attributes you again need to open a php like this:
and continue like this...
Php fcan only return string or json data
Let me know if it work for you....
Right now i am working around one PHP script based on http://simplehtmldom.sourceforge.net/.
Here is my code:
<?PHP
foreach ($html->find('li.tooltip') as $ul) {
$id = $ul->id;
$dt = "data-text";
$dt = "data-text";
$cid = "colvar-id";
$datatext = $ul->$dt;
$colvarid = $ul->$cid;
$countN = count($id);
$number = 1;
$N = $i++;
if ($N == "") {
$N = 0;
}
}
?>
<?PHP echo "Result is: $countN"; ?>
This code is suposed to count the number of founded occurencies, but it displays nothing.
All i want is to count the founded occurencies and simply display the number of occurencies.
Thanks in advance!
You can use count():
$html = str_get_html(<<<EOF
<ul>
<li>not a tooltip</li>
<li class="tooltip">tooltip</li>
<li class="tooltip">also a tooltip</li>
</ul>
EOF
);
echo count($html->find('li.tooltip'));
// 2