Xtremer360 Posted April 16, 2012 Share Posted April 16, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/261044-default-option-and-if-returning-null/ Share on other sites More sharing options...
kicken Posted April 16, 2012 Share Posted April 16, 2012 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/261044-default-option-and-if-returning-null/#findComment-1337840 Share on other sites More sharing options...
batwimp Posted April 16, 2012 Share Posted April 16, 2012 $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. Quote Link to comment https://forums.phpfreaks.com/topic/261044-default-option-and-if-returning-null/#findComment-1337841 Share on other sites More sharing options...
Xtremer360 Posted April 16, 2012 Author Share Posted April 16, 2012 @kicken even if the returned function returns null will it still create the array and add that to the empty index for the view in your idea which looks good btw Quote Link to comment https://forums.phpfreaks.com/topic/261044-default-option-and-if-returning-null/#findComment-1337845 Share on other sites More sharing options...
Xtremer360 Posted April 16, 2012 Author Share Posted April 16, 2012 Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/261044-default-option-and-if-returning-null/#findComment-1337882 Share on other sites More sharing options...
Xtremer360 Posted April 17, 2012 Author Share Posted April 17, 2012 *bump* Quote Link to comment https://forums.phpfreaks.com/topic/261044-default-option-and-if-returning-null/#findComment-1338142 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.