473,382 Members | 1,409 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,382 software developers and data experts.

Read and Write file contents

pradeepjain
563 512MB
Hii this is pradeep here . i am a newbie to PHP .can u help me out.
i hve a file called con.conf(configuration file) tht contains
name:pradeep
place:bangalore

i need to read this file and display the contents in form type in browser . and then gve a submit button so as to edit and update the file .
can u gve me a code for this process.
email:[ REMOVED ]
Jul 29 '07 #1
59 4108
mwasif
802 Expert 512MB
Checkout file() function of PHP.
Do not post your email address and choose a descriptive title.
Posting Guidelines
Jul 29 '07 #2
pradeepjain
563 512MB
Checkout file() function of PHP.
Do not post your email address and choose a descriptive title.
Posting Guidelines

but plzz gve me code for tht program if u can.i read tht file() but cld not do it. plzz can u help me wit code..i wll be great ful to u
Jul 30 '07 #3
kovik
1,044 Expert 1GB
but plzz gve me code for tht program if u can.i read tht file() but cld not do it. plzz can u help me wit code..i wll be great ful to u
Unless you have money, your "great"-fulness isn't worth much (at least not to me :-D).

What you want to do is something that a database could do easily. If you really want to do it with a file, then go back to the documentation for the file() function and read the documentation for ALL of the file functions (fread(), fwrite(), fopen(), etc.) and make the code.

It's not hard to decipher. I only help those who help themselves. Show some effort, and you'll get some assistance.
Jul 30 '07 #4
pradeepjain
563 512MB
Unless you have money, your "great"-fulness isn't worth much (at least not to me :-D).

What you want to do is something that a database could do easily. If you really want to do it with a file, then go back to the documentation for the file() function and read the documentation for ALL of the file functions (fread(), fwrite(), fopen(), etc.) and make the code.

It's not hard to decipher. I only help those who help themselves. Show some effort, and you'll get some assistance.



OK COOL..I KNOW ALL THE FILE OPERATIONS..BUT THE FILE CONTAINS ENTIES LKE
NAME:PRADEEP
PLACE:BLORE

THE NAME AND PLACE SHLD BE A FIXED FIELD IN THE FORM AND THE REST MUST VARY..PLZZ HELP ME HOW TO FETCH THE VARIABLES FROM FILE....OR ELSE GVE ME SOME SUGGESTIONS AS TO HOW TO GO ON WIT IT.
Jul 30 '07 #5
nathj
938 Expert 512MB
OK COOL..I KNOW ALL THE FILE OPERATIONS..BUT THE FILE CONTAINS ENTIES LKE
NAME:PRADEEP
PLACE:BLORE

THE NAME AND PLACE SHLD BE A FIXED FIELD IN THE FORM AND THE REST MUST VARY..PLZZ HELP ME HOW TO FETCH THE VARIABLES FROM FILE....OR ELSE GVE ME SOME SUGGESTIONS AS TO HOW TO GO ON WIT IT.
1) What have you got written so far? Posting code you are stuck with will get you better help.
2) Have you read about the explode() function

The explode() function will enable you to retreive the contents of your file into an array.

I must say that the best way to do this would be with a database. It's not difficult, read some tutroials on W3Schools and you'll be up and running in now time.

Cheers
nathj
Jul 30 '07 #6
pradeepjain
563 512MB
1) What have you got written so far? Posting code you are stuck with will get you better help.
2) Have you read about the explode() function

The explode() function will enable you to retreive the contents of your file into an array.

I must say that the best way to do this would be with a database. It's not difficult, read some tutroials on W3Schools and you'll be up and running in now time.

Cheers
nathj


FOR THIS I AM NOT SUPOSED TO USE A DB THTS THE PROB..CAN U GVE ME CODE FOR THIS ONE ..IF U CAN
Jul 30 '07 #7
nathj
938 Expert 512MB
FOR THIS I AM NOT SUPOSED TO USE A DB THTS THE PROB..CAN U GVE ME CODE FOR THIS ONE ..IF U CAN
Okay, so you can't use a database, that shouldn't be a problem.

Have you tried using the explode() function I pointed out?

Try that and let me know how you get on? When you are playing around with it you may also find print_r(arrayname) useful.

The explode function takes a string and converts it to an array, you can then print the array (print_r) to see what you've got and how you can use it best.

Have a go and post the code you get stuck on. Having a go is the best way to learn.
Jul 30 '07 #8
pradeepjain
563 512MB
Okay, so you can't use a database, that shouldn't be a problem.

Have you tried using the explode() function I pointed out?

Try that and let me know how you get on? When you are playing around with it you may also find print_r(arrayname) useful.

The explode function takes a string and converts it to an array, you can then print the array (print_r) to see what you've got and how you can use it best.

Have a go and post the code you get stuck on. Having a go is the best way to learn.

see i wll tell how i wnt to move abt


1st open the file in read mode
then take each line in a string using a for loop or so
then using explode searching for the : i wll split name:pradeep into 2 and then assign it some 2 variables;
and then print them in the form .
then use $_post method to read from the form and then write them to the file..

do u hve any suggstions on this
Jul 30 '07 #9
pradeepjain
563 512MB
Okay, so you can't use a database, that shouldn't be a problem.

Have you tried using the explode() function I pointed out?

Try that and let me know how you get on? When you are playing around with it you may also find print_r(arrayname) useful.

The explode function takes a string and converts it to an array, you can then print the array (print_r) to see what you've got and how you can use it best.

Have a go and post the code you get stuck on. Having a go is the best way to learn.


after reading from the form say $_post['name']
how to write back to file in same format as
name:pradeep
Jul 30 '07 #10
nathj
938 Expert 512MB
after reading from the form say $_post['name']
how to write back to file in same format as
name:pradeep
You need to try this for yourself, using the file functions referenced earlier. Have a go and post some code and the results if you run into trouble.

We are all prepared to help but we won't write the code for you.

Cheers
nathj
Jul 30 '07 #11
pradeepjain
563 512MB
You need to try this for yourself, using the file functions referenced earlier. Have a go and post some code and the results if you run into trouble.

We are all prepared to help but we won't write the code for you.

Cheers
nathj

1st open the file in read mode
then take each line in a string using a for loop or so
then using explode searching for the : i wll split name:pradeep into 2 and then assign it some 2 variables;
and then print them in the form .
then use $_post method to read from the form and then write them to the file..

do u hve any suggstions on this
ok i am ready to try .but is the way i am going abt is it alright.
Jul 30 '07 #12
pradeepjain
563 512MB
Srry If I Am Bothering U All
Jul 30 '07 #13
nathj
938 Expert 512MB
Srry If I Am Bothering U All
You are not bothering us all, this is what forums are for. It's just that the best way to learn is to have a go.

1st open the file in read mode
then take each line in a string using a for loop or so
then using explode searching for the : i wll split name:pradeep into 2 and then assign it some 2 variables;
and then print them in the form .
then use $_post method to read from the form and then write them to the file..

do u hve any suggstions on this
ok i am ready to try .but is the way i am going abt is it alright.
This process sounds okay to me, I think you are heading down the right road with this idea. Also it's good to plan the process out before you write so full marks there.

I look forward to seeing the result. I'm sure you'll get this working as you need it to and that you will understand what you have done and why it works.

Cheers
nathj
Jul 30 '07 #14
pradeepjain
563 512MB
<?php
$file=fopen("/var/lib/penguin/Desktop/welcome.txt","r")or exit("Unable to open file!");
$x=0;
for($i=0 ; $i != feof($file) ; $i++)
{
$data = fgets($file,$x);
$str = explode(':', $data);
$name[$i] = $str[0]; // piece1
$value[$i] = $str[1]; // piece2
echo $name[$i];
echo $value[$i];
$x=$x+1024;
$i=$i+1;
}
$lcount=$i;
?>
<html>
<head>
</head>
<body>
<?php
print $lcount;
?>
<FORM METHOD=post ACTION="test1.php">
for($i=0;$i <= $lcount ;$i++){
<input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

}
<INPUT TYPE=SUBMIT>
</form>
</body>
</html>


i hve written this code...there is some error in the html combining wit the php..
can any one sujjest me something....
Jul 31 '07 #15
nathj
938 Expert 512MB
i hve written this code...there is some error in the html combining wit the php..
can any one sujjest me something....
Good to see you have written some code.

Try turning on the error hanlding and telling what you see on the page when you run it. This will shed some light on what is happening.

Also when yo say 'there is some error' what do you mean? What are you currently seeing, what do you think is happening.

My old university tutor would have sent me packing if I'd said 'there's some error please help.' You have to define the problem a bit more, especially as we cannot see your system/setup completely but just the code you post (thanks for posting it).

Let us know what comes back from your next set of tests.

Cheers
nathj
Jul 31 '07 #16
pradeepjain
563 512MB
Good to see you have written some code.

Try turning on the error hanlding and telling what you see on the page when you run it. This will shed some light on what is happening.

Also when yo say 'there is some error' what do you mean? What are you currently seeing, what do you think is happening.

My old university tutor would have sent me packing if I'd said 'there's some error please help.' You have to define the problem a bit more, especially as we cannot see your system/setup completely but just the code you post (thanks for posting it).

Let us know what comes back from your next set of tests.

Cheers
nathj

oh ok....its not showning anything in the form jst form text box is appearing and then within it it appears
for($i=0;$i <= $lcount ;$i++){ type=text > } lke this in html page
Jul 31 '07 #17
gregerly
192 Expert 100+
First thing that jumps out at me is that you have PHP code not inside PHP tags:

[HTML]<FORM METHOD=post ACTION="test1.php">
for($i=0;$i <= $lcount ;$i++){
<input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

}
<INPUT TYPE=SUBMIT>
</form>[/HTML]

which should be:

[HTML]<FORM METHOD=post ACTION="test1.php">[/HTML]
[PHP]<?
for($i=0;$i <= $lcount ;$i++){
<input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

}
?>[/PHP]
[HTML]<INPUT TYPE=SUBMIT>
</form>[/HTML]

Greg
Jul 31 '07 #18
pradeepjain
563 512MB
First thing that jumps out at me is that you have PHP code not inside PHP tags:

[HTML]<FORM METHOD=post ACTION="test1.php">
for($i=0;$i <= $lcount ;$i++){
<input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

}
<INPUT TYPE=SUBMIT>
</form>[/HTML]

which should be:

[HTML]<FORM METHOD=post ACTION="test1.php">[/HTML]
[PHP]<?
for($i=0;$i <= $lcount ;$i++){
<input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

}
?>[/PHP]
[HTML]<INPUT TYPE=SUBMIT>
</form>[/HTML]

Greg

i tried wht u told but not working
error:type=text > } ?>
Jul 31 '07 #19
nathj
938 Expert 512MB
Hi,

Try changing this section

[PHP]
<?
for($i=0;$i <= $lcount ;$i++){
<input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

}
?>

[/PHP]

To something more like this:
[PHP]
<?php
for ($i=0; $i <= $lcount; $i++)
{
echo '<input type="text" name=' . $name[$i] . 'value=' . $value[$i] . ' />' ;
}

?>
[/PHP]
The first sample has nested php tags, I don't know if this is a problem or not but it sure looks weird. Also the raw HTML inside the php tags is a potential problem.
The second sample removes the need for this and should work just fine, assuming that the two arrays are correct.

So, your final page could look like this (all in php):
[PHP]
<?php
echo '<form name = "testform" method="post" action="test1.php">
';
for ($i=0; $i <= $lcount; $i++)
{
echo '<input type="text" name=' . $name[$i] . 'value=' . $value[$i] . ' />' ;
}
echo '<input type="submit" value="Complete"/>';
echo '</form>';
?>
[/PHP]


Have a play with this and let us know what you get.

Cheers
nathj
Jul 31 '07 #20
pradeepjain
563 512MB
<?php
$file=fopen("/var/lib/penguin/Desktop/welcome.txt","r")or exit("Unable to open file!");
$x=0;
for($i=0 ; $i != feof($file) ; $i++)
{
$data = fgets($file,$x);
$str = explode(':', $data);
$name[$i] = $str[0]; // piece1
$value[$i] = $str[1]; // piece2
echo $name[$i];
echo $value[$i];
$x=$x+1024;
$i=$i+1;
}
$lcount=$i;
?>
<html>
<head>
</head>
<body>


<?php

echo '<form name = "testform" method="post" action="test1.php">

';

for ($i=0; $i <= $lcount; $i++)

{

echo '<input type="text" name=' . $name[$i] . 'value=' . $value[$i] . ' />' ;

$i = $i + 1;
}

echo '<input type="submit" value="Complete"/>';

echo '</form>';

?>



</form>
</body>
</html>

this is wht i hve used as code..but still not working...

error:'; for ($i=0; $i <= $lcount; $i++) { echo '' ; $i = $i + 1; } echo ''; echo ''; ?>


i thnk there is a problem in 2 arrays...is th 1st php code proper
Jul 31 '07 #21
nathj
938 Expert 512MB
What information is in the two arrays you have? It may be that we need to adjust the code to include quote marks if the data in the array contains spaces.


Also are you getting an error message - what is the message exactly?

Cheers
nathh
Jul 31 '07 #22
pradeepjain
563 512MB
What information is in the two arrays you have? It may be that we need to adjust the code to include quote marks if the data in the array contains spaces.


Also are you getting an error message - what is the message exactly?

Cheers
nathh

the file contains
name:pradeep
palce:blore

i am taking the 1st line into data and then using explode so as 2 split into 2 arrays say name and pradeep..and storing them in name[$i] and value[$i]
then i am moving to next line wit help of $x+1024 and then splitting the 2nd line also..
then using the variables in the html form..
there is no proper error ..but its displaying jst tht wht i said in last msg.
Jul 31 '07 #23
nathj
938 Expert 512MB
the file contains
name:pradeep
palce:blore

i am taking the 1st line into data and then using explode so as 2 split into 2 arrays say name and pradeep..
explode() will generate one array:
[PHP]
<?php
$lcFileContent = file(_FileName_);
$laTest = explode(":", $lcFileContent);
print_r($laTest);
[/PHP]

This single array will then contain 'name' and 'pradeep'. The code abpve will print the array and you will be able to see what is in it and how best to manipulate.

Cheers
nathj
Jul 31 '07 #24
pradeepjain
563 512MB
explode() will generate one array:
[PHP]
<?php
$lcFileContent = file(_FileName_);
$laTest = explode(":", $lcFileContent);
print_r($laTest);
[/PHP]

This single array will then contain 'name' and 'pradeep'. The code abpve will print the array and you will be able to see what is in it and how best to manipulate.

Cheers
nathj

but this is wht in found i php manual

If you want to split a price (float) into pounds and pence.

or dollors and cents etc etc.

$price = "6.20";

$split = explode(".", $price);
$pound = $split[0]; // piece1
$pence = $split[1]; // piece2

echo "&pound $pound . $pence\n";

seeing this only i used tht...

hey i am not finding a way wht to do..if i dont show the code before SATURDAY i wll kicked off frm the job..,
Jul 31 '07 #25
nathj
938 Expert 512MB
but this is wht in found i php manual

If you want to split a price (float) into pounds and pence.

or dollors and cents etc etc.

$price = "6.20";

$split = explode(".", $price);
$pound = $split[0]; // piece1
$pence = $split[1]; // piece2

echo "&pound $pound . $pence\n";

seeing this only i used tht...

hey i am not finding a way wht to do..if i dont show the code before SATURDAY i wll kicked off frm the job..,
First of all please use CODE tags, it makes reading your post much easier!

Second, you may be in a panic but I'm just trying to help and perhaps you need to take a breather before reading this and come back to it with a clearer mind. I know I need to do that sometimes.

The code sample you posted
[PHP]
$price = "6.20";

$split = explode(".", $price);
$pound = $split[0]; // piece1 - "6"
$pence = $split[1]; // piece2 - "20"

echo "&pound $pound . $pence\n";
[/PHP]
Simply takes string and splits on the occurrence of '.' into one array called $split. This array has numeric keys, starting at 0 and can be referenced accoridingly.

In your case all that is different is the source of the original string - you want it to come from a file.

What you need to do, is what you were doing earlier (I showed the file() function and print_r() to help you understand what happens with the xplode function).

Open the file and loop through one line at a time.
For each line create a variable of the line.
Explode the variableit into an array
Loop through this array (this is now a nested loop)
This array will, according to your data structure, contain a pair of values, do what you want with these. Note; as its a numeric array it is simple to loop with a for loop based on the count() of the array - remember arrasy start counting at 0.

This is the psuedo code for you. Just write it up. I've given enough help now on this. I do have a job to do myself you know, with my own deadlines to meet - you're not the only one under pressure.

If you write the code, as outlined, and need some help post it back here with lots of information and use the CODE or PHP tags to format it.

nathj
Jul 31 '07 #26
ak1dnar
1,584 Expert 1GB
pradeepjain,

Please read the Forum rules and Posting guidelines while you using the forums.And please pay your kind attention to these;
Jul 31 '07 #27
pradeepjain
563 512MB
First of all please use CODE tags, it makes reading your post much easier!

Second, you may be in a panic but I'm just trying to help and perhaps you need to take a breather before reading this and come back to it with a clearer mind. I know I need to do that sometimes.

The code sample you posted
[PHP]
$price = "6.20";

$split = explode(".", $price);
$pound = $split[0]; // piece1 - "6"
$pence = $split[1]; // piece2 - "20"

echo "&pound $pound . $pence\n";
[/PHP]
Simply takes string and splits on the occurrence of '.' into one array called $split. This array has numeric keys, starting at 0 and can be referenced accoridingly.

In your case all that is different is the source of the original string - you want it to come from a file.

What you need to do, is what you were doing earlier (I showed the file() function and print_r() to help you understand what happens with the xplode function).

Open the file and loop through one line at a time.
For each line create a variable of the line.
Explode the variableit into an array
Loop through this array (this is now a nested loop)
This array will, according to your data structure, contain a pair of values, do what you want with these. Note; as its a numeric array it is simple to loop with a for loop based on the count() of the array - remember arrasy start counting at 0.

This is the psuedo code for you. Just write it up. I've given enough help now on this. I do have a job to do myself you know, with my own deadlines to meet - you're not the only one under pressure.

If you write the code, as outlined, and need some help post it back here with lots of information and use the CODE or PHP tags to format it.

nathj


[PHP]<?php
$file=fopen("/var/lib/penguin/Desktop/welcome.txt","r")or exit("Unable to open file!");
$x=0;
for($i=0 ; $i != feof($file) ; $i++)
{
$data = fgets($file,$x);
$str = explode(':', $data);

print_r($str); //ok as u said this wll contain name pradeep

//how to use 2nd loop..i did not get u on this.


$x=$x+1024;
$i=$i+1;
}
$lcount=$i;
?>[/PHP]
Jul 31 '07 #28
pradeepjain
563 512MB
If u cld show how to use 2nd loop and how to store "name" and "pradeep" in 2 diff variables...i wll b graetful to u..
Aug 1 '07 #29
pradeepjain
563 512MB
First of all please use CODE tags, it makes reading your post much easier!

Second, you may be in a panic but I'm just trying to help and perhaps you need to take a breather before reading this and come back to it with a clearer mind. I know I need to do that sometimes.

The code sample you posted
[PHP]
$price = "6.20";

$split = explode(".", $price);
$pound = $split[0]; // piece1 - "6"
$pence = $split[1]; // piece2 - "20"

echo "&pound $pound . $pence\n";
[/PHP]
Simply takes string and splits on the occurrence of '.' into one array called $split. This array has numeric keys, starting at 0 and can be referenced accoridingly.

In your case all that is different is the source of the original string - you want it to come from a file.

What you need to do, is what you were doing earlier (I showed the file() function and print_r() to help you understand what happens with the xplode function).

Open the file and loop through one line at a time.
For each line create a variable of the line.
Explode the variableit into an array
Loop through this array (this is now a nested loop)
This array will, according to your data structure, contain a pair of values, do what you want with these. Note; as its a numeric array it is simple to loop with a for loop based on the count() of the array - remember arrasy start counting at 0.

This is the psuedo code for you. Just write it up. I've given enough help now on this. I do have a job to do myself you know, with my own deadlines to meet - you're not the only one under pressure.

If you write the code, as outlined, and need some help post it back here with lots of information and use the CODE or PHP tags to format it.

nathj

I HVE TRIED THIS IS THIS WHT U SAID TO DO

[PHP]<?php
$file=fopen("/var/lib/penguin/Desktop/welcome.txt","r")or exit("Unable to open file!");
$x=0;
for($i=0 ; $i != feof($file) ; $i++)
{
$data = fgets($file,$x);
$str = explode(':', $data);
print_r($str);
for($j = 0; $j < count($str); $j++){
echo "str #$j = $str[$j]<br />";
}
$x=$x+1024;
}
$lcount=$i;
?>
[/PHP]
Aug 1 '07 #30
nathj
938 Expert 512MB
Hi,

That's about it, I only mentioned print_r() to aid your understanding.

Without that line in, and adjusting the output you get:

[php]
<?php
$file = fopen("/var/lib/penguin/Desktop/welcome.txt","r")or exit("Unable to open file!");
$x = 0;
for($i = 0 ; $i != feof($file) ; $i++)
{
$data = fgets($file,$x);
$str = explode(':', $data);

for($j = 0; $j < count($str); $j++)
{
echo $str[$j] . "<br />";
}
$x=$x+1024;
}
$lcount=$i;
?
[/php]

This should, I beleive, output the contents of the file to screen.

Now, run it and see what you get. This should enable you to see what you still need to do.

Cheers
nathj
Aug 1 '07 #31
pradeepjain
563 512MB
Hi,

That's about it, I only mentioned print_r() to aid your understanding.

Without that line in, and adjusting the output you get:

[php]
<?php
$file = fopen("/var/lib/penguin/Desktop/welcome.txt","r")or exit("Unable to open file!");
$x = 0;
for($i = 0 ; $i != feof($file) ; $i++)
{
$data = fgets($file,$x);
$str = explode(':', $data);

for($j = 0; $j < count($str); $j++)
{
echo $str[$j] . "<br />";
}
$x=$x+1024;
}
$lcount=$i;
?
[/php]

This should, I beleive, output the contents of the file to screen.

Now, run it and see what you get. This should enable you to see what you still need to do.

Cheers
nathj
this is wht is getting printed in the o/p

"; } $x=$x+1024; } $lcount=$i; ?>
"); $i = $i + 1; } ?>


this is the code tht i am using


[PHP] <?php
$file = fopen("/var/lib/penguin/Desktop/welcome.txt","r")or exit("Unable to open file!");
$x = 0;
for($i = 0 ; $i != feof($file) ; $i++)
{
$data = fgets($file,$x);
$str = explode(':', $data);

for($j = 0; $j < count($str); $j++)
{
echo $str[$j] . "<br />";//lets say $str[0] contains "name" and $str[1] contains "pradeep" .but as we come to 2nd line of the file and this for loop the str[0] wll contain "place" and and str[1] wll contain "bangalore" .this wll be a prob in html calling ..

}
$x=$x+1024;
}
$lcount=$i;
?>
[/PHP]
[HTML]<html>
<head>
</head>
<body>

<FORM METHOD=post ACTION="test1.php">

<?
print $lcount;
for($i=0;$i <= $lcount ;$i++){

print("<input name=" . $str[$i] ." type=text value=" . $str[$i+1] ." >");


$i = $i + 1;
}

?>
<INPUT TYPE=SUBMIT>
</form>
</body>
</html>
[/HTML]

ok how to call the in the html separetely i.e name and pradeep..
ie the it shld be lke
the html page must be lke this

name:<input type="text" value="pradeep>
place:<input type="text" value="bangalore>

see line 11
Aug 1 '07 #32
nathj
938 Expert 512MB
Just a suggestion but you could format the file differently. So that you have || to separate between full items such as the name and place combination and then && to separate between sub entities. This would give you file like

name:pradeep&&location:bangalore||name:smith&&loca tion:somewhere

Without the need for the low level file functions, as you have no line breaks you do:
[php]
<?php

$lcFileContents = file(_FileName_);
$laContents = explode("||", $lcFileContents); // element 0 = name:pradeep&&location:bangalore

$lnArrayLength = count($laContents);
for(i=0; i<=$lnArrayLength; $i++)
{
$lcMainSet = $laContents[$i];
$laSubArray = explode["&&", $lcMainSet]; //elemnt 0 = name:pradeep

// nest another appropriate loop and youre away
}
?>
[/php]

Hopefully get the idea, the use of nested loops can get messy so be warned and write useful comments in the code to help sign post the process to anyone else who reads it.

Have a play and let me know how you get on.

Cheers
nathj
Aug 1 '07 #33
pradeepjain
563 512MB
Just a suggestion but you could format the file differently. So that you have || to separate between full items such as the name and place combination and then && to separate between sub entities. This would give you file like

name:pradeep&&location:bangalore||name:smith&&loca tion:somewhere

Without the need for the low level file functions, as you have no line breaks you do:
[php]
<?php

$lcFileContents = file(_FileName_);
$laContents = explode("||", $lcFileContents); // element 0 = name:pradeep&&location:bangalore

$lnArrayLength = count($laContents);
for(i=0; i<=$lnArrayLength; $i++)
{
$lcMainSet = $laContents[$i];
$laSubArray = explode["&&", $lcMainSet]; //elemnt 0 = name:pradeep

// nest another appropriate loop and youre away
}
?>
[/php]

Hopefully get the idea, the use of nested loops can get messy so be warned and write useful comments in the code to help sign post the process to anyone else who reads it.

Have a play and let me know how you get on.

Cheers
nathj


the idea got rejected from my boss...the file structure is same.
name:pradeep
place:bangalore
addess:bsk
Aug 1 '07 #34
pradeepjain
563 512MB
Just a suggestion but you could format the file differently. So that you have || to separate between full items such as the name and place combination and then && to separate between sub entities. This would give you file like

name:pradeep&&location:bangalore||name:smith&&loca tion:somewhere

Without the need for the low level file functions, as you have no line breaks you do:
[php]
<?php

$lcFileContents = file(_FileName_);
$laContents = explode("||", $lcFileContents); // element 0 = name:pradeep&&location:bangalore

$lnArrayLength = count($laContents);
for(i=0; i<=$lnArrayLength; $i++)
{
$lcMainSet = $laContents[$i];
$laSubArray = explode["&&", $lcMainSet]; //elemnt 0 = name:pradeep

// nest another appropriate loop and youre away
}
?>
[/php]

Hopefully get the idea, the use of nested loops can get messy so be warned and write useful comments in the code to help sign post the process to anyone else who reads it.

Have a play and let me know how you get on.

Cheers
nathj

ANY OTHER SUGGESTION AS HOW TO CONTINUE WITH THE OLD FILE STRUCTURE ITSELF....
Aug 1 '07 #35
nathj
938 Expert 512MB
ANY OTHER SUGGESTION AS HOW TO CONTINUE WITH THE OLD FILE STRUCTURE ITSELF....
First, please don't shout at me. You are not paying me, I am helping you because I am kind that way.

Second, you need to take the code you've got and try different things. It may be that you have to hard code some rules into the php. I know it sounds nasty but it's not that bad.

Please stop panicing and this project, take a break and then play around with what you've got. My mantra is keep trying and keep learning. That's exactly what you need to do.

I've provided you psuedo code and code samples. It's about time you did something for yourself. As you can see my patience is running out.

nathj
Aug 1 '07 #36
pradeepjain
563 512MB
First, please don't shout at me. You are not paying me, I am helping you because I am kind that way.

Second, you need to take the code you've got and try different things. It may be that you have to hard code some rules into the php. I know it sounds nasty but it's not that bad.

Please stop panicing and this project, take a break and then play around with what you've got. My mantra is keep trying and keep learning. That's exactly what you need to do.

I've provided you psuedo code and code samples. It's about time you did something for yourself. As you can see my patience is running out.

nathj


hey really srry if u felt lke i was shouting on u...
hey look at this code .i hve done it properly but its still not working can u try this and tell me


[HTML] <html>
<head>
</head>
<body>


<?php

$filehandle = fopen("/var/www/html/welcome.txt","r");

$line=true;

echo '<form method=post action=action.php>';

while($filehandle && $line){

$line = fgets($filehandle);

$str = explode(':', $line);

echo '<input name="'.$str[0].'" value="'.$str[1].'"><br />';
}

echo '<input type=submit value= submit>';
echo '</form>'
?>
</body>
</html>
[/HTML]
Aug 1 '07 #37
nathj
938 Expert 512MB
hey really srry if u felt lke i was shouting on u...
hey look at this code .i hve done it properly but its still not working can u try this and tell me


[HTML] <html>
<head>
</head>
<body>


<?php

$filehandle = fopen("/var/www/html/welcome.txt","r");

$line=true;

echo '<form method=post action=action.php>';

while($filehandle && $line){

$line = fgets($filehandle);

$str = explode(':', $line);

echo '<input name="'.$str[0].'" value="'.$str[1].'"><br />';
}

echo '<input type=submit value= submit>';
echo '</form>'
?>
</body>
</html>
[/HTML]

Apology accepted.

The final echo line doesn't have a semi-colon on the end.

When yo usay it's nt working what exactly are you getting on screen and in the generated source code?

Also turn on the error reporting in php and see what it tells you, this may give you a clue as to what is going on.

All in all it looks like a nice solution.

Cheers
nathj
Aug 1 '07 #38
pradeepjain
563 512MB
Apology accepted.

The final echo line doesn't have a semi-colon on the end.

When yo usay it's nt working what exactly are you getting on screen and in the generated source code?

Also turn on the error reporting in php and see what it tells you, this may give you a clue as to what is going on.

All in all it looks like a nice solution.

Cheers
nathj
its printing the echo commands at it is lke its giving text box and inside it its giving $str[1]..i am not able to properly explain whts ..so i asked u to see it once by trying it.

hey i added this file to /var/www/html/ and then within html i created welcome.txt
and the accessed it in webbrowser by http://localhost/i.html(html filename)..is this process correct..
Aug 1 '07 #39
nathj
938 Expert 512MB
Hi pradeepjain,

I have tried this with the following file:

Expand|Select|Wrap|Line Numbers
  1. name:micahel
  2. location:Newcastle
  3. name:Steven
  4. location:Liverpool
  5. name:Jamie
  6. location:Liverpool
  7.  
Using the following php
[php]
<?php
$filehandle = fopen("test.txt","r");
$line=true;
echo '<form method=post action=action.php>';
while($filehandle && $line)
{
$line = fgets($filehandle);
if (!empty($line))
{
$str = explode(':', $line);
echo $str[0] . ': <input type="text" name="'.$str[0].'" value="'.$str[1].'"><br />';
}
}

echo '<input type=submit value= submit>';
echo '</form>'
?>
[/php]

This gives me a page with 6 text boxes and labels on.

The text file is currently in the same directory as the php file but that shouldn't make any difference.

Hopefully you will get this finished in time for Saturday and will have learnt a bit more about PHP.

Please take some time after this project to read some php tutorials. This will give you a better understanding of the basics and help you to get better at it very quickly.

Cheers
nathj
Aug 2 '07 #40
pradeepjain
563 512MB
Hi pradeepjain,

I have tried this with the following file:

Expand|Select|Wrap|Line Numbers
  1. name:micahel
  2. location:Newcastle
  3. name:Steven
  4. location:Liverpool
  5. name:Jamie
  6. location:Liverpool
  7.  
Using the following php
[php]
<?php
$filehandle = fopen("test.txt","r");
$line=true;
echo '<form method=post action=action.php>';
while($filehandle && $line)
{
$line = fgets($filehandle);
if (!empty($line))
{
$str = explode(':', $line);
echo $str[0] . ': <input type="text" name="'.$str[0].'" value="'.$str[1].'"><br />';
}
}

echo '<input type=submit value= submit>';
echo '</form>'
?>
[/php]

This gives me a page with 6 text boxes and labels on.

The text file is currently in the same directory as the php file but that shouldn't make any difference.

Hopefully you will get this finished in time for Saturday and will have learnt a bit more about PHP.

Please take some time after this project to read some php tutorials. This will give you a better understanding of the basics and help you to get better at it very quickly.

Cheers
nathj
wht u ment to say is tht this code is working properly or not..ok 6 text boxes are coming but is the contents being displayed properly..
lke
name(label) micheal(with in a text box) in the page
Aug 2 '07 #41
nathj
938 Expert 512MB
Yes! It is working correctly. The item on the left of the : in the txt file is the label and the item on the right of the : in the ext file is the contents of the input box.

If you could have been bothered to run this yourself you would see that it is working in the way you described.

Please run this and see! This is after all you rproject and I feel that you are doing very little to help yourself. I keep helping you becasue I am kind and like to see everyone succeed but if you do not help yourself a bit more I'll disappear.

Cheers
nathj
Aug 2 '07 #42
pradeepjain
563 512MB
Yes! It is working correctly. The item on the left of the : in the txt file is the label and the item on the right of the : in the ext file is the contents of the input box.

If you could have been bothered to run this yourself you would see that it is working in the way you described.

Please run this and see! This is after all you rproject and I feel that you are doing very little to help yourself. I keep helping you becasue I am kind and like to see everyone succeed but if you do not help yourself a bit more I'll disappear.

Cheers
nathj


ya ok i agree on wht u r saying..but i hve tried it ...but its not working for me here...see this is the code tht i am using.....

[HTML]<html>
<head>
</head>
<body>
<?php

$filehandle = fopen("welcome.txt","r");

$line=true;

echo '<form method=post action=action.php>';

while($filehandle && $line)

{

$line = fgets($filehandle);

if (!empty($line))

{

$str = explode(':', $line);

echo $str[0] . ': <input type="text" name="'.$str[0].'" value="'.$str[1].'"><br />';

}

}



echo '<input type=submit value= submit>';

echo '</form>';

?>

</body>
</html>
[/HTML]

u might be saying tht i am not doing any thing..but i am putting a lot of effort..i am reading tht php bible...
Aug 2 '07 #43
nathj
938 Expert 512MB
In what whay is this not working for you?

Do you have permission to read the text file on your system?

I do not know what you mean when you say it is not working for you. The code I gave in my previous post works just fine for me.

The code you supplied alos works for me. When I change the file name to the test file I have set up it works perfectly. I do not understand what the problem is now.

Cheers
nathj
Aug 2 '07 #44
pradeepjain
563 512MB
In what whay is this not working for you?

Do you have permission to read the text file on your system?

I do not know what you mean when you say it is not working for you. The code I gave in my previous post works just fine for me.

The code you supplied alos works for me. When I change the file name to the test file I have set up it works perfectly. I do not understand what the problem is now.

Cheers
nathj
i am jst executing the code i passed u in browser by giving http://localhost/i.html is there any other php settings tht i hve to do..
Aug 2 '07 #45
nathj
938 Expert 512MB
i am jst executing the code i passed u in browser by giving http://localhost/i.html is there any other php settings tht i hve to do..
Do you have php installed?

what happens if you have page called test.php that has only the following code in it:

[php]
<?php phpInfo() ;?>
[/php]

Run this via //localhost/test.php and tell me what you get on screen.

Also, what web server and O/S are you running on? There are somethingns you need to set up in IIS to get php to work.

Cheers
nathj
Aug 2 '07 #46
pradeepjain
563 512MB
Do you have php installed?

what happens if you have page called test.php that has only the following code in it:

[php]
<?php phpInfo() ;?>
[/php]

Run this via //localhost/test.php and tell me what you get on screen.

Also, what web server and O/S are you running on? There are somethingns you need to set up in IIS to get php to work.

Cheers
nathj

i did as u said it gave me a blank screen...

i am using linux os and apache server...

which php to install and how and wht r the conf details
Aug 2 '07 #47
nathj
938 Expert 512MB
i did as u said it gave me a blank screen...

i am using linux os and apache server...

which php to install and how and wht r the conf details
Hi,

How frustrating, after all this you don't have PHP installed!

You need to go to the main php site and figure this out there, unless someone else can help. I am running PHP on Windows and have never used Linux. The instal process for Windows uses an msi and is all self explanatory.

Perhaps you could post another question asking for help installing php on linux, I know the LAMP (Linux Apache, MySQL, PHP) combination is very popular (if not the most popular) so you will be able to get some help on that. Though ask the question without mentioing this thread.

Hopefully, once installed your code should work fine.

I hope you get this resolved in time - the code will work once the environment is in place.

Cheers
nathj
Aug 2 '07 #48
pradeepjain
563 512MB
[HTML]<html>
<head>
</head>
<body>
<?php

$filehandle = fopen("welcome.txt","r");

if(filesize("welcome.txt")=='0')
{
echo "file empty";
}
else
{
$line=true;

echo '<form method=post action=action.php>';

while($filehandle && $line)

{

$line = fgets($filehandle);

if (!empty($line))

{

$str = explode(':', $line);

echo $str[0] . ': <input type="text" name="'.$str[0].'" value="'.$str[1].'"><br />';

}

}



echo '<input type=submit value= submit>';

echo '</form>';
}
?>

</body>
</html>

[/HTML]

yahoo !!! the read part is working correctly....


but now writing back to file???

$POST wll do it but the name of the text box???


any ideas on this...
Aug 3 '07 #49
nathj
938 Expert 512MB
yahoo !!! the read part is working correctly....


but now writing back to file???

$POST wll do it but the name of the text box???


any ideas on this...
Glad we finally got there on that.

As for writing back to the file the $_POST variable is really an array, so you can loop through the array and then write to file when you hit a key() value of name or location. This should sort you out nicely.

Have a go and if you get stuck post the code you've got and I'll have a play around with it.

Cheers
nathj
Aug 3 '07 #50

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

Similar topics

4
by: JDJones | last post by:
I'm trying to write a script that will read from a text list of songs that I burned onto my CD (Albums011.txt), then write to the database text the new text made ready for inserting into a...
2
by: Amy G | last post by:
I am looking to make this code a little "nicer"... any suggestions??? I want to do a "read+" where I would be able to first read the contents of the file... then either close the file out or write...
0
by: Joan Bos | last post by:
Hi, Is there somewhere on the Internet a description of what a .NET application config file should contain? For our application, I have to write passwords encrypted in a config file. The...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
1
by: cnu | last post by:
My program generates a log file for every event that happens in the program. So, I open the file and keep it open till the end. This is how I open the file for writing: <CODE> public...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
10
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus...
8
by: kepioo | last post by:
I currently have an xml input file containing lots of data. My objectiv is to write a script that reports in another xml file only the data I am interested in. Doing this is really easy using SAX....
3
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.