New array from a foreach

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

By rjbathgate - August 11, 2010 - edited: August 11, 2010

Hello,

I have listviewer running on perPage. During the foreach stage, it runs a script to output a variable.

I want that variable available for the where of the listviewer - which is backwards/impossible.

So, need to do something like this:

load all records
foreach
{
run script and generate variable = xxxx
}

now load all records again but with variable xxx available


This is normally easy, but I need the use the SECOND set of records in the perPage.

So, I take from that I need do to this:

list all records
foreach
{
run script and generate variable = xxxx
add this record and new variable into array
}

now list this newly created array

foreach in this new array
{
}
perPage based on this new array


Dave in his expert wisdom has tackled a similar scenerio a few times for me before but I'm not sure if the above principle is right.

If it is, how do I create the new array and then call it for the subsequent foreach?

If it isn't the right way... any pointers?!

If none of this makes sense, sorry!

Cheers

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: [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