Vivid Lust Posted October 29, 2008 Share Posted October 29, 2008 Hey, how can I check if a value exists in the array: $site[] Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/130584-solved-value-in-array/ Share on other sites More sharing options...
rhodesa Posted October 29, 2008 Share Posted October 29, 2008 like this? if(isset($site[2])){ echo "2 is set"; } Quote Link to comment https://forums.phpfreaks.com/topic/130584-solved-value-in-array/#findComment-677486 Share on other sites More sharing options...
Maq Posted October 29, 2008 Share Posted October 29, 2008 Do you want to see if there is any value or a specific one? First case use rhodesa's code, latter use in_array. Quote Link to comment https://forums.phpfreaks.com/topic/130584-solved-value-in-array/#findComment-677489 Share on other sites More sharing options...
rhodesa Posted October 29, 2008 Share Posted October 29, 2008 isset() => if a specific key is set count() => will tell you how many elements are in an array in_array() => check to see if a value is somewhere in the array *you probably want this one Quote Link to comment https://forums.phpfreaks.com/topic/130584-solved-value-in-array/#findComment-677491 Share on other sites More sharing options...
Vivid Lust Posted October 29, 2008 Author Share Posted October 29, 2008 Hmm, foo[1] = $POST_[bar]; how can I check if the value of $POST_[bar] is contained in a foo[] array? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/130584-solved-value-in-array/#findComment-677498 Share on other sites More sharing options...
rhodesa Posted October 29, 2008 Share Posted October 29, 2008 if(in_array($_POST['bar'],$foo)){ echo "found it"; } Quote Link to comment https://forums.phpfreaks.com/topic/130584-solved-value-in-array/#findComment-677501 Share on other sites More sharing options...
Vivid Lust Posted October 29, 2008 Author Share Posted October 29, 2008 Im getting this error: Parse error: syntax error, unexpected T_SR Code line: <?php <input type="checkbox" name="b" value="http://www.bbc.com" <?php if(in_array("http://www.bbc.com",$b)){echo "checked";}>>BBC<br /> ?> thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/130584-solved-value-in-array/#findComment-677507 Share on other sites More sharing options...
Maq Posted October 29, 2008 Share Posted October 29, 2008 You don't close your PHP tag. >BBC Quote Link to comment https://forums.phpfreaks.com/topic/130584-solved-value-in-array/#findComment-677512 Share on other sites More sharing options...
Vivid Lust Posted October 29, 2008 Author Share Posted October 29, 2008 Still isnt working! Codes: <?php $b[1] = $row2['box1']; $b[2] = $row2['box2']; $b[3] = $row2['box3']; $b[4] = $row2['box4']; $b[5] = $row2['box5']; $b[6] = $row2['box6']; $b[7] = $row2['box7']; $b[8] = $row2['box8']; $b[9] = $row2['box9']; $b[10] = $row2['box10']; $b[11] = $row2['box11']; $b[12] = $row2['box12']; $b[13] = $row2['box13']; $b[14] = $row2['box14']; $b[15] = $row2['box15']; ?> <input type="checkbox" name="a" value="http://www.cnn.com" <?php if(in_array("http://www.cnn.com",$b)){echo "checked";} ?>>CNN<br /> <input type="checkbox" name="b" value="http://www.bbc.com" <?php if(in_array("http://www.bbc.com",$b)){echo "checked";}?>>BBC<br /> <input type="checkbox" name="c" value="http://www.nbc.com" <?php if(in_array("http://www.nbc.com",$b)){echo "checked";} ?>>NBC<br /> <input type="checkbox" name="d" value="http://www.abcnews.go.com" <?php if(in_array("http://www.abcnews.com",$b)){echo "checked";} ?>>ABC News<br /> <input type="checkbox" name="e" value="http://www.usatoday.com" <?php if(in_array("http://www.usatoday.com",$b)){echo "checked";} ?>>USA Today<br /> <input type="checkbox" name="f" value="http://www.msnbc.msn.com" <?php if(in_array("http://www.msnbc.msn.com",$b)){echo "checked";} ?>>MSNBC<br /> <input type="checkbox" name="g" value="http://www.nytimes.com" <?php if(in_array("http://www.nytimes.com",$b)){echo "checked";} ?>>The New York Times<br /> <input type="checkbox" name="h" value="http://www.ajc.com" <?php if(in_array("http://www.ajc.com",$b)){echo "checked";}?>>AJC<br /> <input type="checkbox" name="i" value="http://www.chicagotribune.com" <?php if(in_array("http://www.chicagotribune.com",$b)){echo "checked";} ?>>Chicago Tribune<br /> <input type="checkbox" name="j" value="http://www.washingtonpost.com" <?php if(in_array("http://www.washingtonpost.com",$b)){echo "checked";} ?>>Washington Post<br /> <input type="checkbox" name="k" value="http://www.chrone.com" <?php if(in_array("http://www.chrone.com",$b)){echo "checked";} ?>>Chrone<br /> <input type="checkbox" name="l" value="http://www.denverpost.com" <?php if(in_array("http://www.denverpost.com",$b)){echo "checked";} ?>>Denver Post<br /> <input type="checkbox" name="m" value="http://www.knoxnews.com" <?php if(in_array("http://www.knoxnews.com",$b)){echo "checked";} ?>>Knox News<br /> <input type="checkbox" name="n" value="http://www.foxnews.com" <?php if(in_array("http://www.foxnews.com",$b)){echo "checked";} ?>>Fox News<br /> <input type="checkbox" name="o" value="http://www.skynews.com" <?php if(in_array("http://www.skynews.com",$b)){echo "checked";} ?>>Sky News<br /> Quote Link to comment https://forums.phpfreaks.com/topic/130584-solved-value-in-array/#findComment-677560 Share on other sites More sharing options...
Maq Posted October 29, 2008 Share Posted October 29, 2008 What do you mean by it's still not working? Are the check boxes just not being check? No errors right? If you don't know how to turn them on put this piece of code at the top of the page: ini_set ("display_errors", "1"); error_reporting(E_ALL); Also, are you sure there are values in the array? Please print out the array by doing: print_r($b); Quote Link to comment https://forums.phpfreaks.com/topic/130584-solved-value-in-array/#findComment-677572 Share on other sites More sharing options...
kenrbnsn Posted October 29, 2008 Share Posted October 29, 2008 I think you have to rethink how your creating the checkboxes. Here's a version that uses the power of PHP arrays correctly: <?php $urls = array('CNN'=>'cnn', 'BBC'=>'bbc', 'NBC'=>'nbc', 'ABC News'=>'abcnews', 'USA Today'=>'usatoday', 'MSNBC'=>'msnbc', 'The New York Times'=>'nytimes', 'AJC'=>'ajc', 'Chicago Tribune'=>'chicagotribune', 'Washington Post'=>'washingtonpost', 'Chrone'=>'chrone', 'Denver Post'=>'denverpost', 'Knox News'=>'knoxnews', 'Fox News'=>'foxnews', 'Sky News'=>'skynews'); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> </head> <body> <form action="" method="post"> <?php $tmp = array(); foreach ($urls as $n => $u) { $chk = (isset($_POST['url_check'][$u]))?'checked':''; $tmp[] = '<input type="checkbox" name="url_check[' . $u . ']" value="http://www.' . $u . '.com" ' . $chk . '>'. $n . '<br />'; } echo implode("\n",$tmp) . "\n"; ?> <input type="submit" name="submit" value="Test It"> </form> <?php if (isset($_POST['submit'])) echo '<pre>' . print_r($_POST,true) . '</pre>'; ?> </body> </html> Ken Quote Link to comment https://forums.phpfreaks.com/topic/130584-solved-value-in-array/#findComment-677585 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.