I have a table structure :
photos : Photo_ID (PK), Title, Orientation etc
keywords : Keyword_ID, Keyword, Category
photokeywords : Photo_ID, Keyword_ID
I originally used hard coded checkboxes that people could use to enter keywords for each photo, but with some help managed to get started with arrays. I actually have it working where the keywords can be updated using an array of checkboxes, using the following code :
<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;
}
$current_category = '';
foreach ($keywordArray as $categoryData)
{
if($current_category <> $categoryData[2]['Category'])
{
//open a new <tr>
echo "<tr class=\"categorycell\">\n";
//print the category as a header
echo "<td colspan=\"10\">". $categoryData[2]['Category']."</td>\n";
//close the <tr>
echo "</tr>\n";
$current_category = $categoryData[2]['Category'];
}
$row_count = 0;
//even row
if($row_count%2==0)
{
$row_type = 'even';
}
//odd row
else
{
$row_type = 'odd';
}
echo("<tr class=\"".$row_type."\">");
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>');
if ($counter==4) {
echo('</tr><tr>');
$counter = 0;
} else {
$counter++;
}
}
echo('</tr>');
$row_count++;
}
?>
I'm having trouble modifying it for my original Add Keywords page - basically there's a previous page that collects the photo info for the photos table, then the user is directed to the add keywords page (so previously the Photo_ID) was passed through as a hidden field :
<input type='hidden' name='Photo_ID' value='<!--FIELDVALUE:Photo_ID-->'>
As I'm still a bit unfamiliar with the syntax for arrays, can anyone help out with modifying the code above for editing the keywords, to just adding them?
Hope that makes sense.
Cheers,
Iain