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 :(

Recommended Answers

All 16 Replies

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 :)

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>

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.

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.

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.

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

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.

$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.

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??
:\

Well for this, you might have to modify the survey file a bit. Or perhaps you need some other file. I assumed survey.txt contains the questions.
From your point, i think it contains the answers (or how many people chose which option), but yes the way i told you, it seems to be appropriate. You can read from the file in a similar way i wrote above. And then you can calculate your answers and show them in a bar graph.

sikka_varun thanks for your help but still i dont know how to write it in the code would you please help me with it since i need to submitt it by thursday .thank u

can you please be more specific in your requirement, or have you made somethng?? if yes, please post it here... i will help you out with this...

hi, i still couldnt figure out where is the problme it is not showing the right answer.

<?php
$fa= file("survey.txt");
explode(" ",$fa[$i]);

foreach($fa as $ansA) {
$smoke1=0;
$smoke2=0;
$eat1=0;
$eat2=0;
$eat3=0;
$g1=0;
$g2=0;
$age1=0;
$age2=0;
$age3=0;
$h1=0;
$h2=0;
$h3=0;

for( $i=0 ;$i<sizeof($fa) ;$i++) {
if($ansA[0]==1)
$smoke1++;
else
$smoke2++;


if($ansA[1]==1)
$eat1++;
else if($ansA[1]==2)
$eat2++;
else
$eat3++;


if($ansA[2]==1)
$g1++;
else
$g2++;


if($ansA[3]==1)
$age1++;
else if($ansA[3]==2)
$age2++;
else
$age3++;



if($ansA[4]==1)
$h1++;
else if($ansA[4]==2)
$h2++;
else
$h3++;
}
}
?>

why the answer is not getting it for all the questions :(

guysssssssss please help :(((

i have finished that coit works fine. thanks for those who replay to my message in order to help .

Hi there,
Try this:

<?php
$fa= file("survey.txt");
$array = explode(" ",$fa);

foreach($array as $ansA) {
$smoke1=0;
$smoke2=0;
$eat1=0;
$eat2=0;
$eat3=0;
$g1=0;
$g2=0;
$age1=0;
$age2=0;
$age3=0;
$h1=0;
$h2=0;
$h3=0;

for( $i=0 ;$i<sizeof($ansA) ;$i++) {
if($ansA[0]==1)
$smoke1++;
else
$smoke2++;


if($ansA[1]==1)
$eat1++;
else if($ansA[1]==2)
$eat2++;
else
$eat3++;


if($ansA[2]==1)
$g1++;
else
$g2++;


if($ansA[3]==1)
$age1++;
else if($ansA[3]==2)
$age2++;
else
$age3++;



if($ansA[4]==1)
$h1++;
else if($ansA[4]==2)
$h2++;
else
$h3++;
}
}
?>

Your variables were all a little messed up (order wise) and you didn't put anything equal to the explode() expression at the top.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.