Welcome Guest, Not a member yet? Register   Sign In
Calculated WHERE based on database field before realising query
#7

[eluser]jonez[/eluser]
Glad you solved it. You could do it with a match table as you mentioned before, but it's not as efficient since you're doing a query to build the match table then another query to find properties. Using a JOIN is a better solution.

Here's a snippet I use sometimes, from MY_array_helper.php:
Code:
function delegate_array( $arr, $key ) {
$results = array( );

foreach ( $arr as $data ) {
  $results[ $data[ $key ] ] = $data;
}

return $results;
}

What this does is make a given key the index for the array. You use it like this:

Code:
// data retrieved by result_array, 0-n indices

$data = array(
    array( 'id' => 123, 'name' => 'Joe' ),
    array( 'id' => 456, 'name' => 'Bob' ),
);

$data = delegate_array( $data, 'id' );

var_dump( $data ) produces:
Code:
// id key as indices

array(
  '123' => array( 'id' => 123, 'name' ='Joe' ),
  '456' => array( 'id' => 456, 'name' => 'Bob' ),
)

Which lets you access items by their ID, using $data[ $id ].


Messages In This Thread
Calculated WHERE based on database field before realising query - by El Forum - 06-30-2014, 05:36 AM



Theme © iAndrew 2016 - Forum software by © MyBB