繁体   English   中英

如何在javascript中遍历php数组

[英]how to iterate through php array in javascript

<!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 代码。

你可以在没有 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>

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM