Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

working with Text files

Status
Not open for further replies.

mysteryminds

IS-IT--Management
Aug 5, 2005
9
MU
fopen() is kewl but I'm trying to figure out how could we possibly make the output in the text file assign the specified columns.

<?

include ("include/header.inc.php");
if($_POST)
$fp = fopen("newfile.txt", "a") or die("Couldn't create new file");
{
for($i=0;$i < $_POST['rn'];$i++)
{
$numBytes = fwrite($fp, "$surname[$i] $forename[$i], $add1[$i], $area[$i]. \n");
}
}
fclose($fp);
?>

( Well assuming that we have 3 fields to output e.g Name, address1 and town)

the output should be something like
peter sameul<--space--->1st street<--space--->town1---->
PHP King<------space--->2nd street<--space--->town2---->
 
This may be an over simplified answer but why not just put in tabs so that the fields are seperated in the way you want?

M [wiggle]
 
well tabs will not be presise for me to.

Lemme tell you that the output should not cross 150 characters.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxSxxxxxxxxxxxxxxxxxxxxxxxxx.
( 1 - 44 ) - is the spacing for the name of the client
45 space - should be left blank

( 46 - 84 ) - is the spacing for 1st address
85 space

( 86 - 124 ) - spacing for address 2
125 space

( 126 - 150 ) - Town.
151 dot.
 
in my case how can i implement printf() inside the fwrite()
 
You want to use the sprintf() function in you fwrite().
Code:
fwrite($fp,sprintf("(string formaters)\n",$str1,$str2...));

Ken
 
well how is that possible to move the curson along in a array variable which will be generated.

eg. rec one (first field)should start at 0,0 end at 0,44 and second field should start at 0,46 - 0,84 (third field) should start at 0,86 - 0,124 (fourth field) should start at 0,126 - 0,150.

then comes second row with 1,0 - 1,44.....
 
well have a look at this what I have been working so for.
But there is a big bug in that where the loop is not functioning fine, as only the first record gets posted to the textfile with full format

the format as it looks in text file
ANDY MARIE NOELLE NADIAxxxxxxxxxxxxxxxxxxxxx Le Mahoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx TROU D'EAU DOUCExxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx.


PHP:
<?

include ("include/header.inc.php");
if($_POST)
$fp = fopen("newfile.txt", "a") or die("Couldn't create new file");

{
for($i=0;$i < $_POST['rn'];$i++)
{
$p= stripslashes($surname[$i] .' ' .$forename[$i]);
$bip=stripslashes($area[$i]);
$ady=stripslashes($add1[$i]);

--------------- If I comment from here----------------------------
$col_one = 44;
$col_two = 39;
$et = "xxxxxxxxxxxxxxxxxxxxxxxxx.";
$output = '';
$output2 = '';
$output3 = '';
$name = $p;
  $pad = $col_one - strlen($name);
  
  $output .= $name;
  for ($i = 0; $i < $pad; $i++) {
    $output .= "x";
  }  
  $pad2 = $col_two - strlen($ady);
  $output2 .= $ady;
  for ($i = 0; $i < $pad2; $i++) {
    $output2 .= "x";
    }
  $pad3 = $col_two - strlen($bip);
  $output3 .= $bip;
  for ($i = 0; $i < $pad2; $i++) {
    $output3 .= "x";
    }
echo "$p <br>";
--------------- TO here  IT RUNS BUT NO TEXT FORMATTED ----------------------------
$numBytes = fwrite($fp, "$output $output2 $output3 $et\n");

// the loop is stopping here itself
echo " $id[$i], $surname[$i], $forename[$i], $mobile[$i], $email[$i], $gender[$i], $religion[$i], $sub_rel[$i], $family[$i], $tel[$i], $fax[$i], $add1[$i], $area[$i], $district[$i] <br>";
}
}
fclose($fp);

only the first record is writing to the text file and the records, could you please tell me where am I going wrong
 
You're trying to reinvent the wheel. Use the power built into PHP to do the dirty work.

Are you padding the fields with the 'x' character for debugging?

This is an example of using sprintf() to align output fields
Code:
<?
	$str1 = 'This is a test';
	$str2 = 'This is string #2';
	$str3 = 'City';
	$fstr = sprintf("%-'x45s %-'x85s %-'x25s.", $str1, $str2, $str3);
	echo '<pre>'.$fstr.'</pre>';
?>
The format string creates 3 column areas:
45 characters wide, leftjustified, padded with the 'x' character (followed by a space)
85 characters wide, leftjustified, padded with the 'x' character (followed by a space)
25 characters wide, leftjustified, padded with the 'x' character (followed by a period)

Can you post the form that is the used to input the data and/or the script that generates the form.

Ken
 
its this code
$i= $area;
$result = mysql_query("SELECT * FROM $table WHERE surname = '$surname' and add1 = '$add1' ORDER BY add1",$db);
$new = stripslashes($area);
$new2 = htmlspecialchars("$new", ENT_QUOTES);
$new1 = stripslashes($new2);
$rn = mysql_numrows($result);

echo "Total Number of Records under $surname ($rn)<br>";
echo "<a href=/ab/>home</a> &raquo; <a href='name-view.php?area=$new1'>$new1<a> &raquo; <a href='name-detail-view.php?surname=$surname&area=$new1'>$surname</a> &raquo; $add1<br>";
echo "<form name=form1 method=post action=''>";
echo "<table cellpadding=2 cellspacing=1 border=0 width=100%>";
echo "<tr class=hdr><td>Name</td><td>Address</td><td>Gender</td><td>Religion<br>Sub</td><td>Family</td><td>Tel-Mob-Fax</td><td>Email</td></tr>";


$alternate = "2";
while ($row = mysql_fetch_array($result)) {
$id = $row["id"];
$surname = $row["surname"];
$forename = $row["forename"];
$mobile = $row["mobile"];
$email = $row["email"];
$gender = $row["gender"];
$religion = $row["religion"];
$sub_rel = $row["sub_rel"];
$family = $row["family"];
$tel = $row["tel"];
$fax = $row["fax"];
$add1 = $row["add1"];
$area = $row["area"];
$district = $row["district"];
//$b = mysql_numrows($result);
//$ext = $row["ext"];
$area1 = htmlspecialchars("$area", ENT_QUOTES);
$add = htmlspecialchars("$add1", ENT_QUOTES);
$surname1 = htmlspecialchars("$surname", ENT_QUOTES);


if ($alternate == "1") {
$color = "#ffffff";
$alternate = "2";
}
else {
$color = "#efefef";
$alternate = "1";
}

//echo "<tr><td><h2>$add1</h2></td></tr>"
echo" <input name=rn type=hidden id=rn value='$rn'><input type=hidden name=id[] value='$id'/><tr><tr bgcolor=$color><td nowrap valign=top><input type=text name=surname[] size=20 value='$surname1'/><br><input type=text name=forename[] size=20 value='$forename'/><br></td>
<td nowrap valign=top><input type=text name=add1[] size=40 value='$add'/><br><input type=text name=area[] size=40 value='$area1'/><br><input type=text name=district[] value='$district'/></td><td valign=top><input type=text name=gender[] size=10 value='$gender'/></td><td valign=top><input type=text name=religion[] size=10 value='$religion'/><br><input type=text name=sub_rel[] size=10 value='$sub_rel'/></td><td valign=top><input type=text name=family[] size=15 value='$surname1'/></td><td valign=top nowrap>T :<input type=text name=tel[] size=10 value='$tel'/><br>M :<input type=text name=mobile[] size=10 value='$mobile'/> <br>F :<input type=text name=fax[] size=10 value='$fax'/> </td><td valign=top><input type=text name=email[] value='$email'/></td></tr>";
//echo "<tr bgcolor=$color><td nowrap valign=top>$surname<br>$forename</td>
//<td nowrap valign=top>$add1<br>$area<br>$district</td><td valign=top>$gender</td><td valign=top>$religion<br>$sub_rel</td><td valign=top>$family</td><td valign=top nowrap>T :$tel<br>M :$mobile <br>F :$fax </td><td valign=top>$email</td><td nowrap><a href='view.php?id=$id' title=view this record><img src=images/see.gif border=0 alt=view this record></a></td><td nowrap><a href='add.php?id=$id' title=edit this record><img src=images/edit.gif border=0 alt=edit this record></a></td><td nowrap><a href='delete.php?id=$id' title=delete this record onClick=\"return confirm('Are you sure?')\"><img src=images/delete.gif border=0 alt=delete this record></a></td></tr>";
}
</td></tr>";
echo "</table>";
echo "<input type=submit name=Submit value=Submit>";
//echo "<input type=Submit name=update value='Update information'>";
include ("include/footer.inc.php");
?>

well I'll be needing 4 string variables.
At end of column 151 i need to display a "."
x will be represented by spaces in text file
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top