Jump to content

[SOLVED] xml in php how to display range of records????


webster

Recommended Posts

Hello,

 

Any one can help me to modify this code to display only selected range of records instead all records.

 

I want to make this code display records from 10 to 20

 

<?php

  $doc = new DOMDocument();

  $doc->load( 'books.xml' );

 

  $books = $doc->getElementsByTagName( "book" );

  foreach( $books as $book )

  {

  $authors = $book->getElementsByTagName( "author" );

  $author = $authors->item(0)->nodeValue;

 

  $publishers = $book->getElementsByTagName( "publisher" );

  $publisher = $publishers->item(0)->nodeValue;

 

  $titles = $book->getElementsByTagName( "title" );

  $title = $titles->item(0)->nodeValue;

 

  echo "$title - $author - $publisher\n";

  }

  ?>

 

 

---------------------

 

I modified this code using break;

 

<?php

 

$xml = new SimpleXMLElement(

' <C>

<J><N>1n</N><K>ships</K></J>

<J><N>2n</N><K>fisch</K></J>

<J><N>3n</N><K>wine</K></J>

<J><N>4n</N><K>wine1</K></J>

<J><N>5n</N><K>wine2</K></J>

<J><N>6n</N><K>wine3</K></J>

<J><N>7n</N><K>wine4</K></J>

</C>');

 

$vals = array();

RecurseXML($xml,$vals);

 

foreach($vals as $key=>$value)

print $value;

 

function RecurseXML($xml,&$vals,$parent="") {

 

$childs=0;

$child_count=-1; # Not realy needed.

$arr=array();

foreach ($xml->children() as $key=>$value) {

if(in_array($key,$arr)){$child_count++;}

else {$child_count=4;}

$arr[]=$key;

$k=($parent == "") ? "$key.$child_count" : "$parent.$key.$child_count";

$childs=RecurseXML($value,$vals,$k);

if($childs==0){$vals[$k]=(string)$value;}

 

if($child_count=="5"){break;} //display only first three records but how to display from 2 to 5

 

}

return $childs;

}

 

?>

 

-----------------

 

In javascript I can use this code (ie > 5 only)

 

x=xmldat.recordset;

for (i=2;i<=5;i++){

x.absoluteposition=i;

block2.innerHTM=block2.innerHTM+x('K')

}

</script>

 

page should contain form named as form1 and textarea named text and a button to call the function.

This example is not i'm asking for but to explain what i'm asking help for (xml in php and renge of records).

Please try on the 1st php code; it is bettter for me.

 

 

Regards,

M.Shair

Link to comment
Share on other sites

Please use code tags when inserting code.

 

Something like the following modifications should do the trick:

 

<?php
  $doc = new DOMDocument();
  $doc->load( 'books.xml' );
  
  /* MOD */ $book_num = 1;
  /* MOD */ $start = 11;
  /* MOD */ $end = 20;
  
  $books = $doc->getElementsByTagName( "book" );
  foreach( $books as $book )
  {
  $authors = $book->getElementsByTagName( "author" );
  $author = $authors->item(0)->nodeValue;

  $publishers = $book->getElementsByTagName( "publisher" );
  $publisher = $publishers->item(0)->nodeValue;

  $titles = $book->getElementsByTagName( "title" );
  $title = $titles->item(0)->nodeValue;
  /* MOD */ if ($book_num >= $start) echo "$title - $author - $publisher\n";
  /* MOD */ if (++$book_num > $end) break;
  }
  ?>

Link to comment
Share on other sites

Please use code tags when inserting code.

 

Something like the following modifications should do the trick:

 

*

 

Thank you for those great MODs; it works :). I tested it also with ajax xmlHttp request by sending $start and $end.

..
var url="getcd.php?s="+s+"&e="+e+"&sid="+Math.random();
..

 

but what do recommend for less server load "direct xml" in php or using "ajax",

I think the first method is good for indexing page text.

 

 

Thanks again.

 

Reagards,

M.Shair

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.