need help with php code for plugin

17 years 10 months ago #13303 by Raj
I'm looking at some of Jeffrey Randall's code for his Random Profile Module (see below). I understand everything down to...

foreach($usernames as $username)

...which has me stumped. I understand that the username is held in the __users table, not in the __comprofile table, so a query to the users table is necessary to extract the username.

However, user ids are unique, so I don't understand the statement foreach($usernames as $username). Presumably, $usernames refers to a collection of records, but there's only one record per user...yes? SO why have this statement at all?

Roger

$query = "SELECT user_id,avatar FROM #__comprofiler WHERE avatarapproved=1 AND avatar NOT LIKE 'NULL' AND banned='0' ORDER BY RAND() LIMIT $number";
$database ->setQuery ($query);
$results = $database->loadObjectList();

foreach($results as $result){
if(preg_match('/(gallery)/',$result->avatar)){
$query_name = "SELECT id,username FROM #__users WHERE id = ".$result->user_id;
$database ->setQuery ($query_name);
$usernames = $database->loadObjectList();

foreach($usernames as $username){
$profileName = $link_gallery.$result->avatar."\"border=\"0\" alt=\"member image\"$setwidth$setheight/>\n<br/>$username->username</a>$MeMambo\n";
$profile .= "$MamboMe<a href=\"index.php?option=com_comprofiler&task=userProfile&user=".$result->user_id."\">$profileName";
}

Post edited by: Raj, at: 2006/05/19 03:01

Please Log in to join the conversation.

17 years 10 months ago #13392 by ericfoster3
Replied by ericfoster3 on topic Re:need help with php code for plugin
This code could have been borrowed from something previous. He probably just left in the foreach()

It doesn't seem to hurt anything. Although it is a seemingly pointless loop.

Post edited by: ericfoster3, at: 2006/05/20 03:50

CB 3rd Party Developer: DatsoTab | KarmaTab
Joomla! 1.0.10
Community Builder 1.0
joomlaboard 1.1.2

Please Log in to join the conversation.

17 years 10 months ago #13400 by mikko
Replied by mikko on topic Re:need help with php code for plugin
That foreach is there because it is the easiest way to fetch the first element of an array. If you refer to that directly, and for some reason it does not exist, the script fails. With foreach, there is some kind of tolerance for incomplete data.

mikko

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.197 seconds

Facebook Twitter LinkedIn