an array of objects?

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
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

an array of objects?

Post by Sevengraff »

Im trying to make an array of objects, and its not working. I want to use it so that i can load some mysql data into it, and then sort it for output. but when i try to do it, everything prints out as object->element name. its strange that it doesnt give an error.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php
...
$query = 'SELECT id from tablename';
$result = mysql_query($query, $conn);
$objs = array();
while( $row = mysql_fetch_object($result) )
	$objsї] = $row;
...
for ($i=0; $i!=count($objs); $i++)
	echo $objsї$i]->id, '<br/>';
?>
although I like the array-versions mysql_fetch_row and mysql_fetch_assoc more ;)
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

hmm, i should check my email for replies more often...

I tried using the example posted above, and modified it to work like i wanted, but it didn't give the desired results.

I am trying to re-create something i've done in my C++ class. There i had to load data from a file into an array of structs and use a swap fuction to sort the data in order. i cant seem to get the same thing in PHP.
how would i do this:

- get data from mysql table (select * from users where id=$id is what i will use)
- load into array (or maybe some other grouping).
- sort it (i've allready writen a function to do this)
- print in descending order

thats sorta the algorithm.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

e.g.

Code: Select all

<?php
function descendingLastNameSort($a, $b)
{
	return -strcmp($a['lastname'], $b['lastname']);
}

$conn = mysql_select(....);
mysql_select_db(..., $conn);

$query = 'SELECT * FROM users WHERE id='.$id; // having a field lastname
$result = mysql_query($query, $conn);

$arr = array();
while($row = mysql_fetch_assoc($result))
	$arr[] = $row;

$arr = usort($arr, 'descendingLastNameSort');

echo '<table>';
foreach($arr as $entry)
{
	echo '<tr>';
	foreach($entry as $field)
		echo '<td>', $field, '</td>';
	echo '</tr>';
}
echo '</table>';
?>
but much simpler and probably faster when done by

Code: Select all

<?php
$conn = mysql_select(....);
mysql_select_db(..., $conn);

$query = 'SELECT * FROM users WHERE id='.$id.' ORDER BY lastname DESC';
$result = mysql_query($query, $conn);

echo '<table>';
while($row = mysql_fetch_assoc($result))
{
	echo '<tr>';
	foreach($row as $field)
		echo '<td>', $field, '</td>';
	echo '</tr>';
}
echo '</table>';
?>
(both script not even tested by compiler ;) )
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

oh my, i wasnt aware of "ORDER BY lastname DESC", that makes things a lot easier!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Post Reply