Functions


#1

Hi all I am a php MySQL noob but I needed to make a quiz for one of my web sites.
Now its all up and ruining and I still cant believe I managed it :D
But now its working I want to iron out a few problems
I have six and then 3 questions on each page now I run the same piece of code 6 times for the six questions then anther piece of code 3 times for the other 3.
I’ve read about functions and have played around with them no problem but I just can’t seem to put my MySQL query into a function .
If I could do this it would mean less code but more inportant I think I could get the id and use an else statement to stop repeating questions.

This i s the code i run six times per page ive changed the output which i have echoing in to a javascript button just to make it easier to read

[php]
#select a query
$sql = “SELECT * from quiz_korean WHERE cat = ‘one_kup_korean’ 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 $Q;
echo $A;

}
[/php]

Now if i could put into a function
i guess i could grap the id from my table and use some an if else statment to stop the same question being outputed.

I assumed i could put above code into a function less the 2 echos

then just use

[php]
my_func ()
{ echo $Q ;
echo $A;
}[/php]

Thanks fro any help .
view quiz Here


#2

What is it or isn’t doing that it shouldn’t be? I have been able to run querys in functions just fine. Maybe simple misunderstanding of they work.

Just in case you have checked here: http://www.php.net is a good place to check into the syntax user-defined functions.


#3

When i put it into a function it never returns the Var ie $A or $Q
I am not sure where i need to put echo $A and $Q in the function or from what i ve read may be even return which i ve tried
May be it because of the way i have my buttons echoing the the results.
ill post one of the the quiz pages so you can see exactly how i have it,
and may be you could post the exact code you used to make it work in the function
Thanks in advance

[php]

<?PHP include 'modules/quiz/includes/config.php'; include 'modules//quiz/includes/opendb.php'; echo ''; echo ''; ############ STRART TABLE ############ echo '
'; echo ''; ############ REFRESH BTN ############ echo ''; ########## QUESTION ONE ################## #select a query $sql = "SELECT * from quiz_korean WHERE cat = 'ten_kup_korean' 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 ''; echo ' '; echo ''; } ########## IMAGE ################## echo''; ########## QUESTION TWO ################## #select a query $sql = "SELECT * from quiz_korean WHERE cat = 'ten_kup_korean' 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 ''; echo ' '; echo' '; } ########## QUESTION THREE ################## #select a query $sql = "SELECT * from quiz_korean WHERE cat = 'ten_kup_korean' 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' '; echo ' '; echo''; } ########## QUESTION FOUR ################## #select a query $sql = "SELECT * from quiz_korean WHERE cat = 'ten_kup_korean' 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' '; echo ' '; echo' '; } ########## QUESTION FIVE ################## #select a query $sql = "SELECT * from quiz_korean WHERE cat = 'ten_kup_korean' 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''; echo ' '; echo' '; } ########## QUESTION SIX ################## #select a query $sql = "SELECT * from quiz_korean WHERE cat = 'ten_kup_korean' 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' '; echo ' '; echo''; } ########## QUESTION SEVEN ################## #select a query $sql = "SELECT * from quiz_term WHERE cat = 'ten_kup_term ' 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' '; echo ' '; echo''; } ########## QUESTION EIGHT ################## #select a query $sql = "SELECT * from quiz_term WHERE cat = 'ten_kup_term ' 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' '; echo ' '; echo''; } ########## QUESTION NINE ################## #select a query $sql = "SELECT * from quiz_term WHERE cat = 'ten_kup_term ' 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' '; echo ' '; echo '

TAGB 10th Kup Theory Test

'.$Q.'  '.$A.'
'.$Q.'  '.$A.'
'.$Q.'  '.$A.'
'.$Q.'  '.$A.'
'.$Q.'  '.$A.'
'.$Q.'  '.$A.'
'.$Q.'  '.$A.'
'.$Q.'  '.$A.'
'.$Q.'  '.$A.'
'; } echo '

Back
'; ?>

[/php]


#4

Why not do something like this:

[php]
#select a query
$sql = “SELECT * from quiz_term WHERE cat = 'ten_kup_term ’ ORDER BY RAND() LIMIT 10”; // Or however many questions you want
[/php]


#5

Hi , yes i did try that but as i am a php noob i could not figure out how to turn the query in to 10 differnt question and answer vars i guess using some kind of array
But as i said i am a complete noob and am learning more and more each day
May be you could show me the code to do that
Thanks :D


#6

[php]<?
$q = “SQL STATEMENT”;
$r = mysql_query($q) or die("ERROR REASON: " . mysql_error());

while($row = mysql_fetch_array($r))
{
$variable = $row[‘field_name’];
$variable2 = $row[‘field_name2’];
}
?>[/php]

This will run through all the results returned by SQL and you can referance the fields you selected as I did above.

Now, from what I gave you should have more than enough to figure out how to make this work for you. :wink:


#7

In fact, the code is already there … 9 times … Take out 8 and change the LIMIT clause on the one left over to LIMIT 9 or somethin. That should do the trick.


#8

@zyppora

:oops:

#9

Heheh, that’s okay. I only noticed it because I kept scrolling past the same piece of code time after time :wink: