/    Sign up×
Community /Pin to ProfileBookmark

Syntax Question

Im trying to create a gui that will allow the user to update/configure the server’s default ftp location. There is a configuration table that contains paths for the ftp site as well as usernames and passwords.

In the following code i attempt to read out of the database, and use the old username as a way to make a regular expression match. once ive found the match, i substitute the old username for the new one. This is to update the path names correctly. However im not successful in storing the value of the old username. This method works fine when the hash iterates through the username BEFORE the paths, but if the config table was loaded ‘upsidedown’ it doesnt work at all, i get a null value for the old username.

here is the code:

[QUOTE]

function make_config($site, $username, $password) {

$sql = “SELECT cname, cvalue FROM config “;
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
$configHash = array(“$row[cname]” => “$row[cvalue]”);

echo “$configHash[ftp_user] here<br>n”;

if($configHash[ftp_user]) {
$reg_match = $configHash[ftp_user];
}

foreach($configHash as $key => $val) {

if($key == “ftp_user”) {
$val = $username;
}
if($key == “ftp_pass”) {
$val = $password;
}
if($key == “ftp_host_id”) {
$val = $site;
}
if($key == “host_location”) {
$val = $site;
}

$home = preg_match(‘/^/home//’, $val);
if(($home) && ($key != “ftp_log_file_send”) && ($key != “ftp_log_file_recon”)) {
print(“Here is reg_match: $reg_match<br>n”);

print(“Here is val before parse: $val<br>n”);
$val = str_replace($reg_match, $username, $val);
print(“And here is val after parse: $val<br>n”);
}

//config_update($key, $val);
}

}

}

[/QUOTE]

Any help would be appreciated, im new to PHP and i THOUGHT i had the syntax nailed down — but apparently this problem is a bit elusive.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@chazzyJun 13.2006 — why would you do it like that though? why not just issue the update, avoid all of this regular expressions and make the change, since it's designed to be an exact match?
Copy linkTweet thisAlerts:
@matt1776authorJun 13.2006 — because in the config table there are paths, i.e.

home/westin/transfer/etc/101/tags/input

and i need to be able to configure them to the new specified username:

home/newusername/transfer/etc/101/tags/input

-- I know the needed paths, but this process needs to be dynamic so that if anyone needs to add a path or change a path, the process will still occur correctly. Its against our 'development culture' here to hard code anything.
Copy linkTweet thisAlerts:
@chazzyJun 13.2006 — what happens if you change from if's to if/else if's?

also, try making this change, see if we get appropriate value
[code=php]
while($row = mysql_fetch_array($result)) {
print_r($row);
$configHash = array("$row[cname]" => "$row[cvalue]");
[/code]
Copy linkTweet thisAlerts:
@matt1776authorJun 13.2006 — in this example it seems as though php is dependent upon the order that the key value pairs come out of the db in. However in perl i can simply specify a value that i want with a direct assignment. i.e.


%given_hash

$given_hash->{value_needed}

but in php this doesnt work, or I dont know how to do it:

$givenHash

$givenHash[value_needed]
[/QUOTE]


and the print_r($row) revealed that i was in fact getting the right values. adding the elseifs does not seem to affect the problem of the order in which the hash values are iterated through.
Copy linkTweet thisAlerts:
@chazzyJun 13.2006 — hmmm...

what if you clear out the values (maybe even unset) at the end of each iteration of the loop. i could imagine there being a scope issue here.

also, it might be good practice to leave the else if's in (even if they prooved to ineffective) - helps avoid logically ambiguities in your code.

also, just to point out, you have no logical connection between this section

[code=php]
if($configHash[ftp_user]) {
$reg_match = $configHash[ftp_user];
}[/code]

and
[code=php]
if(($home) && ($key != "ftp_log_file_send") && ($key != "ftp_log_file_recon")) {
print("Here is reg_match: $reg_match<br>n");
[/code]


another thing to consider - since you are always replacing the 2nd cluster, what if you break down the path into an array and replace the 2nd value ($array[1]) with the correct value.

to understand better, you have sets of 5 entries, various keys, right? well if you read in 5 entries at a time (until it goes null) wouldn't you have all the data you need at once? then there would be no dependency on order (basically I think the underlying issue is a scope problem).
Copy linkTweet thisAlerts:
@matt1776authorJun 14.2006 — Chazzy,

That worked. I exploded the path and got the second path dir. Then replaced it with the username value, this allowed me to circumvent the order issue.

However I still dont understand why I could not simply directly assign the value of my $reg_match variable out of the hash i created from the query:

$configHash->{ftp_user} or $configHash[ftp_user]

Im not sure how this is done in php, but in perl, i can make a direct assignment in the while loop, and use that assigned variable anywhere else in my data structure. the value is not reset or made null.

in this instance however, i was not able to do so. And I may be a bit confused. ?

At any rate,

Thanks mucho
Copy linkTweet thisAlerts:
@chazzyJun 14.2006 — i think to best understand it, we have to look at this:

[code=php]
$configHash = array("$row[cname]" => "$row[cvalue]");
[/code]

for one, you have an error here - it should be $row['cname'] and $row['cvalue']. second, i don't think that php likes this format, might be a bug, but whatever, it doesn't seem to like it. furthermore, it's not really a structure, as the key/value change from row to row. you might get better - and more acceptable results. but that doesn't really solve the issue here. i think what it is is that you were expecting the data from one iteration of the foreach to be alive in other versions - it's not, maybe that's what you were thinking but it's not how it works. php has really funky scopes and lifetimes.
×

Success!

Help @matt1776 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...