Clinton Posted May 21, 2013 Share Posted May 21, 2013 Okay, I've looked at WHILE and FOREACH and can't seem to figure this out. This code works fine. It finds all the people who have signed up to participate in an event. <? $result3 = mysql_query("SELECT * FROM log WHERE cid='$cid'"); $num_rows = mysql_num_rows($result3); $seatsleft = $seats - $num_rows; ?> <font size="5"><b>Event: <? echo $ename; ?> Date: <? echo $date; ?></b></font> <form action="testle2.php" name="forward" method="post"> <table width="85%" border="1" cellspacing="0" cellpadding="0"> <tr> <td class="title"><u>NAME</u></td> <td class="title"><u>ATTENDED</u> </td> </tr> <? $sql = "SELECT FirstName, LastName, id FROM users WHERE id IN (SELECT uid from participantlog WHERE cid = '$cid') ORDER BY LastName"; $result=mysql_query($sql); while($row = mysql_fetch_array($result)) { $fname = $row['FirstName']; $lname = $row['LastName']; $id = $row['id']; ?> Now, directly underneath this code is the rest of the form. I want to select the individuals who attended and press the button and have a following page either update the database saying that they attended or did not attend. <tr> <td width="40%"><font size="5"><? echo $lname; ?>, <? echo $fname; ?> - <? echo $id; ?></font></td> <td width="60%"><input type="checkbox" name="uid" value="<? echo $id; ?>[]"><? if ($type != "nmo") { ?> Hours: <input type="text" name="hours" size="4"> <? }?></td> </tr> <? } ?> <input type="hidden" name="date" value="<? echo $date; ?>"> <input type="hidden" name="type" value="<? echo $type; ?>"> <input type="hidden" name="cid" value="<? echo $cid; ?>"> </table> <input type="submit" value="Complete & Delete"> </form> So it does everything correctly up to this point. If there are 5 individuals and I mark the checkbox that all 5 individuals attended and go to the next page, it only brings over one UID (the checkbox). I need to figure out how to correctly bring over all selected individuals and then loop it so that the DB updates for each. I've looked at both WHILE and FOREACH but none seem to do the trick. Any help appreciated. Thanks! Clinton Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/ Share on other sites More sharing options...
Solution Barand Posted May 21, 2013 Solution Share Posted May 21, 2013 Name all the checkboxes "uid[]" and give them values of the uid eg name="uid[]" value="1" name="uid[]" value="2" The checkboxes are then posted as an array. To update $idlist = join(',', array_map('intval', $_POST['uid'])); UPDATE table SET ya=da WHERE uid IN ($idlist); Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431454 Share on other sites More sharing options...
Clinton Posted May 22, 2013 Author Share Posted May 22, 2013 Thanks, Barand! Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431550 Share on other sites More sharing options...
Clinton Posted May 22, 2013 Author Share Posted May 22, 2013 Can I ask a follow-up question?? If it's a certain type of event, then this appears on the form: <? if ($type != "nmo") { ?> Hours: <input type="text" name="<? echo $id; ?>hours" size="4"> So if 4 people are generated and their ID's are 1, 2, 3, and 4, then the names become 1hours, 2hours, 3hours, and 4 hours. SO... When I hit submit and the UID is sent as an array and then the number of hours are also went, how can I break down the UID array on the processing page and append . 'hours' onto the end of it, so that I can update the database with the number of hours somebody worked? Does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431637 Share on other sites More sharing options...
Clinton Posted May 22, 2013 Author Share Posted May 22, 2013 I'd name them as name="hours[$id]" so you can match to checked checkboxes. You aren't appending "hours" the hours value when you store the table, are you? No, i'm not. I'll change that. Okay, so I'm not good with arrays. Doing some research it looks like that by doing that I'm creating an associative array, right? hours1 => "7", hours2 => "9", etc. So in the processing page, how do I extract the data so it can be put into the database? Do I have to get rid of the 'hours' to extract the $id? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431641 Share on other sites More sharing options...
Clinton Posted May 22, 2013 Author Share Posted May 22, 2013 (edited) So, so far this is all I've been able to do: On Form Page: Hours: <input type="text" name="hours[<? echo $id; ?>]" size="4"> Process with $array = $_POST['hours']; print_r($array); Gives me: Array ( [16] => 10 [23] => 20 ) So the right ID's are there (16 and 23) along with the associated hours (10 and 20)... but how do I extract so that I can say something like ... UPDATE form SET hours=$hours WHERE id=$id The biggest thing to remember is that I never know what or how many ID's there are going to be. It all depends on what user, and how many of them, sign up for a class. Edited May 22, 2013 by hunna03 Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431648 Share on other sites More sharing options...
Barand Posted May 22, 2013 Share Posted May 22, 2013 Have you not noticed the pattern? $hours[$id] is the hours value that goes with id = $id Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431666 Share on other sites More sharing options...
Clinton Posted May 22, 2013 Author Share Posted May 22, 2013 Yup. See the pattern. Just not getting how to extract the data. That's all. I'm no Guru. Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431682 Share on other sites More sharing options...
kicken Posted May 22, 2013 Share Posted May 22, 2013 foreach ($_POST['uid'] as $id){ $hours = $_POST['hours'][$id]; echo "User {$id} has hours {$hours}<br>"; } You use the ID to index into the hours array and grab the value. Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431684 Share on other sites More sharing options...
Barand Posted May 22, 2013 Share Posted May 22, 2013 Yup. See the pattern. Just not getting how to extract the data. That's all. I'm no Guru. Earlier today you sent me a PM containing I apparently had a conversation with you in 2007 I thought that after six years you might be able handle an array Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431688 Share on other sites More sharing options...
Clinton Posted May 22, 2013 Author Share Posted May 22, 2013 Barand, I greatly appreciate the help you provide here but "What's your problem (run out of coffee or cigarettes?)" <-- You said that back in 2007. I understand you're in your late 60's and probably hate the world but there's no need to be a condescending jerk. Either help or keep your smuggish comments to yourself. Perhaps consider retiring? I mean, if anybody asks you for anything above and beyond the basics you jump down their throat. Nobody wants that. PHP isn't my full time job. It's not even a part-time job. I have an understanding of the basics but have really never dabbled in array's, let's call that a weakness of mine (most people have them). I have always, though, tried to research the answer before coming to the forums and, even when an answer is provided, try to understand it further before posting additional posts. I understand the patterns, I understand that array's hold the data, I just simply don't understand how I can unpackage this data from the box. It may be simple and stupid to you so I apologize if it stoops below your ability to help me. So next time, don't. Again, you're a wealth of knowledge and I greatly appreciate what you have provided, but leave the rest of the comments out. Help or don't. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431696 Share on other sites More sharing options...
TOA Posted May 22, 2013 Share Posted May 22, 2013 [rant] Beggars can't be choosers. If you ask for help, you have to accept the criticisms also. Sorry to jump in on something that is not my business, but I truly get tired of reading posts like this. Nothing personal hunna03, you sound like you can put a thought together. I just get tired of reading how the help people received wasn't what they wanted to hear. Tough. And no I haven't run out of cigarette's. I'll butt out now. [/rant] Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431720 Share on other sites More sharing options...
Jessica Posted May 22, 2013 Share Posted May 22, 2013 Dude, Barand is the nice one. If you can't handle him you're in trouble. Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431721 Share on other sites More sharing options...
Barand Posted May 22, 2013 Share Posted May 22, 2013 if anybody asks you for anything above and beyond the basics you jump down their throat. Nobody wants that. Sorry if I upset you but I consider arrays to be one of the "basics" . I thought it was reasonable to assume that someone who has been a member of this forum for six years would have a grasp of them. You say you haven't dabbled in arrays - have you never used $_POST or $_GET or mysql_fetch_xxx()? Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431722 Share on other sites More sharing options...
Clinton Posted May 23, 2013 Author Share Posted May 23, 2013 Rant away, TOA. I understand. Jessica, it's more of a pattern than anything else. You've always been fantastic! Barand, I understand what you're saying. This isn't about feelings and I get that arrays are basic. Yes, I have used POSTs and GETs before. :-) Here's what I know, so normally the array would be something like, name="hours[]". Then we could cycle through the arrays using something like: $hours = $_POST["hours"]; $arrlength=count($hours); for($x=0;$x<$arrlength;$x++) { echo $hours[$x]; echo "<br>"; } Which should give us something like 10d, 20, etc. if I put 10 hours in for one person, 20 hours for the next, etc. Now, instead of "hours[]" it is "hours[<? echo $id; ?>]" . So not only have I thrown a variable in there I've never seen/used I have no idea how to get it to do what I want. I'll keep scouring the books and interweb. CJA Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431948 Share on other sites More sharing options...
Jessica Posted May 23, 2013 Share Posted May 23, 2013 Use foreach Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431949 Share on other sites More sharing options...
Clinton Posted May 23, 2013 Author Share Posted May 23, 2013 Thanks, Jessica. Getting closer. Hours: <input type="text" name="hours[<? echo $id; ?>]" size="4"> So on my form I enter 10 for one individual and 20 for another. When I run the foreach it echos the 10 and 20 correctly. But I still don't understand how to pull out the $id and associate it with hours (10 or 20). This way I can use it to update a DB. Does that make any more sense or am I just spinning my wheels missing something completely obvious? Thanks! foreach ($_POST['hours'] as $value) { echo $value . "<br>"; } CJA Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431958 Share on other sites More sharing options...
Jessica Posted May 23, 2013 Share Posted May 23, 2013 Yes, you are foreach($arr AS $key => $val) Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431959 Share on other sites More sharing options...
Clinton Posted May 23, 2013 Author Share Posted May 23, 2013 LoL. Yes, I was. It's always the simple things. Thanks, Jessica AND Barand! Quote Link to comment https://forums.phpfreaks.com/topic/278252-while-loop-for-attendance-roster/#findComment-1431967 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.