Jump to content

output php code to a text file


snnbbc

Recommended Posts

Hello,

 

I would like to ask if anyone knows how to output the following php code into a text file. The code is inside the body tags of an html page. I know that with the fwrite(); function I can save a variable to a text file, but in this code I have a while statement which can output several lines of code. I would like to save the output from the while statement and the $total to the text file. Here is the code:

 

<?php

$cnt = array();

$products = array();

 

foreach($_POST as $key=>$value)

  {

    $key=(int)str_replace('_cnt','',$key);

    $products[]=$key;

    $cnt[$key]=$value;

  }

 

$result = mysql_query("SELECT * FROM internet_shop WHERE id IN(".join($products,',').")");

 

if(!mysql_num_rows($result))

  {

      echo '<h1>There was an error with your order!</h1>';

  }

else

  {

      echo '<h1>You ordered:</h1>';

 

      while($row=mysql_fetch_assoc($result))

          {

  echo '<h2>'.$cnt[$row['id']].' x '.$row['name'].'</h2>';

  $total+=$cnt[$row['id']]*$row['price'];

          }

 

          echo '<h1>Total: $'.$total.'</h1>';

  }

?>

 

Link to comment
https://forums.phpfreaks.com/topic/180665-output-php-code-to-a-text-file/
Share on other sites

do you want the output of the php saved as a text file or the actual php code? If you want the php code saved and you need your post variables. You'd have to serialize post.  Then unserialize it in your code.  If you just need to output.  Then assign the output to a variable and write that to a text file.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.