473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

read | delimited fields from record in variables

I am loading from a .data file and each line has all of the variables divided by "|". The code I use is in Perl and I need to have access to these variables just the same as with my perl code but with PHP. Please let me know if you have any idea what I can do. Thanks in advance. Here is the code.

$data_file="ads.data";

open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;
close(DAT);



foreach $ads (@raw_data)
{
chop($ads);
($ad_number,$user_name,$user_type,$date,$var_1,$va r_2,$var_3,$var_4,$var_5,$var_6,$var_7,$name,$var_ 8,$city,$province,$postal_code,$country,$phone_num ber,$var_9,$email,$var_10,$category,$subcategory,$ caption_header,$caption,$ad_text,$price,$var_11,$v ar_12,$var_13,$var_14,$var_15,$var_16,$private,$va r_17)=split(/\|/,$ads);



if ($city eq "Toronto") {
print "<table width=100%><tr><td>
$city

</td></tr></table>";
}
}
Mar 25 '07 #1
9 1461
ronverdonk
4,258 Expert 4TB
Welcome to TSDN. Next time enclose your code within php or code tags, as stated in the Posting Guidelines at the top of this forum.

Use the explode function of PHP. See http://nl3.php.net/manual/en/function.explode.php

Sample usage for your script it would be something like:

[php]
// read your record in $data
list($ad_number,$user_name,$user_type,$date,$var_1 ,$var_2,
$var_3,$var_4,$var_5,$var_6,$var_7,$name,$var_8,$c ity,
$province,$postal_code,$country,$phone_num ber,$var_9,
$email,$var_10,$category,$subcategory,$caption_hea der,
$caption,$ad_text,$price,$var_11,$var_12,$var_13,$ var_14,
$var_15,$var_16,$private,$va r_17)= explode("|", $data);[/php]

Ronald :cool:
Mar 25 '07 #2
Thank you for the reply!

So my code should look like this?

<?php

$data_file="data.txt";



list($ad_number,$user_name,$user_type,$date,$var_1 ,$var_2,
$var_3,$var_4,$var_5,$var_6,$var_7,$name,$var_8,$c ity,
$province,$postal_code,$country,$phone_num ber,$var_9,
$email,$var_10,$category,$subcategory,$caption_hea der,
$caption,$ad_text,$price,$var_11,$var_12,$var_13,$ var_14,
$var_15,$var_16,$private,$va r_17)= explode("|", $data);








if ($city == "Toronto") {
print "<table width=100%><tr><td>$city</td></tr></table>";
}


?>


This makes the page not work. Is there something I'm doing wrong? Thanks alot for the help!
Mar 25 '07 #3
ronverdonk
4,258 Expert 4TB
Where is your read (of the file) statement? As I pointed out in the sample:

[php]// read your record in $data
[/php]

Ronald :cool:
Mar 25 '07 #4
Would it be something like this?
[php]
<?php
$data_file="data.txt";
$myFile = "data.txt";
$fh = fopen($myFile, 'r');
$data = fread($fh, 5);

list($ad_number,$user_name,$user_type,$date,$var_1 ,$var_2,
$var_3,$var_4,$var_5,$var_6,$var_7,$name,$var_8,$c ity,
$province,$postal_code,$country,$phone_num ber,$var_9,
$email,$var_10,$category,$subcategory,$caption_hea der,
$caption,$ad_text,$price,$var_11,$var_12,$var_13,$ var_14,
$var_15,$var_16,$private,$va r_17)= explode("|", $data);

if ($city == "Toronto") {
print "<table width=100%><tr><td>$city</td></tr></table>";
}
fclose($fh);
?> [/php]
Not to sure what you mean. New to this. Thanks for your help!
Mar 25 '07 #5
ronverdonk
4,258 Expert 4TB
You were requested in a previous post to enclose your code within php or code tags! Read the Posting Guidelines. before you continue.

moderator
Mar 25 '07 #6
You were requested in a previous post to enclose your code within php or code tags! Read the Posting Guidelines. before you continue.

moderator

I'm sorry. I am so new! I thought you meant within the <?php ?> tags. Then after my last post I realized what you meant but I could not find an edit feature! I'm sorry.
Mar 25 '07 #7
ronverdonk
4,258 Expert 4TB
php command fgets will read one text line at a time. So the process is:
Expand|Select|Wrap|Line Numbers
  1. open file
  2. while (not EOF)
  3.    read text line
  4.    assign content to variables
  5.    .. what else to process ...
  6. end while
  7. close file
  8.  
Ronald :cool:
Mar 25 '07 #8
So it should look something like this?? This deffiantely isn't it though.

[PHP]
<?php

$data_file="data.txt";



open file
while (not EOF)





list($ad_number,$user_name,$user_type,$date,$var_1 ,$var_2,
$var_3,$var_4,$var_5,$var_6,$var_7,$name,$var_8,$c ity,
$province,$postal_code,$country,$phone_num ber,$var_9,
$email,$var_10,$category,$subcategory,$caption_hea der,
$caption,$ad_text,$price,$var_11,$var_12,$var_13,$ var_14,
$var_15,$var_16,$private,$va r_17)= explode("|", $data);








if ($city == "Toronto") {
print "<table width=100%><tr><td>$city</td></tr></table>";
}


end while
close file




?>
[/PHP]
Mar 26 '07 #9
I am very confused. Is there something I am supposed to be adding to that?
Mar 26 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Luke Airig | last post by:
I am using the Saxon xml parser and I need some help transforming the following 2 xml files: gps.xml: -------- <?xml version="1.0"?> <root> <record> <longitude>-105.111111</longitude>...
3
by: Avi | last post by:
I need to create a text file that has the data from the 10 tables in the database. The number of fields in the tables exceeds 255 and so I cannot make a new table with all the fields and then...
7
by: GregoryD | last post by:
I have a flat file that I'm trying to stick into a MySQL database. One record per line, multiple fields per record, and many of them are null fields which are just double quotes without a space...
23
by: ShaneO | last post by:
Hello, I wish to extract embedded string data from a file using a Binary Read method. The following code sample is used in VB.NET and similar code is used in VB6 - (Assume variable...
3
by: Ray | last post by:
Hello World, I made a Windowsform that reads data from a CSV file. It works fine, but when I have read the data of a record I have to re-Debug the form to read another record. So when I put a...
0
by: drharris | last post by:
First, please forgive my newness to XML. I've used it to serialize/ deserialize objects, exporting and importing datasets, and other such things that pretty much automate reading in the file. I've...
1
by: solinear | last post by:
OK, I am going to preface this by saying I'm very new to VB coding (or any other kind, for that matter), but know a lot about Windows, AD and Security. We are making adjustments to our environment...
3
by: Damon Getsman | last post by:
Okay so I'm writing a script in python right now as a dirty fix for a problem we're having at work.. Unfortunately this is the first really non-trivial script that I've had to work with in python...
8
by: =?Utf-8?B?TTFpUw==?= | last post by:
I’m trying to parse out Amazon S3 server logs which are space delimited. However date fields are in the following form: When I try to use the following code to split the record on the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.