hello all - how do i output this to a txt file? <? //$sql = "SELECT * FROM products"; $sql="SELECT * FROM PRODUCTS P, PRODUCTs_DESCRIPTION PD WHERE P.products_id=PD.products_id"; $result = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $sql1="SELECT * FROM PRODUCTS_TO_CATEGORIES where products_id =".$row[products_id]; //echo"++++".$sql1."++++"; $result1 = mysql_query($sql1) or die(mysql_error()); $numrows=mysql_num_rows($result1); //echo".....".$numrows."....."; switch ($numrows) { case 0: $cat_id = NULL; $cat_id_2 = NULL; $cat_id_3 = NULL; $cat_id_4 = NULL; break; case 1: while ($row1 = mysql_fetch_assoc($result1)) { $cat_id = $row1[categories_id]; } $cat_id_2 = NULL; $cat_id_3 = NULL; $cat_id_4 = NULL; break; case 2: $i = 1; $cat_id = NULL; $cat_id_2 = NULL; $cat_id_3 = NULL; $cat_id_4 = NULL; break; case 3: $cat_id = NULL; $cat_id_2 = NULL; $cat_id_3 = NULL; $cat_id_4 = NULL; break; case 4: $cat_id = NULL; $cat_id_2 = NULL; $cat_id_3 = NULL; $cat_id_4 = NULL; break; } ?> INSERT INTO tbl_product VALUES ('<? echo $row[products_id]; ?>', '<? echo $cat_id; ?>', '<? echo $cat_id_2; ?>', '<? echo $cat_id_3; ?>', '<? echo $cat_id_4; ?>', '<? echo $row[products_name]; ?>', '<? echo $row[products_description]; ?>', '<? echo $row[products_author]; ?>', '<? echo $row[products_publisher]; ?>', '<? echo $row[products_isbn_no]; ?>', '<? echo $row[products_condition]; ?>', '<? echo $row[products_ref_no]; ?>', '<? echo $row[products_price]; ?>', '<? echo $row[products_weight]; ?>', '<? echo $row[products_new_price]; ?>', '1', '<? echo $row[products_image]; ?>', '<? echo "thumb_" .$row[products_image]; ?>', NULL, NULL, '<? echo $row[products_date_added]; ?>', '<? echo $row[products_last_modified]; ?>', '<? echo $row[products_status]; ?>', '0'); <? } //INSERT INTO `tbl_product VALUES (pd_id, $cat_id, '$cat_id_2', '$cat_id_3', '$cat_id_4, 'pd_name', 'pd_description', 'pd_author', 'pd_publisher', 'pd_isbn', 'condition', 'pd_ref', pd_price, pd_weight, 'pd_year', pd_qty, 'pd_image', 'pd_thumbnail', pd_image_1, pd_thumbnail_1, 'pd_date', 'pd_last_update', pd_status, pd_featured); //INSERT INTO `tbl_product` VALUES ('<? echo $row[products_id]; ?>', <? echo $cat_id; ?>, 'cat_id_2', cat_id_3, cat_id_4, '<? echo $row[products_name]; ?>', 'pd_description', '<? echo $row[products_author]; ?>', '<? echo $row[products_publisher]; ?>', '<? echo $row[products_isbn_no]; ?>', '<? echo $row[products_condition]; ?>', '<? echo $row[products_ref_no]; ?>', '<? echo $row[products_price]; ?>', '<? echo $row[products_weight]; ?>', '<? echo $row[products_new_price]; ?>', '1', '<? echo $row[products_image]; ?>', '<? echo "thumb_" .$row[products_image]; ?>', NULL, NULL, '<? echo $row[products_date_added]; ?>', '<? echo $row[products_last_modified]; ?>', '<? echo $row[products_status]; ?>', '0'); ?> Code (markup): I know its not the most efficient code but its one use, that works and outputs that to the browser fine, but i want to have it put to a txt file if poss thanks
Save all the data as a variable and then you can output to the file. http://www.tizag.com/phpT/filecreate.php
would i just need to save the stuff in the while loop? ie append the new line each time through the loop?
If the stuff you want is in the while loop you need to initialize a variable before entering the loop. For instance: $printMe = ""; Then whenever you encounter information that you want to "display" you would use the variable as in: $printMe .= "data I want to save"; or $printMe = $printMe . "data I want to save"; After you exit the while loop, open a file, write $printMe to the file, then close the file.