BarneyJoe Posted December 7, 2006 Share Posted December 7, 2006 This is something I've been trying to crack.I have a table, photos, PK Photo_ID, a table keywords, PK Keyword_ID, and a table photokeywords, with Photo_ID and Keyword_ID as integers.The application is to store info on photos in various text fields in the photos table, and assign various descriptive keywords to each photo.To add a new record, I have an input form for the photos table, which then goes to a page full of checkboxes that people can use to select the keywords they want.So far so good.The part I've come unstuck at is the page whereby people might want to edit the keywords selected for a particular photo.So I have a photo details page that pulls together the content from the photos table, as well as the matching keywords from the photokeywords table, and a link to another page full of keyword checkboxes.The trick is to get this page to have the current keyword checkboxes checked.I have [code]$Photo_ID = intval($_GET['Photo_ID']);[/code] to match the Photo_ID, but can't get it to match the keywords.I initially had :[code]<input <?php if (!(strcmp($row_Recordset1['Keyword_ID'],1))) {echo "checked";} ?> name="ckbox[2]" type="checkbox" class="tickbox2" id="ckbox[2]">[/code]for each checkbox, but this was only checking the first matching checkbox.So since then I've been trying to get it to work with an array, and have gotten as far as :[code]<?php$Photo_ID = 1; if (isset($_GET['Photo_ID'])) { $Photo_ID = intval($_GET['Photo_ID']); } mysql_select_db($database_Photolibrary, $Photolibrary); $query_Keyword_Match = sprintf("SELECT * FROM photokeywords WHERE Photo_ID = %s", $Photo_ID); $Keyword_Match = mysql_query($query_Keyword_Match, $Photolibrary) or die(mysql_error()); $photokeywords = array(); while ($row_Keyword_Match = mysql_fetch_assoc($Keyword_Match)) { $photokeywords[] = $row_Keyword_Match['Keyword_ID']; } $totalRows_Keyword_Match = mysql_num_rows($Keyword_Match); $sql = "SELECT * FROM Keywords ORDER BY Keyword_ID"; $query = mysql_query($sql); while ($result=mysql_fetch_assoc($query)) { echo('<td><input '); if (in_array($result['Keyword_ID'],$photokeywords)) { echo "checked "; } echo('name="ckbox['.$result['Keyword_ID'].']" type="checkbox" class="tickbox2" id="ckbox['.$result['Keyword_ID'].']"></td>'); echo('<td>'.$result['Keyword'].'</td>'); } ?>[/code]Which seems to work, except that the checkboxes just appear as an unstyled, unorganised rows at the top of my page.Is it possible from here to control where each checkbox appears on the page, ideally as I had it originally with each checkbox positioned in a table, eg :[code]<tr bgcolor="#E9FEEA"><td valign="middle"><input name="ckbox[12]" type="checkbox" class="tickbox2" id="ckbox[12]"></td><td>Golf Course </td><td valign="middle"><input name="ckbox[13]" type="checkbox" class="tickbox2" id="ckbox[13]"></td><td>Historic House </td><td valign="middle"><input name="ckbox[14]" type="checkbox" class="tickbox2" id="ckbox[14]"></td><td>Industrial Building </td><td valign="middle"><input name="ckbox[15]" type="checkbox" class="tickbox2" id="ckbox[15]"></td><td>Lighthouse </td><td><input name="ckbox[16]" type="checkbox" class="tickbox2" id="ckbox[16]"></td><td>Model Village</td></tr>[/code]Using whatever code to display the checkboxes dynamically, eg something along the lines of :[code]<input <?php if (in_array(2,$photokeywords)) ?> name="ckbox[2]" type="checkbox" class="tickbox2" id="ckbox[2]"><input <?php if (in_array(3,$photokeywords)) ?> name="ckbox[3]" type="checkbox" class="tickbox2" id="ckbox[3]">etc[/code]So if you have for example in the photokeywords table144, 2144, 3etcThen checkboxes 2 and 3 etc would appear as checked, wherever they were placed on the page?Not sure how well I've explained that, but hopefully it makes sense to someone!Cheers,Iain Link to comment https://forums.phpfreaks.com/topic/29787-help-with-checkboxes-and-arrays-getting-into-a-tabular-form/ Share on other sites More sharing options...
BarneyJoe Posted December 7, 2006 Author Share Posted December 7, 2006 I think I'm getting there. I now have :[code]<table width="954px" border="0" cellspacing="0" cellpadding="0" align="center"><tr><span class="style26">Photo ID : </span><span class="style27"><?php echo $Photo_ID; ?></span></tr> <form name="form1" method="POST" action="photoKeywordsEdited.php"><input type='hidden' name='Photo_ID' value='<?php echo $Photo_ID; ?>'><?php require_once('Connections/Photolibrary.php');error_reporting(E_ALL & E_STRICT);$Photo_ID = 1; if (isset($_GET['Photo_ID'])) { $Photo_ID = intval($_GET['Photo_ID']); } mysql_select_db($database_Photolibrary, $Photolibrary); $query_Keyword_Match = sprintf("SELECT * FROM photokeywords WHERE Photo_ID = %s", $Photo_ID); $Keyword_Match = mysql_query($query_Keyword_Match, $Photolibrary) or die(mysql_error()); $photokeywords = array(); while ($row_Keyword_Match = mysql_fetch_assoc($Keyword_Match)) { $photokeywords[] = $row_Keyword_Match['Keyword_ID']; } $totalRows_Keyword_Match = mysql_num_rows($Keyword_Match); $sql = "SELECT * FROM Keywords ORDER BY Keyword_ID";$keywordArray = array(); $query = mysql_query($sql); while ($result=mysql_fetch_assoc($query)) { $keywordArray[$result['Category']][] = $result; } foreach ($keywordArray as $categoryData) { echo('<tr>'); foreach ($categoryData as $keywordData) { echo('<td><input '); if (in_array($keywordData['Keyword_ID'],$photokeywords)) { echo "checked "; } echo('name="ckbox['.$keywordData['Keyword_ID'].']" type="checkbox" class="tickbox2" id="ckbox['.$keywordData['Keyword_ID'].']"></td>'); echo('<td>'.$keywordData['Keyword'].'</td>'); } echo('</tr>'); } ?> <tr><input type="submit" name="Submit" value="Update Photo Keywords"> <input type="reset" name="Reset" value="Reset form"> </form> </tr> </table>[/code]Which is displaying the correct checkboxes, and is correctly updating the new selection using my page that's processing the changes.The only thing is that the checkboxes are breaking out of my wrapper <div>, forcing every keyword in each category to take up an entire row.Any suggestions as to how I can keep it within the table width?Cheers. Link to comment https://forums.phpfreaks.com/topic/29787-help-with-checkboxes-and-arrays-getting-into-a-tabular-form/#findComment-136864 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.