Page 1 of 1

checkbox repopulation issue (may be in the arrays)

Posted: Sat Aug 23, 2003 2:14 pm
by m3rajk
http://24.91.157.113/findyourdesire/phpTest/control.php
click on interests. that's the section this is in, with some debugging lines added.

i realized after adding that it's not my repopulation function, but the arrays. i just don't understand why the arrays are causing an issue.

Code: Select all

function interest(){ # in interests
  include("/home/joshua/includes/fyd.vars.php"); # includes file (precautionary measure)
  $result; $selections;
  if(isset($_POST['act'])){ # update the db
    $pw=$_COOKIE['pw']; $un=$_COOKIE['un'];
    /* connect to db. give error messages is pw doesn't match */
    /* update db with new selections (for interests that have selections) and thenretrieve the new entries */
    /* make each interest intoit's own string, with html formatting and set $selections to it */
  }else{ # call the database and retrieve what's needed
    $pw=$_COOKIE['pw']; $un=$_COOKIE['un'];
    /* connect to db. give error messages is pw doesn't match */
    /* make each interest into it's own string, with html formatting and set $selections to it */
  }

  /* this has "test selections" in the arrays to test the function for cheching this */
  $cbooks=array('Comedy', 'Mystery', ''); $cmisc=array('Cars/Trucks', 'Religions', ''); 
  $cmovies=array('Action, Fantasy, Science-Fiction', ''); $cmusic=array('Blues', 'Techno', ''); 
  $cpets=array('Birds', 'Mice', 'Snakes', ''); $csports=array('Baseball', 'Fencing', 'Luge', 'Tennis', '');
  $books=array('books'=>'Art','books'=>'Childrens', 'books'=>'Comedy', 'books'=>'Comic', 'books'=>'Cook/Cooking', 'books'=>'Fantasy', 'books'=>'Horror', 'books'=>'Mystery', 'books'=>'Picture', 'books'=>'Poetry', 'books'=>'Romance', 'books'=>'Science-Fiction', 'books'=>'Other');
  $misc=array('misc'=>'Aircraft (Commercial)', 'misc'=>'Aircraft (Military)', 'misc'=>'Cars/Trucks', 'misc'=>'Computer Games', 'misc'=>'Computer Hardware', 'misc'=>'Computer Programming', 'misc'=>'Occult', 'misc'=>'Religions', 'misc'=>'Role Playing Games (RPGs)', 'misc'=>'Other');
  $movies=array('movies'=>'Action', 'movies'=>'Animated', 'movies'=>'Comedy', "movies"=>"Children's (Disney)", 'movies'=>'Documentary', 'movies'=>'Drama', 'movies'=>'Fantasy', 'movies'=>'Foreign', 'movies'=>'Horror','movies'=>'Romance', 'movies'=>'Science-Fiction', 'movies'=>'Other');
  $music=array('music'=>'Blues', 'music'=>'Christian', 'music'=>'Classical', 'music'=>'Country/Western', 'music'=>'Dance', 'music'=>'Folk', 'music'=>'Gospel', 'music'=>'Jazz', 'music'=>'Opera', 'music'=>'Polka', 'music'=>'Pop','music'=>'Rap', 'music'=>'Rock', 'music'=>'Salsa', 'music'=>'Techno', 'music'=>'Other');
  $pets=array('pets'=>'Birds', 'pets'=>'Cats', 'pets'=>'Dogs', 'pets'=>'Fish', 'pets'=>'Gerbils', 'pets'=>'Hermit Crabs', 'pets'=>'Horses', 'pets'=>'Lizards', 'pets'=>'Mice', 'pets'=>'Rabbits', 'pets'=>'Rats', 'pets'=>'Snakes', 'pets'=>'Other');
  $sports=array('sports'=>'Baseball', 'sports'=>'Basketball', 'sports'=>'Curling', 'sports'=>'Dance', 'sports'=>'Fencing', 'sports'=>'Field Hockey', 'sports'=>'Fishing', 'sports'=>'Football', 'sports'=>'Gymnastics', 'sports'=>'Hockey', 'sports'=>'Ice Skating', 'sports'=>'Inline Skating', 'sports'=>'Luge', 'sports'=>'Martial Arts', 'sports'=>'Skate Boarding', 'sports'=>'Sky Diving', 'sports'=>'Snow Boarding', 'sports'=>'Soccer', 'sports'=>'Street Luge', 'sports'=>'Surfing', 'sports'=>'Swimming', 'sports'=>'Track & Field', 'sports'=>'Tennis', 'sports'=>'Volleyball', 'sports'=>'Other');

    $i=0; $j=0; $boxes=''; $done=FALSE; # keep track selection/cells; make choices; are we done?
  function checkopts($selections, $choices, $row){ # makes check box options
    $ssize=count($selections); $csize=count($choices); # size of the arrays
    $nnoc=(ceil($csize/$row)*$row); # how many cells do we need
    while(!($done)){
    foreach($choices as $key=>$value){
      if(($j%$row)==0){ $boxes.='		    <tr>'; } # we're starting a row
      if($selections[$i]==$value){ # it was selected
	$boxes.='<td><input type="checkbox" name="'.$key."[]" selected value="$value">&nbsp;$value</td>";
	$i++; # go to the next selection
      }else{ # it wasn't
	$boxes.='<td><input type="checkbox" name="'.$key."[]" value="$value">&nbsp;$value</td>";
      }
      if(($j%$row)==0){ $boxes.='</tr>\n'; } # we finished the row
      $j++; # we finished a cell, add to the number of cells
    }
    if($j<$nnoc){ # we don't have a complete row
      for($k=$j;$k<$nnoc;$k++){ # finish off the row with empty cells
	$boxes.='<td>&nbsp;</td>';
      }
      $boxes.='</tr>\n';
    }
    $done=TRUE;
    }
    return $boxes;
  }
  $bookboxes=checkopts($cbooks, $books, 7); $miscboxes=checkopts($cmisc, $misc, 4); # books/misc options
  $movieboxes=checkopts($cmovies, $movies, 6); $musicboxes=checkopts($cmusic, $music, 7);
  $petboxes=checkopts($cpets, $pets, 7); $sportboxes=checkopts($csports, $sports, 7);
  ## make the page
  echo <<<END
      <p>$result</p>
      <p>Your Current Selections: $selections (note check boxes populated with a "sample selection array" via a new function being tested)</p>
      <form action="$_SERVER[PHP_SELF]" method="POST">
	<input type="hidden" name="fn" value="interest"><input type="hidden" name="act" value="process">
	<table frame="void" bgcolor="#000000" text="#c8c8c8" border="0" cellpadding="0" cellspacing="0">
	    <tr>
	      <td colspan="2" align="center"><!-- inner table for interest: Books -->
		<table frame="box" bgcolor="#000000" border="1" cellpadding="0" cellspacing="0" text="#c8c8c8" width="100%">
		    <tr><th colspan="7" >Please check off all Genres of Books that interest you</th></tr>
$bookboxes
		</table>
	      </td>
	    </tr>
	    <tr>
	      <td colspan="2" align="center"> <!-- inner table for interest: Misc-->
		<table frame="box" bgcolor="#000000" border="1" cellpadding="0" cellspacing="0" text="#c8c8c8" width="100%">
		    <tr><th colspan="4">Please check off all Miscellaneous Interests that interest you</th></tr>
$miscboxes
		</table>
	      </td>
	    </tr>
	    <tr>
	      <td colspan="2" align="center"><!-- inner table for interest: Movies  -->
		<table frame="box" bgcolor="#000000" border="1" cellpadding="0" cellspacing="0" text="#c8c8c8" width="100%">
		    <tr><th colspan="6">Please check off all Genres of Movies that interest you</th></tr>
$movieboxes
		</table>
	      </td>
	    </tr>
	    <tr>
	      <td colspan="2" align="center"><!-- inner table for interest: music -->
		<table frame="box" bgcolor="#000000" border="1" cellpadding="0" cellspacing="0" text="#c8c8c8" width="100%">
		    <tr><th colspan="7">Please check off all Genres of Music that interest you</th></tr>
$musicboxes
		</table>
	      </td>
	    </tr>
	    <tr>
	      <td colspan="2" align="center"> <!-- inner table for interest: Pets-->
		<table frame="box" bgcolor="#000000" border="1" cellpadding="0" cellspacing="0" text="#c8c8c8" width="100%">
		    <tr><th colspan="7">Please check off all Pets that interest you</th></tr>
$petboxes
		</table>
	      </td>
	    </tr>
	    <tr>
	      <td colspan="2" align="center"> <!-- inner table for interest: Sports-->
		<table frame="box" bgcolor="#000000" border="1" cellpadding="0" cellspacing="0" text="#c8c8c8" width="100%">
		    <tr><th colspan="7">Please check off all Sports that interest you</th></tr>
$sportboxes
		</table>
	      </td>
	    </tr>
	    <tr>
	      <td align="center"><input type="submit" value="Update Interests"></td>
	      <td align="center"><input type="reset" value="Nah, Nothing's Changed"></td>
	    </tr>
	</table>
      </form>
    </center>
  </body>
</html>
END;
}

Posted: Sat Aug 23, 2003 11:17 pm
by nathus
Your problem is that you are just keeping on replacing the array keys with a new value, so each array only has 1 item it.

Code: Select all

$movies=array('movies'=>'Action', 'movies'=>'Animated', 'movies'=>'Comedy', "movies"=>"Children's (Disney)", 'movies'=>'Documentary', 'movies'=>'Drama', 'movies'=>'Fantasy', 'movies'=>'Foreign', 'movies'=>'Horror','movies'=>'Romance', 'movies'=>'Science-Fiction', 'movies'=>'Other');
will make an array like so...

Code: Select all

Array
(
    &#1111;movies] => Other
)

Posted: Sun Aug 24, 2003 7:05 pm
by m3rajk
ok.,. changed slightly. it does the checking and all, but what it fails at is getting the row correct. it screws up the first one.

edit: nevermind. i needed to vary the check for the row andmake sure it's not the first time. it's working now. anyone need a way to repopulate check boxes?

Code: Select all

function checkopts($selections, $choices, $row, $name){ # makes check box options
    $i=0; $j=0; $k=0; $boxes=''; # keep track selection/cells; make checkboxes
    $ssize=count($selections); $csize=count($choices); # size of the arrays
    $nnoc=(ceil($csize/$row)*$row); # how many cells do we need
    echo "<br />i=$i &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j=$j <br />num of selections: $ssize<br />num of choices: $csize<br />num of needed cells: $nnoc<br /> num of cells per row: $row";
    foreach($choices as $choice){
      if(($j%$row)==0){ $boxes.='                   <tr>'; } # we're starting a row
      if($selections[$i]==$choice){ # it was selected
        $boxes.='<td><input type="checkbox" name="'.$name."[]" checked value="$choice">&nbsp;$choice</td>";
        $i++; # go to the next selection
        echo "<br />i=$i &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j=$j <br /> $boxes";
      }else{ # it wasn't
        $boxes.='<td><input type="checkbox" name="'.$name."[]" value="$choice">&nbsp;$choice</td>";
      }
      if((($k/$row)==0)&&(k!=0)){ $boxes.='</tr>'; } # we finished the row
      $j++; $k++; # we finished a cell, add to the number of cells
        echo "<br />i=$i &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j=$j  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k=$k <br /> $boxes";
    }
    if($j<$nnoc){ # we don't have a complete row
      for($k=$j;$k<$nnoc;$k++){ # finish off the row with empty cells
        $boxes.='<td>&nbsp;</td>';
      }
      $boxes.='</tr>';
    }
    return $boxes;
  }