Yii框架 - 来自api

时间:2017-09-17 04:26:30

标签: php yii pagination

我是yii的新手,并且正在学习它。我已经成功制作了带复选框的页面,这些已连接到api。当用户点击任何复选框时,一组数据将进入并将显示在视图上。我想对这些数据进行分页,但不知道从哪里开始。

这是我的索引页面的控制器代码。 :

public function actionIndex()
{   
    $alldata = array();
    $personas = HUB::getResourcePersonas();
    $startupStage = HUB::getResourceStartupStages();
    $industries = HUB::getResourceIndustries();
    $categories = HUB::getResourceCategories();
    $locations = HUB::getResourceGeofocuses();

    if(!empty($_GET) ){


        // do anything you want with your response
        //print_r($response); exit;
        //Yii::import('application.modules.controllers.admin.YourController');

        $persona = '';$stage = '';$stage = '';$cat = '';
        if(isset($_GET['persona'])){
            $persona =  implode(",",$_GET['persona']);
        }
        if(isset($_GET['stage'])){
            $stage =  implode(",",$_GET['stage']);
        }
        if(isset($_GET['industry'])){
            $industry =  implode(",",$_GET['industry']);
        }
        if(isset($_GET['cat'])){
            $cat =  implode(",",$_GET['cat']);
        }
        if(isset($_GET['location'])){
            $location =  implode(",",$_GET['location']);
        }
        $post = [
        'persona' => $persona,
        'stage'=>$stage,
        'industry'=>$industry,
        'location'=>$location,
        'cat'=>$cat,
        'page'=>1

        ];

        $ch = curl_init('http://api-hub.mymagic.my/v1/getResourceAllActive');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

        // execute!
        $response = curl_exec($ch); // print_r($response);

        // close the connection, release resources used
        curl_close($ch);



    }

    $this->render('index',array('personas'=>$personas,'startupStage'=>$startupStage,'industries'=>$industries,'categories'=>$categories,'locations'=>$locations,'data'=>$response));
} 

我的观点/前端代码。 Div#list-resource是显示数据的位置(preview the screenshot of the page):

    <section class="container">
      <div id="">
        <div class="col col-sm-3">
          <div id="sidebar" class="content-main-left">
            <form id="searchResource-form" method="GET" action="<?php echo $this->createUrl('/resource/')  ?>">
              <div class="box-filter rounded-md checkbox checkbox-info">
                <p class="lead">Persona</p>
                <?php $filteredPersona = $_GET['persona']; foreach($personas as $persona): ?>
                <span class="item"><input  <?php if(in_array($persona['slug'],$filteredPersona)){ echo 'checked="checked"'; } ?> id="persona-<?php echo $persona['slug'] ?>" name="persona[]" value="<?php echo  $persona['slug'] ?>" type="checkbox">&nbsp;
  <label for="persona-<?php echo $persona['slug'] ?>"><?php echo $persona['title'] ?></label>
  <i class="btn-popover fa fa-info-circle text-info pull-right" data-container="body" data-toggle="popover" data-trigger="hover" data-placement="right" data-content="<?php echo $persona['title'] ?>" data-original-title="" title=""></i>
  </span><br>
                <?php endforeach; ?>

              </div>
              <div class="box-filter rounded-md checkbox checkbox-info">
                <p class="lead">Startup Stages</p>
                <?php $filteredStage = $_GET['stage']; foreach($startupStage as $sst): ?>
                <span class="item">
  <input <?php if(in_array($sst['slug'],$filteredStage)){ echo 'checked="checked"'; } ?> id="stage-<?php echo $sst['slug'] ?>" name="stage[]" value="<?php echo  $sst['slug'] ?>" type="checkbox">&nbsp;
  <label class="" for="stage-<?php echo $sst['slug'] ?>"><?php echo $sst['title'] ?></label>
  <i class="btn-popover fa fa-info-circle text-info pull-right" data-container="body" data-toggle="popover" data-trigger="hover" data-placement="right" data-content="<?php echo $sst['title'] ?>" data-original-title="" title=""></i>
  </span><br>
                <?php endforeach; ?>
              </div>
              <div id="box-fiter-cat" class="box-filter rounded-md checkbox checkbox-info">
                <p class="lead">Industry</p>

                <?php $filteredIndustries = $_GET['industry']; foreach($industries as $ind): ?>
                <div id="heading-<?php echo $ind['slug'] ?>" class="panel-heading" role="tab">
                  <span class="item">
  <input  <?php if(in_array($ind['slug'],$filteredIndustries)){ echo 'checked="checked"'; } ?> id="industry-<?php echo $ind['slug'] ?>" type="checkbox" name="industry[]" value="<?php echo $ind['slug'] ?>">
  <label for="industry-<?php echo $ind['slug'] ?>"><?php echo $ind['title'] ?></label>
  </span>
                  <a class="pull-right" role="button" data-toggle="collapse" href="#collapse-<?php echo $ind['slug'] ?>" aria-expanded="true" aria-controls="collapse-<?php echo $ind['slug'] ?>"><i class="fa fa-chevron-down"></i></a>
                </div>

                <div id="collapse-<?php echo $ind['slug'] ?>" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading-<?php echo $ind['slug'] ?>">
                  <ul class="list-group">
                    <?php foreach($ind['childs'] as $ch): ?>
                    <li>
                      <span class="item">
  <input id="subcategory-<?php echo $ind['slug'] ?>-<?php echo $ch['slug'] ?>" name="subcategory[]" value="<?php echo $ind['slug'] ?>.<?php echo $ch['slug'] ?>" type="checkbox">
  <label for="subcategory-<?php echo $ind['slug'] ?>-<?php echo $ch['slug'] ?>"><?php echo $ch['slug'] ?></label>
  </span>
                    </li>
                    <?php endforeach; ?>

                  </ul>
                </div>
                <?php endforeach; ?>

              </div>
              <div id="box-fiter-cat" class="box-filter rounded-md checkbox checkbox-info">
                <p class="lead">Categories</p>
                <?php //print_r($categories); ?>

                <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                  <?php $filteredCategories = $_GET['cat1']; $filteredCategories2 = $_GET['cat2']; foreach($categories as $cat): ?>
                  <div class="panel panel-default">
                    <div id="heading-<?php echo $cat['slug'] ?>" class="panel-heading" role="tab">
                      <span class="item"><input  <?php if(in_array($cat['slug'],$filteredCategories)){ echo 'checked="checked"'; } ?> id="cat-<?php echo $cat['slug'] ?>" type="checkbox" name="cat1[]" value="<?php echo $cat['slug'] ?>"> <label for="cat-<?php echo $cat['slug'] ?>"><?php echo $cat['title'] ?></label></span>
                      <a class="pull-right" role="button" data-toggle="collapse" href="#collapse-<?php echo $cat['slug'] ?>" aria-expanded="true" aria-controls="collapse-bizfund"><i class="fa fa-chevron-down"></i></a>
                    </div>
                    <div id="collapse-<?php echo $cat['slug'] ?>" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading-<?php echo $cat['slug'] ?>">
                      <ul class="list-group">
                        <?php foreach($cat['childs'] as $childs_cat): ?>
                        <li>
                          <span class="item">
  <input  <?php if(in_array($childs_cat['slug'],$filteredCategories2)){ echo 'checked="checked"'; } ?> id="cat2-<?php echo $cat['slug'] ?>-<?php echo $childs_cat['slug'] ?>" name="cat2[]" value="<?php echo $childs_cat['slug'] ?>" type="checkbox">
  <label for="cat2-<?php echo $cat['slug'] ?>-<?php echo $childs_cat['slug'] ?>"><?php echo $childs_cat['title'] ?></label>
  </span>
                        </li>
                        <?php endforeach; ?>

                      </ul>
                    </div>


                  </div>
                  <?php endforeach; ?>
                </div>
              </div>
              <div id="box-fiter-cat" class="box-filter rounded-md checkbox checkbox-info">
                <p class="lead">Location</p>
                <?php $filteredLocation = $_GET['location']; $filteredLocation2 = $_GET['malaysia']; foreach($locations as $location): ?>
                <div id="heading-<?php echo $location['slug'] ?>" class="panel-heading" role="tab">
                  <span class="item"><input  <?php if(in_array($location['slug'],$filteredLocation)){ echo 'checked="checked"'; } ?> id="location-<?php echo $location['slug'] ?>" type="checkbox" name="location[]" value="<?php echo $location['slug'] ?>"> <label for="location-<?php echo $location['slug'] ?>"><?php echo $location['title'] ?></label></span>

                  <a class="pull-right" role="button" data-toggle="collapse" href="#collapse-<?php echo $location['slug'] ?>" aria-expanded="true" aria-controls="collapse-<?php echo $location['slug'] ?>"><i class="fa fa-chevron-down"></i></a>
                </div>
                <div id="collapse-<?php echo $location['slug'] ?>" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading-<?php echo $location['slug'] ?>">
                  <ul class="list-group">
                    <?php foreach($location['childs'] as $childs_location): ?>
                    <li>
                      <span class="item">
  <input <?php if(in_array($childs_location['slug'],$filteredLocation2)){ echo 'checked="checked"'; } ?> id="<?php echo $location['slug'] ?>-<?php echo $childs_location['slug'] ?>" name="malaysia[]" value="<?php echo $childs_location['slug'] ?>" type="checkbox">
  <label for="<?php echo $location['slug'] ?>-<?php echo $childs_location['slug'] ?>"><?php echo $childs_location['title'] ?></label>
  </span>
                    </li>

                    <?php endforeach; ?>



                  </ul>
                </div>


                <?php endforeach; ?>

              </div>
              <noscript>&lt;input type="submit" class="btn btn-success btn-block" /&gt;</noscript>
            </form>
            <div id="box-search-keyword" class="box-filter rounded-md checkbox checkbox-info">
              <p class="lead">Search</p>

              <script>
                (function() {
                  var cx = '003080515321755030470:ls0frruy5mm';
                  var gcse = document.createElement('script');
                  gcse.type = 'text/javascript';
                  gcse.async = true;
                  gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
                  var s = document.getElementsByTagName('script')[0];
                  s.parentNode.insertBefore(gcse, s);
                })();
              </script>
              <div id="___gcse_0">
                <div class="gsc-control-cse gsc-control-cse-en">
                  <div class="gsc-control-wrapper-cse" dir="ltr">
                    <form class="gsc-search-box gsc-search-box-tools" accept-charset="utf-8">
                      <table cellspacing="0" cellpadding="0" class="gsc-search-box">
                        <tbody>
                          <tr>
                            <td class="gsc-input">
                              <div class="gsc-input-box" id="gsc-iw-id1">
                                <table cellspacing="0" cellpadding="0" id="gs_id50" class="gstl_50 " style="width: 100%; padding: 0px;">
                                  <tbody>
                                    <tr>
                                      <td id="gs_tti50" class="gsib_a"><input autocomplete="off" type="text" size="10" class="gsc-input" name="search" title="search" id="gsc-i-id1" style="width: 100%; padding: 0px; border: none; margin: -0.0625em 0px 0px; height: 1.25em; outline: none; background: url(&quot;http://www.google.com/cse/static/images/1x/googlelogo_lightgrey_46x16dp.png&quot;) left center no-repeat rgb(255, 255, 255); text-indent: 48px;"
                                          x-webkit-speech="" x-webkit-grammar="builtin:search" lang="en" dir="ltr" spellcheck="false" placeholder="Custom Search"></td>
                                      <td class="gsib_b">
                                        <div class="gsst_b" id="gs_st50" dir="ltr"><a class="gsst_a" href="javascript:void(0)" style="display: none;"><span class="gscb_a" id="gs_cb50">×</span></a></div>
                                      </td>
                                    </tr>
                                  </tbody>
                                </table>
                              </div><input type="hidden" name="bgresponse" id="bgresponse"></td>
                            <td class="gsc-search-button"><input type="image" src="https://www.google.com/uds/css/v2/search_box_icon.png" class="gsc-search-button gsc-search-button-v2" title="search"></td>
                            <td class="gsc-clear-button">
                              <div class="gsc-clear-button" title="clear results">&nbsp;</div>
                            </td>
                          </tr>
                        </tbody>
                      </table>
                      <table cellspacing="0" cellpadding="0" class="gsc-branding">
                        <tbody>
                          <tr>
                            <td class="gsc-branding-user-defined"></td>
                            <td class="gsc-branding-text">
                              <div class="gsc-branding-text">powered by</div>
                            </td>
                            <td class="gsc-branding-img"><img src="https://www.google.com/cse/static/images/1x/googlelogo_grey_46x15dp.png" class="gsc-branding-img" srcset="https://www.google.com/cse/static/images/2x/googlelogo_grey_46x15dp.png 2x"></td>
                          </tr>
                        </tbody>
                      </table>
                    </form>
                    <div class="gsc-results-wrapper-overlay">
                      <div class="gsc-results-close-btn" tabindex="0"></div>
                      <div class="gsc-tabsAreaInvisible">
                        <div class="gsc-tabHeader gsc-inline-block gsc-tabhActive">Custom Search</div><span class="gs-spacer"> </span></div>
                      <div class="gsc-tabsAreaInvisible"></div>
                      <div class="gsc-above-wrapper-area-invisible">
                        <table cellspacing="0" cellpadding="0" class="gsc-above-wrapper-area-container">
                          <tbody>
                            <tr>
                              <td class="gsc-result-info-container">
                                <div class="gsc-result-info-invisible"></div>
                              </td>
                              <td class="gsc-orderby-container">
                                <div class="gsc-orderby-invisible">
                                  <div class="gsc-orderby-label gsc-inline-block">Sort by:</div>
                                  <div class="gsc-option-menu-container gsc-inline-block">
                                    <div class="gsc-selected-option-container gsc-inline-block">
                                      <div class="gsc-selected-option">Relevance</div>
                                      <div class="gsc-option-selector"></div>
                                    </div>
                                    <div class="gsc-option-menu-invisible">
                                      <div class="gsc-option-menu-item gsc-option-menu-item-highlighted">
                                        <div class="gsc-option">Relevance</div>
                                      </div>
                                      <div class="gsc-option-menu-item">
                                        <div class="gsc-option">Date</div>
                                      </div>
                                    </div>
                                  </div>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                      <div class="gsc-adBlockInvisible"></div>
                      <div class="gsc-wrapper">
                        <div class="gsc-adBlockInvisible"></div>
                        <div class="gsc-resultsbox-invisible">
                          <div class="gsc-resultsRoot gsc-tabData gsc-tabdActive">
                            <table cellspacing="0" cellpadding="0" class="gsc-resultsHeader">
                              <tbody>
                                <tr>
                                  <td class="gsc-twiddleRegionCell">
                                    <div class="gsc-twiddle">
                                      <div class="gsc-title">Web</div>
                                    </div>
                                    <div class="gsc-stats"></div>
                                    <div class="gsc-results-selector gsc-all-results-active">
                                      <div class="gsc-result-selector gsc-one-result" title="show one result">&nbsp;</div>
                                      <div class="gsc-result-selector gsc-more-results" title="show more results">&nbsp;</div>
                                      <div class="gsc-result-selector gsc-all-results" title="show all results">&nbsp;</div>
                                    </div>
                                  </td>
                                  <td class="gsc-configLabelCell"></td>
                                </tr>
                              </tbody>
                            </table>
                            <div>
                              <div class="gsc-expansionArea"></div>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="gsc-modal-background-image" tabindex="0"></div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="col col-sm-9">

          <div class="jumbotron">
            <h1>Resource Tool</h1>
            <p>The <strong>MaGIC Central resource centre</strong> explore comprehensive collection of over 700 products &amp; services from over 180 organisations available for entrepreneurs. </p>
            <p>This Resource Tool acts as the virtual platform for information and referrals to all resources available within the Malaysian startup ecosystem. Get started, search for resources.</p>

          </div>
          <h1><u>Featured Resources</u></h1>

          <div id="list-resource">
            <?php 
  $jdata = json_decode($data);
  if(!empty($jdata->data)){
  foreach($jdata->data as $jdkey=>$jdvalue){ ?>
            <div class="item row">
              <div class="col-xs-9">
                <h3><a href="<?=$jdvalue->slug ?>"><?=$jdvalue->title ?> </a></h3>
                <?php echo ysUtil::truncate($jdvalue->htmlContent, 250) ?>
                <div class="text-muted margin-top-lg">
                  <?=$jdvalue->resourceCategories[0]->typefor ?> -
                    <?=$jdvalue->resourceCategories[0]->title ?>
                </div>
              </div>
              <div class="col-xs-offset-1 col-xs-2 pull-right">
                <a href="<?=$jdvalue->slug ?>" class="thumbnail"><img width="125" height="71" src="<?=$jdvalue->imageLogoUrl ?>" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="1innocert_0-259x148"></a>
              </div>
            </div>

            <?php } 

  } ?>




          </div>

我认为我应该将数据限制在10左右,并为下一组数据分页。

1 个答案:

答案 0 :(得分:0)

Yii框架附带了一些组件,可以让您更轻松地完成此类操作。我假设你正在使用yii1,如果没有,yii2有相似的组件。

首先,您需要使用CDataProvider进行查询,这将处理所有排序&amp;为你分页。对于您的观点,您应该使用CListViewCListView supports both sorting and pagination of the data items.检查代码示例的链接。