Page 1 of 1

Extract table and redisplay

Posted: Thu May 08, 2008 9:35 am
by demarigny
Hi folks,

I've written a script that generates an HTML table based on input from a previous page using _POST. At the moment the table displays correctly but I'd also like to be able to 'extract' the HTML code to display it below the table so it can be copied and pasted easily from the same page.

Is this even possible? And if it is, how should I best go about it?

Cheers,
M

Re: Extract table and redisplay

Posted: Thu May 08, 2008 4:19 pm
by Jade
Isn't that the same thing as posting the same table twice?? If that's true, just copy the code you have and paste it below the first table...

Re: Extract table and redisplay

Posted: Thu May 08, 2008 4:46 pm
by EverLearning
If i understood you correctly you could use htmlspecialchars() on output of your script or function

Code: Select all

/* this will display the table */
echo $tableData;
 
/* this will display the HTML source for the table*/
echo htmlspecialchars($tableData);
 

Re: Extract table and redisplay

Posted: Fri May 09, 2008 3:41 am
by demarigny
@EverLearning

That looks about right, thank you. Just one other question, how'd you recommend getting the table data into $tabledata? Sorry if that sounds a bit stupid, I'm still kinda new to php.

Thanks,

M

Re: Extract table and redisplay

Posted: Fri May 09, 2008 3:46 am
by EverLearning
Show me the code that generates the table.

Re: Extract table and redisplay

Posted: Fri May 09, 2008 3:58 am
by demarigny
@EverLearning -

This is the code that's doing all the work, it's repeated throughout the script.

Code: Select all

//BUTTONS
echo "  <td>Buttons/Keys</td>";
            //Check for buttons in the right hand
            if ($varButton){
                echo "\t <td colspan=\"2\"><center> $varButton </td> \n";}
                else{
                echo "\t<td bgcolor=\"$varBlank\" colspan=\"2\"></td>\n";}
 
            //Check for buttons in the left hand
            if ($varButtonLeft){
            //Check the bass type, assign $varButtonLeft to correct column
            switch ($varBass){
                case "FB":
                    echo"\t<td><center><center>$varButtonLeft</td><td bgcolor=\"$varBlank\">\n";
                break;
                case "SB":
                    echo"\t<td bgcolor=\"$varBlank\"></td><td><center>$varButtonLeft</td>\n";
                break;
                }}
                else{
                    //if there are no buttons in the left hand, grey the cell out.
                    echo"\t<td colspan=\"3\" bgcolor=\"$varBlank\"></td>\n";
                    }
 
//close this section of the table
echo "  </tr>\n";
echo "  <tr>\n";
Thanks,

M

Re: Extract table and redisplay

Posted: Fri May 09, 2008 4:18 am
by EverLearning
From the snippet you posted, I couldn't tell if that code is part of some function(e.g. displayTable()), or is in a separate file. I would suggest putting it in a function.

If you have the code in a function that you can use this:

Code: Select all

$table= displayTable();
/* displays the table */
echo $table;
 
/* displays the HTML source of the table */
echo htmlspecialchars($table);
 
If its in a separate file you can use output buffering:

Code: Select all

ob_start(); /* start output buffering */
include 'display_table.php'; /* this displays the table */
$table = ob_get_contents(); /* stores the table data into a variable*/
 
/* displays the HTML source of the table */
echo htmlspecialchars($table);
 
If you have your table generating code as a part of a larger script, you can also use the output buffering approach above.