Reputation: 38
If you download multiple files with the same name, a number is automatically added to the file name, for example: filename(1), filename(2) etc.
.
What I am trying to do is when I am creating a name and there is already a same one, the name I create to appear in the list as: name(1), name(2) etc.
This is the code that I use now that only creates: name and name (1)
, then I get this error when trying to create the next same name, example: name (1) already exists in your list.
, but I shouldn't get that error and should create the next name in the list: name (2) etc.
.
$deviceUser = "07NAV" . $var;
$sameNames = $mktApi->comm("/ppp/secret/getall", array(
".proplist" => ".id",
"?name" => $deviceUser
));
$i = 0;
if($sameNames) {
$i++;
$mktApi->comm("/ppp/secret/add", array(
"name" => $deviceUser . " ($i)",
"remote-address" => $IPs[$ipAddress],
"password" => $devicePass,
"service" => "pppoe",
"comment" => $fullAddress
));
}
Also, I've tried using to use
$i = 0;
if($sameNames) {
for($i = 0; $i < 99999; $i++) {
$i++;
$mktApi->comm("/ppp/secret/add", array(
"name" => $deviceUser . " ($i)",
"remote-address" => $IPs[$ipAddress],
"password" => $devicePass,
"service" => "pppoe",
"comment" => $fullAddress
));
}
}
but when I create only one name in my list, automatically are created X names in my list.
Edit after @Martin's answer:
@Martin, thank you for your answer, but the code is not working, maybe is because of me (maybe I did something wrong...)
$deviceUser = $deviceUserOriginal = "07NAV" . $var;
do {
// First - check if a duplicate exists...
$sameNames = $mktApi -> comm("/ppp/secret/getall", array(
".proplist" => ".id",
"?name" => $deviceUser
));
// Second - update and prepare for rechecking...
if($sameNames === true) {
$i ++;
$mktApi -> comm("/ppp/secret/add", array(
"name" => $deviceUser = $deviceUserOriginal . "(". $i .")",
"remote-address" => $IPs[$ipAddress],
"password" => $devicePass,
"service" => "pppoe",
"comment" => $fullAddress
));
} else {
$mktApi -> comm("/ppp/secret/add", array(
"name" => $deviceUser,
"remote-address" => $IPs[$ipAddress],
"password" => $devicePass,
"service" => "pppoe",
"comment" => $fullAddress
));
}
// Finally, below, if check failed, cycle and check again with the new updated name...
}
while($sameNames === true);
// Finally, tidy up...
// If you need the original value of $deviceUser, you can retain it.
unset($i, $deviceUserOriginal);
Upvotes: 0
Views: 148
Reputation: 22760
Your issue appears to be you never recheck the name value before updating.
You want some very simply PHP loop mechanism like this, first to check and then to update before rechecking; this is a Do...while
PHP loop.
I assume $sameNames
returns a boolean true if the name value already exists.
So, I found what the problem is... the $sameNames returns an array with the IDs that have the same name
// start your update from here:
$i = 0;
$deviceUser = $deviceUserOriginal = "07NAV" . $var;
$sameNames = [];
do {
// first; check if a dupe exists...
$sameNames = $mktApi->comm("/ppp/secret/getall", array(
".proplist" => ".id",
"?name" => $deviceUser
));
// second update and prepare for rechecking...
if(count($sameNames) > 0){
$i++;
$deviceUser = $deviceUserOriginal." (".$i.")";
}
// finally, BELOW, if check failed, cycle and check again with the new updated name.
}
while (count($sameNames) > 0);
// Finally, tidy up.
// If you need the original value of Device User you can retain it.
unset($i,$deviceUserOriginal);
//Continue your script from here:
$mktApi->comm("/ppp/secret/add", array(
"name" => $deviceUser,
"remote-address" => $IPs[$ipAddress],
"password" => $devicePass,
"service" => "pppoe",
"comment" => $fullAddress
));
// etc....
So the DO
part of the script always runs, but only is acted upon on the second or future iteration based on the result of the first test in the while
part.
Upvotes: 1