#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    25
    Rep Power
    0

    Count of XML Nodes using 1 Variable


    I have an xml with 20 child and each child has 4 nodes. However, when I combined into a single variable $result, it returns 1. What is the correct way to display the count?

    ---result.php---

    foreach ($category->Child as $child)
    {
    $result.="
    ".'<p><h3><strong>Title: </strong>'.$$title.'</a></p><p><strong>Description: </strong>'.$desc.'</p><p><strong>Popularity: </strong>'.$popular.'</p><p><strong>Date Released: </strong>'.$date.'</p>'."
    ";
    }

    echo count($result);


    ----output in browser----

    11111111111111111111

    --------------
    Appreciate anybody help please.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,454
    Rep Power
    594
    Last edited by gw1500se; December 2nd, 2012 at 07:08 AM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    25
    Rep Power
    0
    Not possible. The $result is not a node element. it consists of a combination of 4 child nodes. Xpath also cannot work.

    I tried to set $result into array() and try count from there but still cannot.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,454
    Rep Power
    594
    Then you need to provide more information. What are you using to parse the XML? Show all the related code. Also use [ PHP ] tags for your code (see the sticky at the top of this forum). Please see ManiacDan's New User Guide.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,054
    Rep Power
    9398
    1. You are correct in that $result is not a SimpleXMLElement object: $result is actually a string. And count() always returns 1 for a string.
    2. Try count() on the $category->Child.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    25
    Rep Power
    0
    Originally Posted by gw1500se
    Then you need to provide more information. What are you using to parse the XML? Show all the related code. Also use [ PHP ] tags for your code (see the sticky at the top of this forum). Please see ManiacDan's New User Guide.

    ---result.php---
    [php]

    $catalog = simplexml_load_file("catalog.xml");
    $category = $catalog->Category;

    foreach ($category->Child as $child)
    foreach ($child->Title as $Title)
    foreach ($site->Description as $desc)
    foreach ($site->Popularity as $popular)
    foreach ($site->Date as $date)
    {
    $result.="
    ".'<table border="1" width="100%"><tbody><tr><td>'."
    ".'<p><h3><strong>Title: </strong>'.$title.'</a></p><p><strong>Description: </strong>'.$desc.'</p><p><strong>Popularity: </strong>'.$popular.'</p><p><strong>Date Released: </strong>'.$date.'</p>'."
    ".'</td></tr></table>'."
    ";
    }

    $rpp = 10; // results per page
    $adjacents = 4;

    $page = intval($_GET["page"]);
    if($page<=0) $page = 1;

    // connect to your DB:
    $link_id = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
    mysql_select_db($dbname, $link_id);

    // select appropriate results from DB:
    $sql = "SELECT * FROM table_name WHERE table_field LIKE '%search string%' ORDER BY field_name";
    $result = mysql_query($sql, $link_id);

    // count total number of appropriate listings:
    $tcount = mysql_num_rows($result);

    // count number of pages:
    $tpages = ($tcount) ? ceil($tcount/$rpp) : 1; // total pages, last page number

    $count = 0;
    $i = ($page-1)*$rpp;
    while(($count<$rpp) && ($i<$tcount)) {
    mysql_data_seek($result,$i);
    $query = mysql_fetch_array($result);

    // output each row:
    echo "<p>" . $query['table_field_1'] . ", " . $query['table_field_2'] . "</p>\n";

    $i++;
    $count++;
    }

    // call pagination function:
    include("paginate.php");
    echo paginate_three($reload, $page, $tpages, $adjacents);


    ?>

    ----------------
    I am trying to do a pagination on XML but since most of the examples online are using SQL/Database(as highlighted in Blue), hence I am trying to convert the display result of child nodes of $child into a single variable (string).

    if I do an echo $result, the display is correct that display 20 result but in order to use pagination, I need to get the count of $result out. Unless you have a better idea.

    Appreciate your advise so far.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    25
    Rep Power
    0
    Originally Posted by requinix
    1. You are correct in that $result is not a SimpleXMLElement object: $result is actually a string. And count() always returns 1 for a string.
    2. Try count() on the $category->Child.

    Hi, I tried that count($child) it is ok, but the count I am trying to get is still $result, reason as shown in above post, trying to do a pagination. If you can advise any better way to do it, thanks.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,454
    Rep Power
    594
    I don't understand why you think it is not possible. '$result' is an XML string so use simplexml_load_string on '$result' and then use 'count' on that object.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  16. #9
  17. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,054
    Rep Power
    9398
    Originally Posted by wargamon
    Hi, I tried that count($child) it is ok, but the count I am trying to get is still $result
    Why try to paginate based on $result when it's much easier to paginate based on the <Child>? $result doesn't have any kind of dynamic structure or unusual logic: if you know the numbers for the <Child> then you know the numbers for $result (whatever those numbers are).
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    25
    Rep Power
    0
    Originally Posted by gw1500se
    I don't understand why you think it is not possible. '$result' is an XML string so use simplexml_load_string on '$result' and then use 'count' on that object.
    Hi, I tried already hence I can say it is not possible.

    1. If I use $child, count($child) is possible because it is still in XML node mode.

    2. I combined 4 different node values into $result, it is already move out of XML mode. $result is a string but does not belong to an object in XML.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    25
    Rep Power
    0
    Originally Posted by requinix
    Why try to paginate based on $result when it's much easier to paginate based on the <Child>? $result doesn't have any kind of dynamic structure or unusual logic: if you know the numbers for the <Child> then you know the numbers for $result (whatever those numbers are).
    Hi, you are right. It would be much easier to work on a already known object which is the XML node values. I did tried but I still cannot display the childnode values of $child, if you can advise me how to display childnode values, like do I loop again in the foreach ($category->Child as $child) { Loop the childnode }
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,454
    Rep Power
    594
    Originally Posted by wargamon
    I combined 4 different node values into $result, it is already move out of XML mode. $result is a string but does not belong to an object in XML.
    But you can make it an XML object with my suggestion. That was the point. However, as requinix has pointed out you probably need to rethink your pagination.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    25
    Rep Power
    0
    Thanks all. I found the solution.

IMN logo majestic logo threadwatch logo seochat tools logo