Jump to content

WHILE / LOOP for Attendance Roster


Clinton
Go to solution Solved by Barand,

Recommended Posts

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

 

 

 

Link to comment
Share on other sites

  • Solution

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);
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by hunna03
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

[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]

Link to comment
Share on other sites

 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()?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.