Extract table and redisplay

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
demarigny
Forum Newbie
Posts: 3
Joined: Thu May 08, 2008 9:29 am

Extract table and redisplay

Post 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
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Extract table and redisplay

Post 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...
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Extract table and redisplay

Post 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);
 
demarigny
Forum Newbie
Posts: 3
Joined: Thu May 08, 2008 9:29 am

Re: Extract table and redisplay

Post 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
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Extract table and redisplay

Post by EverLearning »

Show me the code that generates the table.
demarigny
Forum Newbie
Posts: 3
Joined: Thu May 08, 2008 9:29 am

Re: Extract table and redisplay

Post 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
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Extract table and redisplay

Post 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.
Post Reply