Skip to main content
Participant
November 9, 2010
Question

Checkbox array from linked table

  • November 9, 2010
  • 2 replies
  • 276 views

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);
?>
This topic has been closed for replies.

2 replies

qloops65Author
Participant
November 9, 2010

Never mind... setup a left join in the SQL query and its working.

qloops65Author
Participant
November 9, 2010

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