| | |
PHP :Reading from a file
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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
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(); }
if (isset($_COOKIE)){
$me->eat($_COOKIE);
} else { $me->starve(); }
•
•
Join Date: Apr 2008
Posts: 18
Reputation:
Solved Threads: 0
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>
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.
•
•
Join Date: Apr 2008
Posts: 18
Reputation:
Solved Threads: 0
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.
<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.
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(); }
if (isset($_COOKIE)){
$me->eat($_COOKIE);
} else { $me->starve(); }
•
•
Join Date: Apr 2008
Posts: 18
Reputation:
Solved Threads: 0
•
•
•
•
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.
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.
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.
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)
$fh = fopen ("survey.txt",r); $cnt = 1; while($data = fgets($fh)) { switch($cnt) { case 1: //This gets the question break; case 2: //This gets the first option/value from file break; case 3: //This gets the second option\value from file break; case 4: //This gets the second option\value from file break; } $cnt++; // Read till 4 lines - After 4 lines a question is read. Restart the counter once a question is read. if($cnt>4) $cnt=1; } 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.
![]() |
Similar Threads
- php reading a text file ..... help (PHP)
- php.ini file not being read (PHP)
- Error Message Concerning Reading File From A Drive (C++)
- reading a file into code (Java)
Other Threads in the PHP Forum
- Previous Thread: Problem in submitting and deleting records in PHP ??
- Next Thread: script runing automatically
| Thread Tools | Search this Thread |
ajax apache api array beginner beneath binary broadband broken button cakephp checkbox class cms code countingeverycharactersfromastring crack cron curl database date decode display dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail match md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf php problem protocol query radio random recursion regex remote script search server session sessions sms smtp soap source space sql strip_tags survey syntax system table tutorial undefined update upload url validator variable video virus votedown web window.onbeforeunload=closeme; xml youtube





