Jump to content

[SOLVED] working with sorting order


vikela

Recommended Posts

 

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.

 

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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 = '';

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.