-4

I got the code below to split pages:

$files = glob('docs/*.xml');
$files = array_slice($files, ($page-1)*10, 10);
foreach ($files as $file){
$xml = new SimpleXMLElement($file, 0, true);
echo'
    <tr>
    <td>' . $xml->doctype . '</td>
    <td><a href="viewdoc.php?docname=' . basename($file, '.xml') . '&username='. $xml->startedby .'&myname='. $_SESSION['username'] .'">' . basename($file, '.xml') . '</a></td>
    <td><a href="viewprofile.php?name='. $xml->startedby .'">'. $xml->startedby .'</a></td>
    <td>'. $xml->date .'</td>
    <td>* * * * *</td>
    <td></td>
    </tr>
    ';
}

But now I'm not sure how can I add pages links (Such as 1 2 3 4 Next) to this code I've thought about adding ++ to variable but that didn't worked to me.

My question is how can I add pages links in loop way?

Thanks in advance.

EDIT: I forgot to tell you the all the stuff in echo suppost to be the page (10 files in 1 page)

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>Next</li>
</ul>
copypaste
  • 175
  • 1
  • 2
  • 13
  • 1
    I'd suggest to learn XLST. – Ondra Žižka Jul 06 '13 at 02:11
  • I would like use simple XML, but if you got answer for me, in any XML family language I would like that. – copypaste Jul 06 '13 at 02:23
  • SimpleXML is, IMO, for really trivial use cases. I'd assume your app will grow over time and sticking with SimpleXMLElement will become a pain. Try XSLT, and if you like it, it's a pleasure to work with it. Mostly :) – Ondra Žižka Jul 06 '13 at 02:25
  • 1
    This is not your code. You got it by someone after you've asked for it. This is not how Stackoverflow works. Take care to not turn into a [Help Vampire](http://meta.stackexchange.com/questions/19665/the-help-vampire-problem). – hakre Jul 07 '13 at 08:16

2 Answers2

1

Converting a comment to an answer.

I'd suggest to use XSLT. Afterall, it was developed for exactly the purpose you do - converting XML to other XML, or, in general, to a text.

PHP has quite usable implementation, and you'll get the advantage of externalized template - i.e. no mixing of PHP code and HTML.

You're solving pagination. That can be achieved using the document(...) function, which gives you a list of XML Document nodes, which you can use like:

<ul>
<xsl:foreach select="$paths">
   <li><xsl:value-of select="position()"/></li>
<ul>

Or so. The $paths is a parameter of the XSLT template, passed from PHP.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
1

You can do this in XSLT, but there's not reason to introduce another language when you just need a simple pager in your output. I'm going to assume for a moment that the $page variable is getting defined properly elsewhere in your code.

$perpage = 10; // Avoid magic numbers
$files = glob('docs/*.xml');
$file_count = count($files);
$pages = $file_count/$perpage;
$files = array_slice($files, ($page-1)*$perpage, $perpage);
foreach ($files as $file){
$xml = new SimpleXMLElement($file, 0, true);
echo'
    <tr>
    <td>' . $xml->doctype . '</td>
    <td><a href="viewdoc.php?docname=' . basename($file, '.xml') . '&username='. $xml->startedby .'&myname='. $_SESSION['username'] .'">' . basename($file, '.xml') . '</a></td>
    <td><a href="viewprofile.php?name='. $xml->startedby .'">'. $xml->startedby .'</a></td>
    <td>'. $xml->date .'</td>
    <td>* * * * *</td>
    <td></td>
    </tr>
    ';
}
?>
<ul>
<?php
for($i=1; $i<=$pages; $i++) {?>
    <li><a href="link_to_page.php?page=<?php echo $i?>"><?php echo $i?></a></li>
<?php } ?>
</ul>
acrosman
  • 12,814
  • 10
  • 39
  • 55
  • Thanks, bounty rewarded as soon as possible.. – copypaste Jul 06 '13 at 04:04
  • I'm afraid it a little bug, since I can't previews page if i got 20 files and i can't see 2nd page if i got 11 files, can you edit code so it will work? Thanks @acrosman – copypaste Jul 06 '13 at 04:11
  • StackOverflow isn't here to write all the code for you. Most likely it has to do with the handling of $page. As I noted above I assume you handle that some place. I don't pull the $page variable back out of the $_GET parameter, ensure a good minimum value, avoid off-by one errors (probably what you're seeing), or validate that it's even a number. That's your job. – acrosman Jul 06 '13 at 04:27