Jump to content

Search the Community

Showing results for tags 'random'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Welcome to PHP Freaks
    • Announcements
    • Introductions
  • PHP Coding
    • PHP Coding Help
    • Regex Help
    • Third Party Scripts
    • FAQ/Code Snippet Repository
  • SQL / Database
    • MySQL Help
    • PostgreSQL
    • Microsoft SQL - MSSQL
    • Other RDBMS and SQL dialects
  • Client Side
    • HTML Help
    • CSS Help
    • Javascript Help
    • Other
  • Applications and Frameworks
    • Applications
    • Frameworks
    • Other Libraries
  • Web Server Administration
    • PHP Installation and Configuration
    • Linux
    • Apache HTTP Server
    • Microsoft IIS
    • Other Web Server Software
  • Other
    • Application Design
    • Other Programming Languages
    • Editor Help (Dreamweaver, Zend, etc)
    • Website Critique
    • Beta Test Your Stuff!
  • Freelance, Contracts, Employment, etc.
    • Services Offered
    • Job Offerings
  • General Discussion
    • PHPFreaks.com Website Feedback
    • Miscellaneous

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


AIM


MSN


Website URL


ICQ


Yahoo


Jabber


Skype


Location


Interests


Age


Donation Link

Found 16 results

  1. SELECT user_id FROM users WHERE active = :active ORDER BY RAND() LIMIT 1 The above query works. But what if the users table is filled with thousands of users? Will this query break or be very slow? If so, what's the alternative solution to this?
  2. Hi All, I'm looking to update a mysql column weekly, where by 5 randomly selected rows are given a new random number, from between 1 and the num_rows / 5. This cycle is looped again until all the entries have a new random group for the week. It is to help mix up the players for teams for 5 aside. It needs to be able to accomodate the posibillity of the num_rows not dividing exactly by 5, using ceil perhaps? As an example Name Group a 1 b 2 c 1 d 1 e 1 f 3 g 2 h 1 i 2 j 2 k 2 Then Next week Name Group a 2 b 1 c 2 d 2 e 1 f 2 g 3 h 1 i 2 j 1 k 1 I hope that makes sense. It'll be automated via cron job. I've been really struggling with it, this is as far as I have got. $totalgroupsraw = $num_rows /5; $totalgroups = ceil ($totalgroupsraw); $i = 1; while ($i<$totalgroups) { $pie = mysql_query("UPDATE table SET columnname=5 order by rand() LIMIT 5"); $i++; }; But as you will no doubt be able to see, this doesn't work well at all. I think I am close, but I just can't seem to sort out a good way to do it. Any help much appreciated. Thanks, Matt
  3. I am working on a quiz app image 1 shows the index.php page image 2 shows the first question image 3 shows the second question image 4 shows the third question image 5 shows the result after completing the quiz image 6 shows the database 'quizzer' and its tables image 7 shows the 'questions' table image 8 shows the 'choices' table THIS LINK CONTAIN ALL THE CODE (and images) I HAVE DONE SO FAR https://www.mediafire.com/folder/g5ao7f5q0fe6y/quiz 1.Now my question is how to select the question RANDOMLY from 'questions' table along with 'choices' (by adding code to the existing file or create a new one). 2.If user refresh/reload the page before starting ('Start Quiz') or click 'Take Again' after finishing the quiz, the question should appear randomly. 3.Basically I want to change the order of question appearing in the browser each time I refresh. 4.My work so far is mentioned above.........Please help me with this "RANDOM" problem !! P.S - Will it be possible, by creating a random function in PHP which will check for repeat questions in a session and check for the 'id' of the question and if it is new display it on the page. If so what should I do and if no then how to do?
  4. Struggling with creating the following: a. The authorized user can select one or both of the two available lottery services (in form of checkboxes) provided by your website. Generate six sets of LOTTO 6/49 numbers (sorted in ascending order) when option one, labeled as “LOTTO 6/49”, is checked. One set of “LOTTO 6/49” numbers consists of 6 unique numbers between 1 and 49 inclusively. Generate six sets of LOTTO MAX numbers (sorted in ascending order) when option two, labeled as “LOTTO MAX”, is checked. One set of “LOTTO MAX” numbers consists of 7 unique numbers between 1 and 49 inclusively. b. When the authorized user submits the form with one or both of the two available services checked, your web server will generate six sets of lottery numbers for each type accordingly and display these numbers of each type of lotteries to the user. Code so far: <?php error_reporting(E_ALL | E_NOTICE); ini_set('display_errors','1'); if(!isset($_SESSION)){ session_start(); } ?> <html> <head> <title> Choose Tickets</title> </head> <body> <h2> Choose your tickets</h2> <?php if (filter_input(INPUT_POST,'form_tickets')){ if(!empty($_SESSION["tickets"])){ $tickets = array_unique( array_merge(unserialize($_SESSION["tickets"]), filter_input(INPUT_POST, 'form_tickets'))); $_SESSION["tickets"] = serialize($tickets); } else { $_SESSION["tickets"] = serialize(filter_input(INPUT_POST, 'form_tickets')); } echo "<p> Your tickets have been selected!</p>"; } ?> <form method="POST" action=""> <p><strong>Select your ticket(s):</strong><br> Lotto 6/49: <input type="checkbox" value="Lotto 6/49"><br /> Lotto Max: <input type="checkbox" value="Lotto Max"><br /> <p><input type="submit" value="submit"/></p> </form> </body> </html> I honestly don't really know what I'm doing.. This class isn't specifically php so he's going through it too quick for me to grasp/learn. I'm sort of going off examples he's given us but I can't seem to fit the pieces together to make this thing. Again, any help is greatly appreciated I have a vague idea of how it's all supposed to work but not sure of the commands to do it or how to structure it.
  5. Hi My knowledge about this stuff is less than basic, I am using this code which I found, changed it little bit with help of logic as original one was not working and make it work but.... I would like to place frames around avatars while avatars are in horizontal order one next to each other...and under them there is nick names So all this is done on Vbulletin board, within one custom widget This is my code now, what I need is frames and that nick names are centered : $member_count = 6; ob_start(); require_once('./includes/functions_user.php'); require_once('./includes/functions_bigthree.php'); // Get Random Members $newusers_get = vB::$db->query_read(" SELECT ".TABLE_PREFIX."user.userid AS userid, ".TABLE_PREFIX."user.username AS username, ".TABLE_PREFIX."user.avatarrevision AS avatarrevision, ".TABLE_PREFIX."customavatar.dateline AS dateline FROM ".TABLE_PREFIX."customavatar LEFT JOIN ".TABLE_PREFIX."user ON ".TABLE_PREFIX."customavatar.userid=".TABLE_PREFIX."user.userid WHERE ".TABLE_PREFIX."customavatar.visible = 1 ORDER BY RAND() LIMIT $member_count"); $output_bits = '<table cellpadding="5" align="center"><tr>'; while($newuser = vB::$db->fetch_array($newusers_get)) { $output_bits .= '<td><a href="member.php?u='.$newuser[userid].'"><img src="image.php?u='.$newuser[userid].'&dateline='.$newuser[dateline].'" alt="'.$newuser[username].'"/ width="120" height="120"><br />'.$newuser[username].'</a></td>'; } $output_bits .= '</tr></table>'; $output = $output_bits; ob_end_clean(); And in attached photo you can see how it looks At least point me in a right direction, not sure what tor read or where to look Thank you Goran
  6. Hi all, I am building a random prize assigner function based on weighted percentages. I am able to assign a basic true/false weighting but am struggling to form a way of selecting an array based prize based on the win rate. Heres my current code: function selectPrize() { $prizes = array( 0 => array( 'name' => 'Top Prize', 'rate' => 50 ), 1 => array( 'name' => 'Middle Prize', 'rate' => 30 ), 2 => array( 'name' => 'Bottom Prize', 'rate' => 20 ), ); // oops } selectPrize(); Could somebody please give me pointers on how i could approach this? Thanks.
  7. Heey, Could some one help me to make like 5 / 10 sum generator with random codes and each sum on a other page? Maybe you got skype and you could help me out and help me? So I got teached also I was thinking about to make a IF - statement to make a If page <= then page 10 he will make random sums and at the end show the result? But I don't know how I need to make that. got this page so far: <?php SESSION_START(); if(!empty($_POST['submit'])) { $pre=$_SESSION['ans'']; $answer=$_POST['ant']; $1= rand(0,30); $2= rand(0,30); $answer=$1+$2; $_SESSION['ans']=$answer; echo "<br>$1+$2="; } else { ?> <html> <head> <title>Math</title> </head> <body> <h2>+</h2> <form method='post' action=''> Your answer:<input name='ans'><br> </select> <br><br> <input name='submit' type='submit' value='next'> <input name='reset' type='reset' value='delete'> </form> <?php } ?> Thank you~
  8. Hey guys. As you can see from my post count i am new here and also new to php. I am trying to modify a code sample of a quiz system that uses mysql database to store all the info such as users,questions,answers,score etc. I have manage to modify the things i want but i am having a hard time making the questions appear in random order when the user takes the test. The table that sores the questions and the answers is named "mst_question" and it has the following structure: que_id | test_id | que_desc | ans1 | ans2 | ans4 | ans4 | true_ans I have tried two things: 1.Modified the query by adding the order by rand() function.I read it is not the fastest way but at this stage i don't care about speed. The results returned were indeed random but the score doesnt seem to work properly and sometimes i get the same question twice. More specific this the query i tried on my effort of eliminating duplicates but as i said it is not working properly: $rs=mysql_query("SELECT DISTINCT * FROM mst_question WHERE que_id IN (SELECT DISTINCT que_id FROM mst_question where test_id=$tid )",$cn) 2. I tried soring the results into an array and use the shuffle function but that didnt work either. I am guessing the query is correct and the reason it doesnt work properly has something to do with the way the results are being displayed but as i said i am a newbie to php/mySQL. This is the modified content of the php file. Any ideas/suggestions are welcome <?php session_start(); include("database.php"); extract($_POST); extract($_GET); extract($_SESSION); if(isset($subid) && isset($testid)) { $_SESSION[sid]=$subid; $_SESSION[tid]=$testid; header("location:quiz.php"); } if(!isset($_SESSION[sid]) || !isset($_SESSION[tid])) //An den exei tethei subject kai testid tous paei sto index.php { header("location: index.php"); } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Online Quiz</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link href="quiz.css" rel="stylesheet" type="text/css"> </head> <body> <?php include("header.php"); $rs=mysql_query("SELECT DISTINCT * FROM mst_question WHERE que_id IN (SELECT DISTINCT que_id FROM mst_question where test_id=$tid ) ORDER BY rand()",$cn) or die(mysql_error()); if(!isset($_SESSION[qn])) { $_SESSION[qn]=0; mysql_query("delete from mst_useranswer where sess_id='" . session_id() ."'") or die(mysql_error()); $_SESSION[trueans]=0; } else { if($submit=='Next Question' && isset($ans)) { mysql_data_seek($rs,$_SESSION[qn]); $row= mysql_fetch_row($rs); mysql_query("insert into mst_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error()); if($ans==$row[7]) { $_SESSION[trueans]=$_SESSION[trueans]+1; } $_SESSION[qn]=$_SESSION[qn]+1; } else if($submit=='Get Result' && isset($ans)) { mysql_data_seek($rs,$_SESSION[qn]); $row= mysql_fetch_row($rs); mysql_query("insert into mst_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error()); if($ans==$row[7]) { $_SESSION[trueans]=$_SESSION[trueans]+1; } echo "<h1 class=head1> Result</h1>"; $_SESSION[qn]=$_SESSION[qn]+1; echo "<Table align=center><tr class=tot><td>Total Question<td> $_SESSION[qn]"; echo "<tr class=tans><td>True Answer<td>".$_SESSION[trueans]; $w=$_SESSION[qn]-$_SESSION[trueans]; echo "<tr class=fans><td>Wrong Answer<td> ". $w; echo "</table>"; mysql_query("insert into mst_result(login,test_id,test_date,score) values('$login',$tid,'".date("d/m/Y")."',$_SESSION[trueans])") or die(mysql_error()); echo "<h1 align=center><a href=review.php> Review Question</a> </h1>"; unset($_SESSION[qn]); unset($_SESSION[sid]); unset($_SESSION[tid]); unset($_SESSION[trueans]); exit; } } $rs=mysql_query("SELECT DISTINCT * FROM mst_question WHERE que_id IN (SELECT DISTINCT que_id FROM mst_question where test_id=$tid )",$cn) or die(mysql_error()); if($_SESSION[qn]>mysql_num_rows($rs)-1) { unset($_SESSION[qn]); echo "<h1 class=head1>Some Error Occured</h1>"; session_destroy(); echo "Please <a href=index.php> Start Again</a>"; exit; } mysql_data_seek($rs,$_SESSION[qn]); $row= mysql_fetch_row($rs); echo "<form name=myfm method=post action=quiz.php>"; echo "<table width=100%> <tr> <td width=30> <td> <table border=0>"; $n=$_SESSION[qn]+1; echo "<tR><td><span class=style2>Que ". $n .": $row[2]</style>"; echo "<tr><td class=style8><input type=radio name=ans value=1>$row[3]"; echo "<tr><td class=style8> <input type=radio name=ans value=2>$row[4]"; echo "<tr><td class=style8><input type=radio name=ans value=3>$row[5]"; echo "<tr><td class=style8><input type=radio name=ans value=4>$row[6]"; if($_SESSION[qn]<mysql_num_rows($rs)-1) echo "<tr><td><input type=submit name=submit value='Next Question'></form>"; else echo "<tr><td><input type=submit name=submit value='Get Result'></form>"; echo "</table></table>"; ?> </body> </html>
  9. Hi, New coder here! I'm making a php If loop that will post my image a set number of times (depending on user input in a text box), and I have that all figured out. However, I can't figure out how to rotate the image a random amount of degrees and have a random width/height for each image. Will rand generate new numbers for each time the image is echo-ed, or will it give the same number for all images? This is what I have: <?php function pic($picture) { $degree = rand(0,360) $num = rand(50,250) $total = 0; while ($total < $picture){ echo '<img src=blabla.gif width="$num" height="$num">'; $total = $total + 1; } } $picture = $_GET['input']; $a = pic($picture); } ?> Thanks in advance!
  10. I'm building a basic photo gallery, and I'm having difficulties with the 'Categories' section. The plan is to display all the picture categories inside divs, and have them 'rotate' their background images through the pictures in the given category. For instance, the 'Nature' category div would change its background every few seconds to a random image in the Nature category. I've succeeded doing this, but the code wasn't very efficient. Basically, I've had a JavaScript file gather all the categories (through uniquely assigned IDs) and "$.post" them to a PHP file which polled images with the given category from a database, loaded them into an array and returned a random key. Then, using intervals it updated the background every couple of seconds. But I'm looking for a better way to do it. I'd prefer to poll the database only once, at the start of the page, and then just let the JS file reload the rand() function each time the interval is called; but I'm not sure how to pass the polled data to the file that call the random function. Any ideas how to do this? Obviously, I'm not expecting anybody to write the code for me, I just need some assistance. Also, sorry if all this is a little unclear, it's 4am here, and I'm falling asleep; if you need me to explain just let me know. Thanks.
  11. I'm building a basic photo gallery, and I'm having difficulties with the 'Categories' section. The plan is to display all the picture categories inside divs, and have them 'rotate' their background images through the pictures in the given category. For instance, the 'Nature' category div would change its background every few seconds to a random image in the Nature category. I've succeeded doing this, but the code wasn't very efficient. Basically, I've had a JavaScript file gather all the categories (through uniquely assigned IDs) and "$.post" them to a PHP file which polled images with the given category from a database, loaded them into an array and returned a random key. Then, using intervals it updated the background every couple of seconds. But I'm looking for a better way to do it. I'd prefer to poll the database only once, at the start of the page, and then just let the JS file reload the rand() function each time the interval is called; but I'm not sure how to pass the polled data to the file that call the random function. Any ideas how to do this? Obviously, I'm not expecting anybody to write the code for me, I just need some assistance. Also, sorry if all this is a little unclear, it's 4am here, and I'm falling asleep; if you need me to explain just let me know. Thanks.
  12. Hi, I'm just messing around with a website and I'm just wondering how would I make it so that it changes the background image to a random one? I was thinking something around this: Images = new Array Images[1] = "Background-1.jpg" Images[2] = "Background-2.jpg" Images[3] = "Background-3.jpg" Images[4] = "Background-4.jpg" But Instead of it just changing when you refresh the page every time, I was hoping to have a timer or something fixed to it? Would it be possible to have it so it gets the time of the server and if that time is equal it'll change? Help will be much appreciated
  13. As a part of a project I'm working on, I just updated an old function of mine. Seeing as a lot of people still keep using time-based[1] techniques for generating password, I thought I should share this one with you all. Hopefully someone will find it useful. /** * Generates and returns a random password, of a random length between min and max. * * Hard limits are minimum 10 chars and maximum 72. * * @author Christian Fagerheim (Fagerheim Software) * @link www.fagsoft.no * @license Creative Commons Attribution-ShareAlike 3.0. http://creativecommons.org/licenses/by-sa/3.0/. * * @param int[optional] $minLen = 10 * @param int[optional] $maxLen = 14 * @return string */ function generatePassword ($minLen = 10, $maxLen = 14) { if ($minLen < 10) { $minLen = 10; } // Discard everything above 72 characters for the password (bcrypt limitation). if ($maxLen > 72) { $maxLen = 72; } $numChars = mt_rand ($minLen, $maxLen); // Create an secure random password, and cut it down to length. $password = base64_encode (mcrypt_create_iv (256, MCRYPT_DEV_URANDOM)); $password = substr ($password, 0, $numChars); // Define the replacements sets and values for strtr (). $find = "10lIO"; $replace = "_-*!?"; // Replace the similar-looking characters with special characters. $password = strtr ($password, $find, $replace); // Save the hashed password in the object, and return it to calling method. return $password; } A copy can be found here: http://pastebin.com/se0YfEx1 [1]Time-based techniques are bad because they are very easy to predict, meaning that an attacked can quite easily guess the generated value as long as he knows the time of a request. Something which completely invalidates the point of having it be random in the first place.
  14. so I'm in the process of making a .gif uploading website, and this is my code for "upload_file.php". <?php $allowedExts = array("jpg", "jpeg", "gif", "png"); $extension = end(explode(".", $_FILES["file"]["name"])); if ((($_FILES["file"]["type"] == "image/gif")) && ($_FILES["file"]["size"] < 5242880) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; if (file_exists("i/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "i/" . $_FILES["file"]["name"]); echo "Stored in: " . "i/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> How can I add a random image name? Preferably 6-8 digits of only #0-9. That way the image name is simple instead of being specific to capital letters etc. Please be as specific with directions as possible, because I'm still learning PHP, and that's one of the main reasons I'm making this website. P.S. Bonus points if you can tell me how to redirect straight to the image without the page that shows the file info.
  15. Hi to all i am trying to create a right variable so can display random questions. Let me explain feather My first question is a checkbox questions with 5 answers. every answer have a 5 more group questions. I need a variables code to do this: Every time the user checks (in first checkbox) the answer 1 and 3 the code automatically selected to display the group of 1 or 3 (not the 2 together) The same philosophy must have and the outher questions, if the answer (on first) is 2, 4 and 5 must display the group of question 2 or 4 or 5. Thanks a lot!
  16. Hey everyone! This is my first post on this site, and I have a question/problem. Well, I have 2 databases (one contains tables and the other contains users). I want users to pick 3 tables in orders -like first_choice, second_choice and third_choice - and I want to have an action that seats users for their choices for each table. Like for first table, the function would search every user in database and find the one that has the first table's ID (1) for the first_choice. And if table is full, then the function should go for the second table. If not, when all the tables are cycled for once, function should run again with second_choices. And same thing for the third_choices. Well, on paper it seemed pretty easy. But not working with PHP and MySQL for a while, hasn't been a good thing for me. If you could help me with codes, resources or ideas, I'd really be thankful. I don't have too much time, so yeah... THANKS!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.