This what my code looks like:
$i=0;
$x=0;
$y=0;
// Load the HTML string from file and create a SimpleXMLElement
$html_string = file_get_contents("data/csr.html");
$root = new SimpleXMLElement($html_string);
// Fetch all div, h2 and span values
$divArray = $hdlsArray = $dtlsArray = array();
foreach ($xml->div as $div) {
$divArray[$x] = $div;
$x++;
}
foreach ($xml->h2 as $h2) {
$hdlsArray[$y] = $h2;
$y++;
}
foreach ($xml->span as $span) {
$dtlsArray[$z] = $span;
$z++;
}
When I run it, I get:
Warning: Invalid argument supplied for foreach() in C:\xamps older\xampp167\htdocs\test\myphp\CSR Demo pack\csr\output.php on line 58
Warning: Invalid argument supplied for foreach() in C:\xamps older\xampp167\htdocs\test\myphp\CSR Demo pack\csr\output.php on line 62
Warning: Invalid argument supplied for foreach() in C:\xamps older\xampp167\htdocs\test\myphp\CSR Demo pack\csr\output.php on line 66
Thanks in advance.
Foreach problem
Moderator: General Moderators
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Foreach problem
That's because there is no variable $xml. Maybe you meant $root?
FYI, you can get rid of all the $x, $y, and $z:
-Shawn
FYI, you can get rid of all the $x, $y, and $z:
Code: Select all
foreach ($root->div as $div) {
$divArray[] = $div;
}mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: Foreach problem
I cant get the data between the tags into the arrays:
// Load the HTML string from file and create a SimpleXMLElement
$html_string = file_get_contents("data/csr.html"); /*the string really is in $html_string*/
$root = new SimpleXMLElement($html_string);
/*Problem starts here when I try to get that the value between the tags: div, h2 and span into an array*/
// Fetch all div, h2 and span values
$divArray = $hdlsArray = $dtlsArray = array();
foreach ($root->div as $div) {
$divArray[] = $div;
echo "".$div."<br />";
}
foreach ($root->h2 as $h2) {
$hdlsArray[] = $h2;
echo "".$h2."<br />";
}
foreach ($root->span as $span) {
$dtlsArray[] = $span;
echo "".$span."<br />";
}
The result of this is a blank page instead of printing the actual tag data
// Load the HTML string from file and create a SimpleXMLElement
$html_string = file_get_contents("data/csr.html"); /*the string really is in $html_string*/
$root = new SimpleXMLElement($html_string);
/*Problem starts here when I try to get that the value between the tags: div, h2 and span into an array*/
// Fetch all div, h2 and span values
$divArray = $hdlsArray = $dtlsArray = array();
foreach ($root->div as $div) {
$divArray[] = $div;
echo "".$div."<br />";
}
foreach ($root->h2 as $h2) {
$hdlsArray[] = $h2;
echo "".$h2."<br />";
}
foreach ($root->span as $span) {
$dtlsArray[] = $span;
echo "".$span."<br />";
}
The result of this is a blank page instead of printing the actual tag data