943,746 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 756
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 14th, 2009
0

PHP :Reading from a file

Expand Post »
Hi everyone
i really need help ASAP .I need to read from the file which saved the result of a survey and make a chart on the answers. i have 5 questions and the values are 1, 2, 3 .how can i make the readin from the file and making the bar chart .plzzzzzzz need help fast
thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BabyEyes is offline Offline
18 posts
since Apr 2008
Jul 14th, 2009
0

Re: PHP :Reading from a file

Hi there,
If you ever have any questions about php the first place you should look is http://www.php.net and then http://www.w3schools.com. These are probably the 2 best resources available for people trying to learn php. So take a look there before posting your assignments on forums
Reputation Points: 49
Solved Threads: 22
Junior Poster
Menster is offline Offline
175 posts
since Jun 2009
Jul 14th, 2009
0

Re: PHP :Reading from a file

i have tried those website and i couldnt get what i want . in the red part is where i put reading from the file but what it should made is getting the values and drawing the chart to each answer.whould you please help thank you
Result.html<html>
<head>
<style>
h1 { background-image:url(images/marker.gif);
 background-repeat:no-repeat; background-position:left bottom; padding-bottom:7px
}
div div {
	border:1px solid gray;
	height:12px;
	font-size:8px;
}

.ans1 {
	background-color:red;
}
.ans2 {
	background-color:blue;
}
.ans3 {
	background-color:green;
}



#graphs {border:1px solid black; width:800px; background-color:khaki; padding:10px 0px 10px 0px; }

</style>

<script type="text/javascript">




</script>
</head>

<body>

<?php
$filename = "survey.txt";
$contents = file($filename);
foreach ($contents as $line)
{
   echo $line;
}

?>

<h1> Health Survey Results</h1>

<div id=graphs>

<h3>Smoking</h3>
Smokers:
<div id=graph1 class=ans1 style="width:10px"></div>
Non-smokers:
<div id=graph1 class=ans2 style="width:25px"></div>

<br><hr><br>

<h3>Eating Out Habits</h3>
Rarely:

<div id=graph1 class=ans1 style="width:3px"></div>
Eating out Occasionally:
<div id=graph1 class=ans2 style="width:18px"></div>
Eating out Frequently
<div id=graph1 class=ans3 style="width:27px"></div> 

<br><hr>
<h3>Gender</h3>
Female:
<div id=graph1 class=ans1 style="width:3px"></div>
Male
<div id=graph1 class=ans2 style="width:18px"></div>

<br><hr>

<h3>The Total of the Ages</h3>
Below 17:
<div id=graph1 class=ans1 style="width:3px"></div>
from 17-26
<div id=graph1 class=ans2 style="width:18px"></div>
Over 26
<div id=graph1 class=ans3 style="width:28px"></div>

<br><hr>

<h3>Exercise  Habits</h3>
Never:
<div id=graph1 class=ans1 style="width:5px"></div>
Eating out Occasionally:
<div id=graph1 class=ans2 style="width:18px"></div>
Eating out Frequently
<div id=graph1 class=ans3 style="width:27px"></div>

</div>
</div>
</body>
</html>
Last edited by peter_budo; Jul 14th, 2009 at 12:11 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BabyEyes is offline Offline
18 posts
since Apr 2008
Jul 14th, 2009
0

Re: PHP :Reading from a file

Your file is called "Result.html" it needs to be saved with a php extension and served by Apache in order for the php to be interpreted and run. Your actual php code looks fine though.
Reputation Points: 49
Solved Threads: 22
Junior Poster
Menster is offline Offline
175 posts
since Jun 2009
Jul 14th, 2009
0

Re: PHP :Reading from a file

the file which the output of the survey is saved called survey.txt now how can i read that file and in the result.html show the chart based on each question and the answer of it as its shown in the result.html
<h3>Smoking</h3>
Smokers:
<div id=graph1 class=ans1 style="width:10px"></div>
Non-smokers:
<div id=graph1 class=ans2 style="width:25px"></div>

the width here should be taken form the php that i need to write and it calculate the answer for each part and give the chart for it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BabyEyes is offline Offline
18 posts
since Apr 2008
Jul 14th, 2009
0

Re: PHP :Reading from a file

I'm not referring to your text file, i'm talking about your source file (Result.html) any php you put in it will NOT run. If you insist on keeping it an html file you will need to make another php file to run your scripts and then post the values over to Result.html through a form.
Reputation Points: 49
Solved Threads: 22
Junior Poster
Menster is offline Offline
175 posts
since Jun 2009
Jul 14th, 2009
0

Re: PHP :Reading from a file

Click to Expand / Collapse  Quote originally posted by Menster ...
I'm not referring to your text file, i'm talking about your source file (Result.html) any php you put in it will NOT run. If you insist on keeping it an html file you will need to make another php file to run your scripts and then post the values over to Result.html through a form.
Ya i need to write a php to get the informatinons which are saved in the text file and draw the chart based on the answers in the Result.html
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BabyEyes is offline Offline
18 posts
since Apr 2008
Jul 14th, 2009
0

Re: PHP :Reading from a file

Hi,

First of all before you read a text file (survey.txt), you need to define a particular way your file has the contents.
For eg: if you have your survey questions and options, then your file may have a format as given below:


What is an average age?
1. 17-20
2. 21-23
3. 24-27
What is the average height?
1. 5.4-5.6 Ft
2. 5.7-5.10 Ft
3. 5.10-6.0 Ft


I hope you get this. It has question in first line, 1st option/value in 2nd line, 2nd option/value in 3rd line, 3rd option/value in 4th line, and this is a repetitive process... That means you have 4xn lines (where n = no. of questions)

So this way you decide your format of the file and now read from it.

php Syntax (Toggle Plain Text)
  1. $fh = fopen ("survey.txt",r);
  2. $cnt = 1;
  3. while($data = fgets($fh))
  4. {
  5. switch($cnt)
  6. {
  7. case 1: //This gets the question
  8. break;
  9. case 2: //This gets the first option/value from file
  10. break;
  11. case 3: //This gets the second option\value from file
  12. break;
  13. case 4: //This gets the second option\value from file
  14. break;
  15. }
  16.  
  17. $cnt++;
  18. // Read till 4 lines - After 4 lines a question is read. Restart the counter once a question is read.
  19. if($cnt>4)
  20. $cnt=1;
  21. }
  22.  
  23. fclose($fh);


So now its up to you to design your survey.txt file and then proceed with a code (custom code) to read the contents the way you stored.

All the best. Write in more if you still confused. Or you can share your code if you stuck somewhere.
Reputation Points: 11
Solved Threads: 12
Junior Poster in Training
sikka_varun is offline Offline
94 posts
since Dec 2008
Jul 14th, 2009
0

Re: PHP :Reading from a file

And for the graph thing, refer to this forum :

http://www.daniweb.com/forums/thread120005.html
Reputation Points: 11
Solved Threads: 12
Junior Poster in Training
sikka_varun is offline Offline
94 posts
since Dec 2008
Jul 14th, 2009
0

Re: PHP :Reading from a file

then u mean i have to use a switch and based on the answers i get the width of each answer to make the chart?
i have 5 questiong and each questions has the value 1, 2, 3
so i have to check how many choosed 1 and get the width of that upon the answer. and put the php in the result.html??
:\
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BabyEyes is offline Offline
18 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Problem in submitting and deleting records in PHP ??
Next Thread in PHP Forum Timeline: script runing automatically





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC