SL_Shuai 2020-09-16 17:00 采纳率: 48.1%
浏览 138
已采纳

想要Js中的div样式根据数据库中内容的数量循环自动生成,但是出错了

PHP

<?PHP
header("Content-Type: text/html; charset=utf-8");
include('conn.php');
$sql = 'select id,art_name from article';
$res = mysqli_query($conn,$sql);
$data = array();
while ($row = mysqli_fetch_assoc($res)) {
    $data[] = $row;
}
echo json_encode($data);
?>

JS

<script type="text/javascript">
    $(document).ready(function(){
        $.ajax({
            url : "./php/active.php",//后台请求的数据,用的是PHP
            type : "post",//请求方式
            async : false,//是否异步请求
            success : function(data) { //如果请求成功,返回数据。
                var res = JSON.parse(data);
                console.log(res);
                var html = "";
                for(let i=0;i<res.length;i++){
                    html += "<div class='card_bordered' style='margin: 20px 0px; border-left: 4px solid rgb(24, 144, 255);'>
                    <div class='card_body'>
                        <div class='cardTitle'>
                            <span>
                                <span id='art_name' style='font-weight: 700;'>&nbsp;&nbsp;</span>
                                <span id='id' style='color: rgb(24, 144, 255); padding-left: 10px;'></span>
                            </span>
                            <span style='float: right;'>
                                <button type='button' class='btn_default' style='margin-left: 15px; color: rgb(24, 144, 255); border-color: rgb(24, 144, 255); border-radius: 3px;'>
                                    <span>客户资料</span>
                                </button>
                                <button type='button' class='btn_default' style='margin-left: 15px; color: rgb(24, 144, 255); border-color: rgb(24, 144, 255); border-radius: 3px;'>
                                    <span onclick='javascript:window.location.href='edit.html''>编辑</span>
                                </button>
                                <button type='button' class='btn_default' style='margin-left: 15px; color: rgb(255, 97, 84); border-color: rgb(255, 97, 84); border-radius: 3px;'>
                                    <span>删除</span>
                                </button>
                            </span>
                        </div>
                        <div style='padding: 0px 20px; height: 80px;'>
                            <div class='row_flex' style='height: 80px;'>
                                <div class='col_span_1'>
                                    <p style='padding: 0px; line-height: 30px;'>推文地址:</p>
                                </div>
                                <div class='col_span' style='border-left: 1px solid rgb(222, 222, 222); padding-left: 10px;'>
                                    <p style='padding: 0px; line-height: 30px; font-size: 12px;'>状态:
                                        <span style='color: rgb(24, 144, 255);'>启用</span>
                                        <span style='color: rgb(130, 130, 130); display: none;'>停止</span>
                                    </p>
                                    <p style='padding: 0px; line-height: 30px;'>
                                        <button type='button' class='btn_default' style='background: rgb(24, 144, 255); color: rgb(255, 255, 255); border-radius: 3px; padding: 0px 10px; border: none; display: none;'>
                                            <span>开始</span>
                                        </button>
                                        <button type='button' class='btn_default' style='background: rgb(247, 179, 57); color: rgb(255, 255, 255); border-radius: 3px; padding: 0px 10px; border: none;'>
                                            <span>停止</span>
                                        </button>
                                    </p>
                                </div>
                                <div class='col_span' style='text-align: right; position: relative;'>
                                    <div style='position: absolute; width: 60px; height: 60px; right: 0px; top: -30px;'>
                                        <img src='./image/1599475563268843.png' style='width: 100%; height: 100%;'>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>";
                }
                $("#tw_xinxi").append(html);
                $("#art_name").append(res[1]);
                $("#id").append("[ID:" + res[0] + "]");
            },
        })
     })
</script>

效果图
图片说明

数据库图
图片说明

已修改PHP代码,前端div显示对应数组数量了,console.log(res)输出为
▼Array(2)

▶0: {id: "1", article_name: "4124214"}
▶1: {id: "3", article_name: "666657"}
length: 2
▶__proto__: Array(0)

但是又出现了新的问题,我console.log(res[0或1].id)才能取到id,因为想要提取id和art_name显示在前端,但是后续如果数据库增加数据,我需要手动再添加对应的$("#art_name").append(res[0或1或其他的].art_name),而且我现在只有一个div能显示id和art_name,有没有办法直接获取对应id和art_name,不用[0或1]这个?

  • 写回答

4条回答 默认 最新

  • 子幽 2020-09-17 10:34
    关注

    php:

    $arr['count']=mysqli_num_rows($res);
    while ($row = mysqli_fetch_array($res,MYSQLI_ASSOC)){
    $arr['data'][] = $row;
    }
    echo json_encode($arr);
    

    js:

    for(let i=0;i<res.count;i++){
                        html += '<div class="card_bordered" style="margin: 20px 0px; border-left: 4px solid rgb(24, 144, 255);">';
                        html += '<div class="card_body">';
                        html += '<div class="cardTitle">';
                        html += '<span>';
                        html += '<span id="art_name" style="font-weight: 700;">&nbsp;&nbsp;'+res.data[i].id+'</span>';
                        html += '<span id="id" style="color: rgb(24, 144, 255); padding-left: 10px;">[ID:'+res.data[i].art_name+']</span>';
                        html += '</span>';
                        html += '<span style="float: right;">';
                        html += '<button type="button" class="btn_default" style="margin-left: 15px; color: rgb(24, 144, 255); border-color: rgb(24, 144, 255); border-radius: 3px;">';
                        html += '<span>客户资料</span>';
                        html += '</button>';
                        html += '<button type="button" class="btn_default" style="margin-left: 15px; color: rgb(24, 144, 255); border-color: rgb(24, 144, 255); border-radius: 3px;">';
                        html += '<span onclick="javascript:window.location.href=\'edit.html\'">编辑</span>';
                        html += '</button>';
                        html += '<button type="button" class="btn_default" style="margin-left: 15px; color: rgb(255, 97, 84); border-color: rgb(255, 97, 84); border-radius: 3px;">';
                        html += '<span>删除</span>';
                        html += '</button>';
                        html += '</span>';
                        html += '</div>';
                        html += '<div style="padding: 0px 20px; height: 80px;">';
                        html += '<div class="row_flex" style="height: 80px;">';
                        html += '<div class="col_span_1">';
                        html += '<p style="padding: 0px; line-height: 30px;">推文地址:</p>';
                        html += '</div>';
                        html += '<div class="col_span" style="border-left: 1px solid rgb(222, 222, 222); padding-left: 10px;">';
                        html += '<p style="padding: 0px; line-height: 30px; font-size: 12px;">状态:';
                        html += '<span style="color: rgb(24, 144, 255);">启用</span>';
                        html += '<span style="color: rgb(130, 130, 130); display: none;">停止</span>';
                        html += '</p>';
                        html += '<p style="padding: 0px; line-height: 30px;">';
                        html += '<button type="button" class="btn_default" style="background: rgb(24, 144, 255); color: rgb(255, 255, 255); border-radius: 3px; padding: 0px 10px; border: none; display: none;">';
                        html += '<span>开始</span>';
                        html += '</button>';
                        html += '<button type="button" class="btn_default" style="background: rgb(247, 179, 57); color: rgb(255, 255, 255); border-radius: 3px; padding: 0px 10px; border: none;">';
                        html += '<span>停止</span>';
                        html += '</button>';
                        html += '</p>';
                        html += '</div>';
                        html += '<div class="col_span" style="text-align: right; position: relative;">';
                        html += '<div style="position: absolute; width: 60px; height: 60px; right: 0px; top: -30px;">';
                        html += '<img src="./image/1599475563268843.png" style="width: 100%; height: 100%;">';
                        html += '</div>';
                        html += '</div>';
                        html += '</div>';
                        html += '</div>';
                        html += '</div>';
                        html += '</div>';
                    }
    
                    $("body").append(html);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn