Advertisement:

Author Topic: How to retrieve custom profile field and display on website outside smf  (Read 6095 times)

Offline james78

  • Semi-Newbie
  • *
  • Posts: 31
Hi this is my first post so please bear with me if i get confused or things wrong,
I have created some custom profile fields in forum smf 2.0 rc3 which all is ok and display correctly however i have a site which is outside forum, site is at somewhere.co. and forum is at somewhere.co.forum/, we use the ssi include thing at top of webpage so we can use ssi_welcome etc but don't see any function to include custom profile fields.

I would like to be able to show what a member has written in custom profile field on main site.
i have copyed a bit of code from one of the boards but this retrives the info but for all members and i only require it for the logged in member and if they are not logged in just to show nothing or something like no data

here is the script which retrives the data but retrives it for all members



mysql_connect("xxxxxx","xxxxxx","xxxxxx");
mysql_select_db("xxxxxx");

$select = "SELECT id_member  FROM smf_themes WHERE variable = 'cust_xbox36' ORDER BY id_member";
$query = mysql_query($select)or die(mysql_error());

echo "<table>";
echo "<tr>";
echo  "<td>Member id no </td><td>member name</td><td>data from input field cust_xbox36</td>" ;
echo "</tr>";

while($list = mysql_fetch_object($query))
{
echo "<tr>";
$id = $list->id_member;



//--- get the instrument from the database
$sql = "SELECT value FROM smf_themes WHERE id_member = " . $id . " AND variable = 'Cust_xbox36'";
$res = mysql_query($sql);
$xbox = mysql_result($res, 0);



//--- get the membername from the database
$sql = "SELECT member_name FROM smf_members WHERE id_member = " . $id;
$res = mysql_query($sql);
$memberName = mysql_result($res, 0);

//--- echo the results

echo  "<td>$id </td><td>$memberName</td><td>$xbox</td>" ;
echo "</tr>";
}
echo "</table>";


I hope someone can help me on my way I am sure it is to do with paticular words used in query but it has baffled me so i pled that someone can show me an example

many thanks in advance

forum version is 2.0 RC3
« Last Edit: May 03, 2010, 04:25:21 PM by james78 »

Offline CapadY

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 3,151
  • Gender: Male
It is impossible to give an answer without knowing the current structure of youre database.

When I look at the first query, you try to select a member_id from the themes table. Normaly there is no member_id in the themes table.

Thats also the reason i didn't had a look at the rest of the queries.

Withouth full infor it's imposible to give a view of it.

Please, don't PM me for support unless invited.
If you don't understand this, you will be blacklisted.

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,982
    • StoryBB/StoryBB on GitHub
When I look at the first query, you try to select a member_id from the themes table. Normaly there is no member_id in the themes table.

No, but there IS an id_member column there, since custom profile fields ARE stored in that table. The table structure is id_member, id_theme, variable, value.

In this case, variable will be dependent on the name of custom field, and id_theme should be 1.
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline james78

  • Semi-Newbie
  • *
  • Posts: 31
Hi my database is standard for smf_themes which is structured as stated by Arantor.
forum is rc3 which has built in custom profile fields and if a user has entered text in a paticular field then they appear in smf_themes with their id_member which is either 1 or more and id_theme is entered as 1 then variable which is cust_whatever i named my cpf and then value which is the text that is entered into field, as shown below,

id_memberid_themevaraiblevalue
11cust_xbox36sometext1
21cust_xbox36sometext2

so could someone perhaps show me how when i log in it checks what my id_member is and say it is 1 then it looks in db and checks to see if there is a cust_xbox36 with id_theme =  1 and them echo out the value field

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,982
    • StoryBB/StoryBB on GitHub
First up, use SSI.php. include() that on the page, and it will figure out what the id_member is, trying to do that yourself is a LOT of work.

Then you just check $context['user']['id'].

Note that SSI.php will also deal with setting up the database connection for you and make $smcFunc available, which should be used for queries.
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline james78

  • Semi-Newbie
  • *
  • Posts: 31
hi we have this at the top of every .php page like stated in examples is this the same as include
<?php require("forum/SSI.php");  then php code closes

would it be possible for someone to show some example of using smcfunc to retrive the data and how it is printed or echo out please
sorry to ask for so much help i am trying to learn , i get the principle but putting that into code goes horribly wrong

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,982
    • StoryBB/StoryBB on GitHub
Code: [Select]
<?php

require('forum/SSI.php');

if (empty(
$context['user']['id']))
  echo 
'The current user is a guest';
else
{
  echo 
'The current user: id '$context['user']['id'], ', username '$context['user']['name'], '<br />';

  
$query $smcFunc['db_query']('''
    SELECT value
    FROM {db_prefix}themes
    WHERE id_member = {int:member}
      AND variable = {string:variable}'
,
    array(
      
'member' => $context['user']['id'],
      
'variable' => 'cust_xbox36',
    )
  );

  
$row $smcFunc['db_fetch_assoc']($query);
  
$smcFunc['db_free_result']($row);

  echo 
'The value of the custom field is '$row['value'];
}
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline james78

  • Semi-Newbie
  • *
  • Posts: 31
Hi Arantor you are a legend, I just have a quick question, I put this in place and all is brillant it returns value of field etc but it also presents an warning/argument which i can get rid of by deleting line 23 but just wondered if that was correct to do as i thought it was there to end the connection but wondered if it would present further problems later by deleting it.
I have shown the error below

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/xxxxxx/public_html/xxx/profilefield.php on line 23

line 23 is this code
$smcFunc['db_free_result']($row);
sorry to ask just thought it better to do so, so i can learn what the error is
but once again i cannot thankyou enough for all your help

Offline Arantor

  • Resident Overthinker
  • SMF Friend
  • SMF Legend
  • *
  • Posts: 71,982
    • StoryBB/StoryBB on GitHub
Bah.

It was supposed to be $smcFunc['db_free_result']($query) not $smcFunc['db_free_result']($row)

The line ensures any resources used by MySQL are freed, so it should be a query resource (like $query, the direct result of a query being run) as opposed to a fetch-assoc which isn't a query resource.
Don’t try to tell me that some power can corrupt a person. You haven’t had enough to know what it’s like.

No good deed goes unpunished / No act of charity goes unresented.

Offline james78

  • Semi-Newbie
  • *
  • Posts: 31
Thankyou so much again, This has taught me a lot, thankyou