sorting label problem

6 posts by 2 authors in: Forums > CMS Builder
Last Post: March 7, 2011   (RSS)

By rez - March 7, 2011

I am using:
<ul>

<?php foreach ($draft_listRecords as $draft): ?>
<?php if ($draft['available']):?>

<li><?php echo $draft['brewer:label'] ?>&nbsp;<?php echo $draft['name'] ?></li>
<?php endif ?>


<?php endforeach ?>
<?php if (!$draft_listRecords): ?>
<li>We are making changes online. Please check back.</li>
<?php endif ?>
</ul>


Brewer is from another editor. When trying to sort in the top of the page, its sorting by brew number instead of label. Is their a way in the body to sort by brewer:label alphabetically? (or some other easy way or at the top?)

I had a some nested loops working for this before and the results were what I needed but it slowed everything down, tremendously. Hoping this newer label situation will work. Its nice and fast. :)

Re: [rez] sorting label problem

By Jason - March 7, 2011

Hi,

The problem here is that we're trying to sort on a value that is not stored in the section where we're returning records.

One way to get around this, would be to select all the records from your brewer section, sorting them by name. You can then assign each of those names to an array. Next, you loop through all of your draft_listRecords and add each record to the brewer array based on it's brewer label. For example:

list($brewerRecords, $brewerMetaData) = getRecords(array(
'tableName' => 'brewer',
'allowSearch' => false,
'orderBy' => 'name ASC'
));

$sortedByBrewers = array();

//create array of brewer numbers
foreach($brewerRecords as $brewer){

$sortedByBrewers[$brewer['num']] = array();

}

//assign each draft list record to it's appropriate brewer record
foreach($draft_listRecords as $draft){

$sortedByBrewers[$draft['brewer']][] = $draft;

}


By the end of this code, you can output $sortedByBrewers the same way you were outputting $draft_listRecords, but they will be sorted alphabetically by brewer name.

Hope this helps get you started.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] sorting label problem

By rez - March 7, 2011 - edited: March 7, 2011

Thanks Jason. I'll give this a shot.

Did I set up the editors best way? It seems a little backwards here maybe but the thing is, new draft beers will be added regularly. I needed the administrator to be able to add new brewers to the brewer list/editor if they didnt exist or if they did exist, quickly enter the draft and assign the brewer from a drop list avoiding spelling errors and differences. But for sorting, by brewer seems most convenient. (ex. brewer: Blue Moon, Name: White)

In the future, i want to list new and rated beers and brewers all over the place. (hurry up with those ratings and comments plugs already ;) )

Re: [rez] sorting label problem

By Jason - March 7, 2011

Hi,

Yes, it sounds like you've set up the editors a good way. Whenever you have lists that will be added to regularly, it's best to keep them in a separate section and using "num" as your value instead of the name is the best way to ensure uniqueness. It also allows you to change the name of a list value without having to resave all the records associated with it!

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [rez] sorting label problem

By Jason - March 7, 2011

Hi,

You're really close. What's happening is that $sortedByBrewers is an array of arrays, so we need a second loop to take care of this.

Try this code:

<ul>

<?php foreach ($sortedByBrewers as $brewer => $draftRecords): ?>

<?php foreach($draftRecords as $draft): ?>

<?php if ($draft['available']):?>

<li><?php echo $draft['brewer:label'] ?>&nbsp;<?php echo $draft['name'] ?></li>
<?php endif ?>

<?php endforeach ?>

<?php endforeach ?>

<?php if (!$sortedByBrewers): ?>
<li>We are making changes online. Please check back.</li>
<?php endif ?>

</ul>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/