Hi, I'm having a bit of a problem making my checkboxes checked and unchecked depending if the variable $checked is set to 1. I'm using a relational database with three tables.
table1
id (pk)
etc
table2
genre_id (pk)
genre
table3
sgid (pk)
id
genre_id
I know I'm missing something. I'm a bit of a newbie, so I'll post the code and hope someone can help me. The first code displays only the genre checkboxes:
<?
include("admin/cfg.php");
$id = $_GET['id'];
$query = "SELECT * FROM anime_genre";
$result = mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() );
/* function groupGenre($genre) {
$id = $_GET['id'];
$genreshow = "SELECT ax.*, ag.* FROM animex ax INNER JOIN anime_sgenre asg USING (id) INNER JOIN anime_genre ag USING (genre_id) WHERE asg.id = '$_GET[id]'";
$result = mysql_query($genreshow) or die ("Error in query: $query. " . mysql_error());
if (mysql_num_rows($result) > 0)
{
// iterate through resultset
// print article titles
while($row = mysql_fetch_object($result))
{
echo "<input type=\"checkbox\" name=\"$genre\" value=\"$genre_id\" checked=\"checked\"> $genre:";
if(isset($_POST['genre_id']))
{
echo 'checked';
}
}
}
} */
while($row = mysql_fetch_array($result)){
$genre = $row["genre"];
$genre_id = $row["genre_id"];
$checked = $row['checked'];
if ($checked == 1)
{
echo "<input type=\"checkbox\" name=\"$genre\" value=\"$genre_id\" checked> $genre";
}
if ($checked == 0)
{
echo "<input type=\"checkbox\" name=\"$genre\" value=\"$genre_id\"> $genre";
}
echo "<br>";
}
?>
This second set of code displays everything plus a couple of checked boxes:
<?
include("admin/cfg.php");
$id = $_GET['id'];
$query = "SELECT * FROM anime_genre, anime_sgenre";
$result = mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() );
/* function groupGenre($genre) {
$id = $_GET['id'];
$genreshow = "SELECT ax.*, ag.* FROM animex ax INNER JOIN anime_sgenre asg USING (id) INNER JOIN anime_genre ag USING (genre_id) WHERE asg.id = '$_GET[id]'";
$result = mysql_query($genreshow) or die ("Error in query: $query. " . mysql_error());
if (mysql_num_rows($result) > 0)
{
// iterate through resultset
// print article titles
while($row = mysql_fetch_object($result))
{
echo "<input type=\"checkbox\" name=\"$genre\" value=\"$genre_id\" checked=\"checked\"> $genre:";
if(isset($_POST['genre_id']))
{
echo 'checked';
}
}
}
} */
while($row = mysql_fetch_array($result)){
$genre = $row["genre"];
$genre_id = $row["genre_id"];
$checked = $row['checked'];
if ($checked == 1)
{
echo "<input type=\"checkbox\" name=\"$genre\" value=\"$genre_id\" checked> $genre";
}
if ($checked == 0)
{
echo "<input type=\"checkbox\" name=\"$genre\" value=\"$genre_id\"> $genre";
}
echo "<br>";
}
?>
And last, this set of code displays kind of what I want, but I'd like all the genres to be present as well as the checked ones that belong to that id.
<?
include("admin/cfg.php");
$id = $_GET['id'];
$query ="SELECT * FROM anime_genre INNER JOIN anime_sgenre USING (genre_id) WHERE anime_sgenre.id = $id";
$result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() );
/* function groupGenre($genre) {
$id = $_GET['id'];
$genreshow = "SELECT ax.*, ag.* FROM animex ax INNER JOIN anime_sgenre asg USING (id) INNER JOIN anime_genre ag USING (genre_id) WHERE asg.id = '$_GET[id]'";
$result = mysql_query($genreshow) or die ("Error in query: $query. " . mysql_error());
if (mysql_num_rows($result) > 0)
{
// iterate through resultset
// print article titles
while($row = mysql_fetch_object($result))
{
echo "<input type=\"checkbox\" name=\"$genre\" value=\"$genre_id\" checked=\"checked\"> $genre:";
if(isset($_POST['genre_id']))
{
echo 'checked';
}
}
}
} */
while($row=mysql_fetch_array($result)){
$genre = $row["genre"];
$genre_id = $row["genre_id"];
$checked = $row['checked'];
if ($checked == 1)
{
echo "<input type=\"checkbox\" name=\"$genre\" value=\"$genre_id\" checked> $genre";
}
if ($checked == 0)
{
echo "<input type=\"checkbox\" name=\"$genre\" value=\"$genre_id\"> $genre";
}
echo "<br>";
}
?>
I hope someone can help point me in the right direction. Thanks!