简体   繁体   English

将返回的SQL数组与随机结果选择器一起使用

[英]Using Returned SQL Array with Random Result Picker

I am writing a new feature plugin for a website I develop and am stuck with how to go about implementing such feature. 我正在为我开发的网站编写一个新的功能插件,并且对如何实现这种功能一无所知。 I'm trying to return an SQL query to an array that I can then pass to a function in either PHP or JavaScript, to perform a random option picker, and display the results to the user. 我试图将SQL查询返回到数组,然后将其传递给PHP或JavaScript中的函数,以执行随机选项选择器,并将结果显示给用户。 I'm trying to perform the random picker by an on-click functionality. 我正在尝试通过单击功能执行随机选择器。

So far, I have figured out how to successfully select my SQL query, return statements, and in JavaScript, work with a random option picker. 到目前为止,我已经弄清楚了如何成功选择SQL查询,返回语句,以及如何在JavaScript中使用随机选项选择器。 However, I have not been able to find a good way to combine all these things together to produce my result. 但是,我一直找不到找到将所有这些东西结合在一起以产生结果的好方法。

I was wondering if anyone knew of a way to do this. 我想知道是否有人知道这样做的方法。 Is it better to perform the entire program functionality in PHP or better to pass it to a JavaScript function and return results through the JS code? 在PHP中执行整个程序功能还是将其传递给JavaScript函数并通过JS代码返回结果更好?

Here are some examples of my code. 这是我的代码的一些示例。 Please assume that my query works. 请假设我的查询有效。 I do not need the display option in my code. 我的代码中不需要显示选项。 It is implemented to check query results. 它用于检查查询结果。 I was hoping to hide the array in the real plugin from users and use the returned results for the on-click functionality attached to an HTML element. 我希望对用户隐藏真正的插件中的数组,并将返回的结果用于附加到HTML元素的单击功能。

<?php
    function choices() {
        $config = parse_ini_file(*database file*);
        $con = mysqli_connect(*database information*);
        if(!$con){
            exit("Please try again later."); 
        }

        $query = "SELECT * FROM *query*";
        $result = @mysqli_query($con, $query);
        $num = mysqli_num_rows($result);
        if ($num > 0) {

            echo "<p>There are currently $num restaurants.</p>\n";

            echo '<table width="60%">
            <thead>
            <tr>
                <th align="left">Restaurant Title: </th>
                <th align="left">Link: </th>
            </tr>
            </thead>
            <tbody>
        ';

            while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
                echo '<tr><td align="left">' . $row['post_title'] . '</td><td align="left">' . $row['guid'] . '</td></tr>
                ';
            }

            echo '</tbody></table>';

            mysqli_free_result($result);
        }

        else {
            echo '<p class="error">The results could not be retrieved.</p>';
        }

        mysqli_close($con);
    }

    $foo = choices();
?>

My JS code. 我的JS代码。

var favorites = ["choice #1, choice #2, choice #3, choice #4"];
var favorite = favorites[Math.floor(Math.random() * favorites.length)];

Thank you very much for any input regarding this question! 非常感谢您提供有关此问题的任何意见! I understand this is a rather lengthy question and I do appreciate time took to help me understand it. 我知道这是一个相当漫长的问题,并且我感谢您花时间帮助我理解它。

Why not just have your database return a single random result since that's all you need? 为什么不只让数据库返回一个随机结果,因为这就是您所需要的?

SELECT * FROM tbl ORDER BY RAND() LIMIT 1

Take a look at this post about getting random rows 看看这篇关于获取随机行的文章

Put the results of the query in a PHP array. 将查询结果放入PHP数组中。 Then use json_encode() to convert that to a Javascript array. 然后使用json_encode()将其转换为Javascript数组。 Then the onclick functionality can display a random element of the array. 然后onclick功能可以显示数组的随机元素。

$array = array();
while ($row = mysqli_fetch_assoc($result)) {
    $array[] = $row['post_title'];
}
?>
<script>
var favorites = <?php echo json_encode($array); ?>;
$("#button").click(function() {
    var favorite = favorites[Math.floor(Math.random() * favorites.length)];
    alert(favorite);
});
</script>

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

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