
|
View Full Version : Ouch, Duplicates Deleted
Helios 11-07-2002, 02:21 AM I'm having a problem with trying to code PHP so that it won't delete every duplicate stance.
This is the HTML I want to use ( I'll just put it that it has been generated from the PHP/MySQL)
<select>
<option>Maria</option>
<option>Maria</option>
<option>John</option>
<option>John</option>
</select>
Okay, now your saying that why is there two Johns and Maria's?
Well assume that each John & Maria is a different person.
Except the only way to identify them is just by their first name.
Now to the point of this form element. I want to remove a Maria or John, but so far anytime I remove either from the database, it ends up removing both. How can I make it so that:
<SELECT Name="test">
$names = "Maria Maria John John";
$list = explode(" ", $names);
$x = 0;
while ( $list[$x] != "" ) {
printf("<option>%s</option>", $list[$x]);
x++;
}
</SELECT>
When submitted to a handler, it won't removed both Maria's even though they are identical.
$final = $names - $test;
echo "final";
^^^^
This will remove both, but I can't figure where to get rid of just one and to keep the other.
Help Please!
Lightspeed 11-07-2002, 02:52 AM Alias, we are windows programmers... But here is a solution that can be converted to php pretty trivially:
// What you will need to do is to copy the first array to the a new array, omitting any duplicate values that have already been processed from the processed array.
ASP Code:
sNames = "Maria Maria John John"
sNameList = Split ( sNames, " " )
//so now we have
//sNameList(0)= "Maria"
//sNameList(1)= "Maria"
//sNameList(2)= "John"
//sNameList(3)= "John"
k = 0
For i = 0 to ubound(sNameList)
bSkipThisDuplicate = False
For j = 0 to i-1 //(check previous sNameList entries for dupes)
If sNameList(i) = sNameList(j) Then bSkipThisDuplicate = True
Next
If Not bSkipThisDuplicate Then
sNewNameList(k) = sNameList(i)
k = k + 1
End If
Next
// Now you should have
// sNewNameList(0) = "Maria"
// sNewNameList(1) = "John"
//
// so you can continue processing as before...
MarkIL 11-07-2002, 05:35 AM Uhm...
$names = array('John', 'Maria', 'John', 'Maria');
$new = array_unique($names);
print join("<br />\n", $new);
Helios 11-07-2002, 01:39 PM $names = array('John', 'Maria', 'John', 'Maria');
$new = array_unique($names);
print join("<br />\n", $new);
Okay Considering that I have pulled $names from the database, and want to remove one Maria and then upload the new list into the database again. (It can be with any list of names)
Will the array_unique be able to handle that? PHP.net doesn't seem to be working for me so I can't look it up.
$new = array($names);
$drop = "Maria";
$finish = $new - $drop;
or would a while loop be better?
$x = 0;
while ( $new[$x] != '' ) {
if ( $new[$x] = $drop ) {
$new[$x] = '';
}
else {
x++;
}
}
When it goes through it should finish that if loop when it reaches the first Maria otherwise keep going till it finds the first instance of it.
Hopefully if this idea works, I will end up with a lot of empty spaces after a while in the database where various people have been dropped.
Is there anyway to painlessly get rid of those empty spaces?
As
$names = " John Maria John";
now. Which would in an explode function, make the first variable in the array empty I think.(If I am wrong feel free to correct me.)
jnestor 11-07-2002, 03:55 PM Wait - are you trying to remove these from a MySQL database? In otherwords you have a table like
Name
------
Maria
Maria
John
John
And you're converting that to a form and trying to delete one? So you get the selected one and do something like:
delete from people where name = '$name'
If so removing the dups from the list isn't going to help. Your problem is with the way the table is setup. You'll need to add another unique column that you can use to identify the particular row.
Helios 11-07-2002, 05:44 PM Let me put up my full source so far.
Then maybe you'll see exactly what I am talking about.
I've been trouble shooting it thus far and here is what I have:
<?php
$list = "*Maria*John*Randall*Maria";
echo "<h4>This is the List Before Anything happens to it</h4>\n".$list;
echo "\n<h4>This is the Item #</h4>\n".$name."\n<br>\n";
if (isset($Submit)) {
$new = explode('*',$list);
echo "\n<h4>This is the list after being accepted and processed by the server.</h4>\n".$new[0]."<br>";
echo $new[1]."<br>".$new[2]."<br>".$new[3]."<br>".$new[4]."<br>";
$x = $name;
unset($new[$x]);
echo "\n<h4>This is after the item you have chosen has been removed.</h4>\n";
echo $new[0]."\n<br>\n".$new[1]."\n<br>\n".$new[2]."\n<br>\n".$new[3]."\n<br>\n".$new[4];
$y = 0;
$new = array_reverse(array_reverse($new));
$list = '';
while ( isset($new[$y])) {
$list = "$list"."*$new[$y]";
$y++;
}
echo "\n<h4>This is after all the server processes have finished</h4>\n";
echo "$list\n";
printr($new);
}
?>
<form name="form1" method="post" action="<?php echo $PHP_SELF;?>">
<p>
<select name="name" size="4" id="name">
<?php
$z = 1;
$test = explode('*',$list);
if (isset($submit)) {
unset($test[1]);
$test = array_reverse(array_reverse($test));
}
while ( $test[$z] != '') {
echo "<option value=".$z.">".$test[$z]."</option>\n";
$z++;
}
?>
</select>
</p>
<p>
<input type="submit" name="Submit" value="Remove From List">
</p>
</form>
Now I've almost got this thing licked, but now in the last section during the while loop, I can't get rid of the extra * that comes up.
If you removed any of those, then you get something similar to:
**John*Randall*Maria
If there is a way to get rid of that first asterisk, my problem is solved.
(btw, the idea is actually from a sorta buddy list, which is called from a person's entry in the database. This is represented at the top of this test document as the $list variable.)
Helios 11-07-2002, 07:01 PM I'm the man! lol j/k.
I got it to finally output the way I wanted it...now I just need help slimming it down...
<?php
$list = "*Maria*John*Randall*Maria";
if (isset($Submit)) {
$new = explode('*',$list);
$x = $name;
unset($new[$x]);
$y = 0;
$new = array_reverse(array_reverse($new));
$list = '';
while ( isset($new[$y])) {
$list = "$list"."*$new[$y]";
$y++;
}
$test = explode('*',$list);
$test = array_slice($test, 1);
$test = array_reverse(array_reverse($test));
}
?>
<form name="form1" method="post" action="<?php echo $PHP_SELF;?>">
<p>
<select name="name" size="4" id="name">
<?php
$z = 1;
while ( $test[$z] != '') {
echo "<option value=".$z.">".$test[$z]."</option>\n";
$z++;
}
?>
</select>
</p>
<p>
<input type="submit" name="Submit" value="Remove Name">
</p>
</form>
At least now it formats it how I want it output.
Edit: Except for on the starting page, I don't have the Options output anymore...Sigh *stumbles back to drawingboard/notepad*
|