News:

Wondering if this will always be free?  See why free is better.

Main Menu

Help me write this code...

Started by Biology Forums, March 09, 2012, 06:33:30 PM

Previous topic - Next topic

Biology Forums

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


DanCarroll

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;">?



Biology Forums

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.

DanCarroll

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.

Biology Forums

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.

IchBin™

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.
IchBin™        TinyPortal

Matthew K.

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).

Biology Forums

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.

Matthew K.

IchBin did. He told you exactly what you need to do.

Biology Forums

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.

IchBin™

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>'
IchBin™        TinyPortal

Matthew K.

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.

Biology Forums

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.

IchBin™

#14
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>'
IchBin™        TinyPortal

Matthew K.

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__
);

IchBin™

Thanks, forgot about that. UPdated the post.
IchBin™        TinyPortal

Matthew K.

No problem...I don't even think it really matters, I think it's more for recording the queries.

Biology Forums

#18
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.

Matthew K.

You have to add it above the other code you need to modify.
// 'Print' the topic info.
$context['topics'][$row['ID_TOPIC']] = array(

IchBin™

If you look at the code snippet I first posted you'll see this:

Code: (add above the code you posted) :)
IchBin™        TinyPortal

Biology Forums

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 :-\

Matthew K.

Go ahead and attach your MessageIndex file WITH the code you added.


Matthew K.


Biology Forums

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 :(

Matthew K.


Biology Forums

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.

Matthew K.

How about a link to your forum, and an admin account.


Matthew K.



Marcus Forsberg

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.

Biology Forums

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.

emanuele

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. ;)


Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

Marcus Forsberg

Now *that's* what I'm talking about.

Biology Forums

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. :-\

Matthew K.

Well even with that code, why use in_array();, why MAKE it an array if it's a single value...?

emanuele

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


Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

Matthew K.

If he does intend on adding more groups, definitely.

Advertisement: