Below is my file read. I am trying to output it in a table form and also i am trying to read any length of tilde document.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<?php 
        $namefile = 'pilots.txt';  
        $OUTFILE = fopen($namefile, 'r') or die("Can't open $namefile for read");  
        $name = fgets($OUTFILE,4096);  

             if($OUTFILE){            
        while (!feof($OUTFILE))   
        {   
		echo"<tr>";
            for($row=fgetcsv($OUTFILE,1024,'~'))
		{ 
 		echo "<td>"; 
            	echo $row[$i];
 		echo "</td>"                
         	}
		echo"</tr>"
       }  
        fclose ($OUTFILE);  
     } 
 ?>  
     <h2> Soucre Code File</h2>      
    <p> 
<?php       
                    show_source(__FILE__);  
    ?>  
                </p>  
</body> 
</html>

Recommended Answers

All 17 Replies

and also i am trying to read any length of tilde document.

I don't know what you mean by that but since you are using fgetcsv() then each line of the file should have columns separated by '~'. If that is the case, try:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<?php 
	$namefile = 'pilots.txt';  
	$OUTFILE = fopen($namefile, 'r') or die("Can't open $namefile for read");
	if($OUTFILE!==FALSE)
	{
		echo PHP_EOL.'<table><tbody>';
		while( ($row=fgetcsv($OUTFILE,1024,'~'))!==FALSE )
		{
			echo PHP_EOL.'<tr><td>'.implode('</td><td>',$row).'</td></tr>';
		}
		echo PHP_EOL.'<tbody></table>';
		fclose ($OUTFILE);
     }
?>  
     <h2> Soucre Code File</h2>      
    <p> 
<?php       
                    show_source(__FILE__);  
    ?>  
                </p>  
</body> 
</html>

I have it doing what i want now one last question. What is i have another text file that choses what row of the text file and i do not know how many columns there are when setting this up

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>Untitled Document</title>  
</head> 
  
<body> 

<?php  

    $temp_name = $_FILES['UploadedFile']['tmp_name'];  
          // check to see if file uploaded, using the super-global array $_FILES  
       if (isset($_FILES['UploadedFile']['tmp_name']))  
       {  
         # $_FILES['fieldname']['name'] gives the uploaded file name  
   
         print ("<p>");  

         $fil_name = $_FILES['UploadedFile']['name'];  

        $namefile = $fil_name;   
        $OUTFILE = fopen($namefile, 'r')  
    or die("Can't open $namefile for read");    
          
    echo('<table>'); 
    while (($data = fgetcsv($OUTFILE,1000, "~")) !== FALSE) { 
    echo('<tr>'); 
     foreach ($data as $index=>$val) { 
                echo("\t<td>$val</td>\r\n"); 
        
   } 
    echo('</tr>'); 
}  
    echo('</table>');           
    fclose($OUTFILE); 
} 
?>   

   <form enctype="multipart/form-data"  
         action="<? $PHP_SELF ?>" method="post">  
   <input type="hidden" name="MAX_FILE_SIZE" value=" " />  
   <input name="UploadedFile" type="file" />  
   <input type="submit" value="UPLOAD" />  

         <h2> Soucre Code File</h2>       
        <p>  
        <?php        
            show_source(__FILE__);   
            ?>   
        </p> 
   
</body>  
</html>

What is i have another text file that choses what row of the text file and i do not know how many columns there are when setting this up

I don't know what you mean.

Here is what i need it to do now. Not sure the best way about doing this. I was thinking of taking foreach loop out and replacing with just a for loop but not sure. Also i am not sure how to read two files, if that iwas the best way to do it.

Need to inspect the first line of the file to determine what column names are available. Then will have to accept a list of desired column names as input. The file name and desired columns can be program arguments, or accepted via the user interface, or even read in from another text file.

For example your data file may look like this cars.txt file:

MAKE~MODEL~TYPE~PRICE
Toyota~Camry~Sedan~18000
Toyota~Tacoma~Truck~19000
Ford~Mustang~Sport~21000
Chevrolet~Corvette~Sport~48000
Ford~F150~Truck~25000
Toyota~Highlander~SUV~35000

And a parameters.txt file may look like this

cars
model~price

Assuming that parameters.txt has the following two lines of Input:
Sport
model~price

Try:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>Untitled Document</title>  
</head> 
  
<body> 

<?php  

//then use the above within the 
$temp_name = $_FILES['UploadedFile']['tmp_name'];  
// check to see if file uploaded, using the super-global array $_FILES  
if (isset($_FILES['UploadedFile']['tmp_name']))  
{


	# $_FILES['fieldname']['name'] gives the uploaded file name
	print ("<p>");  
	$fil_name = $_FILES['UploadedFile']['name'];  
	$namefile = $fil_name;   
	$OUTFILE = fopen($namefile, 'r') or die("Can't open $namefile for read");


	//read the entire file into an array
	$params=file('parameters.txt');

	//after this $type will have "SPORT"
	$type=strtoupper($params[0]);

	//after this $columns[0] will have "MODEL" && $columns[1] will have "PRICE"
	$columns=explode('~',strtoupper($params[1]));

	//read the first line of the csv
	$data = fgetcsv($OUTFILE,1000, "~")
	
	//now find which column corresponds to "TYPE"
	$typeIndex=array_search($type, $data);

	//now find out the column index for each of the columns specified in parameters.txt
	foreach($columns as $i=>$v)
	{
		if(FALSE==($columns[$i]=array_search($v, $data)) )
		{
			echo "Column $v does not seem to exist";
			unset($columns[$i]);
		}
	}

	echo('<table>'); 
	do{
		echo('<tr>');

		//make sure that the type value of the current $data row matches what
		//was provided in $type (in parameters.txt)
		if( $data[$typeIndex] == $type )
		{	
			//now iterate only over the set of requested columns
			foreach($columns as $dataIndex){
				echo("\t<td>{$data[$dataIndex]}</td>\r\n"); 
			}
		}
		echo('</tr>'); 
	}while (($data = fgetcsv($OUTFILE,1000, "~")) !== FALSE);  
	echo('</table>');           
	fclose($OUTFILE); 
}
?>   

   <form enctype="multipart/form-data"  
         action="<? $PHP_SELF ?>" method="post">  
   <input type="hidden" name="MAX_FILE_SIZE" value=" " />  
   <input name="UploadedFile" type="file" />  
   <input type="submit" value="UPLOAD" />  

         <h2> Soucre Code File</h2>       
        <p>  
        <?php        
            show_source(__FILE__);   
            ?>   
        </p> 
   
</body>  
</html>

What do i need to change if there is different parameters in the parameters.txt file?

if you change it FROM:
Sport
model~price

to:
SUV
make~price

it should STILL work!

in line 40 of the code i am getting the error Parse error: syntax error, unexpected T_VARIABLE. Any help would be great.

in line 40 of the code i am getting the error Parse error: syntax error, unexpected T_VARIABLE. Any help would be great.

At the end of line 37 I missed a semi-colon. Make sure you add it.

It will run but i am getting that the column does not exist

after line 28 put print_r($params); do you see the data you had in parameters.txt? If not, the most likely the path to your file is wrong.

This is what i get when i put the print_r in. Not sure why it wont read
Array ( [0] => Car [1] => model~price ) Column PRICE does not seem to exist

are you sure that the FIRST line in cars.txt has:
MAKE~MODEL~TYPE~PRICE

(all in upper case)?

Also, try changing: $columns=explode('~',strtoupper($params[1])); to: $columns=explode('~',strtoupper(trim($params[1])));

Now i am just getting this Array ( [0] => Car [1] => MODEL~PRICE )

And here is my Car File
MAKE~MODEL~TYPE~PRICE
Toyota~Camry~Sedan~18000
Toyota~Tacoma~Truck~19000
Ford~Mustang~Sport~21000
Chevrolet~Corvette~Sport~48000
Ford~F150~Truck~25000
Toyota~Highlander~SUV~35000

and parameters file
Car
MODEL~PRICE

If someone can look at this. I really need help today. Here is the code below i am getting this as an output "Array ( [0] => Car [1] => MODEL~PRICE )" What i am doing is getting a file and have another file called parameters that will specify what columns i want to get. Let me know what i have done wrong

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>Untitled Document</title>  
</head> 
  
<body> 

<?php  

//then use the above within the 
$temp_name = $_FILES['UploadedFile']['tmp_name'];  
// check to see if file uploaded, using the super-global array $_FILES  
if (isset($_FILES['UploadedFile']['tmp_name']))  
{
	# $_FILES['fieldname']['name'] gives the uploaded file name
	print ("<p>");  
	$fil_name = $_FILES['UploadedFile']['name'];  
	$namefile = $fil_name;   
	$OUTFILE = fopen($namefile, 'r') or die("Can't open $namefile for read");

	//read the entire file into an array
	$params=file('parameters.txt');
	print_r($params);

	//after this $type will have "SPORT"
	$type=strtoupper($params[0]);

	//after this $columns[0] will have "MODEL" && $columns[1] will have "PRICE"
	$columns=explode("~",strtoupper(TRIM($params[1])));

	//read the first line of the csv
	$data = fgetcsv($OUTFILE,1000, "~");
	
	//now find which column corresponds to "TYPE"
	$typeIndex=array_search($type, $data);

	//now find out the column index for each of the columns specified in parameters.txt
	foreach($columns as $i=>$v)
	{
		if(FALSE==($columns[$i]=array_search($v, $data)) )
		{
			echo ("Column $v does not seem to exist");
			unset($columns[$i]);
		}
	}

	echo('<table>'); 
	do{
		echo('<tr>');

		//make sure that the type value of the current $data row matches what
		//was provided in $type (in parameters.txt)
		if( $data[$typeIndex] == $type )
		{	
			//now iterate only over the set of requested columns
			foreach($columns as $dataIndex){
				echo("\t<td>{$data[$dataIndex]}</td>\r\n"); 
			}
		}
		echo('</tr>'); 
	}while (($data = fgetcsv($OUTFILE,1000, "~")) !== FALSE);  
	echo('</table>');           
	fclose($OUTFILE); 
}
?>   

   <form enctype="multipart/form-data"  
         action="<? $PHP_SELF ?>" method="post">  
   <input type="hidden" name="MAX_FILE_SIZE" value=" " />  
   <input name="UploadedFile" type="file" />  
   <input type="submit" value="UPLOAD" />  

         <h2> Soucre Code File</h2>       
        <p>  
        <?php        
            show_source(__FILE__);   
            ?>   
        </p> 
   
</body>  
</html>

and parameters file
Car
MODEL~PRICE

You cannot have Car on the first line and expect to have a result because if you look closer at Cars.txt NO record has "Car" listed under ANY of the columns. You need to be more explicit. So instead of "Car" use "Truck" since "Truck" DOES appear in the TYPE column.

Additionally, you also need to specify in WHICH column to look for "Truck". So use this as your parameters.txt file:

Type:truck
MODEL~PRICE

and use the following code instead:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>Untitled Document</title>  
</head> 
  
<body> 

<?php  


// check to see if file uploaded, using the super-global array $_FILES  
if (isset($_FILES['UploadedFile']['tmp_name']))  
{
	# $_FILES['fieldname']['name'] gives the uploaded file name
	print ("<p>");  
	$filename = dirname(__FILE__).'/'.$_FILES['UploadedFile']['name']; 
	move_uploaded_file($_FILES['UploadedFile']['tmp_name'], $filename);
	$OUTFILE = fopen($filename, 'r') or die("Can't open $filename for read");

	//read the entire file into an array
	$params=file('parameters.txt');

	//after this $type will have "SPORT"
	$search=explode(':',strtoupper($params[0]));
	$searchColumn=trim($search[0]);
	$searchValue=trim($search[1]);

	//after this $columns[0] will have "MODEL" && $columns[1] will have "PRICE"
	$columns=explode("~",strtoupper(trim($params[1])));

	//read the first line of the csv
	$data = fgetcsv($OUTFILE,1000, "~");

	//now find which column corresponds to "TYPE"
	$typeIndex=array_search($searchColumn, $data);

	//now find out the column index for each of the columns specified in parameters.txt
	foreach($columns as $i=>$v)
	{
		if(FALSE===($columns[$i]=array_search($v, $data)) )
		{
			echo ("Column $v does not seem to exist");
			unset($columns[$i]);
		}
	}

	echo('<table>'); 
	do{
		

		//make sure that the type value of the current $data row matches what
		//was provided in $type (in parameters.txt)
		if( strtoupper($data[$typeIndex]) == strtoupper($searchValue) )
		{	
			echo('<tr>');
			//now iterate only over the set of requested columns
			foreach($columns as $dataIndex){
				echo("\t<td>{$data[$dataIndex]}</td>\r\n"); 
			}
			echo('</tr>'); 
		}
		
	}while (($data = fgetcsv($OUTFILE,1000, "~")) !== FALSE);  
	echo('</table>');           
	fclose($OUTFILE); 
}
?>   

   <form enctype="multipart/form-data"   action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">  
   <!-- <input type="hidden" name="MAX_FILE_SIZE" value=" " />   -->
   <input name="UploadedFile" type="file" />  
   <input type="submit" value="UPLOAD" />  

         <h2> Soucre Code File</h2>       
        <p>  
        <?php        
            //show_source(__FILE__);   
            ?>   
        </p> 
   
</body>  
</html>

i was wondering if you could help me once more this time with using <,>,=,<=,>= this time instead of using the semicolon?

My Parameter file
Quote
Price<3500
MAKE~MODEL~PRICE

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.