Jump to content

Update / Retrieve Checkbox Database data - SHOULD be a simple fix..???


methodman

Recommended Posts

Hey everybody! I've been struggling with this issue for a bit now and it's beginning to get more  elusive the longer i work on it!! lol. SO, i am trying to post "checked" checkboxes to my database and retrieve this dynamic list to generate the form. NOTE that the checkboxes are dynamically generated by the DB query.

 

Table names are:

affiliations - contains a list of different affiliations to choose from

aff_list - contains the selected / checked affiliations for each ID

 

      <td><label for="affiliations">Affiliations</label></td>
      <td>
  <?php 
  	$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('ERROR connecting to database');
	$affiliationsQuery = "SELECT * FROM affiliations";
  	$affiliationsData = mysqli_query($dbc, $affiliationsQuery) or die (mysqli_error($dbc)); 
	$affiliation = array();

	$affQuery = "SELECT affiliation_id FROM aff_list WHERE breed_id = '$breedId'";
	$data2 = mysqli_query($dbc, $affQuery);
    	$aff_list = array();
	while($row2 = mysqli_fetch_array($data2)){
		array_push($aff_list, $row2);
	}

	echo '<fieldset><table><tr>';

	while($row10 = mysqli_fetch_array($affiliationsData)){
		array_push($affiliation, $row10);
	}
	foreach($affiliation as $aff){
		if($row2['affiliation_id'] == 2){
			echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" checked="checked" />' . $aff['affiliation'] . '</td>';
		}
		else{
			echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" />' .
					$aff['affiliation'] . '</td>';	
		}
	}
	echo $row2['affiliation_id'] . '<br />';
	print_r($aff_list);
	// *******************************************************************************************************

	echo '</tr></table></fieldset>';
	mysqli_close($dbc);
  ?>

OPERATIONALS:

  • i am able to dynamically generate the checkboxes for the form with no problems
  • I am able to update the database with the selected information with no problems
  • I can retrieve the data contained in the "aff_list" table and use array_push() to get this data into an array

ISSUES:

  • i cannot retrieve the values from this array
  • i cannot use this information to compare the values to determine if this checkbox has been submitted..

IDEAS:

i am thinking i can use a conditional like:

 

if($aff['affiliation_id'] == $aff_list['affiliation_id']){

      echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" checked="checked" />' . $aff['affiliation'] . '</td>';

}

else{

      echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" />' . $aff['affiliation'] . '</td>';

}

 

-----------

>:(

i have tried many variations of this - but i'm not making any progress and i really can't waste anymore time on this issue. ANY help or even a point in the right direction will be of great assistance.

 

Thanks in advance guys!!

Link to comment
Share on other sites

I just had an idea

 

what about using javascript (ajax) to popluate the checkboxes with the $aff_list array data???

 

SO:

pull the aff_list table data with PHP then use javascript to read the incoming data and assign "checked" value to each box??

 

sounds like a good plan to me i'm going to attempt that

 

Again, any help / feeback is appreciated!

Link to comment
Share on other sites

what if javascript is off???

 

just before your check for the value of affiliate echo out its value

 

foreach($affiliation as $aff){
                        echo $row2['affiliation_id'];
		if($row2['affiliation_id'] == 2){
			echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" checked="checked" />' . $aff['affiliation'] . '</td>';
		}
		else{
			echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" />' .
					$aff['affiliation'] . '</td>';	
		}
	}

 

obviously stick it inside some tags so it appears in the expected place...

Link to comment
Share on other sites

what if javascript is off???

 

just before your check for the value of affiliate echo out its value

 

foreach($affiliation as $aff){
                        echo $row2['affiliation_id'];
		if($row2['affiliation_id'] == 2){
			echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" checked="checked" />' . $aff['affiliation'] . '</td>';
		}
		else{
			echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" />' .
					$aff['affiliation'] . '</td>';	
		}
	}

 

obviously stick it inside some tags so it appears in the expected place...

 

That echo statement returns no value (nothing prints). However, the print_r($aff_list) function returns this string :

Array ( [0] => Array ( [0] => 2 [affiliation_id] => 2 ) [1] => Array ( [0] => 1 [affiliation_id] => 1 ) )

 

this page is actually an admin page, so the user will always have javascript enabled.. I would prefer to fix this problem the right way though.. using the PHP that builds it instead of the Javascript "fix"  :-\

Link to comment
Share on other sites

OK here is a clue for you...

 

when the affiliate id is anything other than 2 you will NOT get the check box checked...

 

so your if statement check the id of the current affiliate and see if its present in the list of affiliates associated with the current breed.

 

Good call, actually that was a typo. i was in the midst of changing random things just to see one of the checkboxes become selected, lol. the code i am trying to use now is:

     <td><label for="affiliations">Affiliations</label></td>
      <td>
  <?php // these affiliations need to populate the attr_list table NOT the breed table.
  	$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('ERROR connecting to database');
	$affiliationsQuery = "SELECT * FROM affiliations";
  	$affiliationsData = mysqli_query($dbc, $affiliationsQuery) or die (mysqli_error($dbc)); 
	$affiliation = array();

	$affQuery = "SELECT affiliation_id FROM aff_list WHERE breed_id = '$breedId'";
	$data2 = mysqli_query($dbc, $affQuery);
    	$aff_list = array();
	while($row2 = mysqli_fetch_array($data2)){
		array_push($aff_list, $row2);
	}

	echo '<fieldset><table><tr>';

	while($row10 = mysqli_fetch_array($affiliationsData)){
		array_push($affiliation, $row10);
	}
	foreach($affiliation as $aff){
		echo '<p>' . $aff_list['affiliation_id'] . '</p>';

		if($aff['affiliation_id'] == $aff_list['affiliation_id']){			
			echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" checked="checked" />' . $aff['affiliation'] . '</td>';
		}
		else{
			echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]" />' .
					$aff['affiliation'] . '</td>';	
		}
	}
	print_r($aff_list);
	// *******************************************************************************************************

	echo '</tr></table></fieldset>';
	mysqli_close($dbc);
  ?>

 

I'm still not having any luck but i have been working on other aspects of this project and giving this a rest for a min - while hoping for a solution on here.

 

I appreciate your efforts!

Link to comment
Share on other sites

You have an array of affiliate IDs and an array of affiliate IDs associated with a breed.

 

Loop through the affiliate IDs and for each one check if that ID is present in the array of IDs associated with a breed.

 

<?php
foreach($affiliation as $aff)
{

$checked = array_search($aff['affiliation_id'], $aff_list) ? ' checked="checked"' : NULL;			
echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . '" name="affiliations[]"' . $checked . ' />' . $aff['affiliation'] . '</td>';


}
?>

Link to comment
Share on other sites

GREAT IDEA ToonMariner, i've been trying to work this out. since it still doesn't return any checked boxes. The code is sound and it should work..?? this is where i am at right now.

  <?php // these affiliations need to populate the attr_list table NOT the breed table.
  	$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('ERROR connecting to database');

	$affiliationsQuery = "SELECT * FROM affiliations";
  	$affiliationsData = mysqli_query($dbc, $affiliationsQuery) or die (mysqli_error($dbc)); 
	$affiliation = array();	

	while($row10 = mysqli_fetch_array($affiliationsData)){
		array_push($affiliation, $row10);
	}

	$affQuery = "SELECT affiliation_id FROM aff_list WHERE breed_id = '$breedId'";
	$data2 = mysqli_query($dbc, $affQuery);
    	$aff_list = array();

	while($row2 = mysqli_fetch_array($data2)){
		array_push($aff_list, $row2);
		echo $row2['affiliation_id'];
	}

	echo '<fieldset><table><tr>';

	foreach($affiliation as $aff){
		$checked = (array_search($aff['affiliation_id'], $aff_list) ? ' checked="checked"' : '');	

			echo '<td><input type="checkbox" value="' . $aff['affiliation_id'] . 
			'" name="affiliations[]"' . $checked . ' />' . $aff['affiliation'] . '</td>';

	}
	print_r($aff_list); // returns : Array ( [0] => Array ( [0] => 1 [affiliation_id] => 1 ) ) 

	echo '</tr></table></fieldset>';
	mysqli_close($dbc);
  ?>

 

I don't see the issue. The Ternary conditional looks good, and the arrays are populated with data..? What gives?

 

Link to comment
Share on other sites

you are not populating your arrays as you think you are...

 

simply pushing an array onto an array creates an element containing an array of the indices you are interested in - making it harder for you to access those elements.

 

use print_r to output the two arrays you build and you should see what I mean.

 

In the mean time try this...

 

 	  <?php // these affiliations need to populate the attr_list table NOT the breed table.
  	$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('ERROR connecting to database');

	$affiliationsQuery = "SELECT * FROM affiliations";
  	$affiliationsData = mysqli_query($dbc, $affiliationsQuery) or die (mysqli_error($dbc)); 
	$affiliation = array();	

	while($row10 = mysqli_fetch_array($affiliationsData)){
		foreach($row10 as $key => $val){
			$affiliation[$key][] = $val;
		}
	}

	$affQuery = "SELECT affiliation_id FROM aff_list WHERE breed_id = '$breedId'";
	$data2 = mysqli_query($dbc, $affQuery);
    	$aff_list = array();

	while($row2 = mysqli_fetch_array($data2)){
		$aff_list[] = $row2['affiliation_id'];
	}

	echo '<fieldset><table><tr>';

	foreach($affiliation['affiliation_id'] as $key => $val){
		$checked = (array_search($val, $aff_list) ? ' checked="checked"' : '');
		printf('<td><input type="checkbox" value="%1$d" name="affiliations[]"%3$s/>%2$d</td>',$val,$affiliation['affiliation_name'][$key],$checked);				
	}
	echo '</tr></table></fieldset>';
	mysqli_close($dbc);
  ?>

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.