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(

Advertisement: