Jump to content

Default Option and if returning null


Xtremer360

Recommended Posts

Trying to figure out how to better write this chunk of code. I'm wanting to get the list of the roster members and then create an array of options for the view dropdown to display inside the select dropdown and also have it have an option to display "Please Select An Option". However what if what is returned from the getAllRoster function is NULL which is what I have returned if no results are returned from a query. How should I handle that which I just want the empty option displayed.

 

Also I need to think about is do a function to retrieve all the allies for that specific matter and then display that ally as the default ally in the dropdown for each dropdown.

 

Controller:

$rosterList = $this->bios->getAllRoster();
$allies = array();
$allies[''] = 'Please Select An Opion';
foreach ($rosterList AS $ally)
{
    $allies[$ally->id] = $ally->rosterName;
}

 

View:

 

<?php echo form_label( 'Ally 1', 'ally1'); ?>
    <div>
        <?php echo form_dropdown( 'ally1', $allies, ''); ?>
   </div>
<?php echo form_label( 'Ally 2', 'ally2'); ?>
    <div>
        <?php echo form_dropdown( 'ally2', $allies, ''); ?>
    </div>
<?php echo form_label( 'Ally 3', 'ally3'); ?>
    <div>
        <?php echo form_dropdown( 'ally3', $allies, ''); ?>
    </div>

 

 

Link to comment
Share on other sites

 

Your posted code does not make any sense.  Your using a foreach on an array you just created which has only one element.  Inside the foreach your doing an operation that seems mostly pointless.  Here's a version of the code that I think maybe you intended, but not sure.  I wrapped the foreach in an if statement so it will only run if your getAllRoster function returns an array.

 

$rosterList = $this->bios->getAllRoster();
$allies = array();
$allies[''] = 'Please Select An Opion';
if (is_array($rosterList)){
    foreach ($rosterList AS $ally)
   {
      $allies[$ally->id] = $ally->rosterName;
   }
}

Link to comment
Share on other sites

$allies = array();

Here, you are setting up the $allies array. There is nothing stored in this array after you initialize it.

$allies[''] = 'Please Select An Opion';

This is a really weird index name. I would suggest changing it to something alphanumeric.

foreach ($allies AS $ally)
{
    $ally[$ally->id] = $ally->rosterName;
}

Here you are doing a foreach through an array that only has one element ('Please Select An Opion') and doing something (strange) with it. You are calling a method on an array index (that doesn't exist) as if it were an object instance, which won't work unless that element of the array is actually an object.

 

You need to populate your actual $allies array with something before you foreach through it.

 

 

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.