Page 1 of 1

Check Box Modify

Posted: Wed Dec 22, 2004 9:29 am
by Etherguy
Happy Holidays All.

I am having a hard time with my modify script using checkboxs. What I am trying to do is have checkboxs checked for the services the user already has, and list the rest of the unchecked boxes so they can be checked and unchecked if needed. Now I can get it display just fine with the already checked boxes, only problem is it repeats the remaining list as many times as the services that are checked. I know that my while and do loop is probably screwed up somewhere.. But after looking at it for a while.. I can not figure out where...

Any Help would be greatly appreciated!

Now The Code :

Code: Select all

<?php
$sql10=mysql_query("select techid,serviceid from cli_mapping where cid=$cid order by serviceid asc");
while ($arr=mysql_fetch_assoc($sql10)){
$sql1=mysql_query("select sid,sname from services order by sid asc");
if ($row1=mysql_fetch_assoc($sql1)){
do{
if ($arr[serviceid] == $row1[sid]){
$selectedtext = " checked";
print "if print :$arr[serviceid] / $row1[sid]";
         } else {
print "else : $arr[serviceid] / $row1[sid]";

   $selectedtext = "";
         }

?>
<tr><td>
<input name=service[] type=checkbox class=textinput id=service value=<?print $row1[sid];?><?
print "$selectedtext";?>><?print $row1[sname];?></td>
<td><select name=tech[] class=textinput><option value=0>SELECT TECH</option>
<?$sql=mysql_query("select uid,fname from users where status='1' and uid !='1'");
while ($row=mysql_fetch_row($sql)){
print "<option value=$row[0]>$row[1]</option>";}
mysql_close;?>
</select></td></tr><?} while ($row1=mysql_fetch_assoc($sql1));}}?></td>

?>

Posted: Thu Dec 23, 2004 7:49 am
by Etherguy
Nothing on this, huh? :cry:

Posted: Sun Jan 02, 2005 7:06 pm
by Etherguy
I am still stumped on this if anyone can lend any help. :(

Posted: Sun Jan 02, 2005 8:29 pm
by feyd
I think it's that there are 3 loops nested, repeating the same query over and over.

The code's very confusing. Line 21 could easily create an infinite loop. Try storing off the results from the query on line 4, then passing over them all to see if the current value is checked. Once that's all done, then print out the checkbox, instead of each pass over it.

Posted: Mon Jan 03, 2005 4:56 am
by n00b Saibot
methinks line#5 shud be moved above line#4. mebbe that wud solve ur prob. :)

Posted: Mon Jan 03, 2005 10:38 am
by Etherguy
Feyd,

I do not quite follow what you are saying. When you say store the results from query line 4, I will assume you mean in a array. I am a bit confused on how to pass over the checked and not checked boxes, since the table has a 1 or 0 defining whether the box is checked.

Noob - Your suggestion did not work. Since reversing it would be executing the query before the query is defined.

Thanks again for the help and suggestions.

Regards,

Posted: Mon Jan 03, 2005 11:23 am
by feyd
basically what I was suggesting is caching the content of the line 4 query into an array like so:

Code: Select all

# cache the techs list
$query = mysql_query( "select uid, fname from users where status='1' and uid !='1'" ) or die(mysql_error());
$techs = array();
while( $row = mysql_fetch_assoc( $query ) )
  $techs&#1111; $row&#1111; 'uid' ] ] = $row&#1111; 'fname' ];

mysql_free_result( $query );


# cache the mappings made
$query = mysql_query( "select techid, serviceid from cli_mapping where cid=$cid order by serviceid asc" ) or die( mysql_error() );
$mapping = array();
while( $row = mysql_fetch_assoc( $query ) )
  $mapping&#1111; $row&#1111;'serviceid'] ] = $row&#1111; 'techid' ];

mysql_free_result( $query );


# run through all the services writing out the checkboxes
$query = mysql_query( "select sid, sname from services order by sid asc" );
while( $row = mysql_fetch_assoc( $query ) )
{
  if( isset( $mapping&#1111; $row&#1111; 'sid' ] ] ) )
    $check = true;
  else
    $check = false;

  echo '&lt;tr&gt;&lt;td&gt;&lt;input name="service&#1111;' . $row&#1111; 'sid' ] . ']" type="checkbox" class="textinput" id="service" value="' . $row&#1111; 'sid' ] . '" ' . ( $check ? ' checked="checked"' : '' ) . ' /&gt; ' . $row&#1111; 'sname' ] . '&lt;/td&gt;' . "\n";
  echo '  &lt;td&gt;&lt;select name="tech&#1111;' . $row&#1111; 'sid' ] . ']" class="textinput"&gt;&lt;option value="0"&gt;SELECT TECH&lt;/option&gt;';
  foreach( $techs as $techid =&gt; $techname )
    echo '&lt;option value="' . $techid . '"' . ( $check &amp;&amp; $techid == $mapping&#1111; $row&#1111; 'sid' ] ] ? ' selected="selected"' : '' ) . '&gt;' . $techname . '&lt;/option&gt;';
  echo '&lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;' . "\n";
}

Posted: Mon Jan 03, 2005 11:47 am
by Etherguy
Feyd my good man. I owe you a beer!