January 23, 2015

Using ‘for loops’ and ‘variable variable names’ to do your heavy lifting

I recently created a trivia game using WordPress as the CMS.
http://classof94.ca/

The problem I was trying to solve was, how do I make a trivia system in which a single post houses all the questions and answers; and how do I make it easy to enter this data in a single form that looks similar to the end interface, for clarity.

I ended up with a solution using post meta data in a huge array of custom fields, but since there were over 50 questions per “game” in this Jeopardy style trivia experiment, I had a really bloated and horribly repetitive functions.php file. I thought there is so much repetition and the only things really changing are the field names and their associated variable names. I wondered if there was a way to use “for loops” to generate the page and variable names automagically. Turns out, I was asking the right questions. I managed to get my code set down from over 500 lines of code to just under 50 lines.

Steps and logic are outlined below:

Strategic Naming of Repeating Elements: (snore)

In this example, this is a trivia game with rounds, categories and questions, I’ve come up with a simple naming convention using shorthand for my field names and PHP variables.

Examples:

Round 1 + Category 2 + Question 1 = r1c2q1

Round 1 + Category 2 + Answer 2 = r1c2a1

Once you figure out some clever and consistent shorthand using numbers, you’re all set to let your code do the heavy lifting for you.

Since this game contains over 100 fields that are getting saved into a database individually, it would suck if we had to write all these variable names one by one to build and populate our form. So, here’s a neat little concept:

The variable variable:

Let’s say I have 2 rounds of trivia, each round has 5 categories, each with 5 questions. That’s potentially 50 variables to write, and keep track of. But what are questions without answers, now you have 100 variables to write and keep track of! Your functions.php file should now be getting bloated and difficult to navigate.

[pastacode lang=”php” message=”50 Variables Longhand.” highlight=”” provider=”manual”]

<php
// round 1
  $r1 = get_post_meta($id, 'r1', true);
// category 1
  $r1c1 = get_post_meta( $id, 'c1', true);
  $r1c1q1 = get_post_meta( $id, 'c1q1', true);
  $r1c1q2 = get_post_meta( $id, 'c1q2', true);
  $r1c1q3 = get_post_meta( $id, 'c1q3', true);
  $r1c1q4 = get_post_meta( $id, 'c1q4', true);
  $r1c1q5 = get_post_meta( $id, 'c1q5', true);
// category 2
  $r1c2 = get_post_meta( $id, 'c2', true);
  $r1c2q1 = get_post_meta( $id, 'c2q1', true);
  $r1c2q2 = get_post_meta( $id, 'c2q2', true);
  $r1c2q3 = get_post_meta( $id, 'c2q3', true);
  $r1c2q4 = get_post_meta( $id, 'c2q4', true);
  $r1c2q5 = get_post_meta( $id, 'c2q5', true);
// category 3
  $r1c3 = get_post_meta( $id, 'c3', true);
  $r1c3q1 = get_post_meta( $id, 'c3q1', true);
  $r1c3q2 = get_post_meta( $id, 'c3q2', true);
  $r1c3q3 = get_post_meta( $id, 'c3q3', true);
  $r1c3q4 = get_post_meta( $id, 'c3q4', true);
  $r1c3q5 = get_post_meta( $id, 'c3q5', true);
// category 4
  $r1c4 = get_post_meta( $id, 'c4', true);
  $r1c4q1 = get_post_meta( $id, 'c4q1', true);
  $r1c4q2 = get_post_meta( $id, 'c4q2', true);
  $r1c4q3 = get_post_meta( $id, 'c4q3', true);
  $r1c4q4 = get_post_meta( $id, 'c4q4', true);
  $r1c4q5 = get_post_meta( $id, 'c4q5', true);
// category 5
  $r1c5 = get_post_meta( $id, 'c5', true);
  $r1c5q1 = get_post_meta( $id, 'c5q1', true);
  $r1c5q2 = get_post_meta( $id, 'c5q2', true);
  $r1c5q3 = get_post_meta( $id, 'c5q3', true);
  $r1c5q4 = get_post_meta( $id, 'c5q4', true);
  $r1c5q5 = get_post_meta( $id, 'c5q5', true);
// round 2
  $r2 = get_post_meta($id, 'r2', true);
// category 1
  $r2c1 = get_post_meta( $id, 'r2c1', true);
  $r2c1q1 = get_post_meta( $id, 'r2c1q1', true);
  $r2c1q2 = get_post_meta( $id, 'r2c1q2', true);
  $r2c1q3 = get_post_meta( $id, 'r2c1q3', true);
  $r2c1q4 = get_post_meta( $id, 'r2c1q4', true);
  $r2c1q5 = get_post_meta( $id, 'r2c1q5', true);
// category 2
  $r2c2 = get_post_meta( $id, 'r2c2', true);
  $r2c2q1 = get_post_meta( $id, 'r2c2q1', true);
  $r2c2q2 = get_post_meta( $id, 'r2c2q2', true);
  $r2c2q3 = get_post_meta( $id, 'r2c2q3', true);
  $r2c2q4 = get_post_meta( $id, 'r2c2q4', true);
  $r2c2q5 = get_post_meta( $id, 'r2c2q5', true);
// category 3
  $r2c3 = get_post_meta( $id, 'r2c3', true);
  $r2c3q1 = get_post_meta( $id, 'r2c3q1', true);
  $r2c3q2 = get_post_meta( $id, 'r2c3q2', true);
  $r2c3q3 = get_post_meta( $id, 'r2c3q3', true);
  $r2c3q4 = get_post_meta( $id, 'r2c3q4', true);
  $r2c3q5 = get_post_meta( $id, 'r2c3q5', true);
// category 4
  $r2c4 = get_post_meta( $id, 'r2c4', true);
  $r2c4q1 = get_post_meta( $id, 'r2c4q1', true);
  $r2c4q2 = get_post_meta( $id, 'r2c4q2', true);
  $r2c4q3 = get_post_meta( $id, 'r2c4q3', true);
  $r2c4q4 = get_post_meta( $id, 'r2c4q4', true);
  $r2c4q5 = get_post_meta( $id, 'r2c4q5', true);
// category 5
  $r2c5 = get_post_meta( $id, 'r2c5', true);
  $r2c5q1 = get_post_meta( $id, 'r2c5q1', true);
  $r2c5q2 = get_post_meta( $id, 'r2c5q2', true);
  $r2c5q3 = get_post_meta( $id, 'r2c5q3', true);
  $r2c5q4 = get_post_meta( $id, 'r2c5q4', true);
  $r2c5q5 = get_post_meta( $id, 'r2c5q5', true);
?>

[/pastacode]

As you can see, there is a butt-load of repetition here. The solution could be using a set of variable variables. A variable variable essentially allows you to store the name of a variable as a string within a parent variable, like this:

[pastacode lang=”php” message=”The Variable Variable” highlight=”” provider=”manual”]

<?php
   $category_var = "r1c1";

// Populate $r1c1 with a database call
 $$category_var = get_post_meta( $id, 'r1c1', true);

?>

[/pastacode]

From this point on you could call “$$category_var” OR “$r1c1” and you would get the same value.

Since there is so much repetition, we need to have a function that happens a set number of times in order to complete the iterations of the code we need.

for loops

The syntax for a “for” loop in PHP is pretty similar to any ECMA script. The following bit of code will execute 10 times.

[pastacode lang=”php” message=”Standard for loop” highlight=”” provider=”manual”]

<?php 
for ($x = 0; $x < 10; $x++) {
   // your repeated code goes here
} 
?>

[/pastacode]

You can also “nest” for loops into one another. We’ll do that to optimize our giant code block.

Let’s take a look at optimizing this code, so it generates the variables and populates their values on the fly. The great thing about PHP is it executes it’s code sequentially, so anything we generate in the code first, becomes available for any functions that come after that. So be aware of the order in which you add these things.

[pastacode lang=”php” message=”Optimized Using Variable Variables” highlight=”” provider=”manual”]

// ROUNDS			
for ($z=1; $z<=2; $z++){
    // CATEGORIES
    for ($x=1; $x<=5; $x++) {
        $category_var= 'r'.$z.'c'.$x;
        $$category_var = get_post_meta( $id, 'r'.$z.'c'.$x, true);
        // QUESTIONS
        for ($y=1; $y<=5; $y++){
            $question_var= 'r'.$z.'c'.$x.'q'.$y;
            $$question_var = get_post_meta( $id, 'r'.$z.'c'.$x.'q'.$y, true);
        }
    }
}

[/pastacode]

I hate repeating myself

Here is an example of the kind of code you want to avoid for sanity sake. Below is a table with 100+ cells, mostly repeated markup and predictable, formulaic value changes. Can you see any optimization tricks we can use in order to create this table and it’s associated functional fields dynamically?

[pastacode lang=”php” message=”Large Repetitive Code Block” highlight=”” provider=”manual”]

<?php 
// Create your custom meta box
    add_action( 'add_meta_boxes', 'add_game_form' );
 
    // Save your meta box content
    add_action( 'save_post', 'save_game_form' );
	
	 // Add a custom meta box to a post
    function add_game_form( $post ) {
 
        add_meta_box(
                'Meta Box', // ID, should be a string
                'Game Form', // Meta Box Title
                'game_form_content', // Your call back function, this is where your form field will go
                'post', // The post type you want this to show up on, can be post, page, or custom post type
                'normal', // The placement of your meta box, can be normal or side
                'high' // The priority in which this will be displayed
            );
 
    }
	
// Content for the custom meta box
    function game_form_content( $post ) {
		$id = $post->ID;
			// CREATE ALL PHP VARIABLES DYNAMICALLY & CREATE ALL DOM ELEMENTS BASED ON VARIABLES
			for ($z=1; $z<=2; $z++){
				for ($x=1; $x<=5; $x++) {
					$category_var= 'r'.$z.'c'.$x;
					$$category_var = get_post_meta( $id, 'r'.$z.'c'.$x, true);
					for ($y=1; $y<=5; $y++){
						$question_var= 'r'.$z.'c'.$x.'q'.$y;
						$answer_var= 'r'.$z.'c'.$x.'a'.$y;
						$$question_var = get_post_meta( $id, 'r'.$z.'c'.$x.'q'.$y, true);
						$$answer_var = get_post_meta( $id, 'r'.$z.'c'.$x.'a'.$y, true);
					}
				}
			}
			$final_q = get_post_meta($id, 'final_q', true);
			$final_a = get_post_meta($id, 'final_a', true);

        echo '<table width="100%" border="0" cellspacing="0" cellpadding="5" style="font-family:Gotham, Helvetica, Arial, sans-serif;">
  <tr>
    <td colspan="5" bgcolor="#000000"><span style="color:#fff">Round 1</span></td>
  </tr>
  <tr>
    <td><label>Category 1: </label>
      <br>
      <textarea style="width:100%" name="r1c1_field" >'.$r1c1.'</textarea></td>
    <td><label> Category 2: </label>
      <br>
      <textarea style="width:100%" name="r1c2_field" >'.$r1c2.'</textarea></td>
    <td><label> Category 3: </label>
      <br>
      <textarea style="width:100%" name="r1c3_field" >'.$r1c3.'</textarea></td>
    <td><label>Category 4: </label>
      <br>
      <textarea style="width:100%" name="r1c4_field" >'.$r1c4.'</textarea></td>
    <td><label> Category 5: </label>
      <br>
      <textarea style="width:100%" name="r1c5_field" >'.$r1c5.'</textarea></td>
  </tr>
  <tr>
    <td colspan="5" bgcolor="#C4C4C4">Question 1</td>
  </tr>
  <tr>
    <td><textarea style="width:100%" name="r1c1q1_field" >'.$r1c1q1.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c1a1_field" >'.$r1c1a1.'</textarea></td>
    <td><textarea style="width:100%" name="r1c2q1_field" >'.$r1c2q1.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c2a1_field" >'.$r1c2a1.'</textarea></td>
    <td><textarea style="width:100%" name="r1c3q1_field" >'.$r1c3q1.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c3a1_field" >'.$r1c3a1.'</textarea></td>
    <td><textarea style="width:100%" name="r1c4q1_field" >'.$r1c4q1.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c4a1_field" >'.$r1c4a1.'</textarea></td>
    <td><textarea style="width:100%" name="r1c5q1_field" >'.$r1c5q1.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c5a1_field" >'.$r1c5a1.'</textarea></td>
  </tr>
  <tr>
    <td colspan="5" bgcolor="#C4C4C4">Question 2</td>
  </tr>
  <tr>
    <td><textarea style="width:100%" name="r1c1q2_field" >'.$r1c1q2.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c1a2_field" >'.$r1c1a2.'</textarea></td>
    <td><textarea style="width:100%" name="r1c2q2_field" >'.$r1c2q2.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c2a2_field" >'.$r1c2a2.'</textarea></td>
    <td><textarea style="width:100%" name="r1c3q2_field" >'.$r1c3q2.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c3a2_field" >'.$r1c3a2.'</textarea></td>
    <td><textarea style="width:100%" name="r1c4q2_field" >'.$r1c4q2.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c4a2_field" >'.$r1c4a2.'</textarea></td>
    <td><textarea style="width:100%" name="r1c5q2_field" >'.$r1c5q2.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c5a2_field" >'.$r1c5a2.'</textarea></td>
  </tr>
  <tr>
    <td colspan="5" bgcolor="#C4C4C4">Question 3</td>
  </tr>
  <tr>
    <td><textarea style="width:100%" name="r1c1q3_field" >'.$r1c1q3.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c1a3_field" >'.$r1c1a3.'</textarea></td>
    <td><textarea style="width:100%" name="r1c2q3_field" >'.$r1c2q3.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c2a3_field" >'.$r1c2a3.'</textarea></td>
    <td><textarea style="width:100%" name="r1c3q3_field" >'.$r1c3q3.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c3a3_field" >'.$r1c3a3.'</textarea></td>
    <td><textarea style="width:100%" name="r1c4q3_field" >'.$r1c4q3.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c4a3_field" >'.$r1c4a3.'</textarea></td>
    <td><textarea style="width:100%" name="r1c5q3_field" >'.$r1c5q3.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c5a3_field" >'.$r1c5a3.'</textarea></td>
  </tr>
  <tr>
    <td colspan="5" bgcolor="#C4C4C4">Question 4</td>
  </tr>
  <tr>
    <td><textarea style="width:100%" name="r1c1q4_field" >'.$r1c1q4.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c1a4_field" >'.$r1c1a4.'</textarea></td>
    <td><textarea style="width:100%" name="r1c2q4_field" >'.$r1c2q4.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c2a4_field" >'.$r1c2a4.'</textarea></td>
    <td><textarea style="width:100%" name="r1c3q4_field" >'.$r1c3q4.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c3a4_field" >'.$r1c3a4.'</textarea></td>
    <td><textarea style="width:100%" name="r1c4q4_field" >'.$r1c4q4.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c4a4_field" >'.$r1c4a4.'</textarea></td>
    <td><textarea style="width:100%" name="r1c5q4_field" >'.$r1c5q4.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c5a4_field" >'.$r1c5a4.'</textarea></td>
  </tr>
  <tr>
    <td colspan="5" bgcolor="#C4C4C4">Question 5</td>
  </tr>
  <tr>
    <td><textarea style="width:100%" name="r1c1q5_field" >'.$r1c1q5.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c1a5_field" >'.$r1c1a5.'</textarea></td>
    <td><textarea style="width:100%" name="r1c2q5_field" >'.$r1c2q5.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c2a5_field" >'.$r1c2a5.'</textarea></td>
    <td><textarea style="width:100%" name="r1c3q5_field" >'.$r1c3q5.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c3a5_field" >'.$r1c3a5.'</textarea></td>
    <td><textarea style="width:100%" name="r1c4q5_field" >'.$r1c4q5.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c4a5_field" >'.$r1c4a5.'</textarea></td>
    <td><textarea style="width:100%" name="r1c5q5_field" >'.$r1c5q5.'</textarea>
      <hr>
      <textarea style="width:100%" name="r1c5a5_field" >'.$r1c5a5.'</textarea></td>
  </tr>
  <tr>
    <td colspan="5">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="5" bgcolor="#000000"><span style="color:#fff">Round 2</span></td>
  </tr>
  <tr>
    <td><label>Category 1: </label>
      <br>
      <textarea style="width:100%" name="r2c1_field" >'.$r2c1.'</textarea></td>
    <td><label> Category 2: </label>
      <br>
      <textarea style="width:100%" name="r2c2_field" >'.$r2c2.'</textarea></td>
    <td><label> Category 3: </label>
      <br>
      <textarea style="width:100%" name="r2c3_field" >'.$r2c3.'</textarea></td>
    <td><label>Category 4: </label>
      <br>
      <textarea style="width:100%" name="r2c4_field" >'.$r2c4.'</textarea></td>
    <td><label> Category 5: </label>
      <br>
      <textarea style="width:100%" name="r2c5_field" >'.$r2c5.'</textarea></td>
  </tr>
  <tr>
    <td colspan="5" bgcolor="#C4C4C4">Question 1</td>
  </tr>
  <tr>
    <td><textarea style="width:100%" name="r2c1q1_field" >'.$r2c1q1.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c1a1_field" >'.$r2c1a1.'</textarea></td>
    <td><textarea style="width:100%" name="r2c2q1_field" >'.$r2c2q1.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c2a1_field" >'.$r2c2a1.'</textarea></td>
    <td><textarea style="width:100%" name="r2c3q1_field" >'.$r2c3q1.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c3a1_field" >'.$r2c3a1.'</textarea></td>
    <td><textarea style="width:100%" name="r2c4q1_field" >'.$r2c4q1.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c4a1_field" >'.$r2c4a1.'</textarea></td>
    <td><textarea style="width:100%" name="r2c5q1_field" >'.$r2c5q1.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c5a1_field" >'.$r2c5a1.'</textarea></td>
  </tr>
  <tr>
    <td colspan="5" bgcolor="#C4C4C4">Question 2</td>
  </tr>
  <tr>
    <td><textarea style="width:100%" name="r2c1q2_field" >'.$r2c1q2.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c1a2_field" >'.$r2c1a2.'</textarea></td>
    <td><textarea style="width:100%" name="r2c2q2_field" >'.$r2c2q2.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c2a2_field" >'.$r2c2a2.'</textarea></td>
    <td><textarea style="width:100%" name="r2c3q2_field" >'.$r2c3q2.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c3a2_field" >'.$r2c3a2.'</textarea></td>
    <td><textarea style="width:100%" name="r2c4q2_field" >'.$r2c4q2.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c4a2_field" >'.$r2c4a2.'</textarea></td>
    <td><textarea style="width:100%" name="r2c5q2_field" >'.$r2c5q2.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c5a2_field" >'.$r2c5a2.'</textarea></td>
  </tr>
  <tr>
    <td colspan="5" bgcolor="#C4C4C4">Question 3</td>
  </tr>
  <tr>
    <td><textarea style="width:100%" name="r2c1q3_field" >'.$r2c1q3.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c1a3_field" >'.$r2c1a3.'</textarea></td>
    <td><textarea style="width:100%" name="r2c2q3_field" >'.$r2c2q3.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c2a3_field" >'.$r2c2a3.'</textarea></td>
    <td><textarea style="width:100%" name="r2c3q3_field" >'.$r2c3q3.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c3a3_field" >'.$r2c3a3.'</textarea></td>
    <td><textarea style="width:100%" name="r2c4q3_field" >'.$r2c4q3.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c4a3_field" >'.$r2c4a3.'</textarea></td>
    <td><textarea style="width:100%" name="r2c5q3_field" >'.$r2c5q3.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c5a3_field" >'.$r2c5a3.'</textarea></td>
  </tr>
  <tr>
    <td colspan="5" bgcolor="#C4C4C4">Question 4</td>
  </tr>
  <tr>
    <td><textarea style="width:100%" name="r2c1q4_field" >'.$r2c1q4.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c1a4_field" >'.$r2c1a4.'</textarea></td>
    <td><textarea style="width:100%" name="r2c2q4_field" >'.$r2c2q4.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c2a4_field" >'.$r2c2a4.'</textarea></td>
    <td><textarea style="width:100%" name="r2c3q4_field" >'.$r2c3q4.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c3a4_field" >'.$r2c3a4.'</textarea></td>
    <td><textarea style="width:100%" name="r2c4q4_field" >'.$r2c4q4.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c4a4_field" >'.$r2c4a4.'</textarea></td>
    <td><textarea style="width:100%" name="r2c5q4_field" >'.$r2c5q4.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c5a4_field" >'.$r2c5a4.'</textarea></td>
  </tr>
  <tr>
    <td colspan="5" bgcolor="#C4C4C4">Question 5</td>
  </tr>
  <tr>
    <td><textarea style="width:100%" name="r2c1q5_field" >'.$r2c1q5.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c1a5_field" >'.$r2c1a5.'</textarea></td>
    <td><textarea style="width:100%" name="r2c2q5_field" >'.$r2c2q5.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c2a5_field" >'.$r2c2a5.'</textarea></td>
    <td><textarea style="width:100%" name="r2c3q5_field" >'.$r2c3q5.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c3a5_field" >'.$r2c3a5.'</textarea></td>
    <td><textarea style="width:100%" name="r2c4q5_field" >'.$r2c4q5.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c4a5_field" >'.$r2c4a5.'</textarea></td>
    <td><textarea style="width:100%" name="r2c5q5_field" >'.$r2c5q5.'</textarea>
      <hr>
      <textarea style="width:100%" name="r2c5a5_field" >'.$r2c5a5.'</textarea></td>
  </tr>
<tr>
    <td colspan="5" bgcolor="#000000"><span style="color:#fff">Final Question</span></td>
  </tr>
  <tr>
    <td colspan="5">
	  <textarea style="width:100%" name="final_q_field" >'.$final_q.'</textarea>
      <hr>
      <textarea style="width:100%" name="final_a_field" >'.$final_a.'</textarea>
	</td>
  </tr>
</table>';
  
    }	
// save newsletter content
    function save_game_form(){
 
        global $post;
 
        // Get our form field
        if( $_POST ) :
		// ROUND 1
			// Categories 
             $r1c1 = esc_attr( $_POST['r1c1_field'] );
             $r1c2 = esc_attr( $_POST['r1c2_field'] );
             $r1c3 = esc_attr( $_POST['r1c3_field'] );
             $r1c4 = esc_attr( $_POST['r1c4_field'] );
             $r1c5 = esc_attr( $_POST['r1c5_field'] );
			 
			 // Questions for Category 1
             $r1c1q1 = esc_attr( $_POST['r1c1q1_field'] );
             $r1c1a1 = esc_attr( $_POST['r1c1a1_field'] );
             $r1c1q2 = esc_attr( $_POST['r1c1q2_field'] );
             $r1c1a2 = esc_attr( $_POST['r1c1a2_field'] );
             $r1c1q3 = esc_attr( $_POST['r1c1q3_field'] );
             $r1c1a3 = esc_attr( $_POST['r1c1a3_field'] );
             $r1c1q4 = esc_attr( $_POST['r1c1q4_field'] );
             $r1c1a4 = esc_attr( $_POST['r1c1a4_field'] );
             $r1c1q5 = esc_attr( $_POST['r1c1q5_field'] );
             $r1c1a5 = esc_attr( $_POST['r1c1a5_field'] );

			 // Questions for Category 2
             $r1c2q1 = esc_attr( $_POST['r1c2q1_field'] );
             $r1c2a1 = esc_attr( $_POST['r1c2a1_field'] );
             $r1c2q2 = esc_attr( $_POST['r1c2q2_field'] );
             $r1c2a2 = esc_attr( $_POST['r1c2a2_field'] );
             $r1c2q3 = esc_attr( $_POST['r1c2q3_field'] );
             $r1c2a3 = esc_attr( $_POST['r1c2a3_field'] );
             $r1c2q4 = esc_attr( $_POST['r1c2q4_field'] );
             $r1c2a4 = esc_attr( $_POST['r1c2a4_field'] );
             $r1c2q5 = esc_attr( $_POST['r1c2q5_field'] );
             $r1c2a5 = esc_attr( $_POST['r1c2a5_field'] );

			 // Questions for Category 3
             $r1c3q1 = esc_attr( $_POST['r1c3q1_field'] );
             $r1c3a1 = esc_attr( $_POST['r1c3a1_field'] );
             $r1c3q2 = esc_attr( $_POST['r1c3q2_field'] );
             $r1c3a2 = esc_attr( $_POST['r1c3a2_field'] );
             $r1c3q3 = esc_attr( $_POST['r1c3q3_field'] );
             $r1c3a3 = esc_attr( $_POST['r1c3a3_field'] );
             $r1c3q4 = esc_attr( $_POST['r1c3q4_field'] );
             $r1c3a4 = esc_attr( $_POST['r1c3a4_field'] );
             $r1c3q5 = esc_attr( $_POST['r1c3q5_field'] );
             $r1c3a5 = esc_attr( $_POST['r1c3a5_field'] );

			 // Questions for Category 4
             $r1c4q1 = esc_attr( $_POST['r1c4q1_field'] );
             $r1c4a1 = esc_attr( $_POST['r1c4a1_field'] );
             $r1c4q2 = esc_attr( $_POST['r1c4q2_field'] );
             $r1c4a2 = esc_attr( $_POST['r1c4a2_field'] );
             $r1c4q3 = esc_attr( $_POST['r1c4q3_field'] );
             $r1c4a3 = esc_attr( $_POST['r1c4a3_field'] );
             $r1c4q4 = esc_attr( $_POST['r1c4q4_field'] );
             $r1c4a4 = esc_attr( $_POST['r1c4a4_field'] );
             $r1c4q5 = esc_attr( $_POST['r1c4q5_field'] );
             $r1c4a5 = esc_attr( $_POST['r1c4a5_field'] );

			 // Questions for Category 5
             $r1c5q1 = esc_attr( $_POST['r1c5q1_field'] );
             $r1c5a1 = esc_attr( $_POST['r1c5a1_field'] );
             $r1c5q2 = esc_attr( $_POST['r1c5q2_field'] );
             $r1c5a2 = esc_attr( $_POST['r1c5a2_field'] );
             $r1c5q3 = esc_attr( $_POST['r1c5q3_field'] );
             $r1c5a3 = esc_attr( $_POST['r1c5a3_field'] );
             $r1c5q4 = esc_attr( $_POST['r1c5q4_field'] );
             $r1c5a4 = esc_attr( $_POST['r1c5a4_field'] );
             $r1c5q5 = esc_attr( $_POST['r1c5q5_field'] );
             $r1c5a5 = esc_attr( $_POST['r1c5a5_field'] );

		// ROUND 2
			// Categories 
             $r2c1 = esc_attr( $_POST['r2c1_field'] );
             $r2c2 = esc_attr( $_POST['r2c2_field'] );
             $r2c3 = esc_attr( $_POST['r2c3_field'] );
             $r2c4 = esc_attr( $_POST['r2c4_field'] );
             $r2c5 = esc_attr( $_POST['r2c5_field'] );
			 
			 // Questions for Category 1
             $r2c1q1 = esc_attr( $_POST['r2c1q1_field'] );
             $r2c1a1 = esc_attr( $_POST['r2c1a1_field'] );
             $r2c1q2 = esc_attr( $_POST['r2c1q2_field'] );
             $r2c1a2 = esc_attr( $_POST['r2c1a2_field'] );
             $r2c1q3 = esc_attr( $_POST['r2c1q3_field'] );
             $r2c1a3 = esc_attr( $_POST['r2c1a3_field'] );
             $r2c1q4 = esc_attr( $_POST['r2c1q4_field'] );
             $r2c1a4 = esc_attr( $_POST['r2c1a4_field'] );
             $r2c1q5 = esc_attr( $_POST['r2c1q5_field'] );
             $r2c1a5 = esc_attr( $_POST['r2c1a5_field'] );
			 // Questions for Category 2
             $r2c2q1 = esc_attr( $_POST['r2c2q1_field'] );
             $r2c2a1 = esc_attr( $_POST['r2c2a1_field'] );
             $r2c2q2 = esc_attr( $_POST['r2c2q2_field'] );
             $r2c2a2 = esc_attr( $_POST['r2c2a2_field'] );
             $r2c2q3 = esc_attr( $_POST['r2c2q3_field'] );
             $r2c2a3 = esc_attr( $_POST['r2c2a3_field'] );
             $r2c2q4 = esc_attr( $_POST['r2c2q4_field'] );
             $r2c2a4 = esc_attr( $_POST['r2c2a4_field'] );
             $r2c2q5 = esc_attr( $_POST['r2c2q5_field'] );
             $r2c2a5 = esc_attr( $_POST['r2c2a5_field'] );

			 // Questions for Category 3
             $r2c3q1 = esc_attr( $_POST['r2c3q1_field'] );
             $r2c3a1 = esc_attr( $_POST['r2c3a1_field'] );
             $r2c3q2 = esc_attr( $_POST['r2c3q2_field'] );
             $r2c3a2 = esc_attr( $_POST['r2c3a2_field'] );
             $r2c3q3 = esc_attr( $_POST['r2c3q3_field'] );
             $r2c3a3 = esc_attr( $_POST['r2c3a3_field'] );
             $r2c3q4 = esc_attr( $_POST['r2c3q4_field'] );
             $r2c3a4 = esc_attr( $_POST['r2c3a4_field'] );
             $r2c3q5 = esc_attr( $_POST['r2c3q5_field'] );
             $r2c3a5 = esc_attr( $_POST['r2c3a5_field'] );

			 // Questions for Category 4
             $r2c4q1 = esc_attr( $_POST['r2c4q1_field'] );
             $r2c4a1 = esc_attr( $_POST['r2c4a1_field'] );
             $r2c4q2 = esc_attr( $_POST['r2c4q2_field'] );
             $r2c4a2 = esc_attr( $_POST['r2c4a2_field'] );
             $r2c4q3 = esc_attr( $_POST['r2c4q3_field'] );
             $r2c4a3 = esc_attr( $_POST['r2c4a3_field'] );
             $r2c4q4 = esc_attr( $_POST['r2c4q4_field'] );
             $r2c4a4 = esc_attr( $_POST['r2c4a4_field'] );
             $r2c4q5 = esc_attr( $_POST['r2c4q5_field'] );
             $r2c4a5 = esc_attr( $_POST['r2c4a5_field'] );

			 // Questions for Category 5
             $r2c5q1 = esc_attr( $_POST['r2c5q1_field'] );
             $r2c5a1 = esc_attr( $_POST['r2c5a1_field'] );
             $r2c5q2 = esc_attr( $_POST['r2c5q2_field'] );
             $r2c5a2 = esc_attr( $_POST['r2c5a2_field'] );
             $r2c5q3 = esc_attr( $_POST['r2c5q3_field'] );
             $r2c5a3 = esc_attr( $_POST['r2c5a3_field'] );
             $r2c5q4 = esc_attr( $_POST['r2c5q4_field'] );
             $r2c5a4 = esc_attr( $_POST['r2c5a4_field'] );
             $r2c5q5 = esc_attr( $_POST['r2c5q5_field'] );
             $r2c5a5 = esc_attr( $_POST['r2c5a5_field'] );


             $final_q = esc_attr( $_POST['final_q_field'] );
             $final_a = esc_attr( $_POST['final_a_field'] );

             // Update post meta
			 // ROUND 1
             update_post_meta($post->ID, 'r1c1', $r1c1);
             update_post_meta($post->ID, 'r1c2', $r1c2);
             update_post_meta($post->ID, 'r1c3', $r1c3);
             update_post_meta($post->ID, 'r1c4', $r1c4);
             update_post_meta($post->ID, 'r1c5', $r1c5);
 
 			// Questions for Category 1
             update_post_meta($post->ID, 'r1c1q1', $r1c1q1);
             update_post_meta($post->ID, 'r1c1a1', $r1c1a1);
             update_post_meta($post->ID, 'r1c1q2', $r1c1q2);
             update_post_meta($post->ID, 'r1c1a2', $r1c1a2);
             update_post_meta($post->ID, 'r1c1q3', $r1c1q3);
             update_post_meta($post->ID, 'r1c1a3', $r1c1a3);
             update_post_meta($post->ID, 'r1c1q4', $r1c1q4);
             update_post_meta($post->ID, 'r1c1a4', $r1c1a4);
             update_post_meta($post->ID, 'r1c1q5', $r1c1q5);
             update_post_meta($post->ID, 'r1c1a5', $r1c1a5);

 			// Questions for Category 2
             update_post_meta($post->ID, 'r1c2q1', $r1c2q1);
             update_post_meta($post->ID, 'r1c2a1', $r1c2a1);
             update_post_meta($post->ID, 'r1c2q2', $r1c2q2);
             update_post_meta($post->ID, 'r1c2a2', $r1c2a2);
             update_post_meta($post->ID, 'r1c2q3', $r1c2q3);
             update_post_meta($post->ID, 'r1c2a3', $r1c2a3);
             update_post_meta($post->ID, 'r1c2q4', $r1c2q4);
             update_post_meta($post->ID, 'r1c2a4', $r1c2a4);
             update_post_meta($post->ID, 'r1c2q5', $r1c2q5);
             update_post_meta($post->ID, 'r1c2a5', $r1c2a5);

 			// Questions for Category 3
             update_post_meta($post->ID, 'r1c3q1', $r1c3q1);
             update_post_meta($post->ID, 'r1c3a1', $r1c3a1);
             update_post_meta($post->ID, 'r1c3q2', $r1c3q2);
             update_post_meta($post->ID, 'r1c3a2', $r1c3a2);
             update_post_meta($post->ID, 'r1c3q3', $r1c3q3);
             update_post_meta($post->ID, 'r1c3a3', $r1c3a3);
             update_post_meta($post->ID, 'r1c3q4', $r1c3q4);
             update_post_meta($post->ID, 'r1c3a4', $r1c3a4);
             update_post_meta($post->ID, 'r1c3q5', $r1c3q5);
             update_post_meta($post->ID, 'r1c3a5', $r1c3a5);

 			// Questions for Category 4
             update_post_meta($post->ID, 'r1c4q1', $r1c4q1);
             update_post_meta($post->ID, 'r1c4a1', $r1c4a1);
             update_post_meta($post->ID, 'r1c4q2', $r1c4q2);
             update_post_meta($post->ID, 'r1c4a2', $r1c4a2);
             update_post_meta($post->ID, 'r1c4q3', $r1c4q3);
             update_post_meta($post->ID, 'r1c4a3', $r1c4a3);
             update_post_meta($post->ID, 'r1c4q4', $r1c4q4);
             update_post_meta($post->ID, 'r1c4a4', $r1c4a4);
             update_post_meta($post->ID, 'r1c4q5', $r1c4q5);
             update_post_meta($post->ID, 'r1c4a5', $r1c4a5);

 			// Questions for Category 5
             update_post_meta($post->ID, 'r1c5q1', $r1c5q1);
             update_post_meta($post->ID, 'r1c5a1', $r1c5a1);
             update_post_meta($post->ID, 'r1c5q2', $r1c5q2);
             update_post_meta($post->ID, 'r1c5a2', $r1c5a2);
             update_post_meta($post->ID, 'r1c5q3', $r1c5q3);
             update_post_meta($post->ID, 'r1c5a3', $r1c5a3);
             update_post_meta($post->ID, 'r1c5q4', $r1c5q4);
             update_post_meta($post->ID, 'r1c5a4', $r1c5a4);
             update_post_meta($post->ID, 'r1c5q5', $r1c5q5);
             update_post_meta($post->ID, 'r1c5a5', $r1c5a5);

			 // ROUND 2
             update_post_meta($post->ID, 'r2c1', $r2c1);
             update_post_meta($post->ID, 'r2c2', $r2c2);
             update_post_meta($post->ID, 'r2c3', $r2c3);
             update_post_meta($post->ID, 'r2c4', $r2c4);
             update_post_meta($post->ID, 'r2c5', $r2c5);
 
 			// Questions for Category 1
             update_post_meta($post->ID, 'r2c1q1', $r2c1q1);
             update_post_meta($post->ID, 'r2c1a1', $r2c1a1);
             update_post_meta($post->ID, 'r2c1q2', $r2c1q2);
             update_post_meta($post->ID, 'r2c1a2', $r2c1a2);
             update_post_meta($post->ID, 'r2c1q3', $r2c1q3);
             update_post_meta($post->ID, 'r2c1a3', $r2c1a3);
             update_post_meta($post->ID, 'r2c1q4', $r2c1q4);
             update_post_meta($post->ID, 'r2c1a4', $r2c1a4);
             update_post_meta($post->ID, 'r2c1q5', $r2c1q5);
             update_post_meta($post->ID, 'r2c1a5', $r2c1a5);
 			// Questions for Category 2
             update_post_meta($post->ID, 'r2c2q1', $r2c2q1);
             update_post_meta($post->ID, 'r2c2a1', $r2c2a1);
             update_post_meta($post->ID, 'r2c2q2', $r2c2q2);
             update_post_meta($post->ID, 'r2c2a2', $r2c2a2);
             update_post_meta($post->ID, 'r2c2q3', $r2c2q3);
             update_post_meta($post->ID, 'r2c2a3', $r2c2a3);
             update_post_meta($post->ID, 'r2c2q4', $r2c2q4);
             update_post_meta($post->ID, 'r2c2a4', $r2c2a4);
             update_post_meta($post->ID, 'r2c2q5', $r2c2q5);
             update_post_meta($post->ID, 'r2c2a5', $r2c2a5);

 			// Questions for Category 3
             update_post_meta($post->ID, 'r2c3q1', $r2c3q1);
             update_post_meta($post->ID, 'r2c3a1', $r2c3a1);
             update_post_meta($post->ID, 'r2c3q2', $r2c3q2);
             update_post_meta($post->ID, 'r2c3a2', $r2c3a2);
             update_post_meta($post->ID, 'r2c3q3', $r2c3q3);
             update_post_meta($post->ID, 'r2c3a3', $r2c3a3);
             update_post_meta($post->ID, 'r2c3q4', $r2c3q4);
             update_post_meta($post->ID, 'r2c3a4', $r2c3a4);
             update_post_meta($post->ID, 'r2c3q5', $r2c3q5);
             update_post_meta($post->ID, 'r2c3a5', $r2c3a5);

 			// Questions for Category 4
             update_post_meta($post->ID, 'r2c4q1', $r2c4q1);
             update_post_meta($post->ID, 'r2c4a1', $r2c4a1);
             update_post_meta($post->ID, 'r2c4q2', $r2c4q2);
             update_post_meta($post->ID, 'r2c4a2', $r2c4a2);
             update_post_meta($post->ID, 'r2c4q3', $r2c4q3);
             update_post_meta($post->ID, 'r2c4a3', $r2c4a3);
             update_post_meta($post->ID, 'r2c4q4', $r2c4q4);
             update_post_meta($post->ID, 'r2c4a4', $r2c4a4);
             update_post_meta($post->ID, 'r2c4q5', $r2c4q5);
             update_post_meta($post->ID, 'r2c4a5', $r2c4a5);

 			// Questions for Category 5
             update_post_meta($post->ID, 'r2c5q1', $r2c5q1);
             update_post_meta($post->ID, 'r2c5a1', $r2c5a1);
             update_post_meta($post->ID, 'r2c5q2', $r2c5q2);
             update_post_meta($post->ID, 'r2c5a2', $r2c5a2);
             update_post_meta($post->ID, 'r2c5q3', $r2c5q3);
             update_post_meta($post->ID, 'r2c5a3', $r2c5a3);
             update_post_meta($post->ID, 'r2c5q4', $r2c5q4);
             update_post_meta($post->ID, 'r2c5a4', $r2c5a4);
             update_post_meta($post->ID, 'r2c5q5', $r2c5q5);
             update_post_meta($post->ID, 'r2c5a5', $r2c5a5);

             update_post_meta($post->ID, 'final_q', $final_q);
             update_post_meta($post->ID, 'final_a', $final_a);


       endif;
 
    }
?>

[/pastacode]