简体   繁体   English

将PHP转换为数组并循环遍历

[英]Casting PHP into an array and looping through

I am working on an application for our karate school and would like to get the technique names from the database, store them in an array in a random order and be able to click a button to move through the entire array one at a time. 我正在为我们的空手道学校申请一个应用程序,并希望从数据库中获取技术名称,以随机顺序将它们存储在一个数组中,并且能够单击一个按钮一次一个地移动整个数组。

I have thought about this in several different ways including just doing it randomly from the database which was pretty easy, but it pulls the same technique multiple times and I only want it done once. 我已经用几种不同的方式考虑过这个问题,包括从数据库中随机进行这很简单,但它多次采用相同的技术,我只想做一次。

Sample code below lists them all randomly just like it should and when I refresh the browser it creates a new list just like I want. 下面的示例代码将它们全部随机地列出,当我刷新浏览器时,它会像我想要的那样创建一个新列表。 Now I would like to know how I could display only one at a time and keep it in the browser until all of them have been gone through. 现在我想知道如何一次只显示一个并将其保留在浏览器中,直到所有这些都已经完成。

$sqlQuery= "Select * from Techniques Order BY Rand()";
$statement = $db->prepare($sqlQuery);
$statement->execute();
while($row = $statement->fetch(PDO::FETCH_ASSOC)):
    $techniqueName = $row['Technique_name'];
?>
    <ul>
    <li><?php echo $techniqueName; ?></li>
    </ul>
<?php endwhile; ?>

I really have no idea if this makes any sense. 我真的不知道这是否有意义。 I would also like to stay away from javascript if possible, although that is not really a requirement. 如果可能的话,我也想远离javascript,尽管这不是真正的要求。 Basically it is a fun little game idea that would allow the students to practice without doing the same technique a bunch of times and missing other ones. 基本上这是一个有趣的小游戏想法,允许学生练习,而不是多次执行相同的技术,并缺少其他技术。

Any thoughts would be much appreciated. 任何想法将不胜感激。

If you want to "stay away from javascript" you can save the values in an array, which is stored in a session. 如果你想“远离javascript”,你可以将值保存在一个存储在会话中的数组中。 On a reload the array in the session is read and one random value is popped from it. 在重新加载时,会话中的数组被读取,并从中弹出一个随机值。 At each request the array in the session gets smaller until the array is empty. 在每个请求中,会话中的数组变小,直到数组为空。 Then you can reload a new set of techniques from the database. 然后,您可以从数据库重新加载一组新技术。 The source code could look like this: 源代码可能如下所示:

<?php
session_start();
if (!isset($_SESSION['data'])) {
    /*
     * Access the database here. For testing purposes
     * create a dummy array instead
     */
    $array = range(1, 10);
    shuffle($array);
    $_SESSION['data'] = $array;
}
$value = array_pop($_SESSION['data']);
if (!count($_SESSION['data'])) {
    // array is empty, delete it so a new one gets created on the next request
    unset($_SESSION['data']);
}

echo "The current value is: ".$value."<br />\n";

// only for debugging:
if (isset($_SESSION['data'])) {
    var_dump($_SESSION['data']);
} else {
    echo "No array in 'data' set\n";
}

If you want to use javascript you can load all the values in a javascript array. 如果你想使用javascript,你可以加载javascript数组中的所有值。 Then you use javascript to iterate over the array and display the value you want until you reached the end. 然后使用javascript迭代数组并显示所需的值,直到结束。 After that you can send a new request to get a new set of techniques from the database. 之后,您可以发送新请求以从数据库中获取一组新技术。

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

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