Jump to content

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


Recommended Posts

Perfect again!!

Well I think we can call this one solved. Dude u really saved my butt...Ill post a link to the final site when its up in a few days. (you can see your work...hahaha)

 

If you ever need design work done hit me up, I owe u.

 

Greg

OK sorry one last question...how do i add the zip code input from the form to match with the id?

So right now I have -

 

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

$lines[$y]=preg_replace('/(.*)"(.+),(.+)"(.*)/', '$1$3 $2$4', $lines[$y]);

 

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

 

 

 

 

I know I need to add $zip to the top like this right?

 

$zip = $_POST['zipcode'];

 

But then where would i need to add that so both ID and Zip numbers match and go to the correct row?

Its probably good to dump the user to an error page if they dont match.

 

EDIT: Completely disregard this post - I hadn't even noticed there was still another page of replies to read when I posted. Need more caffeine.

 

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

 

 

the opening line

 

while($y<$numlines){

 

has no closing "}"

 

or is it the

while($x<$numheaders){

 

either way, there's a mis-match

Like this??-

     

 

    <?

$results=0;

$id = $_POST['idnumber'];

$zip =$_POST['zipcode'];

 

$sep = ",";

 

$file = "accounts.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 = 2 cellpadding = 6 cellspacing= 5 align=center bordercolor=#34587A><tr>";

 

//loop through the headers outputting them into their own <TD> cells

while($i<$numheaders){

        $headers = str_replace("\"", "", $headers);

        echo "<td bgcolor=#34587A class=style15 >".$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;

$lines[$y]=preg_replace('/(.*)"(.+),(.+)"(.*)/', '$1$3 $2$4', $lines[$y]);

 

                $fields = explode($sep, $lines[$y]);

                if ($fields[0]=="$id")&&($fields[2]=="$zipcode"){

  $results++;

                  echo "<TR>";

                  $fields = str_replace("\"", "", $fields);

 

                  while($x<$numheaders){

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

                    $x++;

                  }

                echo "</TR>";

                }   

        $y++;

              }

 

//close the table.

echo "</table>";

 

if ($results==0){

  print "No results";

}

 

?>

OK changed it to this with same results-

 

<?

$results=0;

$id = $_POST['idnumber'];

$zip = $_POST['zipcode'];

 

$sep = ",";

 

$file = "accounts.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 = 2 cellpadding = 6 cellspacing= 5 align=center bordercolor=#34587A><tr>";

 

//loop through the headers outputting them into their own <TD> cells

while($i<$numheaders){

        $headers = str_replace("\"", "", $headers);

        echo "<td bgcolor=#34587A class=style15 >".$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;

$lines[$y]=preg_replace('/(.*)"(.+),(.+)"(.*)/', '$1$3 $2$4', $lines[$y]);

 

                $fields = explode($sep, $lines[$y]);

                if (($fields[0]=="$id")&&($fields[2]=="$zip")){

  $results++;

                  echo "<TR>";

                  $fields = str_replace("\"", "", $fields);

 

                  while($x<$numheaders){

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

                    $x++;

                  }

                echo "</TR>";

                }   

        $y++;

              }

 

//close the table.

echo "</table>";

 

if ($results==0){

  print "No results";

}

 

?>

 

Ok this is what it spits out when i do that -

 

() ()

ID Number Owner Name Street City State Zip Code Amount 

No results 

 

Of course the Fields are in a table.

 

Here is my html for the form-

 

  <form action="account.php" method="post">

<table width="302" height="112" border="0" align="center" cellpadding="4" cellspacing="4" bordercolor="#0092DF" bordercolorlight="#0077BB" bordercolordark="#336699" bgcolor="#CCCCCC" summary="feedback form">

<tr>

  <th width="96" bgcolor="#006699"><span class="style14">ID Number</span></th>

  <td width="168"><input name="idnumber" type="text" size="25" /></td>

</tr>

<tr><th bgcolor="#006699"><div align="left">

  <p align="center" class="style27 style2 style4 style15">Zip Code</p>

  </div></th>

<td><input name="zipcode" type="text" size="25" /></td>

</tr>

<tr>

  <th colspan="2"><input type="submit" value="Login" /></th>

</tr>

<tr></tr>

</table>

</form>

 

 

Here is the PHP -

<?

$results=0;

$id = $_POST['idnumber'];

print "($fields[2]) ($zip)<br>";

$zip = $_POST['zipcode'];

 

$sep = ",";

 

$file = "accounts.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 = 2 cellpadding = 6 cellspacing= 5 align=center bordercolor=#34587A><tr>";

 

//loop through the headers outputting them into their own <TD> cells

while($i<$numheaders){

        $headers = str_replace("\"", "", $headers);

        echo "<td bgcolor=#34587A class=style15 >".$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;

$lines[$y]=preg_replace('/(.*)"(.+),(.+)"(.*)/', '$1$3 $2$4', $lines[$y]);

 

                $fields = explode($sep, $lines[$y]);

              if (($fields[0]=="$id")&&(trim($fields[2])=="$zip")){

  $results++;

                  echo "<TR>";

                  $fields = str_replace("\"", "", $fields);

 

                  while($x<$numheaders){

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

                    $x++;

                  }

                echo "</TR>";

                }   

        $y++;

              }

 

//close the table.

echo "</table>";

 

if ($results==0){

  print "No results";

}

 

?>

 

no. put it here so you can see what we are trying to compare.

 

$fields = explode($sep, $lines[$y]);
print "($fields[2]) ($zip)<br>";
if (($fields[0]=="$id")&&(trim($fields[2])=="$zip")){
  $results++;

 

and post your code inside of code tags.

 

Man thanks for showing me how to print the results....im so stupid.

I realized i added one more field to the csv file and that was throwing it off!! Im sorry man I smacked myself for that one.

 

But it does work! It matches id and zip.

had to change it to

if (($fields[0]=="$id")&&(trim($fields[3])=="$zip")){

 

 

 

 

Thanks again

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.