Link to home
Create AccountLog in
Avatar of TLN_CANADA
TLN_CANADAFlag for Afghanistan

asked on

Taking random record from MYSQL DB using PHP

Hi all,

I have a webpage setup that when the user clicks the submit button a pop-window appears and I want the text in the window to be selected at random from a field in the database.

Could someone give me an example of how to do this?

I have the database setup and connecting correctly. The name of the table where I want to take the text from is called gen_enquiry_table and the field is called gen_enq_txt
<?php 
 // Connects to your Database 

 mysql_connect("bubblesdb.db.10022112.hostedresource.com", "bubblesdb", "xxx") or die(mysql_error()); 

 mysql_select_db("bubblesdb") or die(mysql_error()); 
 
 ?>

<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
	
  function modalPopup() {    
    $("#screen").css("display", "block");
    $("#result").css("display", "block");
  }
  function hide() {
    $("#screen").css("display", "none");
    $("#result").css("display", "none");
  }
</script>
<style type="text/css">
.screen {
        position:fixed;
        top:0px;
        left:0px;
        display:none;
        width:100%;
        height:100%;
        background-color: #333; 
        -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"; 
        filter: alpha(opacity=50); 
        opacity: 0.5;
        z-index:200;
    }
    .result {
        position: absolute;
        top: 100px;
        left: 125px;
        width: 140px;
        height: 90px; 
        background-color:#fff;
        padding:15px;
        z-index:300;
        display:none;
    }
.style1 {font-size: 36px}
</style>
</head>
<body>
<div>
    <p><span class="style1">General Enquiry Page</span> <br>
      <br>
      <img src="Pictures/question.jpg" width="352" height="438"></p>
    <p><input type="button" value="Get an Enquiry Question " onClick="javascript:modalPopup()" />
    </p>
</div>
<div>
    <div id="screen" class="screen"></div>
    <div id="result" class="result">
        <div>
            <a href="#" onClick="javascript:hide()" title="Hide Preview">Close</a>
        </div>
    </div>
</body>
</html>

Open in new window

Thanks,

Derek
Avatar of Robert Granlund
Robert Granlund
Flag of United States of America image

@Derek is this what you mean or do you need the entire query written?

SELECT gen_enq_txt FROM gen_enquiry_table ORDER BY RAND() LIMIT 1

Open in new window

Avatar of TLN_CANADA

ASKER

Hi rgranlund,

If you could write out the full query that would be greatly appreciated and how to display it on the pop up window.

Many thanks,

D
ASKER CERTIFIED SOLUTION
Avatar of Robert Granlund
Robert Granlund
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thank you rgranlund,

Could you tell me where to insert the <?php echo $quote; ?> so that it displays on the pop-up window?
<div id="screen" class="screen"></div>

Open in new window


Should be:

<div id="screen" class="screen"><?php echo $quote; ?> </div>

Open in new window

Thank you! I put it after the other div tag and it's working now. Would it be easy to change the script so if I close the pop-up window and press the submit button for the second time, it takes a different record from the database?
No, you would need to adjust the code a little bit.
Okay, thanks I will play with it and open a new question if necessary.
if (isset "") going off of the submit button?
I'm not sure I understand what you're asking?