Inserting Page List into form->select array


I'm pulling a page list into a custom block edit.php in order to populate values in a dropdown list in a select field I'm generating from the form helper.

Ultimately, in the array of pages, I expect that the page collection ID would be the value and the page collection name would be the option text.

So THIS is what I'd like to achieve...

<select name="my_select_field" id="my_select_field" class="ccm-input-select">
<option value="123">My Awesome Page</option>
<option value="542">My Other Awesome Page</option>
<option value="983">Only Sort of Awesome Page</option>

Only, that's not what I'm getting at all. It seems no matter what I've tried in the array to get the page ID to be the option value, it gets ignored.

So here is the code I'm using to grab the page list and create the array.

$pl = new PageList();
// I'm specifying a parent page to look under / filter by here
$cParentID = '503';
$pages = $pl->getPage();
$venues = array();
foreach ($pages as $page) :
  $venues[$page->getCollectionID()] = $page->getCollectionName();
// I'd like to put these into the array
// I don't think that's a problem right?
array_unshift($venues, "-- Choose One --");
array_push($venues, "To Be Determined");

My select form code looks like:
echo $form->select('my_select_field', $venues, $my_select_field);

As I said, I'm getting regular 0,1,2,3,etc as my option values instead of the page ID like I expected. So the output looks like this:

<select name="my_select_field" id="my_select_field" class="ccm-input-select">
<option value="0">My Awesome Page</option>
<option value="1">My Other Awesome Page</option>
<option value="2">Only Sort of Awesome Page</option>

I tried to dump the page ids into one array and then the titles into another and then do an array_combine but had zero luck with that too.

Thanks in advance for any help! :D

View Replies:
exchangecore replied on at Permalink Reply
Hmmm.. Maybe the array unshift is nabbing you? Not sure if PHP *thinks* that those collection ID's are a numeric key or not but from the PHP docs:

array_unshift() prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won't be touched.
JohntheFish replied on at Permalink Reply
You can insert the variable dump or logging code in this howto to see just where $venues is going astray:

I expect push does the same as unshift, re-indexing numerically.
(EDIT - for some reason I missed @exchangecore's answer until after I had posted, so have removed duplication)

$venues = array("-- Choose One --");
foreach ($pages as $page){
  $venues[$page->getCollectionID()] = $page->getCollectionName();
$venues[] = "To Be Determined";