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> 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 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 } ?> 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']; 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 />"; } ?> 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? 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 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 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'? 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??? 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 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
Archived
This topic is now archived and is closed to further replies.