jkatcherny Posted January 18, 2010 Share Posted January 18, 2010 Hi everyone, I'm working on allowing a user to update previous information she submitted. One portion is to allow the user to change which performers performed in a given track. I am populating the list of performers pulled from the Performer_Info table. Each name has a checkbox by it, and during the update process, I would like to allow the user to see which of the performer names they had previously checked. Everything is working fine - but I'm not seeing the checkboxes that were previously checked - the whole list just comes up as unchecked. Any help would be so appreciated, thank you. -- This pulls the performer and track IDs from the Multi_Performer table. That table allows multiple performers to be associated with a single track i.e. track id performerid 1 3 1 2 1 4 1 5 // POPULATE MULTIPLE PERFORMERS LIST $perfSql="SELECT trackid, performerid FROM Multi_Performer WHERE trackid ='".$_SESSION['updateTrackId']."'"; $perfResult=mysql_query($perfSql); while ($row=mysql_fetch_array($perfResult)) { $_SESSION['trackid']=$row['trackid']; $_SESSION['multiperformerid']=$row['performerid']; } - - The code below populates the checkbox list. $sortby = ($_GET['sortby'] == 'perfLast')? $_GET['sortby'] : 'perfUnion'; $perfSql="SELECT * FROM Performer_Info ORDER BY perfLast"; $perfResult=mysql_query($perfSql); echo "<center><div style='height:210;width:350px;overflow:scroll;'>"; echo "<table width='100%' cellpadding=1 cellspacing=1 border=1 class=style5><tr>"; echo "<td class=style4><font class='colTitle'>Select</font></td>"; echo "<td class=style4><font class='colTitle'>Performer Name</font></td>"; echo "</tr>"; echo "<form method='POST' action='multi_performer_process.php' onSubmit='return checkrequired(this)'>"; while ($row=mysql_fetch_array($perfResult)) { $_SESSION['performerid'] = $row['performerid']; $_SESSION['perfFirst'] = $row['perfFirst']; $_SESSION['perfLast'] = $row['perfLast']; $_SESSION['perfInst'] = $row['perfInst']; $_SESSION['perfUnion'] = $row['perfUnion']; if ($counter % 2) { echo "<tr bgcolor='#AFBD22'><td>"; } else { echo "<tr bgcolor='#B1BE52'><td>"; } echo "<input type='checkbox' name='requiredperformerid[]' value='".$_SESSION['performerid']."' $selected></td><td><a href='performer_info.php?title=".$_SESSION['performerid']."' class='smallScroll' target='_new'>".$_SESSION['perfFirst']." ".$_SESSION['perfLast']."</a></td><td>"; $counter++; } - - Here what I'm trying to do is compare the multiperformerid I just pulled from the Multi_Performer table to the performerid populated from the Performer_Info table above. If they match, I want the checkbox to show up at as 'checked'. // GENERATE SELECTED CHECKBOXES $selected = ($_SESSION['multiperformerid'] == $_SESSION['performerid']) ? ' checked="checked"' : ''; Quote Link to comment https://forums.phpfreaks.com/topic/188905-recall-which-checkboxes-were-checked-during-update/ Share on other sites More sharing options...
kickstart Posted January 18, 2010 Share Posted January 18, 2010 Hi You appear to be trying to store a list of performers in a string (although you will overwrite each time so only keep the last one). I think you want to use an array. However I also suspect that you would be best off using a single piece of SQL with a JOIN. Something vague like this:- <?php $sortby = ($_GET['sortby'] == 'perfLast')? $_GET['sortby'] : 'perfUnion'; $perfSql="SELECT a.performerid, a.performerid AS multiperformerid, a.perfFirst, a.perfLast, a.perfInst, a.perfUnion FROM Performer_Info a LEFT OUTER JOIN Multi_Performer b ON a.performerid = b.performerid AND trackid ='".$_SESSION['updateTrackId']."' ORDER BY perfLast"; $perfResult=mysql_query($perfSql); echo "<center><div style='height:210;width:350px;overflow:scroll;'>"; echo "<table width='100%' cellpadding=1 cellspacing=1 border=1 class=style5><tr>"; echo "<td class=style4><font class='colTitle'>Select</font></td>"; echo "<td class=style4><font class='colTitle'>Performer Name</font></td>"; echo "</tr>"; echo "<form method='POST' action='multi_performer_process.php' onSubmit='return checkrequired(this)'>"; while ($row=mysql_fetch_array($perfResult)) { echo "<tr bgcolor='".(($counter % 2) ? '#AFBD22':'#B1BE52')."'><td>"; echo "<input type='checkbox' name='requiredperformerid[]' value='".$row['performerid']."' ".(($row['multiperformerid'] == $row['performerid']) ? ' checked="checked"' : '')."></td><td><a href='performer_info.php?title=".$row['performerid']."' class='smallScroll' target='_new'>".$row['perfFirst']." ".$row['perfLast']."</a></td><td>"; $counter++; } ?> (simplified by taking out the SESSION variables) All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/188905-recall-which-checkboxes-were-checked-during-update/#findComment-997386 Share on other sites More sharing options...
jkatcherny Posted January 18, 2010 Author Share Posted January 18, 2010 Hey Kickstart, thanks for your help! Yes I am storing them in an array called 'requiredperformerid[]'... is that what you meant? As far as putting data into the database and pulling it back, it's working just fine. What I'm struggling with is getting appropriate check boxes to be checked depending on whether or not they were initially checked by the user, during the update. That's what I was trying to tackle here: $selected = ($_SESSION['multiperformerid'] == $_SESSION['performerid']) ? ' checked="checked"' : ''; I used something similar for updates to drop down menus populated by the contents of the database (see below), and it works great. I was sort of hoping there was a similar method for check boxes? - - $compSql="SELECT companyid, companyName FROM Company_Info"; $compResult=mysql_query($compSql); $compOptions=""; while ($row=mysql_fetch_array($compResult)) { $companyid=$row['companyid']; $companyName=$row['companyName']; $selected = ($_SESSION['companyid'] == $companyid) ? ' selected="selected"' : ''; $compOptions .= '<option value="'.$companyid.'"'.$selected.'>'.$companyName; } Quote Link to comment https://forums.phpfreaks.com/topic/188905-recall-which-checkboxes-were-checked-during-update/#findComment-997490 Share on other sites More sharing options...
kickstart Posted January 18, 2010 Share Posted January 18, 2010 Hi Not quite. In your original code you have:- while ($row=mysql_fetch_array($perfResult)) { $_SESSION['trackid']=$row['trackid']; $_SESSION['multiperformerid']=$row['performerid']; } That will just keep overwriting $_SESSION['multiperformerid'] with the latest $row['performerid'], leaving you with only one that can ever match later on. Similarly:- $selected = ($_SESSION['multiperformerid'] == $_SESSION['performerid']) ? ' checked="checked"' : ''; only checks against a single multiperformerid. What you possibly want is $_SESSION['multiperformerid'] = array(); while ($row=mysql_fetch_array($perfResult)) { $_SESSION['trackid']=$row['trackid']; $_SESSION['multiperformerid'][]=$row['performerid']; } checked with :- $selected = ((in_array($_SESSION['performerid'],$_SESSION['multiperformerid'])) ? ' checked="checked"' : ''); However not sure why you are using session variable so often, and still suspect you would be better off using a single piece of SQL joining the 2 tables together. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/188905-recall-which-checkboxes-were-checked-during-update/#findComment-997502 Share on other sites More sharing options...
jkatcherny Posted January 18, 2010 Author Share Posted January 18, 2010 Ahh okay I see what you're saying. Great thank you, I'll test that out. Quote Link to comment https://forums.phpfreaks.com/topic/188905-recall-which-checkboxes-were-checked-during-update/#findComment-997567 Share on other sites More sharing options...
jkatcherny Posted January 18, 2010 Author Share Posted January 18, 2010 Kickstart, that worked like a charm. Thank you so much. Quote Link to comment https://forums.phpfreaks.com/topic/188905-recall-which-checkboxes-were-checked-during-update/#findComment-997596 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.