Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP reading a file

    I have a code that needs to read in a file and has a parameter file. I need to know how to put in <=,<,=,>=,> operators that will be in my parameter file.

    Any help would be great

    Code:
    <!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>
    the file i will be reading in

    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
    My Parameter file

    Price<3500
    MAKE~MODEL~PRICE


  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    So, let me get this into my head.

    You upload a file with a list of vehicles and price ... like this:
    Toyota~Camry~Sedan~18000
    Toyota~Tacoma~Truck~19000

    And you have another file that tells what to search for ... like this:
    Price<3500
    Toyota~Tacoma~Truck

    So the result would be all "Toyota~Tacoma~Truck" that are less than $3500?

    ==============

    Why is there a "parameter.txt" file, and not a drop-down select (like a search)?

    And can you use "wildcards", like:
    Price<3500
    Toyota~*~Truck

    So it would display ALL Toyota Trucks less than $3500?

    ==========

    I'm not sure of the purpose of your script.
    Wondering if you're going about it in a different method.




    .

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Location
    Grahamstown, South Africa
    Posts
    237
    Thanks
    6
    Thanked 17 Times in 17 Posts
    Sometimes people over-complicate things.

    Re-Evaluate your whole process, im sure you can come up with a simpler solution.

  • #4
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    my chained select box generator would be perfect for you. check it out

    http://www.blueicestudios.com/chaine...hp-mysql-ajax/

    little video clip showing how it works.

  • #5
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts
    I like the generator but why don't you use a database instead of the text file
    I can help you with that, I would like to see the finished product being
    populated by the database

    PS: Here are the most used escape codes on PHP:

    Code:
    \t  tab         echo ("separated\tby\ttabs");
    \r  return      echo ("first line\r\nsecond line");
    \n  new line    echo ("first line\r\nsecond line");
    \\  backslash   echo ("Backslash: \\");
    \$  dollar sign echo ("Total Amount: \$210.00");
    \"  quotes      echo ("\"yes\" - he replied");
    \[  left bracket
    \]  right bracket
    \{  left brace
    \}  right brace echo ("\[ \] and \{ \} are used");
    Last edited by DataTalk; 12-09-2010 at 03:12 PM.

  • #6
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    Quote Originally Posted by DataTalk View Post
    I like the generator but why don't you use a database instead of the text file
    I can help you with that, I would like to see the finished product being
    populated by the database
    it does use a database, its php, mysql and jquery.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •