Is it possible?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Is it possible?

Post by gjb79 »

:?: I've received an interesting assignment. My Client wants to have a php page created that will proccess comma or tab seperated values from a text file or rich text file format. It would break the values down and reorganize them on a website.

I'm trying to get a firm grasp on how this should be set up if possible at all. I have two thoughts on the subject, if anything thinks I have it right or has a better idea, please tell me - as clearly as you can - THANKS!

:arrow: Idea 1 - The php form would be conditioned to dynamically create a variable for each item between the commas, it would then dynamically generate an html document with the items filled in. The proccess would involve an upload form of some sort that would allow him to use a text or rtf file from his desktop. I'm guessing it would read the data and dump the form after the pages where created.

:arrow: Idea 2 - The upload form would store the file on the server. A php page is setup that would automatically scan the document whenever a person views that webpage. Since the page is already uploaded and saved to the server, One php page would be needed to read the contents generate and generate a page that could set amounts of information on the page, say 5 entries at a time.

I'm thinking the second idea is the better one, it is easily updatable as only the text file needs to be replaced to update the content. I'm just not sure how I could get the php code to read the content of the document and pull out comma separated values from it.

Thanks again, :)
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

spliting a comma/tab separated file is easy, if there are no comma's in the text you can simply use explode(',',$text) for comma or explode("\t",$text) for tab... If the text can be complex and/or its CSV files where data encapsulation can be in severeal ways it would be easiest to use a regex (preg_split)..

If it is RTF and not pure text its no problem splitting, but the harder task will be to convert the data to plain text from all the Bill Gatish \codes and {groups}
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Excellent... However...

Post by gjb79 »

Thank you!

I never knew about the explode function. It is very easy to see how I could manipulate it to extract variables from a string separated by commas. However I'm still at a loss as to how I can seperate one string of values from the next when each comma separated string is divided by a return.

item1,item2,item3,item4
item1,item2,item3,item4
item1,item2,item3,item4

As you can see I have 3 strings with a return between them and each string has 4 items in it. (This isn't to say my actual data has only 3 strings, this is only an example.)

Once I resolve this problem I still need to address the issue of pulling these strings from a .txt file.

Thanks again for your help! :D
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

Something like

Code: Select all

<?php
  $mytext = file('/path/to/filename.txt');
  foreach ($mytext as $line)
  {
     $values = explode(',',$line);
     foreach ($values as $value)
     {
        # Do something useful with $value
     }
  }
?>
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Amazing

Post by gjb79 »

That is amazing. I had worked up in my head this huge code that would perform all those functions one step at a time, and basically had it worked out thanks to your help earlier and some stuff I found because of it.

Then I check the post again and find you've condenced everything in my head into a simple short code.

Amazing 8O

I'll post back a little later with the results of what I found and how it worked.

Thanks a lot!!!
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Somthing still isn't right.

Post by gjb79 »

Well I thought I had it right but what I'm finding is this. I have 3 rows of text with 5 items within each row. All this is within a text file.

This is one row of my test text file (the other two rows are the same with the number value increased by one)
name1,email1,address1,phone1,fax1

When I use an echo statement (to print the arrays [0] - [4]) with Stokers code above I receive this:
name1email1address1phone1fax1name1email1address1phone1fax1name1email1address1....
then it goes on to do the same with name2email2address2 etc...

Any idea how I can get it to display 1 of each of item1 then move on to item2 and 3?

BTW here is my code
$mytext = file('test.txt');
foreach ($mytext as $line)
{
$values = explode(',',$line);
foreach ($values as $value)
{

echo "\$values[0] = $values[0] <BR> \$values[1] = $values[1] <BR>";
echo "\$values[2] = $values[2] <BR> \$values[3] = $values[3] <BR>";
echo "\$values[4] = $values[4] <BR>";


}
}

when my echo statement points to $value it gives me the same thing but it prints out one letter at a time rather than an entire row of text

Thanks.
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

you missunderstood my code, the innerloop cycled thru each item of the values array... if you are going to use it that way you can simply just delete the inner loop (foreach)..

And please use the forum php or code tags when posting source
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

I feel dumb

Post by gjb79 »

Please disregard the last post. I've been sitting too close to my screen again.

I do not know why my mind produced such an ugly echo statement.

Here is where I am at.

$mytext = file('test.txt');
foreach ($mytext as $line)
{
$values = explode(',',$line);
foreach ($values as $value)
{

echo "$value <BR>";


}
}

I still need to drop each item into its own variable though.

I really appreciate your help. Sorry about that last post, I guess I just had to write it all out to get it through my system. :oops:
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Missed your post

Post by gjb79 »

Sorry I'll use the tags in the future.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

well then, it sounds like a modification of what he's done is what you need. sounds to me like you need to prep it to a multi-dimensional array, and later go through and toss that on a page, so starting with what stoker gave you....

Code: Select all

<?php 
  $mytext = file('/path/to/filename.txt'); 
  $multidimenstional_array=array();

  foreach ($mytext as $line) 
  { 
     $values = explode(',',$line); 
     $multidimensional_array[]=$values;
     echo "<p>retrived the following to array for table use"; #debug line for
     foreach ($values as $value){ # debug
        echo "<br>$value";  # debug
     } 
  } 
?>
at the end you should have a multidimensional array that has all the values in order. obviously you can take the debuggging lines out onceyou have it working right. i expect that the end would look more like...

Code: Select all

<?php 
  $mytext = file('/path/to/filename.txt'); 
  $multidimenstional_array=array();

  foreach ($mytext as $line) 
  { 
     $values = explode(',',$line); 
     $multidimensional_array[]=$values;
     echo "<p>retrived the following to array for table use"; #debug line for
     foreach ($values as $value){ # debug
        echo "<br>$value";  # debug
     } 
  } 
?><p>&nbsp;<p>&nbsp;<p>&nbsp;
user information:
<table>
<tr><td>name</td><td>e-mail</td><td>address</td><td>phone</td><td>fax</td></tr>
<?php
foreach ($multidimensional_array as $array){
  echo '<tr>';
  foreach ($array as $info){
    echo "<td>$info</td>";
  }
  echo '</tr>';
}
?>
</table>
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

Wonderful

Post by gjb79 »

Just writing in to thank everyone. :)

I got the code working as I needed, Much thanks and kudos to all that helped me.

If your interested, this is the solution to my troubles:

Code: Select all

<?php 
$mytext = file('test.txt');

foreach ($mytext as $myarray)
{ 

     $split = explode(",", $myarray);

     $txt1       = $split[0];
     $txt2       = $split[1];
     $txt3       = $split[2];
     $txt3       = $split[3];


echo "<P>" . "$txt1" . "," . "$txt2" . "," . "$txt3" . "," . "$txt" . "</P>";


}

?>
Within the echo statement I should be able to format those items to be more pleasing to the eye. 8)

If your wondering about my conversion from $split[#] to $txt# it is so that I can create tags which are easy to understand for the client when he is deciding on a layout. $txt1 will probibly be renamed $name, $txt2 will be $address, etc...

Thanks so much! :D
jmarcv
Forum Contributor
Posts: 131
Joined: Tue Jul 29, 2003 7:17 pm
Location: Colorado

Post by jmarcv »

or:

Code: Select all

list($txt1,$tx2,$txt3,$txt4) = explode(",", $myarray); 

echo "<P>" . "$txt1" . "," . "$txt2" . "," . "$txt3" . "," . "$txt" . "</P>";
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

as jmarcv pointed out there's an easier way to get your array, however, since you're using " instead of ' there's two things you should know.

1:"" will evaluate all variables, therefore you just need echo "<P>$txt1;$txt2;$txt3;$txt4</P>";
it'll print out exactly as you have it.

2: if you have " in the $txt1 or such, it may (i'm not familliar enought ot be taken as knowing for sure, i just know i avoid it) exit the echo early becasue of the " not being \", so you might want: echo '<p>'.$txt1.';'.$txt2.';'.$txt3.';'.$txt4.'</p>';

hope that helps.
and thanx for letting us know it worked out. it's nice to know we've been of help
jmarcv
Forum Contributor
Posts: 131
Joined: Tue Jul 29, 2003 7:17 pm
Location: Colorado

Post by jmarcv »

m3rajk
didn't catch that. Your #1 will expand quotes just fine if they are defined in a variable. Its only when you don't escape a query that there seems to be problems.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

thanx jmarcv. looks like i'm also learning something with this thread :)
Post Reply