Endless Paradigm

Full Version: Need help with php
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
Im trying to make a plugin like zinga's plugin(username color just that), im trying to make this:

replace in functions.php:
this:

PHP Code:
function format_name($username, $usergroup, $displaygroup="")
{
	global $groupscache, $cache;

	if(!is_array($groupscache))
	{
		$groupscache = $cache->read("usergroups");
	}

	if($displaygroup != 0)
	{
		$usergroup = $displaygroup;
	}
	
	
	$ugroup = $groupscache[$usergroup];
	$format = $ugroup['namestyle'];
	$userin = substr_count($format, "{username}");
	if($userin == 0)
	{
		$format = "{username}";
	}
	$format = stripslashes($format);
	return str_replace("{username}", $username, $format);
}

with this:

PHP Code:
function format_name($username, $usergroup, $displaygroup="")
{
	global $db, $groupscache, $cache;
	$query = $db->simple_select(TABLE_PREFIX."userfields", "fid5", "ufid='".intval($uid)."'");

	if(!is_array($groupscache))
	{
		$groupscache = $cache->read("usergroups");
	}

	if($displaygroup != 0)
	{
		$usergroup = $displaygroup;
	}
	$ugroup = $groupscache[$usergroup];
	$format = "<span style=\"color:{$userfields['fid5']};\">{username}</span>"; // This the 'namestyle' can be easily changed in the admin cp for each group but i want to change it for each user
	$userin = substr_count($format, "{username}"); // function that counts how many times {username} substring is in the $formats string
	if($userin == 0) // if {username} substring wasnt found in the $format string, then
	{
		$format = "{username}"; // the $format string = "{username}"
	}
	$format = stripslashes($format); // The stripslashes() function removes backslashes from the $format string
	return str_replace("{username}", $username, $format); // replaces {username} with $username, so the $format string will contain $username instead of {username}
}


It doesn't show the colors...but it affects the colors because it shows the default color in all usernames

Hmm, i haven't got too much experience in PHP scripting, but it should be ok... Probably mistake:D

{Translated with google -.-}
but it isn't working lool and what was translated with google?
Updated with a new question as i resolved my other problem.
Also zinga changed the php code i don't know why but its:

            $ugroup = $groupscache[$usergroup];
            $format = "<span style=\"color:{$userfields['fid5']};\">{username}</span>";
            $userin = substr_count($format, "{username}");

not

            $ugroup = $groupscache[$usergroup];
            $format = "<span style=\"color:{$userfields['fid5']};\">{username}</span>";
            $userin = substr_count($format, "{username}");
Firstly, the code you supplied is rather illogical and obviously doesn't work...
Secondly, it's very inefficient - you make a query every time you have to format a name - for example, if there's 100 names needing to be formatted, your code queries the database 100 times just to do that.
My method hardly affects speed, but to achieve that, a lot of code edits are needed.

I'll point out some problems with your script, however, I'm not going to provide you with a full solution (since it won't be "good" anyway):

PHP Code:
    $query = $db->simple_select(TABLE_PREFIX."userfields", "fid5", "ufid='".intval($uid)."'");


  1. The query isn't used anywhere
  2. $uid is not initialized, and means nothing

The rest looks like it will work.

Pirata Nervo Wrote:Updated with a new question as i resolved my other problem.
Also zinga changed the php code i don't know why but its:

            $ugroup = $groupscache[$usergroup];
            $format = "<span style=\"color:{$userfields['fid5']};\">{username}</span>";
            $userin = substr_count($format, "{username}");

not

            $ugroup = $groupscache[$usergroup];
            $format = "<span style=\"color:{$userfields['fid5']};\">{username}</span>";
            $userin = substr_count($format, "{username}");
What???
You probably changed the php bbcode so it doesn't say / it says \

PHP Code:
$format = "<span style=\"color:{$userfields['fid5']};\">

its not that.
its:
$format = "<span style=\"color:{$userfields['fid5']};\">


O and thanks, but it isn't working with my code lol

I deleted everything in this post.

Its not \ its \ , i don't know what does it means but its not that, but it says that inside of php tags.

im trying to make a function:

PHP Code:
function format_namePN($username, $usergroup, $displaygroup="")
{
	global $groupscache, $cache;

	if(!is_array($groupscache))
	{
		$groupscache = $cache->read("usergroups");
	}

	if($displaygroup != 0)
	{
		$usergroup = $displaygroup;
	}
	if($userfields['fid5'])
	{
		$format = "<span style=\"color:{$userfields['fid5']};\">{username}</span>";
	}
	else
	{
		$ugroup = $groupscache[$usergroup];
		$format = $ugroup['namestyle'];
	}
    	$userin = substr_count($format, "{username}"); // function that counts how many times {username} substring is in the $formats string
    	if($userin == 0) // if {username} substring wasnt found in the $format string, then
    	{
      	$format = "{username}"; // the $format string = "{username}"
    	}
    	$format = stripslashes($format); // The stripslashes() function removes backslashes from the $format string
    	return str_replace("{username}", $username, $format); // replaces {username} with $username, so the $format string will contain $username instead of {username}
} 


I don't know why it isn't working do i need to call anything before using the userfields['fid5'] ?
Because it always uses the else condition insteaad of using the first one.

double you tee eff?  First thing, $userfields is undeclared...
but how do i check if it exists in the db? that's my main question.
Im also using require_once "./member.php"; and still doesn't work.
Check what exists in the DB?  You have an undeclared variable - that's your issue.  A lot of scripting languages don't require explicit variable declaration, so sticking in a variable that doesn't exist won't cause an error.  In C, for example, using an undeclared variable would cause a compiler error, eg:

Code:
#include <stuff>
int main()
{
 printf("%s",nonExistantVariable);
 return 0;
}

The above would obviously not compile.
In PHP, no error would be generated however - nonExistantVariable would just be null.


Pirata Nervo Wrote:Im also using require_once "./member.php"; and still doesn't work.
Do you have the path right?  But why do you want to include member.php anyway?
Pages: 1 2 3 4
Reference URL's