Forum Moderators: coopster

Message Too Old, No Replies

Redirecting php output to a new webpage

         

callingrohit

1:13 pm on May 11, 2008 (gmt 0)

10+ Year Member



hi there,

I've written a php code for parsing a CSV file.

I have also managed to pass the arrays with the CSV data stored into a tabular format for displaying on a webpage.

Now, my question, is it possible to output this tabular formatted data to a new webpage. So when the data is generated, I would like it to be displayed on a new webpage instead of being displayed on the same page from where the script was executed.

Thank you in advance
vivek

chorny

3:11 pm on May 11, 2008 (gmt 0)

10+ Year Member



It is solved with HTML. <a href='..' target='_new'

callingrohit

3:21 pm on May 11, 2008 (gmt 0)

10+ Year Member



Thanks for your reply chorny.

Can you give me an example of its usuage ? I'm unable to understand how using this statement and at what position would get my tabulated data over to a new page.

Also, I think I should have explained this properly. I'm generating arrays and storing data from the CSV files. And using 'for' loops I fill in the data in the HTML table I create within php by using commands such as


....<snip>....

echo '<table>';
echo '<tr>';

for ($x=0; $x < $number_of_fields; $x++)
{
echo '<td align="Left" nowrap="nowrap">'.$data[$x].'</td>';
}
echo '</tr>';
echo '</table>';

....<snip>....

Thanks
Vivek

mehh

4:00 pm on May 11, 2008 (gmt 0)

10+ Year Member



what do you mean by new page? another file on the server (like example.html)? storing the information for another PHP script?

callingrohit

4:23 pm on May 11, 2008 (gmt 0)

10+ Year Member



Hi mehh,

by new page i mean suppose I have a blank "example.html" which has nothing in its <body> tags.

So can I display the table that i create using my PHP script as shown in the above snippet on "example.html" rather than on the same page where the script was executed.

Thanks
vivek

g1smd

4:24 pm on May 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Will this new page have the same or a different URL to the one you started out on?

callingrohit

4:41 pm on May 11, 2008 (gmt 0)

10+ Year Member



hi g1smd,

the new page will be a different url because the script is executed from a 'my_script.php' and I would like the tabulated data to be shown on 'example.html'.

thanks
vivek

mehh

7:29 pm on May 11, 2008 (gmt 0)

10+ Year Member



something like this?

<?
$out ='<html><body><table><tr>';
for ($x=0; $x < $number_of_fields; $x++)
{
$out.='<td align="Left" nowrap="nowrap">'.$data[$x].'</td>';
}
$out.='</tr></table></body></html>';
$fh = fopen("example.html", "w");
fwrite($fh,$out);
fclose($fh);
header("Location: example.html");
?>

callingrohit

12:55 am on May 12, 2008 (gmt 0)

10+ Year Member



Hi mehh,

Thanks for the reply. The script looks correct didn't think of using fwrite and fopen.

Let me try it out and I'll post my comments later today.

Thanks
vivek

callingrohit

5:53 pm on May 13, 2008 (gmt 0)

10+ Year Member



Thank you mehh, your code worked flawlessly.

What I also did was added a button to my script which when clicked will open a popup window with the 'example.html' file loaded on it.

the popup code i used


<input type="submit" value="See my CSV Data " onClick="javascript: window.open('example.html', 'CSV Table', 'toolbar=1, scrollbars=1, location=0, statusbar=1, menubar=1, resizable=1,width = 600, height = 400');">

Just one last question, is there a way which I'm sure there is, for php to create the 'example.html' automatically on the fly if at all a flag such as "record the parsed csv data" is checked.

This way I won't need to manually create a 'example.html' file so that php can open it up and then write on it. Also, then in my popup code

window.open('example.html'.....

the above filename could be replaced by the one php creates on the fly.

Therefore, if example.html already exists and if the record flag is checked, the php script can create example_001.html and write the data on it and then pass this filename to the popup script so that if the user clicks the button, then only the recently newly created 'example_001.html' file is opened.

Thanks
Vivek

mehh

8:00 pm on May 13, 2008 (gmt 0)

10+ Year Member



so something like this:
<?
if($_POST['record']){
$out ='<html><body><table><tr>';
for ($x=0; $x < $number_of_fields; $x++)
{
$out.='<td align="Left" nowrap="nowrap">'.$data[$x].'</td>';
}
$out.='</tr></table></body></html>';
$filename="example";
$file_EXT=".html";
$count=0;
while(file_exists($filename.($count>0?"_".$count:"").$file_EXT)){
$count++;
}
$filename=$filename.($count>0?"_".$count:"").$file_EXT;
$fh = fopen($filename, "w");
fwrite($fh,$out);
fclose($fh);
header("Location: ".$filename);
}
?>

callingrohit

3:02 am on May 14, 2008 (gmt 0)

10+ Year Member



great mehh i think that script will 100% work.

Thank you for help. Really appreciate it.

Vivek

g1smd

8:15 am on May 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Be aware that your "Location" header sends a 302 redirect.

You might want that to be a 301. Consider that very carefully.

callingrohit

2:23 pm on May 15, 2008 (gmt 0)

10+ Year Member



Thanks g1smd for the warning.

I'm using a popup script and leave it to the user if or not they wanna see the data on "example.html".

Thanks
Vivek