Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Biology Forums on March 09, 2012, 06:33:30 PM

Title: Help me write this code...
Post by: Biology Forums on March 09, 2012, 06:33:30 PM
In messageindex... I want the following argument:

If the person who started the topic is in group 15 or 21, show the topic title in red.

I have narrowed down where I am supposed to make the changes in MessageIndex.php.

// 'Print' the topic info.
$context['topics'][$row['ID_TOPIC']] = array(
'id' => $row['ID_TOPIC'],
'first_post' => array(
'id' => $row['ID_FIRST_MSG'],
'member' => array(
'membergroup' => $context['redbold']['member'][$row['firstID_MEMBER']]['membergroup'],
'username' => $row['firstMemberName'],
'name' => $row['firstDisplayName'],
'id' => $row['firstID_MEMBER'],
'href' => !empty($row['firstID_MEMBER']) ? $scripturl . '?action=profile;u=' . $row['firstID_MEMBER'] : '',
'link' => !empty($row['firstID_MEMBER']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['firstID_MEMBER'] . '" title="' . $txt[92] . ' ' . $row['firstDisplayName'] . '">' . $row['firstDisplayName'] . '</a>' : (!empty($modSettings['MemberColorGuests']) ? '<span style="color:'.$modSettings['MemberColorGuests'].';">' : '').$row['firstDisplayName'].(!empty($modSettings['MemberColorGuests']) ? '</span>' : '')
),
'time' => timeformat($row['firstPosterTime']),
'timestamp' => forum_time(true, $row['firstPosterTime']),
'subject' => $row['firstSubject'],
'preview' => $row['firstBody'],
'icon' => $row['firstIcon'],
'icon_url' => $settings[$context['icon_sources'][$row['firstIcon']]] . '/post/' . $row['firstIcon'] . '.gif',
'href' => $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0',
'link' => '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0"'  . $row['nice_tooltip_first_msg'] . '>' . $row['firstSubject'] . '</a>'


Specifically where it says: 'link' => '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0"'  . $row['nice_tooltip_first_msg'] . '>' . $row['firstSubject'] . '</a>'

This is for 1.x
Title: Re: Help me write this code...
Post by: Biology Forums on March 11, 2012, 01:59:46 PM
Help!
Title: Re: Help me write this code...
Post by: DanCarroll on March 11, 2012, 04:45:07 PM
Liam, I am not familiar with that section of code but if I was really wanting to change the style myself given a certain condition I might look at jQuery or inline styles. Can you identify which ones you want to change? How about adding an inline <span style="color:red;">?


Title: Re: Help me write this code...
Post by: Biology Forums on March 11, 2012, 05:21:13 PM
Quote from: DanCarroll on March 11, 2012, 04:45:07 PM
Liam, I am not familiar with that section of code but if I was really wanting to change the style myself given a certain condition I might look at jQuery or inline styles. Can you identify which ones you want to change? How about adding an inline <span style="color:red;">?

I want it to be red when a person of a particular membergroup posts the topic. For instance, all topics started by users of membergroup 5 will have red topics.
Title: Re: Help me write this code...
Post by: DanCarroll on March 11, 2012, 06:12:48 PM
Okay, without developing the code for you, let's see what you have. You say you have the code to be modified narrowed down to the 'link' line of code? Do you have the logic set up to filter by the groups you need? If so, you can set a style class up in the anchor tag and then set the color in your style sheet. Or if you don't mind inline styles, just set the color style in your anchor tag.

Hopefully all of this makes sense to you. I don't know what your level of proficiency or understanding is but this is a problem that will require some php, html, and css to synthesize a solution.
Title: Re: Help me write this code...
Post by: Biology Forums on March 11, 2012, 09:57:12 PM
Quote from: DanCarroll on March 11, 2012, 06:12:48 PM
Okay, without developing the code for you, let's see what you have. You say you have the code to be modified narrowed down to the 'link' line of code? Do you have the logic set up to filter by the groups you need? If so, you can set a style class up in the anchor tag and then set the color in your style sheet. Or if you don't mind inline styles, just set the color style in your anchor tag.

Hopefully all of this makes sense to you. I don't know what your level of proficiency or understanding is but this is a problem that will require some php, html, and css to synthesize a solution.

I understand what you mean.

No, I don't mind in-line styles at all.

I just want all topics displayed in the message index to show a different color, let's say red, when the original author (topic starter) is in membergroup 15, for instance.
Title: Re: Help me write this code...
Post by: IchBin™ on March 12, 2012, 01:21:31 PM
Not as easy as you'd think. You'll need to pull the user's member group based on the ID of the $row['id_first_msg']. Most likely will cost you an additional query. Once you have their groups, then you can do a check to see if their groups match any of the groups you want. If it matches then you just change the 'link' section in the first post to put a style tag in with the color you want.
Title: Re: Help me write this code...
Post by: Matthew K. on March 12, 2012, 01:44:19 PM
As Brad said, for something so small and minor, it's really not worth the performance (although you wouldn't notice it, it's still there).
Title: Re: Help me write this code...
Post by: Biology Forums on March 12, 2012, 02:10:34 PM
Quote from: Labradoodle-360 on March 12, 2012, 01:44:19 PM
As Brad said, for something so small and minor, it's really not worth the performance (although you wouldn't notice it, it's still there).

I still would like this done, can anyone help.
Title: Re: Help me write this code...
Post by: Matthew K. on March 12, 2012, 02:11:10 PM
IchBin did. He told you exactly what you need to do.
Title: Re: Help me write this code...
Post by: Biology Forums on March 12, 2012, 02:32:17 PM
Quote from: Labradoodle-360 on March 12, 2012, 02:11:10 PM
IchBin did. He told you exactly what you need to do.

I understand what he means, but I don't know how to put it together. I'm really not a computer programmer.
Title: Re: Help me write this code...
Post by: IchBin™ on March 12, 2012, 03:07:52 PM
Don't be afraid to try Liam_michael. You'll never learn if you don't. If you get stuck you post your code and people can show you how to fix things.

Code (add above the code you posted) Select

$myquery = $smcFunc['db_query']('', '
SELECT id_group
FROM {db_prefix}members
WHERE id_member = {int:id_mem}',
array('id_mem' => $row['first_id_member'])
);
$style = '';
$mygroups = array(1,3);
$groups = array();
while($row2 = $smcFunc['db_fetch_row']($myquery))
$groups[] = $row2[0];

if(array_intersect($groups, $mygroups))
$style = 'style="color: red;"';


You can of course change the style color to be anything you want.

Code (Find in your code) Select
'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['first_subject'] . '</a>'


Code (Replace with this) Select
'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0" ' . $style . '>' . $row['first_subject'] . '</a>'
Title: Re: Help me write this code...
Post by: Matthew K. on March 12, 2012, 03:46:26 PM
He's not using SMF 2.0 as he said above, so the code would have to utilize db_query() rather than $smcFunc, and mysql_fetch_assoc() rather than $smcFunc['db_fetch_assoc']().

Furthermore, it may be more simple to use loadMemberData and loadMemberContext in this scenario.
Title: Re: Help me write this code...
Post by: Biology Forums on March 12, 2012, 04:12:21 PM
Don't be afraid to try Liam_michael. You'll never learn if you don't. If you get stuck you post your code and people can show you how to fix things.

Thanks for the encouragement, IchBin™. It's just that my knowledge in php is *very* limited. I know how to create simple arguments, but that's just it. I tried your code and I forgot to mention it's for 1.x.
Title: Re: Help me write this code...
Post by: IchBin™ on March 12, 2012, 04:28:41 PM
There's no need to use loadMemberData() when all your doing is grabbing the groups. You'll be pulling in much more information than you need.

Code change for SMF 1.1.x
$myquery = db_query("
SELECT id_group
FROM {$db_prefix}members
WHERE id_member = ". $row['first_id_member'],  __FILE__, __LINE__);

$style = '';
$mygroups = array(1,3);
$groups = array();
while($row2 = mysql_fetch_row($myquery))
$groups[] = $row2[0];

if(array_intersect($groups, $mygroups))
$style = 'style="color: red;"';


'link' => '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0" ' . $style . '>' . $row['firstSubject'] . '</a>'
Title: Re: Help me write this code...
Post by: Matthew K. on March 12, 2012, 04:40:12 PM
Forgot something that's supposed to be at the end of 1.1x queries.
, __FILE__, __LINE__
Usage:
$myquery = db_query("
SELECT id_group
FROM {$db_prefix}members
WHERE id_member = ". $row['first_id_member'], __FILE__, __LINE__
);
Title: Re: Help me write this code...
Post by: IchBin™ on March 12, 2012, 04:44:43 PM
Thanks, forgot about that. UPdated the post.
Title: Re: Help me write this code...
Post by: Matthew K. on March 12, 2012, 04:47:18 PM
No problem...I don't even think it really matters, I think it's more for recording the queries.
Title: Re: Help me write this code...
Post by: Biology Forums on March 12, 2012, 05:17:50 PM
Thanks guys, this next question stems from my poor php understanding... Where exactly do I place this code in messageindex.php?:

$myquery = db_query("
SELECT id_group
FROM {$db_prefix}members
WHERE id_member = ". $row['firstID_MEMBER'],  __FILE__, __LINE__);

$style = '';
$mygroups = array(1,3);
$groups = array();
while($row2 = mysql_fetch_row($myquery))
$groups[] = $row2[0];

if(array_intersect($groups, $mygroups))
$style = 'style="color: red;"';


I've attached my messageindex.php file for reference.
Title: Re: Help me write this code...
Post by: Matthew K. on March 12, 2012, 05:21:02 PM
You have to add it above the other code you need to modify.
// 'Print' the topic info.
$context['topics'][$row['ID_TOPIC']] = array(
Title: Re: Help me write this code...
Post by: IchBin™ on March 12, 2012, 05:32:26 PM
If you look at the code snippet I first posted you'll see this:

Code: (add above the code you posted) :)
Title: Re: Help me write this code...
Post by: Biology Forums on March 12, 2012, 05:45:06 PM
Quote from: Labradoodle-360 on March 12, 2012, 05:21:02 PM
You have to add it above the other code you need to modify.
// 'Print' the topic info.
$context['topics'][$row['ID_TOPIC']] = array(


I did, and I get this crazy error:

Please try again. If you come back to this error screen, report the error to an administrator.

It won't display the messages in the messageindex anymore :-\
Title: Re: Help me write this code...
Post by: Matthew K. on March 12, 2012, 05:50:56 PM
Go ahead and attach your MessageIndex file WITH the code you added.
Title: Re: Help me write this code...
Post by: Biology Forums on March 12, 2012, 06:02:57 PM
Thank you, here it is!

Title: Re: Help me write this code...
Post by: Matthew K. on March 12, 2012, 06:14:02 PM
Try the attached file :)
Title: Re: Help me write this code...
Post by: Biology Forums on March 12, 2012, 06:23:13 PM
Quote from: Labradoodle-360 on March 12, 2012, 06:14:02 PM
Try the attached file :)

Thanks Labradoodle-360,

This time there was no error, but when I set:

$mygroups = array(1,3);

To: $mygroups = array(2);

Which is the group I want to show red links, no red links are being shown, they are all blue still :(
Title: Re: Help me write this code...
Post by: Matthew K. on March 12, 2012, 06:31:19 PM
Try attached.
Title: Re: Help me write this code...
Post by: Biology Forums on March 12, 2012, 07:06:13 PM
Quote from: Labradoodle-360 on March 12, 2012, 06:31:19 PM
Try attached.

Labradoodle-360, I know you're a smart guy, but after I tell you this you'll probably run out of steam:

It didn't work still :-\ Same result as before - links are showing as normal, not read.
Title: Re: Help me write this code...
Post by: Matthew K. on March 12, 2012, 07:14:56 PM
How about a link to your forum, and an admin account.
Title: Re: Help me write this code...
Post by: Biology Forums on March 12, 2012, 07:21:23 PM
PM sent.
Title: Re: Help me write this code...
Post by: Matthew K. on March 12, 2012, 07:36:57 PM
I'll take a peak soon.
Title: Re: Help me write this code...
Post by: Biology Forums on March 12, 2012, 08:28:13 PM
Quote from: Labradoodle-360 on March 12, 2012, 07:36:57 PM
I'll take a peak soon.

Thanks
Title: Re: Help me write this code...
Post by: Marcus Forsberg on April 04, 2012, 08:03:38 AM
Any update on this? A quick look at MessageIndex.php tells me that the style is being applied to the member's name, and not the topic title as the OP requested.
Also, what is with the $groups array at all? It makes no sense to use an array when there's always just a single row to think about. Not to mention that this extra query could probably be merged with a previous one. A single line of code could do all of this very easily.
Title: Re: Help me write this code...
Post by: Biology Forums on April 04, 2012, 05:43:36 PM
Quote from: Marcus Forsberg on April 04, 2012, 08:03:38 AM
Any update on this? A quick look at MessageIndex.php tells me that the style is being applied to the member's name, and not the topic title as the OP requested.
Also, what is with the $groups array at all? It makes no sense to use an array when there's always just a single row to think about. Not to mention that this extra query could probably be merged with a previous one. A single line of code could do all of this very easily.

Marcus, a lot of people have tried and failed. If you can develop a code that I can insert into the messageindex.php, I'd be pretty thankful.
Title: Re: Help me write this code...
Post by: emanuele on April 04, 2012, 06:15:31 PM
Search for:
Code (find) Select
mf.smileysEnabled AS firstSmileys
and replace with
Code (replace with) Select
mf.smileysEnabled AS firstSmileys, memf.ID_GROUP

then:
Code (find) Select
'link' => '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0">' . $row['firstSubject'] . '</a>'
Code (replace with) Select
'link' => '<a ' . (in_array($row['ID_GROUP'], array(1)) ? ' style="color: red"' : '' ) . 'href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0">' . $row['firstSubject'] . '</a>'

Not sure if it is what you are looking for, let me know. ;)
Title: Re: Help me write this code...
Post by: Marcus Forsberg on April 04, 2012, 06:32:00 PM
Now *that's* what I'm talking about.
Title: Re: Help me write this code...
Post by: Biology Forums on April 04, 2012, 09:10:07 PM
Quote from: Marcus Forsberg on April 04, 2012, 06:32:00 PM
Now *that's* what I'm talking about.
It still didn't work, links are showing their normal color, and I parsed it correctly. :-\
Title: Re: Help me write this code...
Post by: Matthew K. on April 04, 2012, 09:49:00 PM
Well even with that code, why use in_array();, why MAKE it an array if it's a single value...?
Title: Re: Help me write this code...
Post by: emanuele on April 05, 2012, 04:05:23 AM
Quote from: Liam_michael on April 04, 2012, 09:10:07 PM
Quote from: Marcus Forsberg on April 04, 2012, 06:32:00 PM
Now *that's* what I'm talking about.
It still didn't work, links are showing their normal color, and I parsed it correctly. :-\
The group you are looking for is the primary or an additional?
I worked in my test forum...

Quote from: Labradoodle-360 on April 04, 2012, 09:49:00 PM
Well even with that code, why use in_array();, why MAKE it an array if it's a single value...?
Because the original question was:
Quote from: Liam_michael on March 09, 2012, 06:33:30 PM
If the person who started the topic is in group 15 or 21, show the topic title in red.
so so extend ad in_array it's easier than put another $row == 2. ;D
Title: Re: Help me write this code...
Post by: Matthew K. on April 05, 2012, 10:12:55 AM
If he does intend on adding more groups, definitely.