【问题标题】:how to get random 10 records form json file in restful api如何在restful api中从json文件中获取随机10条记录
【发布时间】:2016-09-17 11:36:23
【问题描述】:

以下是我的代码,请检查并告诉如何从 json 中获取 10 条随机记录并希望在 marquee 标记中显示。感谢 > 提前请检查我获得 10 条记录但想要 > 随机显示的代码。谢谢

<html>
    <head>
        <meta charset="UTF-8">
            <title>Sabkideal.com Todays findmystay.com</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script> <!-- Boostarp -->
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" media="all" /> 
<!-- Boostrap -->
<link rel="stylesheet" href="bootstrap/css/custom.css" media="all" />
<script type="text/javascript">


function disableSelection(target){
if (typeof target.onselectstart!="undefined") //IE route
    target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
    target.style.MozUserSelect="none"
else //All other route (ie: Opera)
    target.onmousedown=function(){return false}
target.style.cursor = "default"
}

//Sample usages
//disableSelection(document.body) //Disable text selection on entire body
//disableSelection(document.getElementById("mydiv")) //Disable text selection on element with id="mydiv"

</script>
<style>
.button {
  background-color: #FF5733;
  -webkit-border-radius: 10px;
  border-radius: 10px;
  border: none;
  color: #FFFFFF;
  cursor: pointer;
  display: inline-block;
  font-family: Arial;
  font-size: 20px;
  padding: 5px 10px;
  text-align: center;
  text-decoration: none;
}
@-webkit-keyframes glowing {
  0% { background-color: #B20000; -webkit-box-shadow: 0 0 3px #B20000; }
  50% { background-color: #FF0000; -webkit-box-shadow: 0 0 40px #FF0000; }
  100% { background-color: #B20000; -webkit-box-shadow: 0 0 3px #B20000; }
}

@-moz-keyframes glowing {
  0% { background-color: #B20000; -moz-box-shadow: 0 0 3px #B20000; }
  50% { background-color: #FF0000; -moz-box-shadow: 0 0 40px #FF0000; }
  100% { background-color: #B20000; -moz-box-shadow: 0 0 3px #B20000; }
}

@-o-keyframes glowing {
  0% { background-color: #B20000; box-shadow: 0 0 3px #B20000; }
  50% { background-color: #FF0000; box-shadow: 0 0 40px #FF0000; }
  100% { background-color: #B20000; box-shadow: 0 0 3px #B20000; }
}

@keyframes glowing {
  0% { background-color: #B20000; box-shadow: 0 0 3px #B20000; }
  50% { background-color: #FF0000; box-shadow: 0 0 40px #FF0000; }
  100% { background-color: #B20000; box-shadow: 0 0 3px #B20000; }
}

.button {
  -webkit-animation: glowing 1500ms infinite;
  -moz-animation: glowing 1500ms infinite;
  -o-animation: glowing 1500ms infinite;
  animation: glowing 1500ms infinite;
}
</style>
    </head>
    <body>
          <nav class="navbar navbar-default" role="navigation">
     <div class="container-fluid">
<!--header section -->
          <div class="navbar-header">
               <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
               <span class="sr-only">Toggle navigation</span>
               <span class="icon-bar"></span>
               <span class="icon-bar"></span>
               <span class="icon-bar"></span>
               </button>
               <a class="navbar-brand" href="index.php">Sabkideal.com  </a>
          </div>
<!-- menu section -->
          <div class="collapse navbar-collapse navbar-ex1-collapse">
               <ul class="nav navbar-nav navbar-right">
               <li><a href="# ">Home</a></li>
               <li><a href="# ">Register and win </a></li>
               <li><a href="#">Conatct Us </a></li>

               </ul>
          </div>
     </div>
</nav>
<div style="margin-left: 150px;
     width: 100px; ">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- sabkidealLeaderborad -->
<ins class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-9170404078755708"
     data-ad-slot="9181804474"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
<div class ="container">
<div class ="row">
<div class="col-lg-2">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- sabkidealskycraper -->
<ins class="adsbygoogle"
     style="display:inline-block;width:160px;height:600px"
     data-ad-client="ca-pub-9170404078755708"
     data-ad-slot="3308237676"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
<div class="col-lg-8">
        <?php
        $c = 0; 

    $ch = curl_init("http://tools.vcommission.com/api/coupons.php?apikey=e159f64e3dd49fddc3bb21dcda70f10c6670ea91aac30c7cb1d4ed37b20c45b8"); // add your url which contains json file
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($ch);
    curl_close($ch);
    $json = json_decode($content, true);
    //print_R($json);
    $count=count($json);



    for($i=0;$i<$count;$i++)
    {



        echo '<marquee behavior="scroll" direction="left" onmouseover="this.stop();" onmouseout="this.start();">';

      echo'
      <a target="_blank" style="width:100%" rel="nofollow, noindex" href='.$json[$i]['link'].'>  
<p>'.$json[$i]['coupon_title'].'</p></a>';




       echo '</marquee>';
        }







?>
</div>
<script type="text/javascript">
var alltables=document.getElementsByTagName("table")
for (var i=0; i<alltables.length; i++)
disableSelection(alltables[i]) //disable text selection within all tables on the page
</script>
<div class="col-lg-2">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- sabkidealskycraper -->
<ins class="adsbygoogle"
     style="display:inline-block;width:160px;height:600px"
     data-ad-client="ca-pub-9170404078755708"
     data-ad-slot="3308237676"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
</div>
</div>


    </body>
</html>

【问题讨论】:

    标签: php json if-statement for-loop while-loop


    【解决方案1】:

    您正在寻找array_rand php 函数

    PHP

    $json = json_decode($content, true);
    $randoms = array_rand($json, 10);
    $count = count($randoms);
    
    
    
    for($i=0;$i<$count;$i++)
    {
          echo '<marquee behavior="scroll" direction="left" onmouseover="this.stop();" onmouseout="this.start();">';
          echo'
          <a target="_blank" style="width:100%" rel="nofollow, noindex" href='.$json[$randoms[$i]]['link'].'>
          <p>'.$json[array_rand($json)]['coupon_title'].'</p></a>';
          echo '</marquee>';
    }
    

    【讨论】:

    • 我试试这个,但它没有显示任何 thi 检查链接http://sabkideal.com/demo.php
    • print_r($json);
    • 是的,它在输出中显示 10,但没有显示我想要显示的 10 个随机值
    • 我得到这个数组([0] => 95 [1] => 123 [2] => 127 [3] => 162 [4] => 195 [5] => 286 [ 6] => 353 [7] => 424 [8] => 608 [9] => 624)
    • 需要一个帮助,选框也作为列表工作,而不是正常(一个接一个)做什么。sabkideal.com/demo.php检查输出
    猜你喜欢
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    相关资源
    最近更新 更多