SimplePortal

Customization => Custom Coding => Topic started by: Tarista on June 10, 2015, 05:22:49 PM

Title: Grab avatar in query (custom block)
Post by: Tarista on June 10, 2015, 05:22:49 PM
Hi,

I apologize, I wrote a long topic about how to modify a code and then ended up figuring out how to do it myself about half an hour later.

I know I could have answered my own question and left the content but it was not a complete block modification anyway so it wouldn't have worked out of the box for others :) I thought that it was better not taking up the recent posts.

Sorry about the unnecessary topic :)

Thanks!


Adding in what I removed since it could be useful according to replies :)

Here is the original topic:
Hope this is the right board for this :)

I'm trying to figure out how to add some information from the members-part of the database to a query. At the moment I'm trying to grab the avatar. All this is then going in a php block with even more codes but I thought I should perhaps not flood the topic with code and think this (below) is the part needed.

If I could just get help to figure out how to add it from the members table to the query I'm hopeful of trying to figuring out how to build it into the array myself. I wouldn't want to waste anyone's time.

I see that the code I have already (code found and edited years ago) joins the members table so now I'm just trying to figure out how to grab the information. Is it as simple as adding a row with mems.avatar and meml.avatar? (seems almost too simple?)

Code: [Select]
if ($do_query == 1)
{
$request = $smcFunc['db_query']("", "
SELECT
      ms.subject AS firstSubject, ms.poster_time AS firstPosterTime, ms.id_topic, t.id_last_msg, t.id_board, b.name AS bname,
      t.num_replies, t.num_views, ms.id_member AS ID_FIRST_MEMBER, ml.id_member AS ID_LAST_MEMBER,
      ml.poster_time AS lastPosterTime, IFNULL(mems.real_name, ms.poster_name) AS firstPosterName,
      IFNULL(meml.real_name, ml.poster_name) AS lastPosterName,
      mems.id_group as mems_group, meml.id_group as meml_group,
      ml.subject AS lastSubject, b.member_groups,
      t.is_sticky, t.locked, ml.modified_time AS lastModifiedTime,
      LEFT(ml.body, 384) AS lastBody, LEFT(ms.body, 384) AS firstBody,
      ml.smileys_enabled AS lastSmileys, ms.smileys_enabled AS firstSmileys, t.id_first_msg,"
      . ($user_info['is_guest'] ? '1 AS isRead, 0 AS new_from' : '
      IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) >= ml.id_msg_modified AS isRead,
      IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from') . "
   FROM (".$db_prefix."messages AS ms, ".$db_prefix."messages AS ml, ".$db_prefix."topics AS t, ".$db_prefix."boards AS b)
      LEFT JOIN ".$db_prefix."members AS mems ON (mems.id_member = ms.id_member)
      LEFT JOIN ".$db_prefix."members AS meml ON (meml.id_member = ml.id_member)
      LEFT JOIN ".$db_prefix."log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = ".$user_info['id'].")
      LEFT JOIN ".$db_prefix."log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = ".$user_info['id'].")
   WHERE t.id_topic = ms.id_topic
      AND b.id_board = t.id_board" . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? " AND b.id_board != ".$modSettings['recycle_board'] : "") . "
      AND ms.id_msg = t.id_first_msg
      AND ml.id_msg = t.id_last_msg
      AND " . $user_info['query_see_board'] . "
   ORDER BY " . $order_clause . " " . $limit_clause,
 array());

Thank you for taking the time to read my question!
Title: Re: Remove this please :)
Post by: Burke Knight on June 10, 2015, 06:10:14 PM
1. There was no reason to remove it.
2. You could have explained how you did it, in case someone else had same issue.
Title: Grab avatar in query (custom block)
Post by: Tarista on June 10, 2015, 07:38:02 PM
Alright :)

I just didn't want to bother people and was uncertain if this was even the right board since my custom block code might be considered unrelated to simpleportal even if I use it in a block with simpleportal. I apologize for not taking the time to explain it instead. I thought it wouldn't be of any use since I'm not really sure if I did it correctly anyway and the codes are still incomplete.

What I ended up doing was actually trying to change the query step by step. I studied how the "mems.id_group as mems_group, meml.id_group as meml_group" was built, compared it to other recent-blocks I found here on this site and then checked the database to find as I suspected that id_group is indeed in the members-table. I then copied that onto a new line and changed the structure/parts of the new line.

Code: (This is the new line) [Select]
mems.avatar as mems_avatar, meml.avatar as meml_avatar
Added that to the query and tried previewing it in my block. It worked as intended and after adding it to the building array I could then actually use it with ['first_post']['member']['avatar'] :) No guaranties it was the correct way to do it since I'm beginner coder still using the trial and error-method.

I was uncertain if this was actually of any help to people so that is why I originally removed it. Also not sure if it was the right board.

If it's of any use and this was actually the correct board it could be left here if you want it to. If not, it can safely be removed since my problem is solved :)
SimplePortal 2.3.8 © 2008-2024, SimplePortal