vikela Posted September 28, 2009 Share Posted September 28, 2009 here is the challenge i am facing.i have a field that contains integers ranging from 1 to 10.how can i pull and display in a select menu numbers that are not yet in the field.i was thinking of creating an array of numbers that range (1-10) then compare with the numbers within the field but i am somehow failing to have the comparison work.. > :'( $arr=array(1,2,3,4,5,6,7); foreach($find as $test ){ if(in_array($test['rank'],$arr)){ echo "NUMBER ... is present"; } else{ echo "TESTTTTTTTTT"; } } whats my challenge?i would like it to show me the numbers that are not in the field 'rank' and this actually will be in a select menu so that a user select any of the non used number. Quote Link to comment https://forums.phpfreaks.com/topic/175810-solved-working-with-sorting-order/ Share on other sites More sharing options...
Mark Baker Posted September 28, 2009 Share Posted September 28, 2009 $used=array(1,2,3,4,5,6,7); print_r($used); echo '<hr />'; $unused = array_diff(range(1,10),$used); print_r($unused); Quote Link to comment https://forums.phpfreaks.com/topic/175810-solved-working-with-sorting-order/#findComment-926424 Share on other sites More sharing options...
Psycho Posted September 28, 2009 Share Posted September 28, 2009 I believe the 'used' ranks are sub elements in the multi-dimensional array $find. So, you would need to iterrate through all of those. <?php //Create an array of the valid options $all_ranks = range(1, 10); //Create new array and fill with used ranks $used_ranks = array(); foreach ($find as $record) { if (!in_array($record['rank']), $used_ranks) { $used_ranks[] = $record['rank']; } } //Get the diffrence of $all_ranks and $used_ranks (i.e. the unused ranks) $unused_ranks = array_diff($all_ranks, $used_ranks); //Create the options for select list $unused_rank_options = ''; foreach ($unused_ranks as $rank) { $unused_rank_options .= "<option value="{$rank}">{$rank}</option> " } ?> Rank: <select name="rank"> <?php echo $unused_rank_options; ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/175810-solved-working-with-sorting-order/#findComment-926430 Share on other sites More sharing options...
redarrow Posted September 28, 2009 Share Posted September 28, 2009 my question was relating to this as well, i got stuck so i asked for help as well ... please read what was said very interesting..... Quote Link to comment https://forums.phpfreaks.com/topic/175810-solved-working-with-sorting-order/#findComment-926508 Share on other sites More sharing options...
vikela Posted September 29, 2009 Author Share Posted September 29, 2009 Parse error: syntax error, unexpected ','... is the error i have got from the code and its the following lines //Create an array of the valid options $all_ranks = range(1,7); //Create new array and fill with used ranks $used_ranks = array(); of the following code <?php //Create an array of the valid options $all_ranks = range(1, 10); //Create new array and fill with used ranks $used_ranks = array(); foreach ($find as $record) { if (!in_array($record['rank']), $used_ranks) { $used_ranks[] = $record['rank']; } } //Get the diffrence of $all_ranks and $used_ranks (i.e. the unused ranks) $unused_ranks = array_diff($all_ranks, $used_ranks); //Create the options for select list $unused_rank_options = ''; foreach ($unused_ranks as $rank) { $unused_rank_options .= "<option value="{$rank}">{$rank}</option> " } ?> Rank: <select name="rank"> <?php echo $unused_rank_options; ?> </select> i did try a number of things even var_dumping but still getting the parse error Quote Link to comment https://forums.phpfreaks.com/topic/175810-solved-working-with-sorting-order/#findComment-926947 Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 He had a small typo, fix: <?php //Create an array of the valid options $all_ranks = range(1, 10); //Create new array and fill with used ranks $used_ranks = array(); foreach ($find as $record) { if (!in_array($record['rank'], $used_ranks)) { $used_ranks[] = $record['rank']; } } //Get the diffrence of $all_ranks and $used_ranks (i.e. the unused ranks) $unused_ranks = array_diff($all_ranks, $used_ranks); //Create the options for select list $unused_rank_options = ''; foreach ($unused_ranks as $rank) { $unused_rank_options .= "<option value="{$rank}">{$rank}</option> " } ?> Rank: <select name="rank"> <?php echo $unused_rank_options; ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/175810-solved-working-with-sorting-order/#findComment-926949 Share on other sites More sharing options...
vikela Posted September 29, 2009 Author Share Posted September 29, 2009 still throughing Parse error now its :Parse error: syntax error, unexpected '{' in... on line 30 and lines 20- 33 and is $used_ranks = array(); foreach($news as $record) { if (!in_array($record['rank'],$used_ranks)) { $used_ranks[] = $record['rank']; } } //Get the diffrence of $all_ranks and $used_ranks (i.e. the unused ranks) $unused_ranks = array_diff($all_ranks,$used_ranks); //Create the options for select list $unused_rank_options = ''; Quote Link to comment https://forums.phpfreaks.com/topic/175810-solved-working-with-sorting-order/#findComment-926959 Share on other sites More sharing options...
Psycho Posted September 29, 2009 Share Posted September 29, 2009 My signature line states I do not always test the code I provide, so there may be some syntax errors. Not to be rude, but this is a help forum. I would hope that the people receiving help can solve simple syntax errors. Anyway, the code below is fixed and tested: <?php //Used for testing $find = array ( array('rank'=>1), array('rank'=>, array('rank'=>4), array('rank'=>, array('rank'=>7), array('rank'=>5), array('rank'=>1), array('rank'=>4), array('rank'=>1), array('rank'=>5) ); //Create an array of the valid options $all_ranks = range(1, 10); //Create new array and fill with used ranks $used_ranks = array(); foreach ($find as $record) { if (!in_array($record['rank'], $used_ranks)) { $used_ranks[] = $record['rank']; } } //Get the diffrence of $all_ranks and $used_ranks (i.e. the unused ranks) $unused_ranks = array_diff($all_ranks, $used_ranks); //Create the options for select list $unused_rank_options = ''; foreach ($unused_ranks as $rank) { $unused_rank_options .= " <option value=\"{$rank}\">{$rank}</option>\n"; } ?> Rank: <select name="rank"> <?php echo $unused_rank_options; ?> </select> Output: Rank: <select name="rank"> <option value="2">2</option> <option value="3">3</option> <option value="6">6</option> <option value="9">9</option> <option value="10">10</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/175810-solved-working-with-sorting-order/#findComment-927029 Share on other sites More sharing options...
vikela Posted September 29, 2009 Author Share Posted September 29, 2009 Thanks you are a star!i must admit i did try my best to decode the syntax but then my best was not enough but then my php experience is growing!! Quote Link to comment https://forums.phpfreaks.com/topic/175810-solved-working-with-sorting-order/#findComment-927046 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.