没有数据库的分页

时间:2014-05-19 12:02:22

标签: javascript php jquery pagination

我打电话给交易api在客户的网站上显示交易历史,我必须在多个页面(分页)中显示响应。我找不到没有数据库的分页逻辑。可以任何身体帮帮我。

我的代码在

下面
     <?php

 error_reporting(E_ALL);

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

$email="fxmarketcast@gmail.com";
$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>

响应的当前显示类似于

enter image description here

可以显示结果,如fallowing

enter image description here

请帮助我任何身体......提前谢谢。

3 个答案:

答案 0 :(得分:1)

谢谢大家的快速回复。最后我得到了逻辑。解决方案的代码如下:

<?php

//error_reporting(E_ALL);

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

$email="fxmarketcast@gmail.com";
$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>

答案 1 :(得分:0)

您可以使用datatables.net,一个jquery插件来显示您的表格。您想要的内容与默认设置非常相似。

答案 2 :(得分:0)

您从服务API获取一个大数组并且没有数据库,对吧?

如果你想在PHP方面解决这个问题,我建议为传入的数据创建一个临时存储。

  • 带有序列化数组数据的临时文件
  • 会话
  • 源码

需要存储以避免从服务API重新获取数据,并允许对脚本执行独立的分页调用。

分页的逻辑基于'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>';
}

更简单的方法是使用基于JS /客户端的解决方案,其中完整数据集只是作为JSON从PHP传递到JS并传递到表脚本,如数据表或tablesorter(http://tablesorter.com/docs/example-pager.html)。