PDA

View Full Version : Learn to HELP YOURSELF


rake
09-01-2004, 10:00 PM
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
LEARN to HELP YOURSELF
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


I decided to write this after observing the types of questions asked in the General Modification Discussion and PHP/MySQL/JS/HTML forums. If people would know a few simple things, i'm sure the number of questions would be reduced by at least 40-50%.

First of all, they need to know that asking on a forum is NOT an efficient way of getting an aswer, and by no means a fast one. It could be days before a member will give you the right solution to your problem. Secondly, you waste BOTH YOUR TIME, and the TIME OF OTHER MEMBERS.

With that in mind, the best way to find an answer is to search for it yourself...

Golden Rule 1: Use Forum Search - i know this has been said over and over, but i just felt the need to repeat it
Golden Rule 2: Use Google


1. EditPlus2 - It's a program. What special about it is the "Find in Files" feature. It will search for a string in multiple files. Use google to find it. I'll give an example of using this later.

2. When you need to find a certain template, keep in mind there's a vBulletin option which will add the names of templates in HTML comments in the HTML source of the page. Turn it on, refresh your vBulletin page, right click on the page > View Source, and then you can just read the name of your template. Also, don't forget there is a search in templates function within vBulletin.

3. If something is wrong in your code, DEBUG, and then post for help. Let's take an example for this. Let's say you have an if statement, which sets a variable to true/false, and based on this you want to allow a user access to a page or not - and it's just not working...


if($something == 'x')
{
$myvar = TRUE;
}
else
{
$myvar = FALSE;
}

if($myvar == FALSE)
{
print_no_permission();
}


Let's say you KNOW the variable should be FALSE, but for some reason it's true... First thing to do, is to check the value of the variable: After the first if, add the following code: echo $myvar; exit; This simple echo-ing method can be used in most cases. I've seen at least 3-4 questions like this in the past couple of days.

4. If something isn't working, and you just can't figure out why, try using alternate methods of getting the same effect.

5. If you need help with a certain php function, don't ask on the forums. I recommend downloading the php manual in Windows Help Format (chm) from php.net. It's very useful as a function reference. If you don't want to download it - again, use google.

6 If you want to find a function that does something, use the manual. Let's say you want to find a function that converts a string to uppercase. You open the manual, click the Search tab, and search for uppercase. There'll be a few items on the list, but the 4th one is what you're looking for: strtoupper.

7. Similarly, if you need to know what a vBulletin function does, first thing to do is to look at it's name, which will in most cases give a pretty big hint on what the function is supposed to do. Look if there are any comments on that function in the code, and finally, try looking the code and figuring out what it's supposed to do.

8. If you want to find a vbulletin function, EditPlus is your friend. Use Find in Files, and you'll have your function.


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
PRACTICAL EXAMPLE OF USING THE ABOVE TIPS
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

What we want

As an example of using the above tips, i'll use this thread: How do I: Have Links From a Specific Domain Open in Same Window (https://vborg.vbsupport.ru/showthread.php?t=68654)

What we're trying to do is have links (in posts) from certain domains open in the same window. By default, vbulletin opens a new window for all links.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

How We do it

First, let's open EditPlus, Search > Find In Files. We'll want to search in our forum folder, for files of type *.*, and the string we'll search for is [url since we know that is the bbcode tag used to generate html links.

There's be some results, but you can quickly see which are the important ones. In includes/functions_bbcodeparse.php there'll be some regular expressions to match the [url tag, and some very useful comments ( // [URL] ). Now you know you've found what you were searching for. Go to that page.


// [URL]
$bbcodes['standard']['find']['[url]'] = '#\[url\](.*)\[/url\]#esiU';
$bbcodes['standard']['replace']['[url]'] = "handle_bbcode_url('\\1', '', 'url')";
$bbcodes['standard']['recurse']['url'][0] = array('handler' => 'handle_bbcode_url');
// [URL=XXX]
$bbcodes['standard']['find']['[url='] = '#\[url=("|"|\'|)(.*)\\1\](.*)\[/url\]#esiU';
$bbcodes['standard']['replace']['[url='] = "handle_bbcode_url('\\3', '\\2', 'url')";
$bbcodes['standard']['recurse']['url'][1] = array('handler' => 'handle_bbcode_url');


By taking a look at the above code, the first thing your eyes should fall on is the "handle_bbcode_url" function...

Use the find in files function again, to see where that function is... Well, what do you know? It's also in the functions_bbcodeparse file. You won't need to examine the entire function to find what's essential, since it's easy to spot:


// standard URL hyperlink
return "<a href=\"$rightlink\" target=\"_blank\">$text</a>";


Now you know this is where you have to hack the function. So what you want to do is to see if $rightlink contains the domain you want, mydomain.com, so you open the php manual and take a look at string functions... voila!

string stristr ( string haystack, string needle)


Returns all of haystack from the first occurrence of needle to the end. needle and haystack are examined in a case-insensitive manner.

If needle is not found, returns FALSE.


So you transform the portion of code you found into this:


if(stristr($rightlink,"domain.com"))
{
// standard URL hyperlink
return "<a href=\"$rightlink\" target=\"_self\">$text</a>";
}
else
{
// standard URL hyperlink
return "<a href=\"$rightlink\" target=\"_blank\">$text</a>";
}


So, we check if rightlink contains domain.com, and if it does, we change the target="_blank" portion, with target="self" so the link will be opened in the same window.

So what if we want to do the above for more domains? Here's a demonstration of point 4, alternate methods of doing the same thing...


if(stristr($rightlink,"domain.com"))
{
// standard URL hyperlink
return "<a href=\"$rightlink\" target=\"_self\">$text</a>";
}
elseif(stristr($rightlink,"seconddomain.com"))
{
// standard URL hyperlink
return "<a href=\"$rightlink\" target=\"_self\">$text</a>";
}
else
{
// standard URL hyperlink
return "<a href=\"$rightlink\" target=\"_blank\">$text</a>";
}




$domains = array(
'domain.com',
'seconddomain.com'
);

foreach($domains as $domain)
{
if(stristr($rightlink,$domain))
{
// standard URL hyperlink
return "<a href=\"$rightlink\" target=\"_self\">$text</a>";
}
else
{
// standard URL hyperlink
return "<a href=\"$rightlink\" target=\"_blank\">$text</a>";
}
}



To make it easier and less repetitive, we made an array of domains, and then looped through them to compare each of them to $rightlink.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

That's it! It's a lot easier than it seems, believe me. Then again, i'm not that good at explaining things. I hope this helps some of you. :)

Rake.

Dean C
09-02-2004, 12:13 PM
Some great stuff but to test for a needle in a haystack use strpos :)

Infopro
09-02-2004, 02:04 PM
https://vborg.vbsupport.ru/external/2011/01/19.gif Good job.

CarCdr
09-02-2004, 02:09 PM
Nice work rake. Good posts like that take time.

Xenon
09-02-2004, 05:31 PM
definitelly a good one!

deathemperor
09-03-2004, 05:39 AM
very very useful.

Thanks Rake !

the only thing I've done before time was download all links at http://php.net/manual/en ^^. This helped me make my first modification myself and understand vbulletin things.

zetetic
09-06-2004, 09:07 PM
Thanks, rake. #2 is particularly helpful; I had no idea that was there. :up:

CarCdr
09-08-2004, 04:34 PM
I would also add that a common problem when asking questions is that the explanation/question is often too brief, as if the questioner has less time that the people reading the post.

In general, if I read a question-style post that is a couple of vague sentences, I will just ignore it. I do not think I am alone in this behaviour. If one can not take the 15 or 20 minutes to enter a decent post with as much information as is reasonably possible, then one should not expect a reply.

Here are some of the problems with question/request/problem posts:

#1 -- Explain what you are trying to do
Take the time to start your post with an explanation of what you are trying to do. After you have done that, move on to describing the problem.

#2 -- Be as specific as possible
Often, it is difficult to tell if one is referring to a PHP script, a template, or some HTTP/browser issue. Be specific about what objects you are working with.

#3 -- Attach or enter problem output
Now really, what good is it to say "I don't get the right XXX" when I try YYY. Show us the XXX you do get. Attach a screenshot if that is showing the problem. Include the code that is not working if that is applicable. Show the output of the database error. ... you get the idea.

#4 -- Hack requests as implementation questions
Do not pose a request for a hack as an implementation question. Instead of asking "How can I change the XXX to include the YYY as well.", explain the purpose of the hack. There might be a more proper or elegant solution if you posed the request in terms of what you want to accomplish rather than in terms of what you have tried or what you have in mind as a solution.

Cheers, C.C