Jump to content


Photo

why code it this way?


  • Please log in to reply
3 replies to this topic

#1 Jamie

Jamie

    New member

  • New Members
  • 9 posts

Posted 04 January 2011 - 03:00 AM

I am following one of the video tutorials but the author isn't explaining the code fully. So I am looking to understand why he is coding it in this way.
<form action="" method="post">
		<div>
			<?php  if($error['alert'] != ''){ 
				echo "<div class='alert'>" . $error['alert'] . "</div>";
			 } ?>
			<label for="username">Username: *</label>
			<input type="text" name="username" value="<?php echo $input['user']; ?>" />
			<div class="error"><?php echo $error['user']; ?></div>
			<label for="password">Password: *</label>
			<input type="password" name="password" value="<?php echo $input['pass']; ?>" />
			<div class="error"><?php echo $error['pass']; ?></div>
			<p class="required"> * required fields</p>
			
			<input type="submit" name="submit" class="submit" value="submit" />
		</div>
	</form>

The question here is why use the $error['alert']? this is an array. I don't see where the $error[] comes from. The only place it is defined is in another file and it is defined as nothing
$error['alert'] = '';

so what I am trying to figure out is why do it this way and not this way

$error = '';

  • 0

#2 Ben

Ben

    Administrator

  • Administrators
  • 5,649 posts
  • LocationChico, CA

Posted 04 January 2011 - 09:23 AM

Rather than having individual variables for input/error messages, I just wanted to group my messages into two arrays, $error and $input. Using these arrays gave the variables a bit more context than just using $user or $pass, and I feel that they make the code slightly easier to read and understand. $error['alert'] isn't the only item in that array - it also contains $error['user'] and $error['pass']. You don't have to do it this way, but it is the way that made the most sense to me.

You are actually correct - $error could be initially defined as "$error = '';". Either would work. Using "$error = '';" could actually save a couple lines of code, and might be the "better" way to do things.
  • 0
Benjamin Falk
Falken Creative : Twitter

#3 Jamie

Jamie

    New member

  • New Members
  • 9 posts

Posted 04 January 2011 - 02:44 PM

okay, that sounds good. I am going to follow through the tutorial and make the login system. It would be awesome if you could explain things a bit more in future video tutorials. I am trying to learn php and I am going to try out this site to see how it works compared to other sites. I have some recommendations if you are interested to improve your site here

P.S, ben I really like your website. It looks great. Did you ever get that company to pay you for stealing your design?
  • 0

#4 Ben

Ben

    Administrator

  • Administrators
  • 5,649 posts
  • LocationChico, CA

Posted 04 January 2011 - 03:03 PM

It would be awesome if you could explain things a bit more in future video tutorials.

I do my best - sometimes things that seem obvious to me may not be obvious to others. It's something I'll probably be able to improve on the more tutorials I do. If you run into any other issues where I don't explain something fully, just post - I keep an eye on the PHP section of the forums.

I have some recommendations if you are interested to improve your site here

Sounds good. Feel free to post them or PM me if you'd prefer.

ben I really like your website. It looks great. Did you ever get that company to pay you for stealing your design?

Thanks! Nope, it never worked out. I did end up modifying my design a bit so they aren't quite so alike, but there isn't much I can do unfortunately.
  • 0
Benjamin Falk
Falken Creative : Twitter




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users