Jump to content

[SOLVED] How


unidox

Recommended Posts

I have a file users.php

 

and what I am trying to do is lets say I have the following code:

<?php

// some code here;

// users array
$GLOBALS["users"]=array(
array("admin","password",1,1,1,"2",7,1),
array("steve","password",1,1,1,"2",7,1),
);
?>

 

what I am trying to do is make a script that will add the array to the array, without deleting the code on top. How would I do that? Thanks

Link to comment
Share on other sites

you still dont get it lol, here is my file.

 

<?php

// Code here.

$GLOBALS["users"]=array(
array("admin","password",1,2,1,"",7,1),
array("steve","password",1,2,1,"",7,1),
); ?>

 

That code works, what I am trying to do is make a form that adds something like this:

<?php

// Code here.

$GLOBALS["users"]=array(
array("admin","password",1,2,1,"",7,1),
array("steve","password",1,2,1,"",7,1),
array("martin","password",1,2,1,"",7,1),
); ?>

 

Just add another array. Get it? :D

Link to comment
Share on other sites

I get it. Thats exactly what my code does.

 

Run this example.

 

<?php

// some code here;

// users array
$GLOBALS["users"]=array(
    array("admin","password",1,1,1,"2",7,1),
    array("steve","password",1,1,1,"2",7,1)
);

echo '<pre>';
print_r($GLOBALS['users']);
echo '</pre>';

$newuser = array("foo","password",1,1,1,"2",7,1);
$GLOBALS['users'][] = $newuser;

echo '<pre>';
print_r($GLOBALS['users']);
echo '</pre>';

?>

Link to comment
Share on other sites

So you want to actually add code dynamically to a script? Why is that so hard to explain?

 

This type of thing could get pretty messing in php. If I was going to do it I would use sed, but anyway. The easiest method would likely be to place a placeholder in your script, much as you would in a templating system. eg;

 

$GLOBALS["users"]=array(
  array("admin","password",1,1,1,"2",7,1),
  array("steve","password",1,1,1,"2",7,1),
  //<newuser>
);

 

Then you could simply do something like....

 

<?php

  $newuser = "array(\"martin\",\"password\",1,2,1,\"\",7,1),\n//<newuser>";
  $file = file_get_contents('config.php');
  $file = str_replace('//<newuser>',$newuser,$file);
  file_put_contents('config.php',$file);

?>

 

The whole idea seems pretty messy to me though.

Link to comment
Share on other sites

You mean like change the username or something? Once again, you could try using str_replace() to overide an existing username, updating those numbers is going to get pretty messy however.

 

Can I ask why your not storing this stuff within a database? They are alot easier to deal with than text files.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.