[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Array getting mixed up, test for me

Posted on 2005-04-23
8
Medium Priority
?
195 Views
Last Modified: 2008-03-06
Basically I have a problem with this code, it works perfectly

However as soon as I click to delete or email just number 10, without number 5 ticked, on the post page it shows that for some reason number 5 is now the option!

Also I cant get the last row of the array (the update button itself) to stop interering.

Heres the code, test and you'll see what I mean.


---
<?php

if(!$_POST['update'])
{ ?> <form action="test" name="caf" method="post">
<table width="40%" cellspacing="1" cellpadding="4" class="table">
    <tr>
      <td class="headbars">-</td>
      <td width="5%" class="headbar2">Email</td>
      <td width="4%" class="headbar2" align="center">Delete</td>
    </tr><tr><td class="cell">5</td>

<td width="5%" class="cell"><input name="email[]" type="checkbox" value="1" checked="checked" />1</td>
      <td width="4%" class="cell" align="center">
        <input name="delete[]" type="checkbox" id="delete[]" value="1" />
        <input type="hidden" name="tid[]" value="5" /></td>
    </tr><tr><td class="cell">10</td>
     
      <td width="5%" class="cell">
          <input name="email[]" type="checkbox" value="1" checked="checked" />1 </div></td>
      <td width="4%" class="cell" align="center">
        <input name="delete[]" type="checkbox" id="delete[]" value="1" />
        <input type="hidden" name="tid[]" value="10" />

</td></tr></table>
<input name="update" class="button" type="submit" value="Update Subscriptions" /></form><? }
     
    else
     
    {
    $tid = $_POST['tid'];
    $email = $_POST['email'];
    $delete = $_POST['delete'];
    $while = (count($_POST['tid']));
     
     
    print_r($_POST); echo '<br /><br />';
while($while >= 0) {
$while--; $a++;
$tidu = $tid["$while"];
    #$email[$while] = ($email[$while] == 1) ? 1 : 0;
    #$delete[$while] = ($delete[$while] == 1) ? 1 : 0;

    if($delete[$while] == 1)
#{query("DELETE FROM $forumzDB.forumz_subscribe WHERE tid = '$tid[$while]' AND uid = '$uid'");}
    {echo "delete thread $tidu, $delete[$while]<br />";}
     
    elseif($tidu != 1 || $tidu == '')
    {echo "updated $tidu, set email to $email[$while]<br />";}
#query("UPDATE $forumzDB.forumz_subscribe SET email = '$email[$while]' WHERE tid = '$tidu' AND uid = '$uid'");}
}#end while
}#end if

#success_box('Subscriptions updated.', "$HTTP_REFERER");
#header("Location: ".$forum_properties['base_url']."/edit_subscriptions");

     
?>
     
0
Comment
Question by:azz0r
8 Comments
 
LVL 3

Expert Comment

by:designbai
ID: 13849280
Question is little bit unclear.

What i understood is,

there is 2 options for forum subscriber, 1 is to set the email subscription, another is to delete from the subscription.

As for my understanding i did the following.

I have changed little bitin teh form & coding.

<?php

if(!$_POST['update'])
{ ?> <form action="" name="caf" method="post">
<table width="40%" cellspacing="1" cellpadding="4" class="table">
    <tr>
      <td class="headbars">-</td>
      <td width="5%" class="headbar2">Email</td>
      <td width="4%" class="headbar2" align="center">Delete</td>
    </tr><tr><td class="cell">5</td>

<td width="5%" class="cell"><input name="email[]" type="checkbox" value="5" checked="checked" />1</td>
      <td width="4%" class="cell" align="center">
        <input name="delete[]" type="checkbox" id="delete[]" value="5" />
        </td>
    </tr><tr><td class="cell">10</td>
     
      <td width="5%" class="cell">
          <input name="email[]" type="checkbox" value="10" checked="checked" />1 </div></td>
      <td width="4%" class="cell" align="center">
        <input name="delete[]" type="checkbox" id="delete[]" value="10" />

</td></tr></table>
<input name="update" class="button" type="submit" value="Update Subscriptions" /></form><? }
     
    else
     
    {
    $tid = $_POST['tid'];
    $email = $_POST['email'];
    $delete = $_POST['delete'];
    $while = (count($_POST['tid']));
     
     
    print_r($_POST); echo '<br /><br />';
      
      // for subscription updation
      for ($i=0; $i<sizeof($email); $i++) {
            #query("UPDATE $forumzDB.forumz_subscribe SET email = '$email[$i]' WHERE tid = '$email[$i]' AND uid = '$uid'");
            echo "updated $email[$i]<br>";
      }
      
                // subscription deletion
                for ($k=0; $k<sizeof($delete); $k++) {
            #query("DELETE FROM $forumzDB.forumz_subscribe WHERE tid = '$delete[$k]' AND uid = '$uid'");
            echo "deleted $delete[$k]<br>";
      }
}
#success_box('Subscriptions updated.', "$HTTP_REFERER");
#header("Location: ".$forum_properties['base_url']."/edit_subscriptions");

     
?>

hope this helps.
0
 

Author Comment

by:azz0r
ID: 13851015
Sorry I didnt explain very clearly.

Lets forget Delete for a minute. Say the user ticked 10 to have email but didnt tick 5, with my code it would update 10 for some reason instead of 5.

The problems with your code; If 5 is ticked to email then it should == 1 on the update, but with your code it equals 5 (enum field with 0 and 1).
0
 
LVL 3

Expert Comment

by:designbai
ID: 13852621
as you said email field is enum field

try this

// for subscription updation
     for ($i=0; $i<sizeof($email); $i++) {
          #query("UPDATE $forumzDB.forumz_subscribe SET email = 1 WHERE tid = '$email[$i]' AND uid = '$uid'");
          echo "updated $email[$i]<br>";
     }

hope this helps.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:azz0r
ID: 13853613
Ah I see, problem is, if the email is unticked then the field needs to be set to 0, also theres no need to update if the rows going to be deleted.

I used to have two seperate buttons for this, it was so much simpler then, but less user friendly
0
 

Author Comment

by:azz0r
ID: 13885138
Bump :x
0
 

Author Comment

by:azz0r
ID: 13900348
:(
0
 
LVL 14

Expert Comment

by:huji
ID: 15990310
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
PAQ with the points refunded.

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Huji
EE Cleanup Volunteer
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 16018319
PAQed with points (125) refunded

DarthMod
Community Support Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question