Jump to content

unserialize within a foreach within a while loop


showme48
Go to solution Solved by showme48,

Recommended Posts

Hello,

I'm trying to unserialize a string and then
compare it and if listed mark the checkbox
as CHECKED.

It works fine as long as I have 1,2,3,4 checked
but if I do something like 1,4 then it only shows
1 as being CHECKED

 

Any help would be appreciated.

 

Thanks in advance.


serialize.gif

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>';
?>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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)

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.