如何从API响应生成动态Ul li(列表项)?

西区解决方案公司

我正在运行API调用并收到以下响应:

{
"recsonpage":"4",
    "4":{
        "orders.orderid":"8890348189",
        "entity.customerid":"15250457",
        "entity.entityid":"88908189",
        "orders.autorenew":"false",
        "orders.endtime":"1604899485",
        "orders.resellerlock":"false",
        "orders.timestamp":"2019-11-09 05:24:46.25876+00",
        "orders.customerlock":"true",
        "entity.entitytypeid":"3",
        "entity.currentstatus":"Active",
        "entitytype.entitytypekey":"domcno",
        "orders.transferlock":"true",
        "orders.creationtime":"1573277085",
        "orders.privacyprotection":"false",
        "entitytype.entitytypename":".COM Domain Name",
        "orders.creationdt":"1573277084",
        "entity.description":"domain1.com"
        },
    "3":{
        "orders.orderid":"8739268259",
        "entity.customerid":"15250457",
        "entity.entityid":"87398259",
        "orders.autorenew":"false",
        "orders.endtime":"1625211562",
        "orders.resellerlock":"false",
        "orders.timestamp":"2020-05-23 05:53:15.586565+00",
        "orders.customerlock":"true",
        "entity.entitytypeid":"3",
        "entity.currentstatus":"Active",
        "entitytype.entitytypekey":"domcno",
        "orders.transferlock":"true",
        "orders.creationtime":"1372750762",
        "orders.privacyprotection":"false",
        "entitytype.entitytypename":".COM Domain Name",
        "orders.creationdt":"1561992837",
        "entity.description":"domain2.com"
        },
    "2":{
        "orders.orderid":"8739768158",
        "entity.customerid":"15250457",
        "entity.entityid":"87398158",
        "orders.autorenew":"false",
        "orders.endtime":"1625300828",
        "orders.resellerlock":"false",
        "orders.timestamp":"2020-05-23 05:54:20.869807+00",
        "orders.customerlock":"true",
        "entity.entitytypeid":"17",
        "entity.currentstatus":"Active",
        "entitytype.entitytypekey":"dotin",
        "orders.transferlock":"true",
        "orders.creationtime":"1372840028",
        "entitytype.entitytypename":".IN Domain Name",
        "orders.creationdt":"1561992195",
        "entity.description":"domain3.in"
        },
"recsindb":"4",
    "1":{
        "orders.orderid":"8701877407",
        "entity.customerid":"15250457",
        "entity.entityid":"87017407",
        "orders.autorenew":"false",
        "orders.endtime":"1622300992",
        "orders.resellerlock":"false",
        "orders.timestamp":"2020-05-23 05:55:45.916596+00",
        "orders.customerlock":"true",
        "entity.entitytypeid":"3",
        "entity.currentstatus":"Active",
        "entitytype.entitytypekey":"domcno",
        "orders.transferlock":"true",
        "orders.creationtime":"1369840192",
        "orders.privacyprotection":"false",
        "entitytype.entitytypename":".COM Domain Name",
        "orders.creationdt":"1558956495",
        "entity.description":"domain4.com"
        }
}

我的逻辑是:

<?php
    global $user_ID, $user_identity, $userdata; wp_get_current_user();
    $current_user = wp_get_current_user();
    $custemail= $current_user->user_email;

get_header();

$url = 'https://httpapi.com/api/customers/details.json?auth-userid=12345&api-key=mykey&username='.$custemail;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
if(curl_error($ch)) { 
echo 'error:' . curl_error($ch);
};
curl_close($ch);
$data = json_decode($json,true);

$custid= $data['customerid'];
$url = 'https://httpapi.com/api/domains/search.json?auth-userid=12345&api-key=mykey&no-of-records=10&page-no=1&customer-id='.$custid;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
if(curl_error($ch)) { 
echo 'error:' . curl_error($ch);
};
curl_close($ch);
$data = json_decode($json,true);

$i = -- $data['recsonpage'];

$t = $data['recsonpage'];

?>
<div>

<script>
        $(document).ready(function(){
            $("#dom-list").click(function(){
                $("#dom-list").append('<li><p class="tld-name-2"><?php echo "" .$data["$t"]['entity.description']; ?></a></p><p class="prop"><span class="prop-head">Created On :</span><span class="data-no"><?php echo "" .$data["$t"]['orders.creationtime']; ?></span></p>      <p class="prop"><span class="prop-head">Expires On :</span><span class="data-no"><?php echo "" .$data["$t"]['orders.endtime']; ?></span></p><p class="prop"><span class="prop-head">Privacy Protection :</span><span class="data-no"><?php echo "" .$data["$t"]['orders.privacyprotection']; ?></span></p>      <p class="prop"><span class="prop-head">Theft Protection :</span><span class="data-no"><?php echo "" .$data["$t"]['orders.customerlock']; ?></span></p>     <p class="prop"><span class="prop-head">Status :</span><span class="data-no"><?php echo "" .$data["$t"]['entity.currentstatus']; ?></span></p></li>');

            });
              for (var counter = 0; counter < "<?php echo $i; ?>"; counter++) {
                  $("#dom-list").click();
              }

        });

</script>

<ul id="dom-list" class="domain-list-ul">

</ul>   

<button id="add-item" type="button" style="display:none">Add New LI Element</button>

</div>

单击该按钮4次(API中的行数)以生成列表项。但是问题是我无法在每个列表中获取不同的数据。所有我在每个列表项中都获得相同的数据。

请帮忙。

预期的HTML输出

在此处输入图片说明

罗希特·巴特拉

您不需要为此目的添加隐藏按钮,也不需要jQuery代码。页面加载时,已经调用了您的API,并且已经存在要在页面加载UI之前呈现的结果。因此,我们需要遍历从API接收的数据并呈现UI。

在下面的代码块中尝试一下:

<?php
global $user_ID, $user_identity, $userdata; wp_get_current_user();
$current_user = wp_get_current_user();
$custemail= $current_user->user_email;
get_header();
$url = 'https://httpapi.com/api/customers/details.json?auth-userid=12345&api-key=mykey&username='.$custemail;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
if(curl_error($ch)) { 
    echo 'error:' . curl_error($ch);
};
curl_close($ch);
$data = json_decode($json,true);
$custid = $data['customerid'];
$url = 'https://httpapi.com/api/domains/search.json?auth-userid=12345&api-key=mykey&no-of-records=10&page-no=1&customer-id='.$custid;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
if(curl_error($ch)) { 
    echo 'error:' . curl_error($ch);
};
curl_close($ch);
$data = json_decode($json,true);
ksort($data);
?>
<div>
    <ul id="dom-list" class="domain-list-ul">
        <?php foreach($data as $key => $value) { ?>
        <?php if(is_array($value)) { ?>
        <li>
            <p class="tld-name-2">
                <?php echo " {$value['entity.description']}"; ?>
            </p>
            <p class="prop">
                <span class="prop-head">Created On :</span>
                <span class="data-no"><?php echo " {$value['orders.creationtime']}"; ?></span>
            </p>
            <p class="prop">
                <span class="prop-head">Expires On :</span>
                <span class="data-no"><?php echo " {$value['orders.endtime']}"; ?></span>
            </p>
            <p class="prop">
                <span class="prop-head">Privacy Protection :</span>
                <span class="data-no"><?php echo " {$value['orders.privacyprotection']}"; ?></span>
            </p>
            <p class="prop">
                <span class="prop-head">Theft Protection :</span>
                <span class="data-no"><?php echo " {$value['orders.customerlock']}"; ?></span>
            </p>
            <p class="prop">
                <span class="prop-head">Status :</span>
                <span class="data-no"><?php echo " {$value['entity.currentstatus']}"; ?></span>
            </p>
        </li>
        <?php }  
} ?>
    </ul>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在angular js的嵌套ul li ul li列表项中重复数据?

来自分类Dev

使用jQuery删除动态添加的LI列表项

来自分类Dev

如何使锚元素(<a>)等于列表项(<li>)的大小

来自分类Dev

如何使用jquery(最好)显示和隐藏无序列表(ul)中的列表项(li)元素?

来自分类Dev

在UL内部使用列表项和锚标记<li> <a> </a> </ li>附加内容

来自分类Dev

使用普通的javascript根据最高的li动态设置与列表项(li)相等的高度

来自分类Dev

增加嵌套(ul ul ul li)下拉导航列表项的宽度

来自分类Dev

如何在ul(无序列表)中查找下一个li(列表项)的ID

来自分类Dev

<ul>比列表项宽

来自分类Dev

如何使用选定的li列表项的文本更改下拉菜单中按钮的文本-jQuery

来自分类Dev

如何使用Eval控制<li>列表项的可见性

来自分类Dev

如何为每个列表项生成列表

来自分类Dev

角度,如何将class设置为单击列表项并从其他li项中删除?

来自分类Dev

角度,如何将class设置为单击的列表项并从其他li项中删除?

来自分类Dev

点击列表项无响应

来自分类Dev

列表项的离子响应列

来自分类Dev

动态创建的列表项onclick

来自分类Dev

如何导入列表项?

来自分类Dev

如何对齐列表项

来自分类Dev

如何获取列表项的ID?

来自分类Dev

如何排列<ol>列表项?

来自分类Dev

JS如何使列表项滑动

来自分类Dev

如何下载列表项版本

来自分类Dev

如何使列表项垂直居中?

来自分类Dev

如何更改列表项的视图

来自分类Dev

如何使用jquery将php中的列表项动态添加到<ul>中?

来自分类Dev

如何替换列表是对象属性的列表项?

来自分类Dev

获取内联列表项以溢出父ul

来自分类Dev

显示列表项时的<输出>与<ul>

Related 相关文章

热门标签

归档