Reputation: 2771
I'm trying get values from a shoutcast songhistory (link).
The markup of page have 2 tables like:
<table>
<tbody>
<tr>
<td>Header Link 1</td>
<td>Header Link 2</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr> <td>Lengh of song 1</td> </tr>
<tr> <td>Song Title 1</td> </tr>
<tr> <td>Lengh of song 2</td> </tr>
<tr> <td>Song Title 2</td> </tr>
</tr>
</tbody>
</table>
I only need get the song title and save this on a database.
This is my code:
<?php
include_once('simple_html_dom.php');
ini_set('display_errors', true);
error_reporting(E_ALL);
$host="localhost";
$username="root";
$password="";
$database="titulos";
mysql_connect($host,$username,$password);
mysql_select_db($database) or die( "No se puede conectar a la base de datos");
$html = file_get_html('http://138.36.236.207:8000/played.html');
$guardardato = "";
// Buscar
foreach($html->find('table', 2)->find('tr', 2) as $datossc) {
foreach($datossc->find('td') as $titulo) {
echo $titulo->plaintext .'<br>';
$guardardato .= $titulo->plaintext;
}
}
$guardardato = mysql_real_escape_string($guardardato);
$query = "INSERT INTO data(name) VALUES('$guardardato')";
mysql_query($query) or die(mysql_error());
$html->clear();
unset($html);
?>
The sql process is ok, but the simple dom does work..
I get that error:
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\proyectos\radioargenta\oyentes\indexprueba.php on line 19
Can help me?
Thanks!
Upvotes: 0
Views: 2257
Reputation: 3692
You have to change the for
loop as follows:
foreach($html->find('table', 2)->find('tr') as $datossc) {
echo $datossc->find('td', 1)->plaintext .'<br>';
$guardardato .= $datossc->find('td', 1)->plaintext;
}
Note that you will have Song Title
in the output as well.
Also, are you sure that you want to append all titles in $guardardato
? This will just concatenate the titles, e.g. Song TitleAle Ceberio - LaCuartetera.NetAlcides - Tan bonita pero muy celosaAgrupaciOn Marylin - Agru...
. Perhaps what you want to do is:
$guardardato = array();
foreach($html->find('table', 2)->find('tr') as $datossc) {
$title = $datossc->find('td', 1)->plaintext;
if ($title != 'Song Title') {
echo $title .'<br>';
$guardardato[] = $title;
}
}
foreach($guardardato as $i) {
$value = mysql_real_escape_string($i);
$query = "INSERT INTO data(name) VALUES('" . $value . "')";
mysql_query($query) or die(mysql_error());
}
Upvotes: 1