...

View Full Version : bit stuck...any takers?



axd
06-20-2010, 08:46 PM
Hi. I am creating a quiz site. (not that thats important) and I have hit a wall.

I have my questions/answers/optional wrong answers in a database, and want to call them out. Is it at all possible to use variables within the square braces to call out fields? eg [ and ]. Here is some code, for a much cleaer example of what i want to do. (as I get nothing returned when i use this code)


Heres what i have originally



$getquiz = mysql_query("SELECT * FROM `questions` WHERE `uid` = '$quizid'")
or die(mysql_error());

while($quiz = mysql_fetch_array( $getquiz ))

{
echo "<div class=\"quiztitle\">".$quiz['quiz_name']."</div>";

if ($quiz['add_com'] > "") {
echo "<div class=\"quizsubtitle\">".$quiz['add_com']."</div>";
}

$i = 1;
while ($i < 21){

$question = "quest_".$i;
$corans = "corr_ans_".$i;
$opt1 = "opt_ans1_".$i;
$opt2 = "opt_ans2_".$i;
$opt3 = "opt_ans3_".$i;


//this is theline I am struggling with, I want to put the above variable in place of a field name below

echo $quiz['$question'];

$i++;
}



}


This all returns no data at all. Any help would be appreciated.

To clarify, I want to put a variable inside the bit which says



echo $quiz['$question'];

//The original line read as :

echo $quiz['quest_01'];

// as you can see from the variables above that, I am trying to use the while loop to create outputs for db fields quest_01, quest_02...etc etc



instead of using a field name from the db. That way i can echo out all 20 questions with a few lines of code instead of coding each question in.

Thankyou in advance, and if none of that made sense please ask for further details


Dan

DJCMBear
06-20-2010, 08:54 PM
can we see the whole file contents?

axd
06-20-2010, 09:29 PM
That pretty much is the whole file contents. Its only the variable I am trying to insert instead of a field name in the braces, just wondering if there is a way. but heres all i have in the file....



include ("include/header.php");
include ("include/database.php");
include ("include/funx.php");
include ("include/m2.php");

$quizid = mysql_real_escape_string($_GET['qz']);

$getquiz = mysql_query("SELECT * FROM `questions` WHERE `uid` = '$quizid'")
or die(mysql_error());

while($quiz = mysql_fetch_array( $getquiz ))

{

echo "<div class=\"quiztitle\">".$quiz['quiz_name']."</div>";

if ($quiz['add_com'] > "") {
echo "<div class=\"quizsubtitle\">".$quiz['add_com']."</div>";
}

$i = 1;
while ($i < 21){

$question = "quest_".$i;
$corans = "corr_ans_".$i;
$opt1 = "opt_ans1_".$i;
$opt2 = "opt_ans2_".$i;
$opt3 = "opt_ans3_".$i;

echo $quiz['$question'];

$i++;
}
}




Thankyou

DJCMBear
06-20-2010, 09:33 PM
well to put a php var between [ and ] you dont use the quotes you just do this $quiz[$question];

axd
06-20-2010, 09:46 PM
Hmmm, tried that, thought that was how it was to be done. but seemingly it does not work. I will keep trying at it though, thankyou

axd
06-20-2010, 09:48 PM
Scrap that, you are a diamond, mate. I was putting the single quotes in, forgot to remove them when i put in the variable. Works a treat and thankyou very much indeed.

Dan

DJCMBear
06-20-2010, 09:56 PM
Glad you have sorted it now =)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum