TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-18-2008, 02:03 AM   #1 (permalink)
The Contributor
 
Join Date: Mar 2008
Posts: 62
Thanks: 2
Seraskier is on a distinguished road
Default external data into a text file

ok, so dont ask why, just trying something....

I am wanting to put that actual data into a text file and using fwrite to go to the end and add the data in.

But I seem to not be able to get the data into an array.

array.txt
Code:
'1'=>array
	(
		'xxxxx@xxxxxxxx.xx.xxx',
		'xxxxxxxxx'
	),
	
'2'=>array
	(
    	'xxxxx@xxxxxxx.xxx',
		'xxxxxx'
	),
users.php
Code:
<?php
///////////////////////////////////////
//gets the id inputed by the user    //
$id=$_GET['id'];                     //
	                            //
//initializes the array              //
$users=array();                      //
                                     //
//shortcut for data in the array     //
$users=$users[$id];                  //
///////////////////////////////////////

//Beginning of the profile code
echo "Email Address: <b>".$users[0]."</b>";
echo "<br />Username: <b>".$users[1]."</b>";
//And, the ending of the profile code

//just here to test something (temporary)
echo "<br /><br />";
echo "<b>We have ".count($users)." registered users.";
?>
I took out the code that I used to try to read the array.txt, doesnt work.

I cant seem to get it to work and I cant find anything online.

Jordan
Send a message via MSN to Seraskier
Seraskier is offline  
Reply With Quote
Old 04-18-2008, 02:18 AM   #2 (permalink)
The Contributor
 
Join Date: Mar 2008
Posts: 62
Thanks: 2
Seraskier is on a distinguished road
Default

found it out, its a little different, but it works:
Code:
//initializes the array
$users=array();
//populates the array
require_once('array.php');
any other better ideas would be great!
Send a message via MSN to Seraskier
Seraskier is offline  
Reply With Quote
Old 04-18-2008, 02:32 AM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,267
Thanks: 90
Wildhoney is on a distinguished road
Default

I'm quite unsure of what you're asking, but have you tried the serialize and unserialize functions? If you're also asking how to read and write, I think I have covered everything you may need to know in the code below.

php Code:
$aItems = array();
$szFile = 'items.txt';

if(file_exists($szFile))
{
    $szContent = file_get_contents($szFile);
    $aItems = unserialize($szContent);
}

$aItems[] = array
(
    'Name' => 'Adam',
    'Email' => 'adam@example.com'
);

file_put_contents($szFile, serialize($aItems));
printf('Items in file: %d', count($aItems));
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 04-18-2008, 03:00 PM   #4 (permalink)
The Contributor
 
Join Date: Mar 2008
Posts: 62
Thanks: 2
Seraskier is on a distinguished road
Default

I know how to edit the file and add stuff to it, I just need a way to populate an array from another file....
Send a message via MSN to Seraskier
Seraskier is offline  
Reply With Quote
Old 04-18-2008, 07:08 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,267
Thanks: 90
Wildhoney is on a distinguished road
Default

Serialize and unserialize will do that all for you.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 04-18-2008, 10:32 PM   #6 (permalink)
The Contributor
 
Join Date: Mar 2008
Posts: 62
Thanks: 2
Seraskier is on a distinguished road
Default

ill try to explain better....

i have a register form, they put in all their info and then php processes it and then uses fopen() and fwrite() it will add their information into a text file (using a text file it would be easier to edit becuase i would need a ); at the end that fwrite() cant edit before that) and then use php to make a new array of $users [$users=array();] and then populate the array with a text document, so i would want it to.

i can populate the array using php by using php
Code:
index.php
----------------------------------------
$users=array();
require_once('users.database.php');
$users=$users[$id]; //for shortcut purposes

########################################
users.database.php
----------------------------------------
$users[1]=array('test@test.com','testAccount','testPassword');
$users[2]=array('test@test2.com','testAccount2','testPassword2);
//end of user arrayed database
if i was to use count($users) or count(count_keys($users)) it would return 3 instead of 2, i am trying to use count() for counting users, but it counts the email address, username, and password for keys instead of the number inside [] in '$users[1]=array(...);'...
so if i used:
Code:
'1'=>array
     (
          'test@test.com' //email address
          'testAccount' //username
     )
and using count() or count(count_keys()) it would return 1 instead of 2, but that being in a text file i cant get it to populate the array

hope that helps explain my situation
Send a message via MSN to Seraskier
Seraskier is offline  
Reply With Quote
Old 04-19-2008, 01:13 PM   #7 (permalink)
The Contributor
 
flyingbuddha's Avatar
 
Join Date: Jan 2008
Location: Birmingham, UK
Posts: 60
Thanks: 10
flyingbuddha is on a distinguished road
Default

Can't you call the two $users variables something different to avoid confusion? Sounds like php is struggling to distinguish which var you're trying to count.
__________________
Pro. Geek
http://www.mikeholloway.co.uk
flyingbuddha is offline  
Reply With Quote
Old 04-20-2008, 05:04 AM   #8 (permalink)
The Contributor
 
Join Date: Mar 2008
Posts: 62
Thanks: 2
Seraskier is on a distinguished road
Default

Quote:
Originally Posted by flyingbuddha View Post
Can't you call the two $users variables something different to avoid confusion? Sounds like php is struggling to distinguish which var you're trying to count.
What do you mean?
Send a message via MSN to Seraskier
Seraskier is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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


All times are GMT. The time now is 02:43 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design