Jump to content

prechecking table generated boxes, using 2nd table in db.


turpentyne

Recommended Posts

Not sure how to do this at all... I'm creating a page with a form to edit existing info in two tables in the database. I need to pre-check some checkboxes and I don't know how. The first query query_selectguest below puts data into the form just fine. query_checked looks in the second table for any rows that match the discount id. Then further down in the form, I have a php snippet that pulls all the classes from a third database. As I'm echoing these out to the page, I want them to be pre-checked if they match a result found in $result_checked

 

here's what I have right now...

 

$query_selectguest = 'SELECT * FROM tbl_discount WHERE discount_id='.$passedID;
$result_guest = mysql_query($query_selectguest);
$g_row = mysql_fetch_array($result_guest);

$query_checked = 'SELECT * FROM active_discounts WHERE disc_id='.$passedID;
$result_checked = mysql_query($query_checked);
$h_row = mysql_fetch_array($result_checked);

?>

<form name="form1" method="post" action="update_discount.php" onSubmit="return validate_form()">

<input name="discount_name" type="text" class="input" id="subject" size="50" maxlength="100" value="<? print $g_row['discount_name'] ?>">
<input name="discount_amount" type="text" class="input" id="subject" size="5" maxlength="3" value="<? print $g_row['discount_amount'] ?>">

<!-- here's what I'm concerned with -->

This discount applies to these classes:<br>
          
           <?php
$quer4=mysql_query("SELECT workshop_id, workshop_title FROM tbl_workshops order by workshop_title");

while($row4 = mysql_fetch_array($quer4)) {
echo "<input type='checkbox' name='workshop_link_1[]' value='".$row4[workshop_id]."'>".$row4[workshop_title]."<BR>";
}

?>

Think I've got my head round your checkbox conundrum.

 

Whilst there are probably more efficient ways of achieving what you want, the simple way is to put all the IDs contained in $result_checked into an array.

Then, in your loop, have something like

while($row4 = mysql_fetch_array($quer4)) {
$checked = (in_array($row4[workshop_id], $myArray)) ? ' checked="yes"' : '';
echo "<input type='checkbox' name='workshop_link_1[]' value='".$row4[workshop_id]."'".$checked.">".$row4[workshop_title]."<BR>";
}

 

or something like that, idk if that helps at all.

 

- mark.

Looks like what I want..

 

I'm trying to create the array right now, but it seems to be empty? here's what I've got:

 

$query_checked = 'SELECT * FROM active_discounts WHERE disc_id='.$passedID;
@$result_checked = mysql_query($query_checked);
@$numRows_checked = mysql_num_rows($result_checked);
$h_row = mysql_fetch_array($result_checked);


$myArray = array();
while ($h_row = mysql_fetch_array($result_checked)) {
$myArray[] = $h_row['class_id'];
}

print_r($myArray);

//when I print out i just get array () with nothing in it... hmmmm. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.