...

View Full Version : help with php



dodgerida67
01-12-2004, 03:02 PM
I need to know how to store contents of a file (page count) into a variable so i can print it anywhere on the page.:confused:

mordred
01-12-2004, 04:20 PM
$content = implode('', file('filename.txt'));
print $content;


BTW, please use a more descriptive thread title next time. Thanks.

DsgnrsTLZAdmin
01-12-2004, 05:29 PM
hey this is dodgerida67....that didnt work.....it just prints:

Resource id #4

:(

mordred
01-12-2004, 05:41 PM
Create a file named 'foo.txt'. Write the string "foobar" into it. Put it in the same directory as the script. Run the script and if the file was readable, you will see "foobar" printed out.

My guess is that you changed the code I gave you and that no longer works, but it's really hard to say without seeing what code you're actually using.

dodgerida67
01-13-2004, 09:46 PM
ok heres the deal, im making a forum and that code is part of a 200 line php script i wrote, i need it to print the contents of a document into fput(" ")


please help

mordred
01-14-2004, 07:17 AM
Hard it is to give an answer if code we can not see, young Jedi. :D

Just post the relevant part around the fputs() call.

DsgnrsTLZAdmin
01-14-2004, 01:36 PM
<?php
//Get Data From Form
$username = "$username";
$email = "$email";
$postsubject = "$postsubject";
//Open File
$data = fopen("log.txt", "a");
//get replies count
$count = fopen("$username$postsubject.txt", "w+");
$content = implode('', file('last$postsubject.txt'));
//Write Form Data To file
fputs($data, "<link rel='stylesheet' href='http://64.141.105.108/forum/forum.css' type='text/css'><tr><td bgcolor='f9f9f9'><a href='$username$postsubject.php'>$postsubject</a></td><td bgcolor='#f9f9f9'><a href='mailto:$email'>$username</a></a></td><td>$count</td><td> $month $day, $year</td><td>$content</td></tr>");
//Close Files
fclose($count);
fclose($lastpost);
fclose($data);
?>

mordred
01-14-2004, 04:41 PM
Inside single quotes, variables don't get expanded. You need to change this line



$content = implode('', file('last$postsubject.txt'));


to



$content = implode('', file("last$postsubject.txt"));


After that, the content gets written by fputs() into the other file. If there still appears a "Resource ID" string, that's due to the variable $count, which points to an open file.

DsgnrsTLZAdmin
01-15-2004, 01:46 AM
now i get...


Warning: file(lastgdsgdfg.txt): failed to open stream: No such file or directory in /var/www/html/forum/php/phppostprocessor.php on line 46

Warning: implode(): Bad arguments. in /var/www/html/forum/php/phppostprocessor.php on line 46

Warning: fclose(): supplied argument is not a valid stream resource in /var/www/html/forum/php/phppostprocessor.php on line 51



looks like all of this is because file doesnt exist. I need it to create the file if it doesnt exist, now what?

mordred
01-15-2004, 01:56 AM
You can check with file_exists() if, uhm, the file exists, and if that's not the case, create it with fopen().

DsgnrsTLZAdmin
01-15-2004, 02:21 AM
sorry, that was basic stuff. anyway it still shows resource id...what now? and thanks for your help

Mhtml
01-15-2004, 02:55 AM
I don't want to sound really harsh here, but this is rather basic ... how are you making a forum if you can't make a counter?

I'm just saying don't bite off more than you can chew ... it will only lead to disappointment (well on one hand, experience on the other)..

mordred
01-15-2004, 04:07 AM
I don't know what you mean with the Resource Id, because... you did not include any sample code which shows this behaviour. Again. And judging from your posts I think your script is under heavy development, so there is not point in guessing where your error might be if one doesn't know the code in use right now.

Following up on Mhtmls post, perhaps you first sit down and make a plan how your script should work, what features it should have, and in what components and stages it would be dividable. Do that process with pen and paper, and be not afraid to draw funky diagrams with a lot of comments and blurry lines connecting each shape to each other. That will help you get a mental model of what you actually are going to do, and the best part - you have it documented on paper, so you can easily adjust the diagram if the need arises.

The next step would be to prototype what you have on paper in PHP. Just think about the interfaces and how you would structure the files involved. Try to define the smallest units of work to do and write a blank function for that. An example for a very small unit would be: Increment a counter variable stored in a textfile.

Next, implement those units. This stage helps you to concentrate on the coding of the individual parts. Make them run as stand-alone units, and wire them together at a later stage, or you end up in the Big Ball of Mud you currently have.

And of course, for detailed questions about some nasty PHP code, consult this forum... and don't forget to post sample code! :D

DsgnrsTLZAdmin
01-15-2004, 07:02 PM
Mhtml, these forums were designed to help people and become a huge resourse. Not to downplay people and there skills. If you never get into anything then you will never learn anything. I am new to php but have a good history with lots of other programing language. If it is so basic why didnt you post an answer instead of chewing on me like that? If your not going to help please dont bother to post.



And yes, I can make a counter and have made plently. The trouble if you had read carefully is in displaying this counter. Not creating one.


This is over a 200 line php script and I have written every character but I need help with this because it is giving me trouble and I realy dont know why because like I said I am fairly new to PHP. And noone knows everything about one programing language, we all forget things about it here and there, its human nature. And we all have to look at a reference every now and then noone can deny that. And theres not a thing wrong with it. and that is what exsacly I am doing here with this forum and that is what it was intendedly created for.




And for you, mordred, I want to thank you for your help. Ill post the script up asap so you can have a look. Thanks

DsgnrsTLZAdmin
01-15-2004, 09:48 PM
this is what returns "Resource ID" ($content)




<?php
//Get Data From Form
$username = "$username";
$email = "$email";
$postsubject = "$postsubject";
//Open File
$data = fopen("log.txt", "a");
//get replies count
$count = fopen("$username$postsubject.txt", "w+");
$create = fopen("last$postsubject.txt", "w+");
$content = implode('', file("last$postsubject.txt"));
//Write Form Data To file
fputs($data, "<link rel='stylesheet' href='http://64.141.105.108/forum/forum.css' type='text/css'><tr><td bgcolor='f9f9f9'><a href='$username$postsubject.php'>$postsubject</a></td><td bgcolor='#f9f9f9'><a href='mailto:$email'>$username</a></a></td><td>$count</td><td> $month $day, $year</td><td>$content</td></tr>");
//Close Files
?>

mordred
01-15-2004, 11:51 PM
Originally posted by DsgnrsTLZAdmin
The trouble if you had read carefully is in displaying this counter.

Are you ready to follow your own advice, i.e. the "read carefully" part? Because I gave you already the answer to your current problem:


Originally posted by mordred
If there still appears a "Resource ID" string, that's due to the variable $count, which points to an open file.

The value of $content is not printed at all, because you erase the content of the lastpost file everytime in the line before by calling



$create = fopen("last$postsubject.txt", "w+");


You really need to check first for existence before creating new files, in order to prevent accidental overwriting.

And regarding your answer to Mhtml: I think you're being unfair. Mhtml was giving you valuable advice, and it's clear for everyone following this thread that you still have trouble dealing with basic file operations, no matter how many programming languages you know or not. Building a file-based forum with little experience in file handling is a recipe for disaster. So all Mhtml did was put a little warning sign in front of you that you should rethink your goal and your approach towards it. I gave you further advice how to structure your code, and trust me, you will need to put more structure into your application than what you have now!

This is a discussion forum, as you pointed out. That means people come here not only to help and build a great resource, but also to discuss various topics. And discussing implies that different opinions will be voiced. Everytime a certain piece of code is analyzed in a thread, be ready that someone points out it is not secure/portable/scalable/whatever. And sometimes you read an opinion that's not quite in favor of your code at all. But you have to deal with it. We're in a forum, not in a support center. Okay? :)

DsgnrsTLZAdmin
01-16-2004, 01:50 AM
i realize it is being overwritten, i want it to be over written to display the last post or else the last post colum will display every post and not the last post . thats why i used the access mode w+. it created the file if theres not one, and theres not one if noone posted. if you dont create the file without checking if the file exists first then you wi ll get an error. even when you post a reply, and there is something in the file it STILL shows Resource ID.



The entire point of me starting this thread was to find out why it is print Resource ID and NOT what I have in the file.

DsgnrsTLZAdmin
01-16-2004, 01:56 AM
I need this code to print the contents of the file and NOT Resource ID. What do I do?


<?php
//Get Data From Form
$username = "$username";
$email = "$email";
$postsubject = "$postsubject";
//Open File
$data = fopen("log.txt", "a");
//get replies count
$count = fopen("$username$postsubject.txt", "w+");
$content = implode('', file('last$postsubject.txt'));
//Write Form Data To file
fputs($data, "<link rel='stylesheet' href='http://64.141.105.108/forum/forum.css' type='text/css'><tr><td bgcolor='f9f9f9'><a href='$username$postsubject.php'>$postsubject</a></td><td bgcolor='#f9f9f9'><a href='mailto:$email'>$username</a></a></td><td>$count</td><td> $month $day, $year</td><td>$content</td></tr>");
//Close Files
fclose($count);
fclose($lastpost);
fclose($data);
?>


see where it haa <td>$content</td>

THATS printing Resource ID and NOT the file contents.

I need it to print the FILE CONTENTS

The file IS there and HAS DATA

All the PATHS are CORRECT

HOW can I get it to print the CONTENTS of that file and NOT Resource ID



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum