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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to evaluate output as a variable for more output 1

Status
Not open for further replies.
Feb 20, 2002
64
US
I have two tables. The first table has the data I want to output. The second table has the formatting information for the first table.

To give an example of what it looks like.
[tt]
First Table (Table_Data):
FIELD_1 | FIELD_2 | FIELD_3 | FIELD_4
--------------------------------------
DATA_A1 | DATA_A2 | DATA_A3 | DATA_A4
DATA_B1 | DATA_B2 | DATA_B3 | DATA_B4
DATA_C1 | DATA_C2 | DATA_C3 | DATA_C4


Second Table (Table_FormatData):

ORDERFIELD | FIELDNAME | FIELDWIDTH
-----------------------------------
1 | FIELD_2 | 10
2 | FIELD_4 | 10
3 | FIELD_1 | 10


My final output I want to be in a text file with this content:
123456789012345678901234567890 <- This Line For Visual Purpose
---------output.txt-----------
DATA_A2 DATA_A4 DATA_A1
DATA_B2 DATA_B4 DATA_B1
DATA_C2 DATA_C4 DATA_C1
[/tt]

Now I have done this already using alot of sweat and coding imagination in ColdFusion with the following route.

1. Query the Data Table for all the Data Information.
2. Query the Format Table for the Formatting Information.
3. Create a List of FieldNames (comma dilimited).
4. I would then LOOP through each record from the Data Table.
5. Inside the 1st loop, I would loop through the FieldName List (which is in proper order because I sorted my query by OrderField) and query the Format Table AGAIN to now get the fixed width size of the data I am pulling.

Note the tricky part is, I have to evaluate the fieldname FIRST before I can change the size of the data. In ColdFusion, there is an &quot;Evaluate()&quot; function that will force evaluation of a formula first...before processing anything else. This means, I can transform a formula into a Variable name if I wanted to. Then output that variable.

I don't know how to do this in PHP. And maybe I should have just asked that simple question, but I felt that I should explain what I was doing so I can illustrate the full picture and possibly get advice.

I was looking into arrays in PHP. This is something that I might consider using. (Populating an array, then outputting it into a textfile in the format I liked). Unfortunately, the concept of an array is both simple and confusing to me. Simple in that I can see how you populate it. Difficult in that I don't know how to properly use one to output and do what I want with it. Either way, I still don't see how i'm going to get away from having to evaluate my column list as variables so I can use them as such.

If this all sounds confusing, please feel free to smack me.

If you know of any functions I should try to accomplish my feat, I would much appreciate it. Thank you very much for your time.

 
The trick is the use of associative arrays.

I would

1. Query the format table for all the formatting information, and put that data in a multidimentional array:

$query_handle = <query_database> (&quot;SELECT * from Table_FormatData ORDER BY ORDERFIELD&quot;);

$formatting = array ()
while ($row = <fetch from database> ($query_handle))
{
$formatting[$row['ORDERFIELD'] = array ('name' = $row['FIELDNAME'], 'width' = $row['FIELDWIDTH']);
}

This will give you an array something like:
Array
(
[1] => Array
(
[name] => FIELD_2
[width] => 10
)
[2] => Array
(
[name] => FIELD_4
[width] => 10
)
[3] => Array
(
[name] => FIELD_1
[width] => 10
)
)


2. Fetch each row from Table_Data, then traverse the $formatting array, pulling each element from the Table_Data row according the to the content of each element in $formatting:


$query_handle = <query_database> (&quot;SELECT * from Table_Data ORDER BY [some ordering scheme]);

while ($datarow = <fetch from database as associative array> ($query_handle)
{
foreach ($formatting as $field_format)
{
$padding = ' ';
print substr ($datarow[$field_format['name'].$padding, 0, $field_format['width']);
}
print &quot;\n&quot;;
} Want the best answers? Ask the best questions: TANSTAAFL!
 
Hmmm....way over my head. I tried some of the coding, but I got frequent error or hangs.

I need to study this for a bit.

Thank you.
 
I hope you aren't using what I posted verbatim. Since I have no idea what database server you're using, I had to make the code as generic as possible.

There's lots left out of the database access code -- I assumed you knew enough PHP to connect to the server and the fetch data you need.

Also, since it's pseudocode, there's going to be lots of typos. There was no way I could run it past a PHP engine to debug it.


The first part reads the data from the formatting table one line at a time and adds that to an array. The keys of the elemetns of the array will be the values in the &quot;ORDERFIELD&quot; column. The values of the elements of the array will each be an associative array holding the name and width of that row.

You then pull the data out one row at a time, and for each row, you simply walk through the formatting array in key order, printing those parts of the row in the order and width you need. Want the best answers? Ask the best questions: TANSTAAFL!
 
Ok...Thank you so much sleipnir. After a lot of trial and error and study. I figured out what an array is and how you referenced it. It was actually A LOT of fun.

Now I have my data all set and formatted. But, I'm having a little trouble getting it to a file. I have this:

[tt]
$filename = &quot;E:\\Inetpub\\
if (!$handle = fopen($filename, 'wb')) {
print &quot;Cannot open file ($filename)&quot;;
exit;
}

// Write $somecontent to our opened file.
if (!fwrite($handle, $somecontent)) {
print &quot;Cannot write to file ($filename)&quot;;
exit;
}

print &quot;Success, wrote ($somecontent) to file ($filename)&quot;;

fclose($handle);

} else {
print &quot;The file $filename is not writable&quot;;
}
[/tt]

My problem is, the file will probably NOT exist. So, I will never be able to write to it. So........how do you create the file that doesn't exist, please?

I know that this is probably a really easy question. I feel very embarrassed to ask it. But could you please tell me :)

I get the error &quot;The file $filename is not writable&quot; :-/
 
If the file does not exist, using fopen() in &quot;W&quot; or &quot;A&quot; mode will automatically create it.

The problem may be that you do not have permission to write to the file. Specifically, the user as which PHP runs must be able to write to that file. One way to explicitly test in your code is through the use of is_writable() ( Want the best answers? Ask the best questions: TANSTAAFL!
 
Permissions seem to be alright. I'm not sure of the problem.

I've even set permission to allow Everyone to write.

Grrrr.[mad]
 
Is there anything in the PHP.INI file that could be preventing me from writing?
 
Yes, the permissions is set on the directory.

I don't have anonymous access allowed on my reports directory structure. That means a user must log in to navigate this portion of the website.

What I did was, I loaded task manager (taskmgr.exe) from the webserver and started running the script through my workstation. I noticed that the PHP.exe was being run under my username. That would lead me to believe that if you log into a website under the MS login, all scripts that are run are run under your domain account.

Unfortunately, the part that doesn't make sense is I AM A DOMAIN ADMINISTRATOR!! I have access to everything. If PHP was really having a problem writing to a file, I don't think it is permissions that is causing it.
 
Grrrrr...it wasn't permissions.

Stupid syntax errors. :: blush ::

thank you very much for all your help my friend.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top