EKsparky Posted April 21, 2007 Share Posted April 21, 2007 Hi all ive been trying to learn php and mysql so I can make a quiz for my web site My quiz will include 10 different categories each category will issue random questions from db So far after much reading I managed to create a simply database with each table contain id question and answer columns. (I could'nt smile wide enough when I executed quiz.sql in phpmyadmin and it worked ) . Much reading later and I ve managed to get a query that works what I need to do now is make it in to a function so I can call question and answer from my main quiz page I guess I will then make 10 functions in a functions.ini calling each table depending on category selected on main quiz page #select a query $sql = "SELECT * from my_table ORDER BY RAND() LIMIT 1"; #excute query $dbname = mysql_query ( $sql ); #write datal while ( $row = mysql_fetch_array ( $dbname ) ) { echo ( "question: " .$row["question"] ); echo (" Answer: " .$row["answer"] ); } I am probably way of and need to do much more reading Thanks for any help Quote Link to comment Share on other sites More sharing options...
keeB Posted April 21, 2007 Share Posted April 21, 2007 I am developing a solution right now.. give me about 5 minutes. Quote Link to comment Share on other sites More sharing options...
keeB Posted April 21, 2007 Share Posted April 21, 2007 Ok I am done, let me explain what is about to follow. I wrote this using a basic array structure the looks much like the following: Array ( [category_a] => Array ( [question_1] = answer1 ) ) From here, I wrote 2 functions, getRandomCategory() and getQuestions() which both use the count function (returns the amount of items in an array) to execute the upper bound of the rand function. Instead of explaining it further, I'll provide my code, and see if you have any questions. It should be easily adaptable to your current situation. <?php $items = array("category_a" => array("question_1" => "answer1")); function getRandomCategory($items) { //return sub-array with questions $max_num = count($items); return $items(rand(0,$max_num - 1)); } function getQuestion($questions) { //return random question $max_questions = count($questions); return $questions(rand(0, $max_questions - 1)); } print_r (getQuestion(getRandomCategory($items))); ?> Quote Link to comment Share on other sites More sharing options...
EKsparky Posted April 21, 2007 Author Share Posted April 21, 2007 Thanks for such a speedy response As I said am very new to php and am only 1/4 way though my book I bought I've read your code and can sort of see where its going but not quite sure how I implement it with mine If you could give me a ruff example I'll probably understand it further and be away Again thanks for such a quick reply Quote Link to comment Share on other sites More sharing options...
keeB Posted April 21, 2007 Share Posted April 21, 2007 The problem is with your current table structure, and without more knowledge in SQL I can't accurately give you information which would help. I would have set the database up in the following manner: CREATE TABLE categories ( categoryID int primary key auto_increment, categoryName text ) CREATE TABLE my_table ( id int primary key auto_increment, categoryID int, question text, answer text ) This solves the problem of trying to get a random category, since you can generate a random number by getting the amount of categories in your category table. <?php $count_sql = "SELECT COUNT(*) FROM categories"; $category_count = mysql_fetch_array(mysql_query($count_sql)); #total amount of categories is stored in $category_count[0] #now we need to generate a random number $random_category = rand(1, $category_count[0]); #next, incorportate findings in to main sql script $sql = "SELECT * from my_table where categoryId = $randome_category ORDER BY RAND() LIMIT 1"; #excute query $dbname = mysql_query ( $sql ); #write datal while ( $row = mysql_fetch_array ( $dbname ) ) { echo ( "question: " .$row["question"] ); echo (" Answer: " .$row["answer"] ); } ?> The limitation is that you'll need at least 1 question/answer for every category. But, I think that's your intent anyway. Edit: updated a problem in the rand() function. Quote Link to comment Share on other sites More sharing options...
EKsparky Posted April 21, 2007 Author Share Posted April 21, 2007 Am totally lost now My mysql are set up like this and there are around 40 questions in each table CREATE TABLE my_table ( ID INT UNSIGNED NOT NULL AUTO_INCREMENT, question char(30) NOT NULL, answer char(30) NOT NULL, PRIMARY KEY (ID) ); And I thought I was getting there as the code I supplied in my first post would generate a different question and answer every time I refreshed the page. So I thought I was close to creating my main quiz page and calling questions and then answer (just on a revel answer button) I just thought I some how need to change my code in to 2 variables some how ie $row["question"] =$question $row["answer"] =$answer then somehow place all this into a function so I could just call the vars obviously I have a long way to go yet and much more to read and more goggling to do Quote Link to comment Share on other sites More sharing options...
keeB Posted April 21, 2007 Share Posted April 21, 2007 Well, keep in mind, what you're trying to throw in the loop is the notion of categories. Your current script is working perfectly if: - You don't need to generate a certain number of questions per category With your current script, you have 2 options: 1. Modify it to do something like I did above, or 2. Keep your same script, and do all of the managing on the script side, which would be a huge pain. Maybe I will write an article on the easiest way to accomplish this. Quote Link to comment Share on other sites More sharing options...
EKsparky Posted April 21, 2007 Author Share Posted April 21, 2007 Look forward to your article thanks for your help I can see what you mean just unsure how to complete it yet but i will carry on till i get it done . Quote Link to comment Share on other sites More sharing options...
per1os Posted April 21, 2007 Share Posted April 21, 2007 If you want a "reveal answer" button you want to use HTML/CSS/JS with the div tag and the style="display:none"; than an onclick on the link "reveal answer" sets the style for the div tag to be "display:block;" IE: <html> <head> <script type="text/javascript"> function displayDiv(myid) { var myDiv = document.getElementById(myid); myDiv.style.display = "block"; return false; } </script> <?php #select a query $sql = "SELECT * from my_table ORDER BY RAND() LIMIT 1"; #excute query $dbname = mysql_query ( $sql ); #write datal $i=0; while ( $row = mysql_fetch_array ( $dbname ) ) { echo ( "question: " .$row["question"] . "<br />"); echo ('<div style="display:none;" id="answerDiv' . $i . '">Answer: ' .$row["answer"] . '</div> <a href="#" onClick="displayDiv(\'answerDiv'.$i++.'\');">Reveal!</a><br /><br />'); } ?> That will allow you to reveal the answer when "Reveal!" is clicked. Quote Link to comment Share on other sites More sharing options...
EKsparky Posted April 21, 2007 Author Share Posted April 21, 2007 thats what ive been tring to do I tried your code and no luck I found some javascriprt code thats works quite well the only problem i am having now Is i created a function that i can call but the javascript has id any one got any ideas how i can change the id sc1 sc2 sc3 etc <?php function my_func () { #select a query $sql = "SELECT * from my_table ORDER BY RAND() LIMIT 1"; #excute query $dbname = mysql_query ( $sql ); #write data while ( $row = mysql_fetch_array ( $dbname ) ) { $Q = ( $row["question"] ); $A = ( $row["answer"] ); echo '<body><h3 onClick="expandcontent(\'sc1\')" style="cursor:hand; cursor:pointer">'.$Q.'</h3> <div id="sc1" class="switchcontent">Answer '.$A.'</div>'; } } ?> now if i call this function from anther page <?php include 'includes/functions.php'; include 'javascript/open.js'; echo my_func ( ) ; echo "<br>"; echo ten_korean ( ) ; ?> Now this work fine if i use one call of my function but no more as i need to add sc1 to sc2 and so on poor call no more than 10 thou Ii tried doing something like this but no luck <?php function my_func () { #select a query $sql = "SELECT * from my_table ORDER BY RAND() LIMIT 1"; #excute query $dbname = mysql_query ( $sql ); #need to make sc1 loop for javascript for ($i = 1; $i <12; $i++); #write data while ( $row = mysql_fetch_array ( $dbname ) ) { $Q = ( $row["question"] ); $A = ( $row["answer"] ); echo '<body><h3 onClick="expandcontent(\'sc'.$i.'\')" style="cursor:hand; cursor:pointer">'.$Q.'</h3> <div id="sc1" class="switchcontent">Answer '.$A.'</div>'; } } ?> that again works once any ideas would be great Quote Link to comment Share on other sites More sharing options...
Barand Posted April 22, 2007 Share Posted April 22, 2007 Go with Keeb's advice and have 2 tables, qcategory and question CREATE TABLE qcategory ( catID int primary key not null auto_increment, category varchar(50) ) CREATE TABLE question ( id int primary key not null auto_increment, catID int, question text, answer text ) Then you can get a random question from each category with <?php $sql = "SELECT * FROM (SELECT c.category, q.question, q.answer FROM question q INNER JOIN qcategory c ON q.catID = c.catID ORDER BY c.catID, RAND()) as a GROUP BY category"; ?> Quote Link to comment Share on other sites More sharing options...
EKsparky Posted April 22, 2007 Author Share Posted April 22, 2007 Ive got it working just not calling as function just using the code for the function multiable times adding a number to the id for the javascript button Not the best way i no but working Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.