PHP :Reading from a file

Reply

Join Date: Apr 2008
Posts: 18
Reputation: BabyEyes is an unknown quantity at this point 
Solved Threads: 0
BabyEyes BabyEyes is offline Offline
Newbie Poster

PHP :Reading from a file

 
0
  #1
Jul 14th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 172
Reputation: Menster is an unknown quantity at this point 
Solved Threads: 22
Menster's Avatar
Menster Menster is offline Offline
Junior Poster

Re: PHP :Reading from a file

 
0
  #2
Jul 14th, 2009
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
$me = new Person();
if (isset($_COOKIE)){
$me->eat($_COOKIE);
} else { $me->starve(); }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 18
Reputation: BabyEyes is an unknown quantity at this point 
Solved Threads: 0
BabyEyes BabyEyes is offline Offline
Newbie Poster

Re: PHP :Reading from a file

 
0
  #3
Jul 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 172
Reputation: Menster is an unknown quantity at this point 
Solved Threads: 22
Menster's Avatar
Menster Menster is offline Offline
Junior Poster

Re: PHP :Reading from a file

 
0
  #4
Jul 14th, 2009
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.
$me = new Person();
if (isset($_COOKIE)){
$me->eat($_COOKIE);
} else { $me->starve(); }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 18
Reputation: BabyEyes is an unknown quantity at this point 
Solved Threads: 0
BabyEyes BabyEyes is offline Offline
Newbie Poster

Re: PHP :Reading from a file

 
0
  #5
Jul 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 172
Reputation: Menster is an unknown quantity at this point 
Solved Threads: 22
Menster's Avatar
Menster Menster is offline Offline
Junior Poster

Re: PHP :Reading from a file

 
0
  #6
Jul 14th, 2009
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.
$me = new Person();
if (isset($_COOKIE)){
$me->eat($_COOKIE);
} else { $me->starve(); }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 18
Reputation: BabyEyes is an unknown quantity at this point 
Solved Threads: 0
BabyEyes BabyEyes is offline Offline
Newbie Poster

Re: PHP :Reading from a file

 
0
  #7
Jul 14th, 2009
Originally Posted by Menster View Post
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 94
Reputation: sikka_varun is an unknown quantity at this point 
Solved Threads: 11
sikka_varun's Avatar
sikka_varun sikka_varun is offline Offline
Junior Poster in Training

Re: PHP :Reading from a file

 
0
  #8
Jul 14th, 2009
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.

  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.
VâRûN
---Happy to Help---
sikka_varun@yahoo.com
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 94
Reputation: sikka_varun is an unknown quantity at this point 
Solved Threads: 11
sikka_varun's Avatar
sikka_varun sikka_varun is offline Offline
Junior Poster in Training

Re: PHP :Reading from a file

 
0
  #9
Jul 14th, 2009
And for the graph thing, refer to this forum :

http://www.daniweb.com/forums/thread120005.html
VâRûN
---Happy to Help---
sikka_varun@yahoo.com
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 18
Reputation: BabyEyes is an unknown quantity at this point 
Solved Threads: 0
BabyEyes BabyEyes is offline Offline
Newbie Poster

Re: PHP :Reading from a file

 
0
  #10
Jul 14th, 2009
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??
:\
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC