xXREDXIIIXx Posted August 6, 2009 Share Posted August 6, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/169160-solved-if-a-var-is-in-an-array-echo-this/ Share on other sites More sharing options...
p2grace Posted August 6, 2009 Share Posted August 6, 2009 in_array() http://us3.php.net/manual/en/function.in-array.php Quote Link to comment https://forums.phpfreaks.com/topic/169160-solved-if-a-var-is-in-an-array-echo-this/#findComment-892571 Share on other sites More sharing options...
smerny Posted August 6, 2009 Share Posted August 6, 2009 <input type='checkbox' name="pgames[]" value="<?php echo $row['gameid']; ?>" <?php if($checkNums == $row['gameid']){ echo " checked";} ?> /> <?php echo $row['title']; ?> <br /> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/169160-solved-if-a-var-is-in-an-array-echo-this/#findComment-892572 Share on other sites More sharing options...
smerny Posted August 6, 2009 Share Posted August 6, 2009 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']; Quote Link to comment https://forums.phpfreaks.com/topic/169160-solved-if-a-var-is-in-an-array-echo-this/#findComment-892577 Share on other sites More sharing options...
xXREDXIIIXx Posted August 7, 2009 Author Share Posted August 7, 2009 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 />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/169160-solved-if-a-var-is-in-an-array-echo-this/#findComment-892595 Share on other sites More sharing options...
smerny Posted August 7, 2009 Share Posted August 7, 2009 echo "Nums: ".$nums."<br />"; what does that output? Quote Link to comment https://forums.phpfreaks.com/topic/169160-solved-if-a-var-is-in-an-array-echo-this/#findComment-892596 Share on other sites More sharing options...
xXREDXIIIXx Posted August 7, 2009 Author Share Posted August 7, 2009 echo "Nums: ".$nums."<br />"; what does that output? Nums: 1, 2, 3, 4, 5, 6, 7 Quote Link to comment https://forums.phpfreaks.com/topic/169160-solved-if-a-var-is-in-an-array-echo-this/#findComment-892597 Share on other sites More sharing options...
smerny Posted August 7, 2009 Share Posted August 7, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/169160-solved-if-a-var-is-in-an-array-echo-this/#findComment-892598 Share on other sites More sharing options...
xXREDXIIIXx Posted August 7, 2009 Author Share Posted August 7, 2009 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'? Quote Link to comment https://forums.phpfreaks.com/topic/169160-solved-if-a-var-is-in-an-array-echo-this/#findComment-892599 Share on other sites More sharing options...
xXREDXIIIXx Posted August 7, 2009 Author Share Posted August 7, 2009 So i cant use the var $nums in the array??? Quote Link to comment https://forums.phpfreaks.com/topic/169160-solved-if-a-var-is-in-an-array-echo-this/#findComment-892605 Share on other sites More sharing options...
xXREDXIIIXx Posted August 7, 2009 Author Share Posted August 7, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/169160-solved-if-a-var-is-in-an-array-echo-this/#findComment-892610 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.