showme48 Posted August 25, 2013 Share Posted August 25, 2013 Hello,I'm trying to unserialize a string and thencompare it and if listed mark the checkboxas CHECKED.It works fine as long as I have 1,2,3,4 checkedbut if I do something like 1,4 then it only shows1 as being CHECKED Any help would be appreciated. Thanks in advance. function getm($id) { $query3 = "select memtype from offer WHERE id = '".$id."'"; $result3 = mysql_query ($query3) or die ("Query failed"); $line3 = mysql_fetch_array($result3); $memtype = $line3["memtype"]; $mydata = unserialize($memtype); echo '<tr bgcolor=#CCCCCC><td>'; echo 'Show to:</td><td>'; if(!empty($mydata)) { foreach( $mydata as $key => $value) { $gamenames[] = $key; } } $getaccountinfo = mysql_query("Select id, mem_name, mem_rank from `memtype` WHERE mem_enabled=1 ORDER BY id ASC"); for ($j = 0; $j < mysql_num_rows($getaccountinfo); $j++) { $offerid = mysql_result($getaccountinfo, $j, "id"); $memname = mysql_result($getaccountinfo, $j, "mem_name"); echo(" $gamenames[$j] <input type=checkbox name=memtype[$offerid] value=$offerid"); if($gamenames[$j]==$offerid){echo(" CHECKED");} echo(">$memname <br>"); } echo '</td></tr>'; } $sql = mysql_query("SELECT * FROM offer"); echo '<table border=0><tr><td>'; while($oto = mysql_fetch_array($sql)) { if($count==1) { echo "</td><td>"; $count=0; } $count++; ?> <h3 align=center>OFFER <?php echo $oto['id']; ?></h3> <form method="POST"> <input type="hidden" name="action" value="updateoffer"> <input type="hidden" name="id" value="<?php echo $oto['id']; ?>"> <table border=0 cellpadding=5 bgcolor=#000000> <tr bgcolor=#CCCCCC><td>Price:</td><td>$<input type="text" name="price" value="<?php echo $oto['price']; ?>"></td></tr> <tr bgcolor=#CCCCCC><td>Commission:</td><td>$<input type="text" name="pdcommission" value="<?php echo $oto['pdcommission']; ?>"></td></tr> <tr bgcolor=#CCCCCC><td>Show after login:</td><td><input type="radio" name="enable" value="1"<?php if($oto['enable'] == 1) echo " CHECKED"; ?>> yes <input type="radio" name="enable" value="0"<?php if($oto['enable'] == 0) echo " CHECKED"; ?>> no</td></tr> <?php $showm=getm($oto['id']); ?> <tr><td colspan="2" align="center"><input type="submit" value="Update"></td></tr> </table> </form> <?php } echo '</td></tr></table>'; ?> Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 25, 2013 Share Posted August 25, 2013 (edited) Change: if($gamenames[$j]==$offerid){echo(" CHECKED") TO ($gamenames[$j]==$offerid) ? ' CHECKED' : NULL; Edited August 25, 2013 by jazzman1 Quote Link to comment Share on other sites More sharing options...
kicken Posted August 25, 2013 Share Posted August 25, 2013 Your values in $gamenames do not line up with your row counter values. When you select 1,4, then $gamenames[0] and $gamenames[1] are set, not $gamenames[0] and $gamenames[3] which is what your code would require as it is right now. What you want to do is check whether $offerid exists anywhere within the $gamenames array, not if it exists specifically at $gamenames[$j]. You can do this by using the in_array function. Quote Link to comment Share on other sites More sharing options...
showme48 Posted August 25, 2013 Author Share Posted August 25, 2013 Change: if($gamenames[$j]==$offerid){echo(" CHECKED") TO ($gamenames[$j]==$offerid) ? ' CHECKED' : NULL; Thanks jazzman1, I tried that and it did not work. I appreciate the effort but it has been solved. Quote Link to comment Share on other sites More sharing options...
Solution showme48 Posted August 25, 2013 Author Solution Share Posted August 25, 2013 Your values in $gamenames do not line up with your row counter values. When you select 1,4, then $gamenames[0] and $gamenames[1] are set, not $gamenames[0] and $gamenames[3] which is what your code would require as it is right now. What you want to do is check whether $offerid exists anywhere within the $gamenames array, not if it exists specifically at $gamenames[$j]. You can do this by using the in_array function. Thanks kicken, That worked perfectly. Sent you a tip... Donation Sent (Unique Transaction ID #5GY846557U109843P) Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 25, 2013 Share Posted August 25, 2013 Thanks jazzman1, I tried that and it did not work. Yes, I know Misread it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.