Forum Moderators: coopster
$array = array(1, 2, 3, 4, 5);
for($i = 0; $i < count($array); $i++)
{
if ( $array[$i] == 3 )
{
** I want to delete this array element **
}
}
The resulting array should = (1, 2, 4, 5)
Note I want to remove the element completely, not just set it to no value, as in (1, 2, '', 4, 5).
$array = array(1, 2, 3, 4, 5);
print '<pre>'; print_r($array); print '</pre>';
for($i = 0; $i < count($array); $i++) {
if ($array[$i] == 3 ) {
unset [php.net]($array[$i]);
}
}
print '<pre>'; print_r($array); print '</pre>';
exit;
I am making a small form that signs users of my site up for a current "event". Then if he is already signed up he should be shown a remove button to be able to remove himself if he no longer wishes to be signed up for that event.
The signups work nicely, but I am unaware of how the coding goes for me to CHECK if certain user ID is in my array of dot separated values.
My database for the events has a column called event_signup and the values for each event are simply the user id's separated by dots. (1.15.20.3)
I am using PHP-Fusion CMS so the database functions may seem strange...
---------------------------------------------------------------------
//this will find me my current signups.
$result3 = dbquery("SELECT event_signup FROM events WHERE
event_id='".$_POST['event_id']."'");
$data3 = dbarray($result3);
// Check to see if its the first
if ( $data3['event_signup'] == "" ){
// We dont need the . as seperators cause theres only 1 id
$signup2 = $_POST['event_signup'];
}else{
// user_ids, is now an array like user_ids[0]=55 ; user_ids[1]=21.
$user_ids = explode(".",$data3['event_signup']);
// Tells how many user ids are signed up
$num_ids = count($user_ids);
//add the new id to the end of the current ones
$user_ids[$num_ids+1] = $_POST['event_signup'];
// Put all our ids back to gether
$signup2 = implode(".", $user_ids);
}
//UPDATE the signups in my database.
$result3 = dbquery("UPDATE events SET event_signup='$signup2'
WHERE event_id='".$_POST['event_id']."'");
-----------------------------------------------------------------
Now... HOW do I get that array, separate everything from it, CHECK if the current user is there, (the user clicking on remove button or something) and unset() ONLY that user from the whole array. Then implode and UPDATE database once more with all other users intact.
I had help with the current coding above, so I am no expert I know. But I am learning this stuff fast and I must say that I like it hehe for a hobby its awesome. Eventually could be that this becomes some par time job for me hehe.
Ah, thanks for any help you guys can provide on this.