Copy link to clipboard
Copied
I am trying to build a dynamic check box with the data determining the checked value coming from a linked table.
The tables are:
music
music_id
category
cat_id
cat_name
MusicCategory --> Linked table
music_id
category_id
The query below is displaying all the checkboxes but is only "checking" one item (the first in the array).
Any help would be appreciated!
thanks.
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_tempscoremusic_dev, $tempscoremusic_dev);
$query_categories = "SELECT cat_id, cat_name, cat_weight FROM category ORDER BY cat_weight ASC";
$categories = mysql_query($query_categories, $tempscoremusic_dev) or die(mysql_error());
$row_categories = mysql_fetch_assoc($categories);
$totalRows_categories = mysql_num_rows($categories);
$colname_MusicCategory = "-1";
if (isset($_GET['music_id'])) {
$colname_MusicCategory = $_GET['music_id'];
}
mysql_select_db($database_tempscoremusic_dev, $tempscoremusic_dev);
$query_MusicCategory = sprintf("SELECT * FROM MusicCategory WHERE music_id = %s", GetSQLValueString($colname_MusicCategory, "int"));
$MusicCategory = mysql_query($query_MusicCategory, $tempscoremusic_dev) or die(mysql_error());
$row_MusicCategory = mysql_fetch_assoc($MusicCategory);
$totalRows_MusicCategory = mysql_num_rows($MusicCategory);
?>
Copy link to clipboard
Copied
Here is the page code:
<?php do { ?>
<input <?php if (!(strcmp($row_categories['cat_id'],$row_MusicCategory['category_id']))) {echo "checked=\"checked\"";} ?> name="category" type="checkbox" value="" /> <?php echo $row_categories['cat_name']; ?>
<?php } while ($row_categories = mysql_fetch_assoc($categories)); ?>
Copy link to clipboard
Copied
Never mind... setup a left join in the SQL query and its working.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now