0

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

My XML file looks like this:

<events>
    <event_data>
    <club>Club 1</club>
    <categoryid>10</categoryid>
    <category>WellFit®</category>
    <startdate realdate="2013-08-24">1377295200</startdate>
    <enddate realdate="2013-09-07">1378504800</enddate>
    </event_data>

    <event_data>
    <club>Club 2</club>
    <categoryid>9</categoryid>
    <category>Golf</category>
    <startdate realdate="2013-08-24">1377295200</startdate>
    <enddate realdate="2013-09-07">1378504800</enddate>
    </event_data>

    <event_data>
    <club>Club 3</club>
    <categoryid>14</categoryid>
    <category>Tennis</category>
    <startdate realdate="2013-08-24">1377295200</startdate>
    <enddate realdate="2013-09-07">1378504800</enddate>
    </event_data>

    ……

</events>

And my php file is this:

<form action="" method="post">
    <div id="slider-range"></div>
    <input type="text" name="startdate" style="margin:40px 0 0">
    <input type="text" name="enddate" style="margin:40px 0 0">

    <div class="chembox_items" style="padding:5px 0 0;">
        <div class="golf">
            <input type="checkbox" name="checkname[]" value="1">
            <span>Golf</span>
        </div>
        <div class="wassersport">
            <input type="checkbox" name="checkname[]" value="9">
            <span>Wassersport</span>
        </div>
        <div class="wellfit">
            <input type="checkbox" name="checkname[]" value="10">
            <span>WellFit®</span>
        </div>
        <div class="tennis">
            <input type="checkbox" name="checkname[]" value="14">
            <span>Tennis</span>
        </div>
    </div>
    <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>
        </select>
    </div>
    <input type="submit" name="submitClub" value="Submit" />
</form>

<?php

?>

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

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

    if($sxe) {

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

            //for checkbox
            foreach($_POST['checkname'] as $key => $box){
                $checkbox = $_POST['checkname'][$key] = $box;
            }
            $varClub = $_POST['clubname'];
            //echo $varClub;    
            $varStart = $_POST['startdate'];
            $varEnd = $_POST['enddate'];

            if($varStart){

                $start = strtotime($varStart);
                $end = strtotime($varEnd);

                $xpath = sprintf(
                    '//event_data[
                    (%1$d >= startdate and %2$d <= enddate)
                    or
                    (%1$d <= enddate and %2$d >= startdate)
                    ]',
                    strtotime($varStart),
                    strtotime($varEnd)
                );

                foreach($sxe->xpath($xpath) as $item){
?>
                    <div class="item <?php echo $item->categoryid ?> clearfix">
                        <div class="left">
                            <div class="header_data">
                                <h2><?php echo $item->name ?></h2>
                                <div class="date"><?php 
                                    $startdate = $item->startdate->attributes()->realdate;
                                    $enddate = $item->enddate->attributes()->realdate;
                                    echo date("d.m.Y", strtotime($startdate)) . ' - ' . date("d.m.Y", strtotime($enddate)); 
                                ?></div>
                                <div class="club"><?php echo $item->club ?></div>
                            </div>
                        </div>
                    </div>
<?php
                }                   
            }               

            if($checkbox) { 
                foreach($sxe->xpath('//event_data') as $item) {
                    $row = simplexml_load_string($item->asXML()); 

                    $v = $row->xpath('//categoryid[. ="' . $checkbox . '"]'); 

                    if($v[0]){
?>
                        <div class="item <?php echo $item->categoryid ?> clearfix">
                            <div class="left">
                                <div class="header_data">
                                    <h2><?php echo $item->name ?></h2>
                                    <div class="date"><?php 
                                        $startdate = $item->startdate->attributes()->realdate;
                                        $enddate = $item->enddate->attributes()->realdate;
                                        echo date("d.m.Y", strtotime($startdate)) . ' - ' . date("d.m.Y", strtotime($enddate)); 
                                    ?></div>
                                    <div class="club"><?php echo $item->club ?></div>
                                </div>
                            </div>
                        </div>
<?php               }
                }
            }           

            //For selectbox

            else if($varClub) { 
                foreach($sxe->xpath('//event_data') as $item) {
                    $row = simplexml_load_string($item->asXML()); 

                    $v = $row->xpath('//club[. ="' . $varClub . '"]'); 
                    if($v[0]){ 
?>
                        <div class="item <?php echo $item->categoryid ?> clearfix">
                            <div class="left">
                                <div class="header_data">
                                    <h2><?php echo $item->name ?></h2>
                                    <div class="date"><?php 
                                        $startdate = $item->startdate->attributes()->realdate;
                                        $enddate = $item->enddate->attributes()->realdate;
                                        echo date("d.m.Y", strtotime($startdate)) . ' - ' . date("d.m.Y", strtotime($enddate)); 
                                    ?></div>
                                    <div class="club"><?php echo $item->club ?></div>
                                </div>
                            </div>
                        </div>

<?php               }
                } 
            } else {
                echo 'Keine Events';
            }
        }


    } else {
        echo '<p>Die Datei konnte nicht geöffnet werden</p>';
    } 
?>
</div>

This code is doing the following: A user has 3 submitting options.

  1. you can submit a date range and then all events with your sumbitted date range are displayed.
  2. you can submit a checkbox then all events with the value of the checkbox are displayed and
  3. you can submit selectfield with the club name, then all events of the chosen club are displayed.

So after the user submitted one of the 3 cases above, all events for the chosen option are displayed. It might be that a result has 20 events which are now displayed on one site.

So instead of listing the events on one page, I want to paging through it.

For example: The submitted club has 20 events and on each page 5 events should be displayed so that we have 4 pages.

I hope it is clear what I want to achieve.

I appreciate everyones help :)

4

0 回答 0