fadedline Posted January 19, 2008 Share Posted January 19, 2008 I would like to know if there is a way to pull out information from a csv file so that it will show only a section of the file in the browser and not the whole file. I am new to php but I know how to pull the file into a page and view it. I just cant seem to get the page to show a certian row. Here is my situation...I have a html page with a simple form. The user will enter their ID number and zip code. The form then posts this information to the php page which in turn will go to this certian ID number. I dont need the user to add or delete information so I was told that MySQL is not nessary. Is there anyway to do this? I found this code out there which will display the entire file in table format with the first line being the headers/field...but I need something that will search headers/fields and diplay appropraite row! This has probably been done a thousand times but I cant find it. Im using comma delimited with 4 columns and 1000 rows if that helps. Any help would be greatly appreciated!! Here is the code Im using to show the file in a nice table...only problem is I need just certain rows: <? $sep = ","; $file = "test.csv"; //read the file into an array $lines = file($file); //count the array $numlines = count($lines); //explode the first (0) line which will be the header line $headers = explode($sep, $lines[0]); //count the number of headers $numheaders = count($headers); $i = 0; //start formatting output echo "<table border = 1 cellpadding = 2><tr>"; //loop through the headers outputting them into their own <TD> cells while($i<$numheaders){ $headers = str_replace("\"", "", $headers); echo "<td>".$headers[$i]."</td>"; $i++; } echo "</tr>"; $y = 1; //Output the data, looping through the number of lines of data and also looping through the number of cells in each line, as this is a dynamic number the header length has to be reread. while($y<$numlines){ $x=0; echo "<TR>"; while($x<$numheaders){ $fields = explode($sep, $lines[$y]); $fields = str_replace("\"", "", $fields); echo "<TD> ".$fields[$x]." </TD>"; $x++; } $y++; echo "</TR>"; } //close the table. echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/ Share on other sites More sharing options...
revraz Posted January 19, 2008 Share Posted January 19, 2008 Give a few examples of the csv file contents. Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-443158 Share on other sites More sharing options...
fadedline Posted January 19, 2008 Author Share Posted January 19, 2008 Thanks for quick reply! OK the comma delimited file will look something like this: ID Number, Owner, Zip Code, Amount 80245, bryan darrell and rhonda, 40000, $146.41 61373, Kathy and Jim Mcgranahan, 40001, $193.88 23413, Mary Kine Whit, 40006, $52.48 75833, barnett david, 40020, $- 000 11141, Laura and Ronald, 40066, $-000 19399, Secretary Of Vetera, 40202, $-000 I just need the form to send two ($_POST) One being the ID and the will be the Zipcode. The problem I have is gettin the php to use the post and compare the data entered with the Header/Fields... then display all four fields as a simple html table. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-443172 Share on other sites More sharing options...
fadedline Posted January 19, 2008 Author Share Posted January 19, 2008 Anyone have any Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-443569 Share on other sites More sharing options...
resago Posted January 19, 2008 Share Posted January 19, 2008 this: $fields = explode($sep, $lines[$y]); $fields = str_replace("\"", "", $fields); echo "<TD> ".$fields[$x]." </TD>"; to this: $fields = explode($sep, $lines[$y]); if ($fields[0]=="$id") { $fields = str_replace("\"", "", $fields); echo "<TD> ".$fields[$x]." </TD>"; } Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-443584 Share on other sites More sharing options...
fadedline Posted January 19, 2008 Author Share Posted January 19, 2008 THanks for your help. Ok now I get this: Parse error: parse error, unexpected $ in /home/dirc/public_html/untitled.php on line 69 But there is no code on line 69? My html code with the form now looks like this: <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css"> <!-- .style2 { font-family: Arial, Helvetica, sans-serif; color: #FFFFFF; } .style4 {font-size: 14px} --> </style> </head> <body> <form action="untitled.php" method="post"> <table width="346" border="0" align="center" cellpadding="4" cellspacing="4" bordercolor="#0092DF" bgcolor="#CCCCCC" summary="feedback form" bordercolorlight="#0077BB" bordercolordark="#336699"> <tr><th width="93" bgcolor="#006699"><div align="left"><span class="style27 style2 style4">Id Number:</span></div></th> <td width="151"><input name="idnumber" type="text" size="25" /></td> </tr> <tr><th bgcolor="#006699"><div align="left"><span class="style27 style2 style4">Zip Code:</span></div></th> <td><input name="zipcode" type="text" size="25" /></td> </tr> <tr> <th colspan="2"> </th> </tr> <tr> <td align="center" colspan="2"> <input type="submit" value="Login" /> <br /></td> </tr> </table> </form></td> </tr> <tr> <td height="57"> </td> </tr> </table></td> </tr> <tr> </body> </html> My php now looks like this: <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Test</title> </head> <body> <? $id = $_POST['idnumber']; $sep = ","; $file = "demoCSV.csv"; //read the file into an array $lines = file($file); //count the array $numlines = count($lines); //explode the first (0) line which will be the header line $headers = explode($sep, $lines[0]); //count the number of headers $numheaders = count($headers); $i = 0; //start formatting output echo "<table border = 1 cellpadding = 2><tr>"; //loop through the headers outputting them into their own <TD> cells while($i<$numheaders){ $headers = str_replace("\"", "", $headers); echo "<td>".$headers[$i]."</td>"; $i++; } echo "</tr>"; $y = 1; //Output the data, looping through the number of lines of data and also looping through the number of cells in each line, as this is a dynamic number the header length has to be reread. while($y<$numlines){ $x=0; echo "<TR>"; while($x<$numheaders){ $fields = explode($sep, $lines[$y]); if ($fields[0]=="$id") { $fields = str_replace("\"", "", $fields); echo "<TD> ".$fields[$x]." </TD>"; } $y++; echo "</TR>"; } //close the table. echo "</table>"; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-443688 Share on other sites More sharing options...
resago Posted January 19, 2008 Share Posted January 19, 2008 you're missing a close brace } Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-443763 Share on other sites More sharing options...
fadedline Posted January 19, 2008 Author Share Posted January 19, 2008 OK, well now it at least runs but it takes forever and it slows down browser bad. After awhile it spits out error line 58 which is this line- echo "</TR>"; It also says error access time more than 30 seconds but it does show the table with fields listed below these errors! Got any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-443784 Share on other sites More sharing options...
resago Posted January 19, 2008 Share Posted January 19, 2008 you changed it to this right?: while($x<$numheaders){ $fields = explode($sep, $lines[$y]); if ($fields[0]=="$id") { $fields = str_replace("\"", "", $fields); echo "<TD> ".$fields[$x]." </TD>"; } } how big is the csv file? Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-443797 Share on other sites More sharing options...
fadedline Posted January 19, 2008 Author Share Posted January 19, 2008 OK well this is strange. actually Im just dumb and put the close brace in the wrong line before... I put the brace here - //Output the data, looping through the number of lines of data and also looping through the number of cells in each line, as this is a dynamic number the header length has to be reread. while($y<$numlines){ $x=0; echo "<TR>"; while($x<$numheaders){ $fields = explode($sep, $lines[$y]); if ($fields[0]=="$id") { $fields = str_replace("\"", "", $fields); echo "<TD> ".$fields[$x]." </TD>"; } $y++; echo "</TR>"; } //close the table. echo "</table>"; } ?> But now when i changed it to here like you said - //Output the data, looping through the number of lines of data and also looping through the number of cells in each line, as this is a dynamic number the header length has to be reread. while($y<$numlines){ $x=0; echo "<TR>"; while($x<$numheaders){ $fields = explode($sep, $lines[$y]); if ($fields[0]=="$id") { $fields = str_replace("\"", "", $fields); echo "<TD> ".$fields[$x]." </TD>"; } } $y++; echo "</TR>"; } //close the table. echo "</table>"; ?> It just hangs up forever and deoesnt even spit out an error with table. The CSV file I have is 42.4KB and is 4 columns and 1010 rows of info. Could the csv file just be bad which is why its taking so long? Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-443820 Share on other sites More sharing options...
resago Posted January 19, 2008 Share Posted January 19, 2008 ooops $fields = explode($sep, $lines[$y]); if ($fields[0]=="$id") { $fields = str_replace("\"", "", $fields); while($x<$numheaders){ echo "<TD> ".$fields[$x++]." </TD>"; } } I think that will get it. Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-443857 Share on other sites More sharing options...
fadedline Posted January 19, 2008 Author Share Posted January 19, 2008 Now I get this error - Parse error: parse error, unexpected $ in /home/gregble/public_html/untitled.php on line 73 Here is my entire php code again - <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Test</title> </head> <body> <? $id = $_POST['idnumber']; $sep = ","; $file = "demoCSV.csv"; //read the file into an array $lines = file($file); //count the array $numlines = count($lines); //explode the first (0) line which will be the header line $headers = explode($sep, $lines[0]); //count the number of headers $numheaders = count($headers); $i = 0; //start formatting output echo "<table border = 1 cellpadding = 2><tr>"; //loop through the headers outputting them into their own <TD> cells while($i<$numheaders){ $headers = str_replace("\"", "", $headers); echo "<td>".$headers[$i]."</td>"; $i++; } echo "</tr>"; $y = 1; //Output the data, looping through the number of lines of data and also looping through the number of cells in each line, as this is a dynamic number the header length has to be reread. while($y<$numlines){ $x=0; echo "<TR>"; while($x<$numheaders){ $fields = explode($sep, $lines[$y]); if ($fields[0]=="$id") { $fields = str_replace("\"", "", $fields); while($x<$numheaders){ echo "<TD> ".$fields[$x++]." </TD>"; } } $y++; echo "</TR>"; } //close the table. echo "</table>"; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-443867 Share on other sites More sharing options...
resago Posted January 19, 2008 Share Posted January 19, 2008 maybe it doesn't like the inline $x++ while($x<$numheaders){ echo "<TD> ".$fields[$x]." </TD>"; $x++; } Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-443875 Share on other sites More sharing options...
fadedline Posted January 20, 2008 Author Share Posted January 20, 2008 Hmmmmmm....Similar result Parse error: parse error, unexpected $ in /home/gregbl/public_html/untitled.php on line 75 Man so close...so close. Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-443955 Share on other sites More sharing options...
resago Posted January 20, 2008 Share Posted January 20, 2008 post the php again, it sounds like an unterminated line. I think its talking about the $y++ Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-443968 Share on other sites More sharing options...
fadedline Posted January 20, 2008 Author Share Posted January 20, 2008 No Problem...again thanks for all your help, u rule! Here it is - <body> <? $id = $_POST['idnumber']; $sep = ","; $file = "demoCSV.csv"; //read the file into an array $lines = file($file); //count the array $numlines = count($lines); //explode the first (0) line which will be the header line $headers = explode($sep, $lines[0]); //count the number of headers $numheaders = count($headers); $i = 0; //start formatting output echo "<table border = 1 cellpadding = 2><tr>"; //loop through the headers outputting them into their own <TD> cells while($i<$numheaders){ $headers = str_replace("\"", "", $headers); echo "<td>".$headers[$i]."</td>"; $i++; } echo "</tr>"; $y = 1; //Output the data, looping through the number of lines of data and also looping through the number of cells in each line, as this is a dynamic number the header length has to be reread. while($y<$numlines){ $x=0; echo "<TR>"; while($x<$numheaders){ $fields = explode($sep, $lines[$y]); if ($fields[0]=="$id") { $fields = str_replace("\"", "", $fields); while($x<$numheaders){ echo "<TD> ".$fields[$x]." </TD>"; $x++; } } $y++; echo "</TR>"; } //close the table. echo "</table>"; ?> </body> Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-443983 Share on other sites More sharing options...
resago Posted January 20, 2008 Share Posted January 20, 2008 did the original code work? I've never used str_replace with an array. //Output the data, looping through the number of lines of data and also looping through the number of cells in each line, as this is a dynamic number the header length has to be reread. while($y<$numlines){ $x=0; echo "<TR>"; while($x<$numheaders){ $fields = explode($sep, $lines[$y]); if ($fields[0]=="$id") { #$fields = str_replace("\"", "", $fields); while($x<$numheaders){ echo "<TD> ".$fields[$x]." </TD>"; $x++; } } $y++; echo "</TR>"; } Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-444002 Share on other sites More sharing options...
resago Posted January 20, 2008 Share Posted January 20, 2008 nevermind, it should be fine. I don't know ??? Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-444013 Share on other sites More sharing options...
resago Posted January 20, 2008 Share Posted January 20, 2008 Found it, we had a leftover while. get a good editor like pspad, it can show unmatched braces and whatnot. //Output the data, looping through the number of lines of data and also looping through the number of cells in each line, as this is a dynamic number the header length has to be reread. while($y<$numlines){ $x=0; echo "<TR>"; $fields = explode($sep, $lines[$y]); if ($fields[0]=="$id") { $fields = str_replace("\"", "", $fields); while($x<$numheaders){ echo "<TD> ".$fields[$x]." </TD>"; $x++; } } $y++; echo "</TR>"; } //close the table. echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-444025 Share on other sites More sharing options...
fadedline Posted January 20, 2008 Author Share Posted January 20, 2008 OH MAN U ARE AWESOME!!! IT WORKS Wow I cant thank you enough. The only problem I see is that the table wants to extend down very far like 1600pixels. And I just realized that any commas in " " dont work. It treats the comma like another column. Believe me Im not complaining though!! Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-444051 Share on other sites More sharing options...
resago Posted January 20, 2008 Share Posted January 20, 2008 //Output the data, looping through the number of lines of data and also looping through the number of cells in each line, as this is a dynamic number the header length has to be reread. while($y<$numlines){ $x=0; $fields = explode($sep, $lines[$y]); if ($fields[0]=="$id") { echo "<TR>"; $fields = str_replace("\"", "", $fields); while($x<$numheaders){ echo "<TD> ".$fields[$x]." </TD>"; $x++; } echo "</TR>"; } $y++; } //close the table. echo "</table>"; this should fix the length issue. Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-444055 Share on other sites More sharing options...
resago Posted January 20, 2008 Share Posted January 20, 2008 post an example of the "," thing Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-444060 Share on other sites More sharing options...
fadedline Posted January 20, 2008 Author Share Posted January 20, 2008 Genius! It works perfect. Ok here is the line with the " " in my CSV 13248,"sims, jesse",42206, $100.00 So what its doing is using sims and jesse to fill the second and third field then its pusing the zip into the ammont field Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-444070 Share on other sites More sharing options...
resago Posted January 20, 2008 Share Posted January 20, 2008 find : echo "<TR>"; $fields = str_replace("\"", "", $fields); change to: echo "<TR>"; $lines[$y]=preg_replace('/(.*)"(.+),(.+)"(.*)/', '$1$3 $2$4', $lines[$y]); $fields = str_replace("\"", "", $fields); watch it break it! Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-444080 Share on other sites More sharing options...
fadedline Posted January 20, 2008 Author Share Posted January 20, 2008 Hahahaha...luckily it didnt break but same result as before. Quote Link to comment https://forums.phpfreaks.com/topic/86716-solved-view-just-certian-rows-in-csv-file-after-user-inputs-id-from-a-form/#findComment-444084 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.