Manju
Manju

Reputation: 765

pagination without database

I am calling a trading api to display the trading history on client's web site,i have to display the response in multiple pages(pagination).I can not find the logic for the pagination without database.Can any body help me please.

My code is below

     <?php

 error_reporting(E_ALL);

$url = "https://www.myfxbook.com/api/login.json?";

$email="[email protected]";
$password="**********";


$url .= '&email='.$email;
$url .= '&password='.$password;

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$result = curl_exec($ch);
$propertyarr = json_decode($result, true);
$session = $propertyarr['session'];
$url2 = "https://www.myfxbook.com/api/get-history.json?";
$url2 .= 'session='.$session;
$url2 .= '&id=908596';

$ch2 = curl_init(); 
curl_setopt($ch2, CURLOPT_URL,$url2); 
curl_setopt($ch2, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);

$result2 = curl_exec($ch2);
$result2 = json_decode($result2);
$result2 = (array)$result2;


$newres = array();
foreach ($result2 as $res) {
            $newres  = $res;

    }
    var_dump($newres['0']);
    ?>
    <div class="trade_history">
    <div id="trade_titles" align="center" >
        <h>OPEN TIME</h>
        <h>CLOSE TIME</h>
        <h>OPEN PRICE</h>
        <h >CLOSE PRICE</h>
        <h>LOTS</h>
        <h>TYPE</h>
        <h>PAIR</h>
        <h>PIPS</h>
        </div>
    <?php
    //
    //echo '<table border="1">';

    for($i=0;$i<=sizeof($newres)-1;$i++)
    {

        ?>
        <div id="trade_data">
        <div id="opent_time">
                <?php echo $newres[$i]->openTime; ?>
                </div>
                <div id="close_time">
                <?php echo $newres[$i]->closeTime; ?>
                </div>
                <div id="opent_price">
                <?php echo $newres[$i]->openPrice; ?>
                </div>
                <div id="close_price">
                <?php echo $newres[$i]->closePrice; ?>
                </div>
                <div id="lots">
                <?php echo $newres[$i]->sizing->value; ?>
                </div>
                <div id="type">
                <?php echo $newres[$i]->action; ?>
                </div>
                <div id="pair">
                <?php echo $newres[$i]->symbol; ?>
                </div>
                <div id="pips">
                <?php echo $newres[$i]->pips; ?>
                </div>

                </div>
<?php


    }?>
    </div>

the current display of the response is like

enter image description here

can i display the result like fallowing

enter image description here

please help me any body...thanks in advance.

Upvotes: 1

Views: 1141

Answers (3)

Manju
Manju

Reputation: 765

thank you all for your quick replay.

Finally i got the logic.The code with the solution is below

<?php

//error_reporting(E_ALL);

$url = "https://www.myfxbook.com/api/login.json?";

$email="[email protected]";
$password="********";


$url .= '&email='.$email;
$url .= '&password='.$password;

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$result = curl_exec($ch);
$propertyarr = json_decode($result, true);
$session = $propertyarr['session'];
$url2 = "https://www.myfxbook.com/api/get-history.json?";
$url2 .= 'session='.$session;
$url2 .= '&id=908596';

$ch2 = curl_init(); 
curl_setopt($ch2, CURLOPT_URL,$url2); 
curl_setopt($ch2, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);

$result2 = curl_exec($ch2);
$result2 = json_decode($result2);
$result2 = (array)$result2;


$newres = array();
foreach ($result2 as $res) {
            $newres  = $res;

    }
    //var_dump($newres['0']);
    ?>
    <div class="trade_history">
    <div id="trade_titles" align="center" >
        <h>OPEN TIME</h>
        <h>CLOSE TIME</h>
        <h>OPEN PRICE</h>
        <h >CLOSE PRICE</h>
        <h>LOTS</h>
        <h>TYPE</h>
        <h>PAIR</h>
        <h>PIPS</h>
        </div>
    <?php
    if(empty($_GET['page_num']))
    {
        $page_num=1;
    }
    else
    {
    $page_num = $_GET['page_num'];
    }

    $i=$page_num*10;
    $srow = $i;
    $endrow = $srow-10;
    /*echo $srow;
    echo $endrow;*/
    //
    //echo '<table border="1">';

    for($i=$endrow;$i<=$srow-1;$i++)
    {

        ?>
        <div id="trade_data">
        <div id="opent_time">
                <?php echo $newres[$i]->openTime; ?>
                </div>
                <div id="close_time">
                <?php echo $newres[$i]->closeTime; ?>
                </div>
                <div id="opent_price">
                <?php echo $newres[$i]->openPrice; ?>
                </div>
                <div id="close_price">
                <?php echo $newres[$i]->closePrice; ?>
                </div>
                <div id="lots">
                <?php echo $newres[$i]->sizing->value; ?>
                </div>
                <div id="type">
                <?php echo $newres[$i]->action; ?>
                </div>
                <div id="pair">
                <?php echo $newres[$i]->symbol; ?>
                </div>
                <div id="pips">
                <?php echo $newres[$i]->pips; ?>
                </div>

                </div>
<?php


    }?>
    </div>

    <div class="pagination">
            <?php $numpages = count($newres)/10;
            $numpages = ceil($numpages);


            $Path='forex-signals';
            $URI='http://localhost/fx/'.$Path;
            $current = $page_num;
            $prev = $current-1;
            $next = $current+1;


                echo '<div id="first"><a href='.$URI.'?page_num=1#trade_history>First</a></div>';   
                echo '<div id="prev"><a href='.$URI.'?page_num='.$next.'#trade_history>Previous</a></div>';
            for($n=1;$n<=$numpages;$n++)
            {
                echo '<div id="page_num"><a href='.$URI.'?page_num='.$n.'#trade_history>'.$n.'</a></div>';
            }
            echo '<div id="next"><a href='.$URI.'?page_num='.$next.'#trade_history>Next</a></div>';
            echo '<div id="last"><a href='.$URI.'?page_num='.$numpages.'#trade_history>Last</a></div>'; 

            ?>

    </div>

Upvotes: 1

Jens A. Koch
Jens A. Koch

Reputation: 41747

You are fetching a big array from a service API and have no database, right?

If you want to solve this on PHP side, i would suggest to create a temporary store for the incoming data.

  • temp file with serialized array data
  • session
  • sqlite

The store is needed to avoid refetching the data from the service API and allows to do independent pagination calls to your script.

The logic for pagination is based around 'array_slice':

$items_total = count($items);
$per_page = 10;    
$max_pages = ceil($items_total / $per_page);    
$page = array_slice($items, $per_page * intval($_GET['page']) - 1, $per_page);

if($_GET['page'] > 1) {
    $prev_link = '<a href="script.php?page='.($_GET['page']-1).'"> previous </a>';
}

if($_GET['page'] < $max_pages) {
    $next_link = '<a href="script.php?page='.($_GET['page']+1).'"> next </a>';
}

An easier approach is to use a JS/client based solution, where the full data set is just passed from PHP to JS as JSON and into a table script, like datatables or tablesorter (http://tablesorter.com/docs/example-pager.html).

Upvotes: 0

John Boker
John Boker

Reputation: 83699

You could probably just use datatables.net, a jquery plugin to display your table. What you want looks very similar to the default setup.

Upvotes: 0

Related Questions