New array from a foreach

7 posts by 4 authors in: Forums > CMS Builder
Last Post: August 13, 2010   (RSS)

Re: [rjbathgate] New array from a foreach

Hi, RJ -

This all sounds very familiar! I had a similar problem with a menu system...

In order to style the menu it was necessary to identify the first and last items in each drop-down. This should be easy enough - until you start filtering entries with if/{continue} arguments.

When this happens, first and last aren't always where you expect them to be!

The only thing you can do is pass each filtered menu item into another array with one foreach loop - and then display it with another foreach loop!

Does your head hurt yet?

:0)

Perchpole

Re: [rjbathgate] New array from a foreach

By Jason - August 12, 2010

Hi,

So you're going to run through your list of records once and create a new array, and then display the array you just created. Is that right?

If you could give me some more details on what would be in that second array, I can try to give you some examples on how that would work.

Thanks.
---------------------------------------------------
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] New array from a foreach

Hi,

One (of a few) instances I need this is a follow up job to some of the custom work Ross/you are doing for me -- on the vehicle pricing!

When Ross has finished it will hopefully work like this:

list all vehicles, based on criteria of a standard search

foreach vehicle
{
Display vehicle info
Run a custom code (ross working on) to output variable $totalprice
Display $totalprice
}


All well and good.

I want to be able to do the following with $totalprice

a) allow the records to be sortable by price (high > low/low > high)

b) allow the search engine to have a max_ and min_ total price

For the above two, the array displayed needs to include the $totalprice as part of it... as far as I cna understand.

Using continue; within the foreach if the $totalPrice doesn't fit within max_ for example meses up Next/Previous links and also doesn't work with Sorting.

This is early in the morning for me, so the below might be stupid, but one thought:

list all vehicles, based on criteria of a standard search

foreach vehicle
{
Run a custom code (ross working on) to output variable $totalprice
add into new array $newVehiclesArray the variables $totalprice and $vehiclenum
}

foreach in newVehiclesArray
{
look up vehicleRecord in CMS where num = $vehicleNum (allowing us to display all the standard CMSB data for this record)

display $totalprice
}


In my head, making the new array of num and totalprice and then using that num to pull back the cmsb vehicle record seems logical.

And possibly possibly just by creating a new array and pushing records to it in each foreach? And then I'd just need to write a foreach based on that new array.

But is there a massive strain of calls to the database doing that?

And does that make no sense?

Cheers!

Re: [rjbathgate] New array from a foreach

By Chris - August 12, 2010

Hi rjbathgate,

That's the main concern with your approach, yes: on every search page, you'd need to load all the records from the database.

Depending on the complexity of the totalprice calculation, it may be possible to get MySQL to calculate it for you during the SELECT, allowing you to use it to paginate and sort.

Another option would be a custom plugin which automatically updates a totalprice field whenever a record in that section is saved.

I'd need to see the specifics on how totalprice is calculated to figure out what approaches would make sense.

I hope this helps! Please let me know if you have any questions.

P.S. Rather than pushing the $totalprice and $vehiclenum into your new array, it would probably be better to push the $totalprice and the entire $record into the array -- then you won't need to look anything up by num!
All the best,
Chris

Re: [chris] New array from a foreach

Hey Chris,

Cheers for the reply.

You mention "on every search page, you'd need to load all the records from the database." --- I'd still be applying the normal search engine criteria so surely it's not different to running it normally?

I'd just be loading the given results, twice... ? And since it's perPage limited, it's only going to be say 20 results max = 40 = not too many...

I think.

I think the totalprice calculation is too complex during the SELECT - it involves relationships between 3 other tables...

And I think the updating totalprice field when a record is saved isn't applicable as it's for a constant quotation system - not wanting to add/save anything to the records as there will be potentially multiple quotes going on at one time.

So, if i take my option (if it's not too many database calls), do you have any sample code whcih lets me create the new array during the original foreach, and then run a new foreach based on this array?

Thanks!

Re: [rjbathgate] New array from a foreach

By Chris - August 13, 2010

Hi rjbathgate,

You're right in that other search criteria, if present, will trim down the number of records you need to get, but pagination needs to be redone.

Consider this: if you wanted to display the second page of results of people with a phone number which is a prime number, which pages of the phone book would you need to look at?

Since one of your criteria is dependent on examining the result set, the simplest solution is to return the entire result set and then paginate in PHP.

I think the totalprice calculation is too complex during the SELECT - it involves relationships between 3 other tables...


SQL tends to be pretty good at this kind of thing. ;)

So, if i take my option (if it's not too many database calls), do you have any sample code whcih lets me create the new array during the original foreach, and then run a new foreach based on this array?


Sure:

<?php

list($phone_bookRecords, $phone_bookMetaData) = getRecords(array(
'tableName' => 'phone_book',
));

// build a new array from some of the records
$newArray = array();
foreach ($phone_bookRecords as $record) {
$isPrime = is_prime($record['phone_number']); // example: function not provided
if ($isPrime) {
$newElement = array($record, $isPrime); // example: storing extra information with the record
array_push($newArray, $newElement);
}
}

// crop out the requested page of results
$perPage = 10;
$pageNum = @$_REQUEST['page'];
if (!$pageNum) { $pageNum = 1; }
$newArrayCurrentPage = array_slice($newArray, ($pageNum - 1) * $perPage, $perPage);

?>

<ul>
<?php foreach ($newArrayCurrentPage as $row): ?>
<li>
<?php list($record, $isPrime) = $row; ?>
<?php echo $record['name'] ?>
<?php echo $record['phone_number'] ?>
<?php echo $isPrime ?>
</li>
<?php endforeach ?>
</ul>


Does that help? Please let me know if you have any questions.
All the best,
Chris