【问题标题】:how to iterate through php array in javascript如何在javascript中遍历php数组
【发布时间】:2020-11-01 10:46:52
【问题描述】:
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Attendance Manager - Home</title>
<link rel="stylesheet" href="../css/skeleton.css">
<link rel="stylesheet" href="../css/subjects.css">
<link rel="icon" type="image/jpg" href="../media/icon.png">
</head>

<body>
<header>
    <div class="left">
        <button id="menuButton" onclick="openMenu()">≡</button>
    </div>

    <div class="center">
        <div>
            <div id="month">
            </div>
            <div id="year">
            </div>
        </div>

        <div id="day">
        </div>

        <img id="icon" src="../media/icon.png" alt="App Icon" width="48" height="48">

        <div id="name">
        </div>
    </div>

    <div class="right">
        <a id="info" href="../html/index.php">l</a>
    </div>
</header>

<main>
    <menu>
        <a href="home.php">Home</a>
        <a href="subjects.php">Subjects</a>
        <a href="criteria.php">Edit Attendance Criteria</a>
        <a href="help.html">How to Use</a>
        <a href="developer.html">Developer Information</a>
        <a href="privacy.html">Privacy Policy</a>
        <a href="contact.html">Contact Us</a>
    </menu>
</main>

<section id="main_container">
    <?php

    session_start();
    $email = $_SESSION["email"];
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';
    $dbname = 'attendance_system';



    $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);


    $query = "SELECT * FROM subject WHERE email='$email'";
    $result = $conn->query($query);
    $n = $result->num_rows;
    $subject_name = array();
    $subject_total_hours = array();
    $subject_hours_completed = array();
    $subject_hours_present = array();
    $subject_hours_absent = array();
    $subject_credit = array();
    $subject_atendance = array();

    while ($row = $result->fetch_assoc()) {
        $subject_name[] = $row['name'];
        $subject_total_hours[] = $row['total_hours'];
        $subject_hours_completed[] = $row['hours_completed'];
        $subject_hours_present[] = $row['hours_present'];
        $subject_hours_absent[] = $row['hours_absent'];
        $subject_credit[] = $row['credit'];
        $subject_attendance[] = $row['attendance'];
    }

    ?>
    <h3>Subjects</h3>
    <table>
        <tr>
            <td>No.</td>
            <td>Name</td>
            <td>Total<br>Classes</td>
            <td>Present</td>
            <td>Absent</td>
            <td>Attendance</td>
            <td>Credit</td>
        </tr>
        <tr>
            <td>1.</td>
            <td>Machine Learning</td>
            <td>40</td>
            <td>36</td>
            <td>4</td>
            <td>90 %</td>
            <td>4</td>
        </tr>

    </table>
</section>

<script type="text/javascript">

/* let subject = document.createElement("tr");
let subject_number = 
subject.innerHTML = '<?php echo $subject_name[0] ?>';

let section = document.getElementById("main_container");
section.appendChild(subject);*/

</script>

<footer>
    <div class="left">
        Attendance Manager<br>2020 ©
    </div>
    <div class="center">
        <span>
            Official Page -
        </span>
        <span>
            <a href="https://twitter.com/AlokPur32580593?s=08">
                <img src="../media/twitter.png" alt="Twitter Icon" height="48px" width="48px">
            </a>
        </span>
    </div>
    <div class="right">
        <a id="privacyPolicy" href="privacy.html">Privacy Policy</a>
    </div>
</footer>

<script type="text/javascript" src="../javascript/home.js"></script>
</body>

</html>

我是 PHP 新手。我想将我的 PHP 代码中创建的所有数组的每一个值从我的 javascript(在脚本标记内)一个一个地添加到 HTML 文档中的表中。我使用从 PHP 中的 MySQL 数据库中提取的值在 PHP 中创建了数组。但问题是,每次提取的值的数量都会不同,因此每次加载文档时数组的大小都会不同。我无法通过使用 javascript 循环访问 PHP 数组元素。所以请告诉我如何动态地做到这一点。我不想为 PHP 数组的每个值编写单独的 javascript 代码。

【问题讨论】:

  • 使用json_encodelink
  • 你能提供更多的细节吗?像一个虚拟代码来解释这个可能吗?
  • 在你的脚本标签var someVar = &lt;?php echo json_encode($data); ?&gt;;中。这为您提供了 json 对象或数组,接下来您应该能够在 js 代码中进行迭代
  • 嗨,请edit 您的问题仅包括我们了解您的问题所需的最低限度 - minimal reproducible example。现在,要阅读的内容太多了,其中大部分都无关紧要。

标签: javascript php arrays server


【解决方案1】:

不用js也可以用php实现

<table>
    <tr>
        <td>No.</td>
        <td>Name</td>
        <td>Total<br>Classes</td>
        <td>Present</td>
        <td>Absent</td>
        <td>Attendance</td>
        <td>Credit</td>
    </tr>

    <?php for ($i = 0; $i < count($subject_name); $i++): ?>
    <tr>
        <td><?= $i ?></td>
        <td><?= $subject_name[$i]; ?></td>
        <td><?= $subject_hours_completed[$i]; ?></td>
        <td><?= $subject_hours_present[$i]; ?></td>
        <td><?= $subject_hours_absent[$i]; ?></td>
        <td><?= $subject_attendance[$i]; ?></td>
        <td><?= $subject_credit[$i]; ?></td>
    </tr>
    <?php endfor;?>

</table>

【讨论】:

  • 这非常有效。太感谢了。基本上我是在服务器端的客户端页面上创建元素,对吗?所以我不需要访问客户端java脚本上的数组。
【解决方案2】:

如果你想通过 javascript 绑定表中的数据,那么你必须将你的值以 json 编码格式存储在 php 脚本中。然后你可以在你的 javascript 中使用它,或者你可以使用 ajax 等从 javascript 中获取和绑定。

$.get(your url + 'your method name',function(response){
            Here you can bind your value using forEach()
        },'json');

【讨论】:

    猜你喜欢
    • 2014-01-13
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2022-01-04
    相关资源
    最近更新 更多