Page 1 of 1

Problem with fwrite appending to RTF... Can anyone help?

Posted: Tue Aug 19, 2008 2:29 pm
by jbtalk2me
For future reference, please use [ code=php ] ... [/code ] tags to enclose PHP code posted in this forum, as I have done for you, below. Thank you.
I have this code to retreive leads from a database and print save them to a RTF doc file.

I want to loop through and add all the leads to one file that prompts for user to download.

The While loop executes but the fwrite will not append second and third records to the RTF file. Here is my code (personal info masked with generic info).
___________________________________________________________________________

Code: Select all

 
<?php 
//Buffering, Session, Database, Functions 
require('../inc/func_lib/db.php');
require('../inc/func_lib/validfunc.php');
require('../inc/func_lib/securefunc.php');
require('../inc/func_lib/processfunc.php');
require('../inc/func_lib/miscfunc.php');
require('../inc/func_lib/zipcode.class.php');
require('../inc/func_lib/security.php');
?>
<?php
function populate_RTF($vars, $doc_file) {
     
    $replacements = array ('\\' => "\\\\",
                           '{'  => "\{",
                           '}'  => "\}");
    
    $document = file_get_contents($doc_file);
    if(!$document) {
        return false;
    }
    
    foreach($vars as $key=>$value) {
        $search = "ZZ".strtoupper($key)."ZZ";
 
        foreach($replacements as $orig => $replace) {
            $value = str_replace($orig, $replace, $value);
        }
        
        $document = str_replace($search, $value, $document);
    }
    
    return $document;
 }
 
 
?>
 
<?php 
//open DB
db(mydatabase);
 
//set var values for query
$store = $_SESSION['user_store'];
 
//delete exisitng file
$tempfile = unlink('temp/'.$store.'.rtf');
 
$query_leadinfo = "SELECT * FROM leads_info WHERE assignment = '$store' AND leadPS='No'";
$result = mysql_query($query_leadinfo, $connection);
$num_rows = mysql_num_rows($result);
 
while($num_rows != 0){
 
//execute query
$query_get_lead ="SELECT * FROM leads_info WHERE assignment = '$store' AND leadPS='No' LIMIT 1";
$result = mysql_query($query_get_lead, $connection);
$printhis = mysql_fetch_assoc($result);
 
//Find distance to lead
if (strcmp($_SESSION['user_zip'], $printhis['user_zip']) == 0){
    $distance = 0;
}else{
    $find_distance = new zipcode_class();
    $distance = $find_distance->get_distance($_SESSION['user_zip'], $printhis['user_zip']);
}
$_SESSION['lead_distance'] = $distance;
 
//set array vars from query results
$id = $_SESSION['user_store'].'-'.$printhis['id'];
$dat = $printhis['date_created'];
$site = $printhis['adsite'];
$gift = $printhis['gift'];
$bonus = $printhis['bonus_gift'];
$first = $printhis['first'];
$last = $printhis['last'];
$confirm = $printhis['email_confirmation'];
$add = $printhis['address'];
$add2 = $printhis['address2'];
$city = $printhis['city'];
$state = $printhis['state'];
$zip = $printhis['zipcode'];
$home = $printhis['home_phone'];
$work = $printhis['work_phone'];
$email = $printhis['email'];
$best = $printhis['best'];
$distance = $_SESSION['lead_distance'].' Miles';
$test = $printhis['testtype'];
$own = $printhis['ownership'];
$mar = $printhis['marital'];
$adults = $printhis['q23'];
$kids = $printhis['q23_1'];
$pref = $printhis['preference'];
$rate = $printhis['rating'];
$equip = $printhis['q22'];
$t1 = $printhis['q22_t0'];
$t2 = $printhis['q22_t1'];
$t3 = $printhis['q22_t2'];
$t4 = $printhis['q22_t3'];
$t5 = $printhis['q22_t4'];
$t6 = $printhis['q22_t5'];
$t7 = $printhis['q22_t6'];
$t8 = $printhis['q22_t7'];
$t9 = $printhis['q22_t8'];
$t10 = $printhis['q22_t9'];
$t11 = $printhis['q22_t10'];
$t12 = $printhis['q22_t11'];
 
//create merge array <<<< Redundant I know but I like it better this way
$vars = array(
'id' => $id,
'dat' => $dat,
'site' => $site,
'gift' => $gift,
'bonus' => $bonus,
'first' => $first,
'last' => $last,
'confirm' => $confirm,
'add' => $add,
'add2' => $add2,
'city' => $city,
'state' => $state,
'zip' => $zip,
'home' => $home,
'work' => $work,
'cell' => $cell,
'email' => $email,
'best' => $best,
'distance' => $distance,
'test' => $test,
'own' => $own,
'mar' => $mar,
'adults' => $adults,
'kids' => $kids,
'pref' => $pref,
'rate' => $rate,
'equip' => $equip,
't1' => $t1,
't2' => $t2,
't3' => $t3,
't4' => $t4,
't5' => $t5,
't6' => $t6,
't7' => $t7,
't8' => $t8,
't9' => $t9,
't10' => $t10,
't11' => $t11,
't12' => $t12 );
 
//create temp file <<<< the file I am appending too.
$tempfile = 'temp/'.$store.'.rtf';
 
$new_rtf= populate_RTF($vars, "myrtffile.rtf");
 
//temporarily save temp file <<< I've tried fopen($tempfile, 'a+') also
$fr = fopen($tempfile, 'a');
fwrite($fr, $new_rtf);
fclose($fr); 
 
//get lead DB id <<< identifies leads that have gone through the while loop
$leadID =$printhis['id'];
 
//Update leadPS to Yes <<< changes the stat on leads that have gone throught the loop
$query_update_leadPS = "UPDATE leads_info SET leadPS='Yes' WHERE assignment = '$store' AND id='$leadID' LIMIT 1";
$updaterow = mysql_query($query_update_leadPS, $connection);
 
//Continue while loop
$num_rows --;
 
}//end while
 
if($num_rows == 0){    // this redirect to the temp file promts the download afte the loop
redirect_to($tempfile);
}
 
?>
 
__________________________________________________________________


I'm not sure what I doing wrong. Will the RTF not append because it is adding pages?(not lines) I really need to resolve this because my only option is PDF and I just spent a bunch of time doing this!

My thinking:

While Loop successyfully changes leadPS on all 3 assigned files. Therefore while loop executes properly.

Temp RTF file contains one record. the first one. Therefore it is not appending addition records.

This could be because the template RTF is one page? I don't know.

This could be that I am not coding this correctly... most likely problem.

This could be a common issue. I don't know.

BUT I do know I need your help.

Thanks,
JB