I have this php codes:
$main_url = "http://www.sports-reference.com/olympics/countries/DEN/summer/1896/";
$main_html=file_get_html($main_url);
$link = $main_html->getElementById('div_sports');
foreach ($link->find('td') as $element){
foreach($element->find('href') as $node){
echo $node->item(0)->nodeValue . "\n";
//$link_clean = $node->getAttribute('href');
echo $link_clean . "\n";
}
}
If I print out $element, I get this output:
<td align="left" >Athletics</td>
<td align="left" >Fencing</td>
<td align="left" >Gymnastics</td>
<td align="left" >Shooting</td>
<td align="left" >Weightlifting</td>
I need to extract this info:
/olympics/countries/DEN/summer/1896/ATH/
/olympics/countries/DEN/summer/1896/FEN/
..........
and so on. the code above is not working. CAn you helpme?
href is not a tag, it is a tag attribute.
So, you have to search for <a>:
foreach( $element->find('a') as $a)
{
echo $a->href . "\n";
(...)
}
Related
I am working on a PHP Simple DOM Parser and i want a simple solution for my question
<tr>
<td class="one">1</td>
<td class="two">2</td>
<td class="three">3</td>
</tr>
<tr>
<td class="one">10</td>
<td class="two">20</td>
<td class="three">30</td>
</tr>...
the html of mine is will look similar to the above
and i am looping over through td something like this
foreach ($sample->find("td") as $ele)
{
if($ele->class == "one")
echo "ONE = ".$ele->plaintext;
if($ele->class == "two")
echo "TWO= ".$ele->plaintext;
}
But is there any simple solution that without if condition getting the plaintext of particular class i dont want shorthand if also
I am expecting something like this below
$ele->class->one
take a look at it:
<?php
$html = "
<table>
<tr>
<td class='one'>1</td>
<td class='two'>2</td>
<td class='three'>3</td>
</tr>
<tr>
<td class='one'>10</td>
<td class='two'>20</td>
<td class='three'>30</td>
</tr>
</table>
";
// Your class name
$classeName = 'one';
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
// Get the results
$results = $xpath->query("//*[#class='" . $classeName . "']");
for($i=0; $i < $results->length; $i++) {
echo $review = $results->item($i)->nodeValue . "<br>";
}
?>
for ($i=0; $i<=$lines; $i++)
{
//get each line and exlplode it..
$part = explode('|', $file[$i]);
//now start printing ..
echo'<tr>
<td width="20%">'.$part[0].'</td>
<td width="20%">'.$part[1].'</td>
<td width="20%">'.$part[2].'</td>
<td width="20%">'.$part[3].'</td>
<td width="20%">'.$part[4].'</td>
</tr>';
}
This is my code, it read's from a text file and explode in table, but I have a little problem here cause this one needs to be link.
<td width="20%">'.$part[2].'</td>
.$part[2]. is just a word from file but it has query like www.somesite.com/?q= There at the end I need to have that
word from file
that kind of code did not work for me
<td width="20%"> <a herf='www.somesite.com/?q=''.$part[2].'> '.$part[2].' </a> </td>
I realy need some help with this...
<?php
//first, get the file...
$file = file('req.txt');
//now count the lines ..
$lines = count($file);
//start the table here..
echo'<table border="2" width="100%">';
echo'<tr>
<td width="20%">Naslov</td>
<td width="20%">Vrsta</td>
<td width="20%">IP</td>
<td width="20%">Dodano (DD.MM.YY - HH.MM)</td>
<td width="20%">Status</td>
</tr>';
//start the loop to get all lines in the table..
for ($i=0; $i<=$lines; $i++) {
//get each line and exlplode it..
$part = explode('|', $file[$i]);
//now start printing ..
echo'<tr>
<td width="20%">'.$part[0].'</td>
<td width="20%">'.$part[1].'</td>
<td width="20%">'.$part[2].'</td>
<td width="20%">'.$part[3].'</td>
<td width="20%">'.$part[4].'</td>
</tr>';
}
//close the table so HTML wont suffer :P
echo'</table>';
?>
This should produce this but ip column need to be link...
I think vprintf() is your friend.
<?php
$fmt = '<tr>
<td>%1$s</td>
<td>%2$s</td>
<td>%3$s</td>
<td>%4$s</td>
<td>%5%s</td>
</tr>';
for ($i=0; $i<=$lines; $i++)
{
// get each line and explode it..
$part = explode('|', $file[$i]);
// now start printing ..
vprintf($fmt, $part);
}
And put the width="20%" into your CSS.
I solve it alone with changing some values in input script "file writer"
$savestring = $title . "|" . $genre . "|<a href=http://www.example.com/ip?ip=" . $ip . ">" . $ip . "|" . $date . "|Za Naložit \n";
it works now ty anyway :)
I have this website here: http://www.wdjc.de/euro/index.php
its not like I only look for the solution without doing anything for myself,
but how every I turn this around I get stuck by the the
endpoint.
From my 2 ideas I normaly would build the script like this:
<?
$ch = curl_init ("http://www.wdjc.de/euro/index.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
preg_match('#<table[^>]*>(.+?)</table>#is', $page, $matches);
foreach ($matches as &$match) {
$match = $match;
}
echo '<table>';
echo $matches[1];
echo '</table>';
?>
this does not work at all it only shows me the table head.
so I got the idea to build this one:
<?
$handle = fopen('http://www.wdjc.de/euro/index.php', 'r');
while (!feof($handle))
{
$html .= fread($handle, 4096);
}
$begin = ' <table width="900" class="clean">';
$end = '</table>';
$beginloc = strpos($html, $begin) + strlen($begin);
$endloc = strpos($html, $end);
$html = substr($html, $beginloc, $endloc - $beginloc);
echo "<table width=100%>";
$html = str_replace("</tr>", "</tr>\n", $html);
$html = str_replace("</td>", "</td>\n", $html);
$html = str_replace("<td width", "<td nowrap ", $html);
echo $html;
echo "</table>";
?>
It also does not work because of the end point which is after every colum.
But the trick of this page is they use double tables:
<table width="900" class="clean"> <tr> <td align="center">
<br><br>
<table width="880">
<tr>
<td width="60" align="left">POSITION</td>
<td width="40"></td>
<td width="400" align="left">ARTIST<br>TITLE</td>
<td width="90" align="center">LAST<br>WEEK</td>
<td width="90" align="center">HIGHEST<br>POSITION</td>
<td width="90" align="center">WEEK</td>
<td width="40" align="center">COVER</td>
<td width="70" align="right">SHOP</td>
</tr>
</table>
<hr>
<table width="880" align="center">
...
Does anybody has an idea?
I have the following within my foreach....this works but there are 2 sets of values per $value and it only shows the first
<?php
$info = simplexml_load_file("https://api.website.co.za/ACCESS_GetAccountSessions?");
echo "<ul info>";
foreach ($info->sessions as $sessions):
$count = $sessions->{'session-count'};
$ip = $sessions->session->{'ip-address'};
$nas = $sessions->session->{'nas-ip-address'};
$port = $sessions->session->{'nas-port'};
$phone = 'N/A';
?>
<?php
echo '<tr>',
'<td class="blockcontentwhite sessionicon">',
'<td class="blockcontentwhite center">' . ("$ip") . '</td>',
'<td class="blockcontentwhite center">' . ("$nas") . '</td>',
'<td class="blockcontentwhite center">' . ("$port") . '</td>',
'<td class="blockcontentwhite center">' . ("$phone") . '</td>',
'</tr>';
endforeach;
?>
so basically within a table on 1st row its showing the $ip , $nas, $port and $phone BUT it is not showing the second values on the 2nd row , any ideas?
Thanks Guys
You are trying to access an object as an array. $info->sessions is of type SimpleXMLElement Object which contains (as a property) the array you want to use in the foreach.
Change your foreach ( foreach ($info->sessions as $sessions): ) to foreach ($info->sessions->session as $sessions): and:
$count = $sessions->{'session-count'};
$ip = $sessions->session->{'ip-address'};
$nas = $sessions->session->{'nas-ip-address'};
$port = $sessions->session->{'nas-port'};
$phone = 'N/A';
to:
$count = $info->sessions->{'session-count'};
$ip = $sessions->{'ip-address'};
$nas = $sessions->{'nas-ip-address'};
$port = $sessions->{'nas-port'};
$phone = 'N/A';
I think Jueecy is correct regarding the question you asked about.
But I noticed something that looks like it might be a problem besides that. It looks like the code will generate multiple tables, but I think you just want multiple rows in the same table.
I would suggest moving your foreach down to where you are generating the rows:
<?php
$info = simplexml_load_file("https://api.website.co.za/ACCESS_GetAccountSessions?");
echo "<ul info>";
?>
<input type="hidden" name="ctl00$ctl00$contentDefault$contentControlPanel$hdnIsSecure" id="ctl00_ctl00_contentDefault_contentControlPanel_hdnIsSecure" value="false" />
<table id="active_sessions_table" class="blocktable centered" cellpadding="0" cellspacing="0">
<tr class="blockheader">
<td id="active_sessions_title" class="left" colspan="2">
<label class="floatleft">Current connections</label></td>
</tr>
<tr id="trconnections">
<td class="blockcellnopadding" colspan="2">
<table cellpadding="4" cellspacing="0" width="100%" style="border-bottom:solid 1px #bfbfbf;">
<tr>
<td class="columntitle center"> </td>
<td class="columntitle center">IP Address</td>
<td class="columntitle center">NAS IP Address</td>
<td class="columntitle center">Line Port</td>
<td class="columntitle center">Telephone Number</td>
</tr>
<?php
foreach ($info->sessions->session as $session) {
$ip = $session->{'ip-address'};
$nas = $session->{'nas-ip-address'};
$port = $session->{'nas-port'};
$phone = 'N/A';
echo '<tr>',
'<td class="blockcontentwhite sessionicon">',
'<td class="blockcontentwhite center">' . ("$ip") . '</td>',
'<td class="blockcontentwhite center">' . ("$nas") . '</td>',
'<td class="blockcontentwhite center">' . ("$port") . '</td>',
'<td class="blockcontentwhite center">' . ("$phone") . '</td>',
'</tr>';
};
?>
Problem:
To style the last row in a SQL query without using any CSS3 selectors.
PHP code:
while ($item = mysql_fetch_assoc($itemsresult))
{
$answer = "SELECT IID, Comment FROM betyg_answers WHERE EID = '{$EID}' AND CID = '{$item['CID']}' ORDER BY ID ASC";
$answerresult = mysql_query($answer) or die ('Error (' . mysql_errno() . ') ' . mysql_error());
$answer = mysql_fetch_assoc($answerresult);
if ($answer['IID'] == $item['IID'])
{
$html .= '
<tr>
<td class="arrow" style="'.$arrow.'"><img src="./img/circle_arrow_right.png" class="arrowimage"></td>
<td class="numbers" style="'.$numbers.'">'.$itemcounter.'.</td>
<td class="text" style="'.$text.'">'.$item['Description'].'</td>
</tr>
';
}
else
{
$html .= '
<tr>
<td class="arrow" style="'.$arrow.'"> </td>
<td class="numbers" style="'.$numbers.'">'.$itemcounter.'.</td>
<td class="text" style="'.$text.'">'.$item['Description'].'</td>
</tr>
';
}
$itemcounter++;
}
Last row in SQL query should instead print:
$html .= '
<tr>
<td class="arrow" style="'.$lastarrow.'"> </td>
<td class="numbers" style="'.$lastnumbers.'">'.$itemcounter.'.</td>
<td class="text" style="'.$lasttext.'">'.$item['Description'].'</td>
</tr>
';
Question:
What needs to be added in the while loop so it recognizes the last row and print a different code instead?
Use a counter:
$i = 0;
$c = mysql_num_rows($itemresult);
while ($item = mysql_fetch_assoc($itemsresult)) {
$i++;
if ($i == $c) {
// This is a last row
} else {
// Regular row
}
}
there is several ways to do that :
use :last-child ( but this isn't working in IE8)
table tr:last-child {
background-color:#ff0000;
}
using jQuery method ( this is browser independent)
in document load function, add following jQuery code,
$("td:last-child").css({background-color:"#ff0000"})