Find current position of object in parent object array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Find current position of object in parent object array

Post by mcccy005 »

Sorry about the poor title. This is in brief to do with pagination (which I'm still trying to conquor slowly slowly).
I have three objects:
result_set (stores the entire list of results for each webpage)
result_field (stores each row of the result set)
result_cell (stores each cell in each row)

Inside result_cell, I'm trying to find out what position (ie. what column) the current result_cell object is.
I have the following code (which I can't yet test as I've got a lot of code to finish before I can):

Code: Select all

//Stores a copy of the cells array stored in the parent object (result_field) of this result_cell object
     private $parent_cells_array=$this->parent_result_field->cells_array;
			
/*Traverses through the cells array to see which position (column) this cell object is located in the result set */
     for($i=0; $i<sizeof($parent_cells_array); $i++)
     {
	if($parent_cells_array[$i]===$this)	//If array value is equal to exactly this object
	{
		$column_number=$i;
		break;
	}
}
What I want to know though, is will the "===" work if somebody changes one of the variables inside of the result_cell object.
The above code is called inside a display( ) method, so in between the display( ) method being called and the result_field in the parent object being created, something could change??
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Wihtout questioning your intent (as I haven't the foggiest why you would want this :p) you can do this:

Code: Select all

<?php

$key = array_search($this, $parent_cells_array, TRUE);

?>
Though it's untested.

However, if you have a copy of the object somewhere, and the param's/properties are all the same then you will get a 'false positive'

e.g. (php4):

Code: Select all

<?php
$a = new stdClass;
$b = $a;

echo ($a === $b) 'true' : 'false';

?>
Also untested.. there could be something quiffy about comparing objects in PHP I am blissfully unaware of..
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

Well I'll post the reason why I'm doing this should this help someone provide me with a better solution.

The idea is that if a user clicks on the title of the result set, the results will be ordered based on the data in the particular column which the user clicked on. (Ie. if there is numerical values, the entire result set will be ordered based on numerical values in that particular column.)

Thus, in order to parse the column number using the$_GET[ ] array, I need to find which particular column the current cell_object is in (ie. which element in the array of result cells stored in the parent) that the parent object.

I think I get what you mean with that false positive thing. And yes I will have a copy of the object, which probably means I don't actually have to worry about anybody changing any values then!
I think my method might actually even be the best option (although I wouldnt have known it without your info on a false positive).
Post Reply