News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

How to retrieve custom profile field and display on website outside smf

Started by james78, May 03, 2010, 04:05:35 PM

Previous topic - Next topic

james78

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

CapadY

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.

Arantor

Quote from: capady on May 03, 2010, 05:25:17 PM
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.

james78

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

Arantor

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.

james78

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

Arantor

<?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'];
}

james78

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

Arantor

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.

james78


Advertisement: