extended profile-field display snippet
larry - October 5, 2007 - 14:11
Could someone please enlighten me as to how I could make the output here links rather than just a text list...displaying the appropriate profile-field select-options? In this case, the output is a list of countries...but they are not displayed as links.
<?php
$fid = 12;// The field id (fid) of the profile field I am interested in
$sql = "SELECT value, COUNT(uid) as count FROM profile_values WHERE fid = %d GROUP BY value";
$results = db_query($sql, $fid);
while ( $data = db_fetch_object($results)) {
// use an if statement to replace users with empty fields with a custom label
if ($data->value == '') {
$data->value = 'Unknown Country';
}
if ($data->count > 0) {
$output .= $data->value . ' (' . $data->count . ')<br />';
}
}
print $output;
?>This code:
if ($data->count > 0) {
$output .= $data->value . ' (' . $data->count . ')<br />';
}This chunk of code from above yields my list nice and pretty...just as plain text. For the life of me I cannot make the text list clickable links displaying the relevant page from my extended profile field. Any help is appreciated.
cheers,
larry

Here is the simple version
This is borrowed from the profile module, it is the code for producing the link but lacks the checks made by the profile module. If the field in question is open to the public and has a 'page' defined this will work fine (otherwise there is more code needed)
Replace
$output .= $data->value . ' (' . $data->count . ')<br />';with
$output .= l($data->value, 'profile/{your profile field name}/'. $data->value) . ' (' . $data->count . ')<br />';Note you will need to replace '{your profile field name}' with your profile field's name, typically something like profile_color.
Very nice...
Thanks nevets.
You said that "it is the code for producing the link but lacks the checks made by the profile module. If the field in question is open to the public and has a 'page' defined this will work fine (otherwise there is more code needed)." I actually have the code in a block with the block's permission's being defined by drupal and everything works fine as you said. My question is...without the additional checks is this some sort of security issue? Silly question and I think not, but you know far more about such issues as I do. Thanks again for the code. I am grateful for your help.
cheers,
larry
--There are no Kangaroos in Austria--
The code is from profile_view_field in the profile module
At the start of the function they have this code
$browse = user_access('access user profiles')&& (user_access('administer users') || $field->visibility != PROFILE_PRIVATE)
&& !empty($field->page);
Which will set $browse to true only if (the user has access to profiles) AND (they can administer users OR the field is not private) AND (there a page to display the results on).
Then they only show the link if $browse is true (otherwise just the value).
$field comes from the tabl profile_fields.
The reason I pointed this out is if the profiile field in question is private or does not have a display page there is an issue. If some users can not view profiles there may be a security issue but that depends on the blocks permissions.
Thanks for explaining that
Thanks for explaining that to me. Since this particular profile field is public and does have a display page...there is no issue, but I had to ask just to be certain. I'm a bit paranoid with security issues, which is why I use Drupal...I can sleep. Once again I thank you for your help. I hope one day I can return the favor.
cheers,
larry
--There are no Kangaroos in Austria--