Newbie code is all wrong (probably).


#1

Introductions are in order… I’m a total newbie to PHP. I haven’t read many tutorials – mostly because they were all too technical for me, and I only really wanted a list of the various commands I could use. Most if not all I’ve learned of PHP I’ve gotten from reading code and fiddling about with it untill it works. But this latest idea of mine has me stumped. I just can’t get my head around it. Each time I do something I think should work, it goes wonky. So now I’m giving it to you… if someone could just point out what I’m doing wrong, I’d be very greatfull.

If someone could give me the url of a guide like I described, I’d also be greatfull. 8)

Thanks!

Emanuel

<?php
	if ($characters[$i]['character_player']) // Character has a player.
	{
		$playercount = 0;
		for ($p = 0; $p < count($players); $p++) // Begining of player loop.
		{
			if ($players[$p]['username'] == $characters[$i]['character_player']) // The players username matches the character's player.
			{
				$playercount = 1;
				if ($players[$p]['user_realname']) // The player has a real name.
				{
					?>
					<B> Player: </B>  <A HREF="players.php#<?php echo $players[$p]['username']; ?>"> <?php echo $players[$p]['user_realname']; ?> </A> <BR>
					<?php
				}
				else // Player has no real name.
				{
					?>
					<B> Player: </B>  <A HREF="players.php#<?php echo $players[$p]['username']; ?>"> <?php echo $players[$p]['username']; ?> </A> <BR>
					<?php
				}
			}
			else // The players username does not equal that of the character's player.
			{
				?>
				<?php
			}
			if ($p = count($players)) // If the script has gone through all the players.
			{
				if ($playercount < 1) // And has yet to find a match.
				{
					?>
					<B> Player: </B>  <?php echo $characters[$i]['character_player']; ?> </A> <BR>
					<?php
				}
				else // And has found a match
				{
					?>
					<?php
				}
			}
			else // If the script hasn't gone through all the players... don't do anything.
			{
				?>
				<?php
			}
		} // End of player loop.
	}
	else // Character has no player.
	{
		?>
		<B> Player: </B> Unknown <BR>
		<?php
	}
?> 

#2

Welcome to the forum.

a) What are you trying to do?
b) What is happening that shouldn`t be happening or vice versa?
c) What error messages are you getting, if any?

Please be a little more specific with your post.


#3

Ther are MANY tutorials out on the web for beginners. codewalker, zend, o’reilly, phpdeveloper, phpfreaks, and more have sections just for beginners. And remember you should also be learning coding logic which is language independant - flowcharts and psuedocode- Don’t be discouraged… we all started somewhere.


#4

Thank you for the welcome, and also for making me clarify without being condesending.

I wrote a detailed reply, but my session timed out and it got lost… go figure. So here we go again.

a)

The page this code is taken from is the character’s page of a roleplaying website. It’s supposed to get the right name of the player from the character’s profile, and make a link to the player’s section if possible.

This is done by modifying phpBB. Both the characters and the players are members of the forum, and just use two different parts of their profile to fill out information. In the character’s case, the important one for this part of the code is character_player, where the user types in the player’s username, or in the case of old players who are no longer affiliated with the group, just the actual name of the player.

For the players part, they have a field called user_realname where they input their real name. On the players page, each section has a # link named the same as their username.

The characters are all part of a usergroup called up in this script as $characters. The players are, obviously, $players. What I want is for the script to search through $players and if it finds a match between character_player and the players username, to make a link of that to that player’s entry on the players page, using user_realname for the text of the link. Obviously, if they don’t have a user_realname then the script should use their username as the text of the link.

However, if character_player does not match any of the usernames, i.e. it’s an ex-member, I want it to simply display character_player as the name of the player, without a link.

And finaly, if the character doesn’t have a player at all, I want it to simply say that the player is unknown.

B) Well, the incarnation I’ve posted here simply posts the character_player without a link. In other words, I think the problem lies in the following line:

if ($players[$p]['username'] == $characters[$i]['character_player']) // The players username matches the character's player.

Because it’s not following the actions of that if.

C) No error messages as such.

This is the webpage in question: http://www.hackwurld.com/players2.php

Emanuel


#5

Have you tried echoing out the variables to see if they hold what you think they do? does the logic flow as you expect? Why not?