Search code examples
phppaginationsubmitsimplexml

SimpleXML Paginate after Submit


I need to build a pagination with php simpleXML after clicking on a submit button.

This is my xml file

 <events>
        <event_data>
           <id>1</id>
       <name>Club 1</name>
        </event_data>

        <event_data>
            <id>2</id>
        <name>Club 2</name>
        <event_data>

        <event_data>
            <id>3</id>
            <name>Club 3</name>
        </event_data>

        <event_data>
            <id>4</id>
            <name>Club 4</name>
        <event_data>

        <event_data>
            <id>5</id>
            <name>Club 5</name>
        </event_data>
 <events>

my php file is this:

<form action="" method="post">
    <div class="select_club">
        <select name="clubname" class="club">
            <option value=""> Club auswählen</option>
            <option value="Club 1">Club 1</option>
            <option value="Club 2">Club 2</option>
            <option value="Club 3">Club 3</option>
            <option value="Club 4">Club 4</option>
            <option value="Club 5">Club 5</option>
        </select>
    </div>
    <input type="submit" name="submitClub" value="Submit" />
</form>

<?php

?>

<div id="eventList">
<?php 
    $startPage = $_GET['page'];
    $perPage = 3;
    $currentRecord = 0;

    $sxe = simplexml_load_file('events.xml');  

    if($sxe) {
        if(isset($_POST['submitClub'])) {
            foreach($sxe->events->event_data as $key => $value) {
                $currentRecord += 1;
                if($currentRecord > ($startPage * $perPage) && $currentRecord < ($startPage * $perPage + $perPage)){

                    echo $value->name;
                    echo "<br>";
                }
            }
            //and the pagination:
            for ($i = 1; $i <= ($currentRecord / $perPage); $i++) {
                echo("<a href='http://hasselbach.rpdweb.de/test?page=".$i."'>".$i."</a>");
            }
        } else {
            echo 'Keine Eevnts vorhanden';
        }
    } else {
        echo 'Datei könnte nicht geöffnet oder gefunden werden!';
    }
?>

</div>

If I do it like in the code above I get the first 3 results but after i click on page 2 i get an empty page. When I delete the submit part in my php file everything works find.

It would be awesome if someone can help me or give me a hint.

I also googled my problem but nothing fitted to my problem.

Thanks for everyone's help


Solution

  • I think your problem become from this line :

    if(isset($_POST['submitClub'])) {
    

    When you call the second page you don't post the form and the $_POST['submitClub'] is not set.

    You can try this :

    <?php 
    $startPage = $_GET['page'];
    $perPage = 3;
    $currentRecord = 0;
    
    $sxe = simplexml_load_file('events.xml');  
    
    if($sxe) {
    
        $submitClub = null;
        if (isset($_POST['submitClub'])) {
            $submitClub = $_POST['submitClub'];
        } elseif (isset($_GET['submit-club'])) {
            $submitClub = urldecode($_GET['submit-club']);
        }
    
        if(!is_null($submitPost)) {
            foreach($sxe->events->event_data as $key => $value) {
                $currentRecord += 1;
                if($currentRecord > ($startPage * $perPage) && $currentRecord < ($startPage * $perPage + $perPage)){
    
                    echo $value->name;
                    echo "<br>";
                }
            }
            //and the pagination:
            for ($i = 1; $i <= ($currentRecord / $perPage); $i++) {
                echo("<a href='http://hasselbach.rpdweb.de/test?page=".$i."&submit-club=".urlencode($submitClub)."'>".$i."</a>");
            }
        } else {
            echo 'Keine Eevnts vorhanden';
        }
    } else {
        echo 'Datei könnte nicht geöffnet oder gefunden werden!';
    }