Jump to content

[SOLVED] View just certian rows in CSV file after user inputs ID from a form??


Recommended Posts

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>";

?>

 

 

 

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!

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>";

                }

 

 

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>

 

 

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?

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?

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?

 

 

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.

 

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>

maybe it doesn't like the inline $x++

 

                while($x<$numheaders){

                    echo "<TD> ".$fields[$x]." </TD>";

                    $x++;

                  }

 

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>

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>";

        }

 

 

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>";

 

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!!

 

 

 

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

 

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

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!  ;D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.