![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
SimpleXML issues
I have an XML document describing a section of a site I'm coding. I'm already using it to generate a page, but now I need to also use it to generate a navigation bar. The documents is structured like so:
<downloads> <category name="blah"> <section name="blah"> <file name="blah" size="somenumber" link="someurl" /> </section> </category> <category name="blah2> ...etc function generateNav()
{
$data = simplexml_load_file("data/downloads.xml");
$out = "";
for($i = 1; $i <= count($data->category); $i++)
{
$out .= "<a href=\"downloads.php?c=$i\">".$data->category[$i]["name"]."</a> ";
}
return $out;
} |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Consider using print_r to see what you get for $data and its constituents. Information is key to debugging. Information is there for the viewing, if you look.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programmer
|
Hmm...print_r shows the document tree the way I expected it to- category[0] through category[3]. Yet count($data->category) still says there's 1 element, when there should be 4. Is there a better way to find the number of elements in the array?
I also tried foreach(). The problem with that is that I need the array index to correctly generate the navigation link. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Can you post here what you get with the print_r, as well as the class definition for data?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Programmer
|
$data isn't a class per se - instead, SimpleXML parses the XML into a structure that can be sort of read like a class. First attachment is the print_r output, second is some sample data structured the same way.
|
|
|
|
|
|
#6 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
First off, arrays are based at 0. Your loop should not begin at 1.
Okay, here is my take on the rest, and my fix. Please understand that I am not a PHP expert. While $data->category is an array, I believe it is being treated, first, as an object. Count will not work on objects, but will return a 1. (There is a method of making objects countable, but I didn't delve into that.) In order to get around the problem, I used foreach to copy 'category' into another array. I then got the count from that array, and produced the links. Here is my code: <?php
function generateNav()
{
$data = simplexml_load_file("data.xml");
foreach ($data->category as $thangy) $myArray [] = $thangy;
$out = "";
for($i = 0; $i < count ($myArray); $i++)
$out .= "<a href=\"downloads.php?c=$i\">".$data->category[$i]["name"]."</a><br/> ";
return $out;
}
echo generateNav ();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Huh!</title>
</head>
<body>
</body>
</html>Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#7 |
|
Programmer
|
For some reason your original foreach didn't work, so it becomes:
function generateNav()
{
$data = simplexml_load_file("data/downloads.xml");
$out = "";
$i = 0;
foreach ($data->category as $cat)
{
$categories[$i++] = $cat;
}
for($j = 0; $j < count ($categories); $j++)
{
$k = $j + 1;
$out .= "<a href=\"downloads.php?c=$k\">".$data->category[$j]["name"]."</a><br/> ";
}
return $out;
} |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Worked for me. That's how it produced the output. You can invoke the page (code shown above) here. Perhaps you didn't renew your right to use the variable, $thangy (tm DaWei).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Programmer
|
Aha, my browser somehow cut out the [] after $myArray.
|
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Here's a way that doesn't waste time copying the array:
$i = 0;
foreach ($data->category as $cat) $i++;
for ($j = 0; $j < $i; $j++)
{
blah blah do da stuff...
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|