Current location: Hot Scripts Forums » Programming Languages » PHP » Assigning Variables


Assigning Variables

Reply
  #1 (permalink)  
Old 09-07-04, 05:00 PM
Lannister Lannister is offline
New Member
 
Join Date: Sep 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Assigning Variables

Hi, I have an html table on my website in which I want to <? php include ?> certain data (2 variables: "name" and "answer") as it is posted to the site. I have already set up the html post code, and have the following php code to write it to a text file:
PHP Code:

<?php 

$name
=$_POST["name"];
$answer=$_POST["answer"];

if( isset( 
$name) ) { 

$hFile fopen"data.txt""a+" ); 
fwrite$hFile"\n$name"\n$answer"); 
fclose$hFile ); 

?>
Obviously this is only sufficient to store the data. I need to assign variable names to the data pieces such that the new info replaces the old, but the old is not written over. Then, I want to php echo each data piece in a different location on the html table. Let me try an example to explain:
John posts "name=John" and "answer=Red" via html form to data.txt. I want to pull "John" and "Red" out of the data file, and assign $name1="John" and $answer1="Red". When Jame subsequently posts "name=Jane" and "answer="blue" to the data file, I want her info pulled out and the variable reassigned so that $name1=Jane, $answer1=blue, and $name2=John, $answer2=red. When Pete posts "name=Pete" and "answer=green", I want $name1=Pete, $answer2=green, and $name2=Jane, $answer2=blue, and $name3=John, $answer3=red.
Can anyone point me in the right direction?
Reply With Quote
  #2 (permalink)  
Old 09-07-04, 07:36 PM
whizmail whizmail is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

Walk through the file from the bottom to top jumping each 2-lines. Use a variable ($i, for example) to keep track of the count. Assign variable names like this:

$name_var = "name".$i;
$answer_var = "answer".$i;
$$name_var = [read from file];
$$answer_var = [read from file];

I hope you get what I mean.

Regards,

Raphael Pirker
Reply With Quote
  #3 (permalink)  
Old 09-07-04, 08:38 PM
infinitylimit's Avatar
infinitylimit infinitylimit is offline
Code Guru
 
Join Date: Jun 2004
Location: Oregon
Posts: 758
Thanks: 0
Thanked 0 Times in 0 Posts
I wonder what you are building because I think your logic might be improved because I couldn't think of a reason to do this. Maybe explain what you are building not what code is giving you problems.
__________________
Hawk Enterprises -- Home to PHP games, open-source code, tutorials and free downloads
Reply With Quote
  #4 (permalink)  
Old 09-07-04, 10:29 PM
mikaelf mikaelf is offline
Wannabe Coder
 
Join Date: Jun 2004
Location: php[dot]net
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
the simplest way is by using pseudo indexing in your flat file.

Code:
//your_file.txt
1;john;doe
2;foo;foobar
3;april;may
and then you can set rules in your php file
PHP Code:

$structure = array(=> 'idx'=> 'name'=> 'answer');


$name=$_POST[$structure[1]]; 
$answer=$_POST[$structure[2]];
$idx $_POST[$structure[0]]; // you can set encoding to $_POST['idx'] to prevent users to know how your script work
//$idx = myDecodeFunction($idx);

if( isset( $name) ) { 

//To add
$hFile fopen"data.txt""a+" ); 
fwrite$hFile"$idx;$name;$answer\n"); 
fclose$hFile );

//You may also sort out the data after
//$toSort = file("data.txt");
//sort($toSort);
//$toSort = implode("",$toSort);
//$hFile = fopen( "data.txt", "w" );
//fwrite( $hFile, $toSort); 
//fclose( $hFile );

//To update
$hFile fopen'data.txt''w+' ); 
$content fread($hFile,filesize('data.txt'));
$content preg_replace("/$idx;[^;]+;[^\n]+/","$idx;$name;$answer",$content);
fwrite($hFile,$content);
fclose($hFile);


Anyway, using database is much better than this 'old conservative' way.
__________________
Useful PHP links:
bugs.php.net - for reporting PHP bugs
pear.php.net - PHP extension and application repository
pecl.php.net - get non standard PHP modules, submit yours
www.phpclasses.org - PHP classes repository
Reply With Quote
  #5 (permalink)  
Old 09-11-04, 10:08 AM
Lannister Lannister is offline
New Member
 
Join Date: Sep 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Question

This isn't working for me:
PHP Code:

<? php

$structure
=array(0=>'idx',1=>'name',2=>'address'); 

$name=$_POST[$structure[1]]; 
$answer=$_POST[$structure[2]]; 
$idx $_POST[$structure[0]]; 

if( isset( 
$name) ) { 
 
$hFile fopen"data.txt""a+" ); 
fwrite$hFile"$idx;$name;$address\n"); 
fclose$hFile ); 
 
$toSort file("data.txt"); 
sort($toSort); 
$toSort implode("",$toSort); 
$hFile fopen"data.txt""w" ); 
fwrite$hFile$toSort); 
fclose$hFile ); 

$hFile fopen'data.txt''w+' ); 
$content fread($hFile,filesize('data.txt')); 
$content preg_replace("/$idx;[^;]+;[^\n]+/","$idx;$name;$address",$content); 
fwrite($hFile,$content); 
fclose($hFile); 



?>
When I post the data, I get a parse error.
Reply With Quote
  #6 (permalink)  
Old 09-11-04, 11:20 AM
mikaelf mikaelf is offline
Wannabe Coder
 
Join Date: Jun 2004
Location: php[dot]net
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
how is the error?? i mean the error message output to the browser. it's normal. i just typed the code without executing it. anyway, it's only an example *a bad example, though.*. i don't encourage you to do it that way.
__________________
Useful PHP links:
bugs.php.net - for reporting PHP bugs
pear.php.net - PHP extension and application repository
pecl.php.net - get non standard PHP modules, submit yours
www.phpclasses.org - PHP classes repository
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Server side variables Puno PHP 13 08-13-05 10:22 PM
NEWBIE QUESTION: Forms - Assigning Values to Variables jenf JavaScript 1 07-23-04 03:36 PM
Can anyone help me ? (problem using php variables in html db insert code) chronic_ PHP 2 06-13-04 11:19 AM
help with displaying variables tisza PHP 9 12-22-03 05:08 PM
adding more variables to the mail() result spinicrus PHP 6 08-28-03 04:55 PM


All times are GMT -5. The time now is 04:25 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.