【问题标题】:How to get same Random () data from MySQL in two documents at the same time?如何在两个文档中同时从 MySQL 获取相同的 Random() 数据?
【发布时间】:2011-12-03 15:03:51
【问题描述】:

是的,这对我来说很难,我无法弄清楚。我有 2 个 .php 文件(一个带有 iframe,另一个带有注释。所以我们分别称它们为 iframe.phpcomment.php)。

在 iframe.php 文件中,我从数据库中获取随机 url 并将其作为 src of <iframe>:

$sql = mysql_query("SELECT url FROM address_book ORDER BY RAND() LIMIT 1")

在 cmets.php 文件中,我从同一个数据库表中获取随机 url:

$sql = mysql_query("SELECT comment FROM address_book ORDER BY RAND() LIMIT 1")

然后在 index.php 页面上我有按钮:

<button id="nextfLuky" onClick="viewNext();return false;">Randomize</button>

此按钮调用以下 javascript 函数,该函数从 iframe.php 文件插入 &lt;div id="iframe"&gt;&lt;/div&gt;(位于 index.php 页面中)。并且comment.php文件中的评论也插入&lt;div id="comment"&gt;&lt;/div&gt;(也位于index.php文件中)

这是&lt;button&gt;调用的javascript

function viewNext()
{
$("#iframe").load("iframe.php");
$("#coment").load("coment.php");
}

问题是我需要从数据库中获取彼此相关的 url 和评论(如果我使用一个 .php 文件我可以做到,但我需要使用两个)。当我在两个不同的文件中随机获取它们时,它们彼此不相关,当显示在 index.php 文件中时。

那么,如果我在两个不同的文件中随机获取 URL 和评论,如何获取彼此相关的 URL 和评论呢?

补充:数据库表结构

______________________________________
id       |int(11)      |Auto Increment
______________________________________
url      |varchar(255)
______________________________________
comment  |text(1000)
______________________________________

【问题讨论】:

    标签: php javascript mysql database random


    【解决方案1】:

    好的,如果您更喜欢 JQuery,我将用 jquery 制作的解决方案替换 XmlHttp 解决方案。看看信息是如何传递的,我觉得根据自己的需要调整不难。

    您只需要创建一个 getUrl.php 文件来获取 url(我已经创建了一个用于测试目的)。 iframe.php 和 comment.php 将保持原样,*只要确保它们从 $_GET['url'] 获取随机 url。*

    我再次注意到这是根据您当前设计的解决方案,而不是通用或最佳解决方案。

    要测试它,假设这是 index.php(你只需要这里的 viewNext() 其他代码用于测试):

    <html>
    <head>
    <script type="text/javascript" 
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
    </script>
    
    <script type="text/javascript"> 
    
    function viewNext()
    { 
    $.get("getUrl.php",null, 
                function(responseText)
                {   $("#iframe").attr('src', 'iframe.php?url='+responseText); 
                    $("#comment").load('comment.php','url='+responseText);
                }
    );//load ends
    }//viewNext ends
    
    </script>
    </head>
    <body onload="viewNext()">
    <input type=submit value=next id="mySubmit" onclick="viewNext()"><br>
    <IFRAME  id="iframe" WIDTH=450 HEIGHT=100>
    Ybrowser doesn't show IFRAME.  
    </IFRAME>
    <div id="comment"></div>
    </body>
    </html>
    

    那么这是 iframe.php(带有测试代码):

    <?PHP
    echo 'Hello from <font color=red>iframe.php</font> I got the url, here it is:<br>
    <font color=blue>',$_GET['url'],'</font>';
    ?>
    

    那么这是comment.php(带有测试代码):

    <?PHP
    echo 'Hello from comment.php I got the url, here it is:<br>
    <font color=blue>',$_GET['url'],'</font>';
    ?
    

    最后是 getUrl.php(复制粘贴 - 或包含 - 您的代码以在此处获取随机 url 并回显它!):

    <?PHP
    echo 'http://www.url',rand(1,9999),'.com';
    ?>
    

    【讨论】:

    • 好吧,对不起,我的错,我没有说清楚))) 所以首先我加载 index.php,然后我有一个通过 javascript 执行 iframe.php 和 coment.php 的按钮我在我的问题中提到。我可以在 index.php 中获取 url,因为它在其他两个页面之前执行,但是每次用户单击 时我都需要新的随机 url
    • 那里有很多东西 ))) 好吧,我试一试,但我会暂时离开这个话题,以防有人有 jQuery 解决方案,因为我的网站是基于它的。谢谢)))
    猜你喜欢
    • 2014-02-14
    • 2022-10-12
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多