webster Posted July 25, 2008 Share Posted July 25, 2008 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 https://forums.phpfreaks.com/topic/116527-solved-xml-in-php-how-to-display-range-of-records/ Share on other sites More sharing options...
TransmogriBenno Posted July 25, 2008 Share Posted July 25, 2008 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 https://forums.phpfreaks.com/topic/116527-solved-xml-in-php-how-to-display-range-of-records/#findComment-599185 Share on other sites More sharing options...
webster Posted July 25, 2008 Author Share Posted July 25, 2008 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 https://forums.phpfreaks.com/topic/116527-solved-xml-in-php-how-to-display-range-of-records/#findComment-599437 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.