TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   external data into a text file (http://www.talkphp.com/advanced-php-programming/2657-external-data-into-text-file.html)

Seraskier 04-18-2008 02:03 AM

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

Seraskier 04-18-2008 02:18 AM

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!

Wildhoney 04-18-2008 02:32 AM

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));

Seraskier 04-18-2008 03:00 PM

I know how to edit the file and add stuff to it, I just need a way to populate an array from another file....

Wildhoney 04-18-2008 07:08 PM

Serialize and unserialize will do that all for you.

Seraskier 04-18-2008 10:32 PM

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

flyingbuddha 04-19-2008 01:13 PM

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.

Seraskier 04-20-2008 05:04 AM

Quote:

Originally Posted by flyingbuddha (Post 13632)
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?


All times are GMT. The time now is 10:53 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0