Check Box Array

Discussion in 'PHP' started by jonathandey, Oct 6, 2010.

  1. #1
    The PHP
    
    if(isset($_POST['submit'])){
    	$thevet = $_POST['vet'];
    	$count = count($thevet);
    
    	for ($i = 0; $i < count($thevet); $i++) {
    	//Get the current status of accepted based on the ID of comment, if current status = no set to yes if checked, set to no if not checked.
    	$check = mysql_fetch_object(mysql_query("SELECT * FROM comments WHERE id = '".$thevet[$i]."'")) or die(mysql_error());
    	
    	if ($check->accepted == "no"){
    	mysql_query("UPDATE comments SET accepted= 'yes' WHERE id = '".$thevet[$i]."'") or die(mysql_error());
    	}else{
    	mysql_query("UPDATE comments SET accepted = 'no' WHERE NOT id = '".$thevet[$i]."'") or die(mysql_error());
    	}
    	
    }
    } 
    
    PHP:
    The HTML
    <form name="show_hide" method="post" action="">
    <table border="1" width="800px">
    	<tr>
    		<td colspan="10">
    			<h1>Comments</h1>
    		</td>
    	</tr>
    	<tr>
    		<td>
    			Name
    		</td>
    		<td>
    			Comment
    		</td>
    		<td>
    			Posted
    		</td>
    		<td>
    			Showing
    		</td>
    		<td>
    			Edit
    		</td>
    	</tr>
    <?
    while($get = mysql_fetch_object($comments)){
    ?>
    	<tr>
    		<td>
    			<? echo $get->name; ?>
    		</td>
    		<td>
    			<? echo comment($get->comment); ?>
    		</td>
    		<td>
    			<? echo date("d/m/Y", $get->dateposted); ?>
    		</td>
    		<td>
    			<input name="vet[]" type="checkbox" value="<? echo $get->id; ?>" <? if($get->accepted == "yes") { echo "checked='checked'"; } ?>' />
    		</td>
    		<td>
    			<img src="images/edit.png" />
    		</td>
    	</tr>
    <?
    
    }
    
    ?>
    <tr>
    	<td colspan="10" align="right">
    		<input type="submit" value="Show/Hide" name="submit" />
    	</td>
    </tr>
    </table>
    </form>
    HTML:
    I can't get it to untick or tick the boxes from my list.
     
    jonathandey, Oct 6, 2010 IP
  2. imperialDirectory

    imperialDirectory Peon

    Messages:
    395
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Maybe your PHP short tag is not enabled? Try changing your php tags in this line from
    
    <input name="vet[]" type="checkbox" value="<? echo $get->id; ?>" <? if($get->accepted == "yes") { echo "checked='checked'"; } ?>' />
    
    From:
    <?
    To:
    <?php
    
    Code (markup):
    Also you have an extra single quote on that line
    
    Change from:
    ?>' />
    
    To:
    ?> />
    
    Code (markup):
    Cheers,
    Felix
     
    imperialDirectory, Oct 6, 2010 IP
  3. jonathandey

    jonathandey Active Member

    Messages:
    112
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    I know that's not the problem because all my other scripts work fine.
    The really strange thing is this script works on a duplicate website with the same database structure.
     
    jonathandey, Oct 6, 2010 IP