Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    axd
    axd is offline
    New Coder
    Join Date
    Mar 2009
    Location
    Leeds, UK
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    bit stuck...any takers?

    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
    Code:
    $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

    Code:
    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
    Last edited by axd; 06-20-2010 at 09:58 PM.

  • #2
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    can we see the whole file contents?
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #3
    axd
    axd is offline
    New Coder
    Join Date
    Mar 2009
    Location
    Leeds, UK
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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....

    Code:
    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

  • #4
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    well to put a php var between [ and ] you dont use the quotes you just do this $quiz[$question];
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #5
    axd
    axd is offline
    New Coder
    Join Date
    Mar 2009
    Location
    Leeds, UK
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #6
    axd
    axd is offline
    New Coder
    Join Date
    Mar 2009
    Location
    Leeds, UK
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #7
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Glad you have sorted it now =)
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • Users who have thanked DJCMBear for this post:

    axd (06-21-2010)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •