Jump to content

Recommended Posts

OK I want to mark a checkbox as checked if the gameid is in the array how can i manage this?

 

<form name="previous_games" method="post" action="edit_games.php">  
   
Select previously played games
<br /><br />  
   
<?php  
$query = mysql_query("SELECT gameid, title FROM games");
$i = 1;

$numbers = mysql_fetch_array(mysql_query("SELECT pgames FROM gamers WHERE gamerid='1'"));
$nums = $numbers['pgames'];
// This is 1, 2, 3, 4, 5, 6, 7

$checkNums = explode(", ", $nums);
echo "checkNums: ".$checkNums;

while ($row = mysql_fetch_array($query))  
{ ?>
<input type='checkbox' name="pgames[]" value="<?php echo $row['gameid']; ?>" <?php if($checkNums == $row['gameid']){ echo 'seleceted="selected"';} ?> /> <?php echo $row['title']; ?> <br />
<?php } ?>  
<br /><br />  
<input type="submit" value="Add Games">  
</form>  

although I would personally do the whole thing in one echo like this

 

echo "<input type='checkbox' name='pgames[]' value='".$row['gameid']."' ".($checkNums
== $row['gameid'] ? "checked")." />".$row['title']; 

 

Using this i get a unexpected ) so i changed to this:

 

echo "<input type='checkbox' name='pgames[]' value='".$row['gameid']."' "; if(in_array($row['gameid'], $checkNums)){echo "checked";} echo " />".$row['title']."<br />";

 

It ONLY works if i define the array manually :S

$checkNums = array(1, 2, 3, 4, 5, 6, 7); // THIS WORKS
$checkNums = array($nums); // THIS DONT

 

<?php  
$query = mysql_query("SELECT gameid, title FROM games");

$numbers = mysql_fetch_array(mysql_query("SELECT pgames FROM gamers WHERE gamerid='1'"));
$nums = $numbers['pgames'];

echo "Nums: ".$nums."<br />";
//$checkNums = array(1, 2, 3, 4, 5, 6, 7); //THIS WORKS
$checkNums = array($nums); // THIS DONT

while ($row = mysql_fetch_array($query))  
{
echo "<input type='checkbox' name='pgames[]' value='".$row['gameid']."' "; if(in_array($row['gameid'], $checkNums)){echo "checked";} echo " />".$row['title']."<br />";
} ?>  

well that's not an array, it's just a string of numbers

 

do this

$checkNums1 = array(1, 2, 3, 4, 5, 6, 7); //THIS WORKS
$checkNums2 = array($nums); // THIS DONT

echo "array(1,2...) = ".$checkNums1."<br>";
echo "array(nums) = ".$checkNums2.";

 

and you'll see the difference between the two....

 

and i actually see that i didn't fully read your post to begin with... didn't realize you were looking for an array of numbers at first

well that's not an array, it's just a string of numbers

 

do this

$checkNums1 = array(1, 2, 3, 4, 5, 6, 7); //THIS WORKS
$checkNums2 = array($nums); // THIS DONT

echo "array(1,2...) = ".$checkNums1."<br>";
echo "array(nums) = ".$checkNums2.";

 

and you'll see the difference between the two

 

how am I going to see a difference if they will output 'Array'?

SOLVED after I was using print_r i saw explode was coming up as an array so i used that

 

<?php  
$query = mysql_query("SELECT gameid, title FROM games");

$numbers = mysql_fetch_array(mysql_query("SELECT pgames FROM gamers WHERE gamerid='1'"));
$nums = $numbers['pgames'];

$checkNums = explode(", ",$nums);

while ($row = mysql_fetch_array($query))  
{
echo "<input type='checkbox' name='pgames[]' value='".$row['gameid']."' "; if(in_array($row['gameid'], $checkNums)){echo "checked";} echo " />".$row['title']."<br />";
} ?>  

 

Thanks Guys good night ;)

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.