douke6027 2011-10-08 16:13
浏览 29
已采纳

如何在两个文档中同时从MySQL获取相同的Random()数据?

Right this is tough one for me and I can't figure it out. I have 2 .php files (one with iframe other with comment. so lets call them iframe.php and comment.php respectively).

In iframe.php file I get random url from database and put it as src of <iframe>:

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

In comments.php file I get random url from same database table:

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

Then on index.php page I have button:

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

This button calls following javascript function that inserts from iframe.php file into <div id="iframe"></div> (located in index.php page). And comment from comment.php file is also inserted in <div id="comment"></div> (Also located in index.php file)

And this is javascript called by <button>

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

The problem is that I need to get url and comment that are related to each other from database (I can do it if I use one .php file, but I need to use two). And as I get them randomly in two different files they are not related to each other, when displayed in index.php file.

So how can I get url and comment that are related to each other if I get each one randomly in two different files?

Addition: Database Table Structure

______________________________________
id       |int(11)      |Auto Increment
______________________________________
url      |varchar(255)
______________________________________
comment  |text(1000)
______________________________________
  • 写回答

1条回答 默认 最新

  • duanri1985 2011-10-08 17:11
    关注

    Okay if you prefer JQuery I am replacing the XmlHttp solution with one made with jquery. Check it out out how the information is passed, I think is not difficult to adjust it to your needs.

    You only have to create a getUrl.php file to get the url (I have created one for testing purposes). iframe.php and comment.php will remain intact, *just make sure they get the random url from $_GET['url'].*

    Again I notice this is a solution according to your current design, not a general or best one.

    To test it, let's say this is index.php (you only need the viewNext() from here the other code is for testing):

    <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>
    

    Then this is iframe.php (with testing code):

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

    Then this is comment.php (with testing code):

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

    And finally getUrl.php (copy paste -or include- your code to get random url here and just echo it!):

    <?PHP
    echo 'http://www.url',rand(1,9999),'.com';
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。