Jump to content

Group of checkboxes generated by a database and compared against the database.


sjslovechild

Recommended Posts

I have a very tricky php problem here. At least for me.

On a page that is editing a job entry. I have a database generated group of checkboxes some of which have been checked when the job was entered.

 

So the script has to do two things generate the complete list of checkboxes and check the ones that have already been selected in the job request. The below script is what I've done trying to figure it out.

 

Description of what I'm trying to do:

I'm generating the the boxes with a call to my database for the list of checkboxes.

Then I'm building an array with all the possible items that could be checked to compare against.

 

Ok here is where I think my first problem is. I need to make a second query to another table "worklog" in the same database. This is where the list of checked items are stored. What is the best way to do this? A second query seems wrong "or at least not efficient"

 

<div class="text">Job Type (<a class="editlist" href="javascript:editlist('listedit.php?edit=typelist',700,400);">edit list</a>):</div>
	<div class="field">

	    	<?PHP

	    		$connection=mysql_connect ("localhost", "user", "password") or die ("I cannot connect to the database.");
	    		$db=mysql_select_db ("database", $connection) or die (mysql_error());
	    		$query = "SELECT type FROM typelist ORDER BY type ASC";
	    		$sql_result = mysql_query($query, $connection) or die (mysql_error());
	    		$i=1;
	    		echo '<table valign="top"><tr><td>';
	    		while ($row = mysql_fetch_array($sql_result)) {
	    			$type = $row["type"];
	    			    if ($i > 1 && $i % 26 == 0) 
  		    				  echo '</td><td>'; 
  		    				  else if ($i) 
  		    				  echo ''; 
  		    				  ++$i;
  		    				  
  		    				  $aTypes = array ("Spec Ad Campaign", "100 x 100 Logo", "100 x 35 (Featured Developer/Broker)", "100 x 40 (Featured Lender)", "120 x 180 (Home Page Auto)", "120 x 45 (Half Tile - Jobs)", "120x60 (Section Sponsor)", "125 x 40 (Profile Page logo)", "135 x 31 (Autos)", "135 x 60 (Autos)", "135 x 60 (Logotile - Jobs)", "150 x 40 (Auto Logo)", "160 x 240 (monster tile)", "160 x 400 (Skyscraper)", "160 x 600 (Tower)", "170x30 (Section Sponsor)", "240 x 180 JPG/GIF Auto Video", "240 x 180 size FLV video ", "300 x 250 (Story Ad)", "300 x 600 (Halfpage)", "468 x 60 (Banner Jpg/Gif)", "728 x 90 (Leaderboard)", "95 x 75 (Sweeps Logo) ", "Agent Profile Page", "Contest", "Creative Change", "Employer Profile", "Holiday Shop", "Home of the Week", "HP Half Banner (234 x 60 Static)", "Mobile Ads (320x53 *300x50*216x36*168x28)", "Newsletter", "Peelback", "Pencil Ad", "Print", "Real Deals", "Resize ad campaign", "Roll-Over Skyscraper", "Site Sponsor logo (170x30)", "Splash Page Production", "Travel Deals Update", "Video Ad", "Web Development", "Web Maintenance", "Web Quote (or Design)");
  		    				  
  		    				  $dbTypes = explode(',',$row['type']);
  		    				  
  		    				  foreach ($aTypes as $type){
  		    				  
  		    				  	if(in_array($aType,$dbTypes)){
  		    				  	
  		    				 		echo "<input style='font-size:10px;' name='type[]' type='checkbox' value='$type' CHECKED><span style='color:#000;'>$type</span></input><br/>";
  		    				  	
  		    				  	}else
  		    				  	
  		    				  	{
  		    				  	
  		    			  		   	echo "<input style='font-size:10px;' name='type[]' type='checkbox' value='$type'><span style='color:#000;'>$type</span></input><br/>";	  	
  		    				  	
  		    				  	  		    				  	
  		    				  	}

  		    				  }

	    				}
	    				  echo '</td></tr></table>';
	    	
	    	?>

	</div>

 

Link to comment
Share on other sites

After looking at your code, I don't see anything that would lead me to believe that the checked items are used in multiple instances (i.e. each user has differnt checked options).

 

If that is the case, just use one table that uses two columns: one for the option name and a second field for whether the option is checked. But, I have to assume your case is more complex than that or you wouldn't have posted here.

Link to comment
Share on other sites

This should probably be in the sql section because you need to do a JOIN query. Sadly, I can't help because I'm not very good at constructing certain queries.

 

With that, you can have a virtual column affixed to each row you fetch which can contain a list of id's/lables etc for your checked boxes related to that row.

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.