Probably asked before!

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
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

Probably asked before!

Post by wesnoel »

OK I know this question has already been asked, but I really do not understand this stuff yet. If someone can point me in the right direction with this stuff maybe a code snipet or two. I would dance a jig and jump for joy. As you can tell I am a little frustrated with myself.

Ok the question:

I have a csv file and the contents of which I need to view in an html table on the fly. I do not know where to start with this. I am using php 4.2 on apache linix machine at least that is what the networking dept. tells me.

I'll I need is a sample to get me started. it doesn't even need to look pretty just a function to read two lines of the the csv file and output them into a table like so:

line1 data
line2 data

or

Name Age
Bob Smith 25


There really isn't anything out there that is clear for a newbe to understand. Sorry if this is a bother to the group. I am really trying to learn this stuff.

TIA

Wes/
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

I had a script that did pretty much what you're tallking about. I can't find it right now but heres some tips:

--Use fopen() to open the file
--Load the content of the file into a variable
--Use explode() to seperate the data from the commas
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Instead of fopen() you can use, file(), e.g. if your file looked like:

Code: Select all

name1, age1
name2, age2
name3, age3

Code: Select all

<?php

$filecontents = file('filename.csv');

?>

<table cellpadding="3" cellspacing="0" width="100%" border="0">
<tr>
    <th width="70%">Name</th>
    <th width="30%">Age</th>
</tr>
<?php

foreach ($filecontents as $line) {
    $linecontents = explode(', ', $line);
    $name = $linecontents[0];
    $age    = $linecontents[1];

?>
<tr>
    <td><?php echo $name; ?></td>
    <td><?php echo $age; ?></td>
</tr>

<?php

} // end foreach

?>

</table>
(code not tested but it should be ok)

Mac
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

Post by wesnoel »

Thanks guys I feel a jig com'n on! :)

I'll work all of this out and let you know what happend.

Thanks again,

Wes/
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

Post by wesnoel »

I danced a jig last night! WoooHoooo!

This is what I came up with:

Code: Select all

<?php

<head>
<STYLE>
 
  body { 
                margin: 12pt; 
		  color: #003366; 
		  font-family: arial;
		   font-size: 10px;
	     }     
   
            
            
  }
 </STYLE>
 
</head>
<body>
<?php 

$note = $_GET['note'];

$mytext =file($note); 
//echo $note;

foreach ($mytext as $myarray) 
{ 

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

     $txt1       = $split[1]; 
     $txt2       = $split[2]; 
     $txt3       = $split[3]; 
     $txt4       = $split[4];
     $txt5       = $split[5];
     $txt6       = $split[6];
     $txt7       = $split[7];
     $txt8       = $split[8];
     $txt9       = $split[9];
     $txt10       = $split[10];
     $txt11       = $split[11];
     $txt12       = $split[12];
     $txt13       = $split[13];
     $txt14      = $split[14];
     $txt15       = $split[15];
     $txt16       = $split[16];
     $txt17      = $split[17];
     $txt18       = $split[18];
     $txt19       = $split[19];
     $txt20       = $split[20];
     $txt21       = $split[21];
     $txt22       = $split[22];
     $txt23       = $split[23];
     $txt24       = $split[24];
     $txt25       = $split[25];
     $txt26       = $split[26];
     $txt27       = $split[27];
     $txt28       = $split[28];



} 

?>
<table border="1" color= "#003366" cellpadding="6" cellspacing="0" bgcolor="#000000">
		<tr>
			<td bgcolor="white"><b><font color="#003366">Service Provider:</font></b> <?php echo $txt3; ?></td>
		</tr>
		<tr>
			<td bgcolor="white">
			<table border="0" cellspacing="5" cellpadding="0" bgcolor="white">
				<tr>
					<td bgcolor="white" width="187"><b><font color="#003366">MID#</font></b></td>
					<td align="left" bgcolor="white" width="156"><b><font color="#003366">Date</font></b></td>
					<td bgcolor="white" width="83"><font color="#003366"><b>Initials</b></font></td>
					<td bgcolor="white" width="12"></td>
					<td bgcolor="white"><b><font color="#003366">Supplies</font></b></td>
						<td align="center" bgcolor="white" width="57"><b><font color="#003366">QTY</font></b></td>
					</tr>
				<tr>
					<td bgcolor="white" width="187"><?php echo $txt4; ?></td>
					<td align="left" bgcolor="white" width="156"><?php echo $txt1; ?></td>
					<td bgcolor="white" width="83"><?php echo $txt2; ?></td>
					<td bgcolor="white" width="12">1.</td>
					<td bgcolor="white"><?php echo $txt14; ?></td>
						<td align="center" bgcolor="white" width="57"><?php echo $txt15; ?></td>
					</tr>
				<tr>
						<td colspan="3" bgcolor="white" width="436"><font color="#003366"><b>Business Name</b></font></td>
						<td bgcolor="white" width="12">2</td>
						<td bgcolor="white"><?php echo $txt16; ?></td>
						<td align="center" bgcolor="white" width="57"><?php echo $txt17; ?></td>
					</tr>
				<tr>
					<td colspan="3" bgcolor="white" width="436"><?php echo $txt5; ?></td>
					<td bgcolor="white" width="12">3.</td>
					<td bgcolor="white"><?php echo $txt18; ?></td>
						<td align="center" bgcolor="white" width="57"><?php echo $txt19; ?></td>
					</tr>
				<tr>
						<td colspan="3" bgcolor="white" width="436"><font color="#003366"><b>Address</b></font></td>
						<td bgcolor="white" width="12">4.</td>
						<td bgcolor="white"><?php echo $txt20; ?></td>
						<td align="center" bgcolor="white" width="57"><?php echo $txt21; ?></td>
					</tr>
				<tr>
					<td colspan="3" bgcolor="white" width="436"><?php echo $txt6; ?></td>
					<td bgcolor="white" width="12">5.</td>
					<td bgcolor="white"><?php echo $txt22; ?></td>
						<td align="center" bgcolor="white" width="57"><?php echo $txt23; ?></td>
					</tr>
				<tr>
					<td bgcolor="white" width="187"><b><font color="#003366">City</font></b></td>
					<td bgcolor="white" width="156"><b><font color="#003366">State</font></b></td>
					<td bgcolor="white" width="83"><b><font color="#003366">Zip</font></b></td>
					<td bgcolor="white" width="12">6.</td>
					<td bgcolor="white"><?php echo $txt24; ?></td>
						<td align="center" bgcolor="white" width="57"><?php echo $txt25; ?></td>
					</tr>
				<tr>
					<td bgcolor="white" width="187"><?php echo $txt7; ?></td>
					<td bgcolor="white" width="156"><?php echo $txt8; ?></td>
					<td bgcolor="white" width="83"><?php echo $txt9; ?></td>
					<td bgcolor="white" width="12">7.</td>
					<td bgcolor="white"><?php echo $txt26; ?></td>
						<td align="center" bgcolor="white" width="57"><?php echo $txt27; ?></td>
					</tr>
				<tr>
					<td bgcolor="white" width="187"><b><font color="#003366">Phone</font></b></td>
						<td colspan="5" bgcolor="white"><b><font color="#003366">Contact Name</font></b></td>
					</tr>
				<tr>
					<td bgcolor="white" width="187"><?php echo $txt10; ?></td>
						<td colspan="5" bgcolor="white"><?php echo $txt11; ?></td>
					</tr>
				<tr>
						<td colspan="6" bgcolor="white">&nbsp;</td>
					</tr>
				<tr>
						<td colspan="6" bgcolor="white"><b><font color="#003366">Notes</font></b></td>
					</tr>
				<tr>
						<td colspan="6" bgcolor="white"><?php echo $txt12; ?></td>
					</tr>
				<tr>
						<td colspan="6" bgcolor="white"><b><font color="#003366">Shipping Notes</font></b></td>
					</tr>
				<tr>
						<td colspan="6" bgcolor="white"><?php echo $txt13; ?></td>
					</tr>
				<tr>
						<td colspan="6" align="right" bgcolor="white"></td>
					</tr>
				<tr>
						<td colspan="6" align="left" bgcolor="white"></td>
					</tr>
			</table>
		</td>
		</tr>
</table>
</body>

?>

This accepts a veriable named note from another page it then displays it all in a nice little table. In this case the veriable is a csv file that contains two lines of information. $note="filename.csv" this worked out better than I thought and now everybody loves me here at work.


Thanks twigletmac! you helped me out a lot.
Cudos to you my friend.

And thanks to everyone else who viewed this or views it in the future. I hope this helps you all. Just me giving back.

Wes/
Post Reply