Jump to content

Can anyone solve my problem? checkbox hell!


spires

Recommended Posts

Help please.

No one can so far can come up with a solution.

All i'm trying to do is have a list of categories and sub cats. (they can be add to and deleted)
That are taken from the database, with a checkbox next to each one.
(This is as far as i've got)
http://theeventorganiser.com/reg_more_cats.php?id=6

Then insert into the database the checked checkboxs. (This is the problem)
The problem is i need to loop through the sub cats from the database which also means each checkbox is within the loop. So, i need to find a way of renaming the checkbox to the value of the sub cat for each loop. at the moment i only have 1 as the value.

I now i'm doing something very wrong, just dont know what.

Any help please.
Here is my messy code.

Head
[code]
<?php
$user_id = $_GET['id'];

if (!empty($_POST['mcsubmit'])) {
if(isset($_POST['1'])) {

$id = $_POST['id'];
$sub = $_POST['sub'];

$ssql2 = "SELECT * FROM categories";
$sresult2 = mysql_query($ssql2) or die ('could not connect');
$scount2 = mysql_num_rows($sresult2);
$srow2 = mysql_fetch_array($sresult2) ;
$category = $srow2['cat_id'];
echo $category;



$doquery = mysql_query("INSERT INTO users_sub (user_id, user_sub) VALUES ('$user_id', '$checked')") or die ("could not insert");
$doquery1 = mysql_query("INSERT INTO users_cat (user_id, user_cat) VALUES ('$user_id', '$category')") or die ("could not insert1");
$doquery2 = mysql_query("INSERT INTO users_categories (user_id, user_cats, user_subs) VALUES ('$user_id', '$category', '$checked')") or die ("could not insert2");

}
}
?>
[/code]

Form
[code]
<form name="more_cats" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">

<?php

$csql = "SELECT * FROM category";
$cresult = mysql_query($csql) or die ('could not connect');
$ccount = mysql_num_rows($cresult);

echo '<center>
<table cellpadding="5" cellspacing="5" border="0" width="600">';

for ($j = 0; $j < $ccount; $j++) {
echo '<tr>';
for ($i = 0; $i < 4; $i++) {
$crow = mysql_fetch_array($cresult);
$cat = $crow['cat'];
$id = $crow['id'];
if ($crow) {
 echo '<td valign="top">
  <table cellpadding="0" cellspacing="0" border="0" width="150" class="TLRB_border" bgcolor="#FFFFFF" valign="top">
<tr>
<td align="center" bgcolor="#EEEEEE" colspan="2">
'.$cat.'
</td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" border="0" width="150" class="TLRB_border" bgcolor="#FFFFFF" valign="top">';


$ssql = "SELECT * FROM cats_sub INNER JOIN categories ON cats_sub.id = categories.id WHERE categories.cat_id = '$id'";
$sresult = mysql_query($ssql) or die ('could not connect');
$scount = mysql_num_rows($sresult);

while ($srow = mysql_fetch_array($sresult)) {
$currentsub = $srow['sub'];
echo '<tr>
<td>
&nbsp;&nbsp;'.$currentsub.'
</td>
<td>
&nbsp;&nbsp;&nbsp;<input name="1" type="checkbox" id="1"><br>
</td>
  </tr>';
echo '<INPUT type="hidden" name="id" value="'.$srow['id'].'">
<INPUT type="hidden" name="sub" value="'.$srow['sub'].'">';
}



 echo '</table><br><br>
</td>';
} else {
break;
}
      }

echo'</tr>';

  }
  echo '</table>';
?>
<input type="submit" name="mcsubmit" value="submit">
</form>
[/code]
Link to comment
Share on other sites

try[code]
while ($srow = mysql_fetch_array($sresult)) {
$currentsub = $srow['sub'];
echo '<tr>
<td>&nbsp;&nbsp;'.$currentsub.'</td>
<td>&nbsp;&nbsp;&nbsp;<input name="catsub[]" type="checkbox" id="1" value="'
        .$cat.'$$$'.$currentsub. // or use $crow['id].'$$$'.$srov['id']
        '"><br>
</td>
</tr>';
//echo '<INPUT type="hidden" name="id" value="'.$srow['id'].'">
//<INPUT type="hidden" name="sub" value="'.$srow['sub'].'">';
}[/code]an i head [code]$user_id = $_GET['id'];

if (!empty($_POST['mcsubmit'])) {
if(isset($_POST['catsub'])) {
foreach($_POST['catsub'] as $catsub) {
$cs = explode('$$$',$catsub);
$category = $cs[0];
$checked = $cs[1];
$ssql2 = "SELECT * FROM categories";
$sresult2 = mysql_query($ssql2) or die ('could not connect');
$scount2 = mysql_num_rows($sresult2);
$srow2 = mysql_fetch_array($sresult2) ;
$category = $srow2['cat_id'];
echo $category;



$doquery = mysql_query("INSERT INTO users_sub (user_id, user_sub) VALUES ('$user_id', '$checked')") or die ("could not insert");
$doquery1 = mysql_query("INSERT INTO users_cat (user_id, user_cat) VALUES ('$user_id', '$category')") or die ("could not insert1");
$doquery2 = mysql_query("INSERT INTO users_categories (user_id, user_cats, user_subs) VALUES ('$user_id', '$category', '$checked')") or die ("could not insert2");
}
}
}
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.