Jump to content

Using explode to fill an array


Dave Kelly

Recommended Posts

I have a form that saves the input data as such:

bass inshore offshore Array, regional, mid, 70, 40, 2 x yr, all, fellowship knowledge information learnig, one, one, three, four, one, one, two, three, three, four, TPWD round table discussions conservation biology, the guide talking about fishing the venturi, Jack Ellis and TAG fishing in the East Texas creeks., none at present

bass inshore offshore Array, regional, mid, 70, 40, 2 x yr, all, fellowship knowledge information learnig, one, one, three, four, one, one, two, three, three, four, TPWD round table discussions conservation biology, the guide talking about fishing the venturi, Jack Ellis and TAG fishing in the East Texas creeks., none at present

bass inshore offshore Array, regional, mid, 70, 40, 2 x yr, all, fellowship knowledge information learnig, one, one, three, four, one, one, two, three, three, four, TPWD round table discussions conservation biology, the guide talking about fishing the venturi, Jack Ellis and TAG fishing in the East Texas creeks., none at present

This php program reads it back to the screen as it is writen and as you see it above.

<?php 
$YourFile = "meeting.survey"; 
$handle = fopen($YourFile, 'r'); 
while (!feof($handle)) 
{ 
$Data = fgets($handle); 
	print $Data; 
        print "<hr>";
} 
fclose($handle); 

?> 

The code below fails. I feel it is because I am using 'explode' incorrectly.

<?php 
$YourFile = "meeting.survey"; 
$handle = fopen($YourFile, 'r'); 

while (!feof($handle)) 
{ 
 $Data = fgets($handle); 
	 $answers = explode(',', $Data);
			print $answers[0];
			print $answers[1];
			print $answers[2];
			print $answers[3];
			print $answers[4];
			print $answers[5];
			print $answers[6];
			print $answers[7];
			print $answers[8];
			print $answers[9];
			print $answers[10];
			print $answers[11];
			print $answers[12];
			print $answers[13];
			print $answers[14];
			print $answers[15];
			print $answers[16];
			print $answers[17];
			print $answers[18];
			print $answers[19];
			print $answers[20];
			print $answers[21];

		  print "<hr>";
} 
fclose($handle); 

?> 

My PHP coding skills are minimal at best, any help, direction, comments you have will be greatly appreciated.

 

If you wish to view the survey html go here

If you wish to view the php code used to produce the data,

 wget http://texasflyfishers.org/quiz/php_tutorial/survey.php 

If you wish to view a readback of the data go here

 

Everything I've been able to find over the web, tells me this is what I need to do if I want to add some text that tells what each responce means.

Link to comment
https://forums.phpfreaks.com/topic/249088-using-explode-to-fill-an-array/
Share on other sites

u can use php's in-built csv function to read an write files for your example I will suggest u to use this

 


<?php
$row = 1;
if (($handle = fopen("meeting.survey", "r")) !== FALSE) {
    while (($data = fgetcsv($handle)) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.