Jump to content

p3nguattack

Members
  • Posts

    15
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

p3nguattack's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I have them defined as single arrays so I can set the values on the page I need to call the function(I'm not very good with arrays). Your function worked great(after only one syntax error XP), though because of my ignorance of arrays, I'm assuming that the $id, is just referencing the keys from each array. With that said, would this still work if I used one array without equal values? I'll show you my arrays first. $form->names =array('name', 'email', 'username', 'password'); $form->titles =array('name', 'email', 'username', 'password'); $form->types =array('text', 'text', 'text', 'password'); $form->values =array('joe', 'matt', 'steve', 'larry'); $form->lengths =array('22', '33', '14', '43'); This was just a test while I was trying to make the function, not a real form obviously, but suppose I took the last two lengths out, would it still call the first two and leave the default for textfield_t($length=35)? I'm thinking it'll throw undefined index errors at me for the [2] and [3] that the $id stands for, or possibly give me a blank value unless I use array push? Of course that wouldn't matter on the lengths. My formarray function was originally just supposed to put them into a multidimensional array, because for some reason I couldn't figure out the syntax when trying to make it just a variable, but I figured if I was doing that, why not just run the textfield_t at the same time, since that's the only reason I'd use that array. I appreciate your help, you shortened that at least 400% lol.
  2. Okay, so I'm trying to make a function to automatically make forms for me based on values I have in 5 arrays(all the requirements I have for the forms). The one I've made works, but only so long as I have exactly 5 values in each array. As I said in the title I need a better way to do this. I've tried foreaching each of the arrays and that gives me forms, but WAY more than what I want. Like 50-60 instead of 5... Anyway look at this code, and I'm sure you'll think I'm stupid for doing it this way lol, but I couldn't think of another way to populate a function with 5 variables that actually worked as I wanted it to. public function formarray() { $names0 = $this->names[0]; $names1 = $this->names[1]; $names2 = $this->names[2]; $names3 = $this->names[3]; $names4 = $this->names[4]; $titles0 = $this->titles[0]; $titles1 = $this->titles[1]; $titles2 = $this->titles[2]; $titles3 = $this->titles[3]; $titles4 = $this->titles[4]; $types0 = $this->types[0]; $types1 = $this->types[1]; $types2 = $this->types[2]; $types3 = $this->types[3]; $types4 = $this->types[4]; $values0 = $this->values[0]; $values1 = $this->values[1]; $values2 = $this->values[2]; $values3 = $this->values[3]; $values4 = $this->values[4]; $lengths0 = $this->lengths[0]; $lengths1 = $this->lengths[1]; $lengths2 = $this->lengths[2]; $lengths3 = $this->lengths[3]; $lengths4 = $this->lengths[4]; $formarray = array( 0 => array( 'title' => $titles0, 'name' => $names0, 'type' => $types0, 'value' => $values0, 'length' => $lengths0 ), 1 => array( 'title' => $titles1, 'name' => $names1, 'type' => $types1, 'value' => $values1, 'length' => $lengths1 ), 2 => array( 'title' => $titles2, 'name' => $names2, 'type' => $types2, 'value' => $values2, 'length' => $lengths2 ), 3 => array( 'title' => $titles3, 'name' => $names3, 'type' => $types3, 'value' => $values3, 'length' => $lengths3 ), 4 => array( 'title' => $titles4, 'name' => $names4, 'type' => $types4, 'value' => $values4, 'length' => $lengths4 )); foreach($formarray as $key => $values) { $title = $values['title']; $name = $values['name']; $type = $values['type']; $value = $values['value']; $length = $values['length']; $this->textfield_t($title, $name, $type, $value, $length); } } and here's the function it populates, just so you can see that too. public function textfield_t($title, $name, $type, $value="", $length=35) { echo '<tr> <td>'.$title.':</td><td><input name="'.$name.'" type="'.$type.'" value="'.@$value.'" maxlength="'.$length.'" /><br /></td> </tr>'; } As I'm sure you can see, I really do need a better way to do this. I'm sure this is ridiculous. I tried calling the array values directly into the $formarray, but it kept giving me syntax errors which I have yet to discover the cause of. It's a pretty handy function, but it could be a lot better, and would be if I didn't have to have exactly 5 values in each of the arrays. If it was more dynamic. I realize I could just put if statements in front of them all, and if that's my only option I'll gladly do so, but I'd like to have some other opinions first. Thanks everyone for your time.
  3. Sounds good, I suppose I'll start with a basic one, make a site or two, then work my way up until I can build whatever I can think of with smarty. Thanks for all the help guys!
  4. I was going to say, the tutorial I learned php from recommended smarty but you beat me to it lol. I have one more question and then I suppose I'll mark this thread resolved. If I were to get a templating engine, say smarty for example. Would I have to rewrite all of my pages? I looked into it quite a bit and it really does seem easier than hardcoding it all, but at this stage in my project, I'm not sure I'd want to commit to that if I did have to rebuild everything (and according to what I read, it looks like I do).
  5. That does look a lot easier, I've honestly haven't looked at any templates. I suppose I was doing it this way partially, because I'm still inexperienced and wanted to learn as much as possible from making the site. I'll have to check that out though. I don't quite understand how the return to controller function works though. I guess that has something to do with never hearing of a controller lol. I've only been working with php for about a month :/. I have to say though, I feel like I'm doing alright so far lol. I have almost a complete website all hardcoded. Of course I do have quite a few classes to make that process a lot easier. Would I be able to find something like that from pear or pecl, or are there specific sites for templates?
  6. you could redirect to the login page and set a variable there for www.login?notlogged=true.com in the event of a user not being logged in. You could also just put that if statement above the content you want to hide and put the content as the else. If you want I have a few functions made for sending information between pages so you don't have to use the $_GET approach every time you do a redirect. They use a session class and the $_SESSION itself, but it's a lot easier than writing that for everything you want to hide from unregistered users.
  7. Alright I have that figured out, I can't believe I missed the $_POST names like that. Just coding a lot must have been tired lol. Thanks Christian! Here's my new improved code. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> COMING SOON!</title> <link href="stylesheets/main.css" rel="stylesheet" type="text/css" /> </head> <?php require_once('includes/initialize.php'); if ($session->is_logged_in()) { $session->message("You are already registered."); redirect_to("index.php"); } // START FORM PROCESSING if (!isset($_POST['submit'])) { // Form has not been submitted. $user = new User; $username = ""; $password = ""; $first_name = ""; $last_name = ""; $email = ""; $join = ""; $auth = ""; } elseif(isset($_POST['submit'])) { // Form has been submitted. $user = new User; $first_name = trim($_POST['firstname']); $last_name = trim($_POST['lastname']); $passwordc1 = trim($_POST['password']); $passwordc2 = trim($_POST['passwordc']); $username = trim($_POST['username']); $emailc1 = trim($_POST['email']); $emailc2 = trim($_POST['emailc']); //$dob = trim($_POST['dob']); $errlist = ""; // Check if first name has a value if(empty($first_name)) { $errlist .= "<li>You must enter your first name</li>"; } // Check if last name has a value if(empty($last_name)) { $errlist .= "<li>You must enter your last name</li>"; } // Check that both email, and email confirm were submitted, and they match if(empty($emailc1)) { $errlist .= "<li>You must enter an email address</li>"; } if(empty($emailc2)) { $errlist .= "<li>You must confirm your email address</li>"; } if($emailc1 !== $emailc2) { $errlist .= "<li>Your email addresses do not match</li>"; } else { $email = $emailc2; } // Check if username has a value. if(empty($username)) { $errlist .= "<li>You must enter a username</li>"; } // Check database to see if username exist. $found_user = User::check_username($username); if($found_user) { $errlist .= "<li>Username already exists</li>"; } // Check that both password, and password confirm were submitted, and they match if(empty($passwordc1)) { $errlist .= "<li>You must enter a password</li>"; } if(empty($passwordc2)) { $errlist .= "<li>You must confirm your password</li>"; } if($passwordc1 !== $passwordc2) { $errlist .= "<li>Your passwords do not match</li>"; } else { $password = $passwordc2; } // if errors, redirect (here) and show them. if(!empty($errlist)) { $errcount = substr_count($errlist, "/"); } if(!$found_user && empty($errlist)) { $user->username = $username; $user->password = $password; $user->first_name = $first_name; $user->last_name = $last_name; $user->joined = trim($_POST['join']); $user->auth = trim($_POST['auth']); $user->email = $email; $user->dob = trim($_POST['join']); $user->create(); $userpass = User::authenticate($username, $password); $session->login($userpass); $session->message("You have successfull registered!"); redirect_to('account.php'); } } ?> <body> <div id="wrapper"> <div id="bodyContent"> <?php require ('layouts/top_nav.php'); ?> <ul> <?php echo output_message($message);?> </ul> <div class="center"> <!-- LEADERBOARD AD --> <?php echo $user->leaderboard; ?> </div> <!-- START BODY CONTENT HERE --> <?php if(!isset($_POST['submit']) || !empty($errlist)) { if(isset($errlist)) { echo "There were ".$errcount." errors with your information."; echo $errlist; } echo ' <table> <form action="register.php" method="post" enctype="multipart/form-data" > <input name="join" type="hidden" value="'. date("Y-m-d H:i:s", time()) .'" maxlength="20" /> <input name="auth" type="hidden" value="1" /> <tr> <td>First Name:</td><td><input name="firstname" type="text" value="'. @$first_name .'" maxlength="20" /><br /></td> </tr> <tr> <td>Last Name:</td><td><input name="lastname" type="text" value="'. @$last_name.'" maxlength="20" /><br /></td> </tr> <tr> <td>E-mail:</td><td><input name="email" type="text" value="'.@$email.'" maxlength="20" /><br /></td> </tr> <tr> <td>Confirm e-mail:</td><td><input name="emailc" type="text" value="'.@$email.'" maxlength="50" /><br /></td> </tr> <tr> <td>Username:</td><td><input name="username" type="text" value="'.@$username.'" maxlength="20" /><br /></td> </tr> <tr> <td>Password:</td><td><input name="password" type="password" value="'.@$password.'" maxlength="20" /><br /></td> </tr> <tr> <td>Confirm password:</td><td><input name="passwordc" type="password" value="'.@$password.'" maxlength="20" /><br /></td> </tr> <tr> <td><input name="submit" type="submit" value="Register" /></td> </tr> </form> </table> '; } ?> <!-- END BODY CONTENT HERE --> </div> </div> </body> </html> Apparently I had to do away with using my output_message function if I wanted to be able to echo the form processing errors along with the values the users put in. It works by storing the message in the $_SESSION then moving it to the session class I'm assuming after a redirect, or a new page(it works from page to page. Any page to any other page on the site for whatever reason, that's what it was made for). So I did a substr_count for that part of the form processing instead, then echo'd that count, followed by the $errlist. Works much better this way. Thanks everyone. Now if only I could get that jquery down lol. Anyone know a link for a tutorial for complete newbies to javascript?
  8. I appreciate the replies, and to first reply, I have my php.ini set up exactly as you described. I did try to use the jquery datepicker, but I'm not familiar with javascript, and I think I had it called wrong or something. Everything I tried had a text box, but no calendar when you clicked it (as the example from the tutorial did). I thought I had my code correct, but something had to be wrong. I'm not as concerned with that at the moment as much as the first two issues I posted though. If I can get them resolved I'll start messing with the datepicker some more.
  9. Hello everyone. I've been working on this script for a while now, and have had some ups and downs. As of now, I don't get any errors, but there are a few problems that I need to fix and can't figure out how. First, When I'm echoing the data back in the "value" of the form, nothing shows up, ever. The !isset(_POST['submit'] at the top used to be at the bottom of the form processing as an else statement(which should have worked fine, but I remembered when I had a variable $message in it, my $session->message() function wouldn't work. I renamed that variable and it started working just fine(my session class automatically puts a $message variable on my pages for my output_message() to work), so I thought maybe it was for some reason removing those values from my variables(even though that shouldn't have fixed the problem considering the only way the output_message() would spit anything out is if a form was submitted) and that's why they wouldn't echo). I know the variables are being set and passed through, because if I don't get any errors and the username doesn't already exist, it registers just fine, and my database reflects the same things entered. My second problem is that neither the code for the passwords or the emails matching are working. I had them working before, but I had them set up poorly and decided to rewrite them. Before I had them as if $_POST['password/email'] == $_POST['password/email confirm'] {$password/email = $_POST['password/email'] } else { $session->message = "failed"; redirect_to('register.php'). Of course they were separate statements, but I think you get the point. That worked for me, but I didn't like having the redirect in there(it wouldn't give all the errors either, that function only displays one anyway until I put them in a list like I have now. It works a lot better.). I'll post the whole page code for you to look at, and if you'd like to know what any of the functions I use look like just ask and I'll post those too, but their names pretty much say exactly what they do. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> COMING SOON!</title> <link href="stylesheets/main.css" rel="stylesheet" type="text/css" /> </head> <?php require_once('includes/initialize.php'); if ($session->is_logged_in()) { $session->message("You are already registered."); redirect_to("index.php"); } // START FORM PROCESSING if (!isset($_POST['submit'])) { // Form has not been submitted. $user = new User; $username = ""; $password = ""; $first_name = ""; $last_name = ""; $email = ""; $join = ""; $auth = ""; } elseif(isset($_POST['submit'])) { // Form has been submitted. $user = new User; $first_name = trim($_POST['firstname']); $last_name = trim($_POST['lastname']); $passwordc1 = trim($_POST['passwordc']); $passwordc2 = trim($_POST['passwordc']); $username = trim($_POST['username']); $emailc1 = trim($_POST['email']); $emailc2 = trim($_POST['email']); //$dob = trim($_POST['dob']); $errlist = ""; // Check if first name has a value if(empty($first_name)) { $errlist .= "<li>You must enter your first name</li>"; } // Check if last name has a value if(empty($last_name)) { $errlist .= "<li>You must enter your last name</li>"; } // Check that both email, and email confirm were submitted, and they match if(empty($emailc1)) { $errlist .= "<li>You must enter an email address</li>"; } if(empty($emailc2)) { $errlist .= "<li>You must confirm your email address</li>"; } if($emailc1 !== $emailc2) { $errlist .= "<li>Your email addresses do not match</li>"; } else { $email = $emailc2; } // Check if username has a value. if(empty($username)) { $errlist .= "<li>You must enter a username</li>"; } // Check database to see if username exist. $found_user = User::check_username($username); if($found_user) { $errlist .= "<li>Username already exists</li>"; } // Check that both password, and password confirm were submitted, and they match if(empty($passwordc1)) { $errlist .= "<li>You must enter an email address</li>"; } if(empty($passwordc2)) { $errlist .= "<li>You must confirm your email address</li>"; } if($passwordc1 !== $passwordc2) { $errlist .= "<li>Your email addresses do not match</li>"; } else { $password = $passwordc2; } // if errors, redirect (here) and show them. if(!empty($errlist)) { $session->message($errlist); redirect_to('register.php'); } if(!$found_user && empty($errlist)) { $user->username = $username; $user->password = $password; $user->first_name = $first_name; $user->last_name = $last_name; $user->joined = trim($_POST['join']); $user->auth = trim($_POST['auth']); $user->email = $email; $user->dob = trim($_POST['join']); $user->create(); $userpass = User::authenticate($username, $password); $session->login($userpass); $session->message("You have successfull registered!"); redirect_to('account.php'); } } ?> <body> <div id="wrapper"> <div id="bodyContent"> <?php require ('layouts/top_nav.php'); ?> <ul> <?php echo output_message($message); echo $first_name; ?> </ul> <div class="center"> <!-- LEADERBOARD AD --> <?php echo $user->leaderboard; ?> </div> <!-- START BODY CONTENT HERE --> <table> <form action="register.php" method="post" enctype="multipart/form-data" > <input name="join" type="hidden" value="<?php echo date("Y-m-d H:i:s", time()); ?>" maxlength="20" /> <input name="auth" type="hidden" value="1" /> <tr> <td>First Name:</td><td><input name="firstname" type="text" value="<?php echo $first_name; ?>" maxlength="20" /><br /></td> </tr> <tr> <td>Last Name:</td><td><input name="lastname" type="text" value="<?php echo $last_name; ?>" maxlength="20" /><br /></td> </tr> <tr> <td>E-mail:</td><td><input name="email" type="text" value="<?php echo $email; ?>" maxlength="20" /><br /></td> </tr> <tr> <td>Confirm e-mail:</td><td><input name="emailc" type="text" value="<?php echo $email; ?>" maxlength="20" /><br /></td> </tr> <tr> <td>Username:</td><td><input name="username" type="text" value="<?php echo $username; ?>" maxlength="20" /><br /></td> </tr> <tr> <td>Password:</td><td><input name="password" type="password" value="<?php echo $password; ?>" maxlength="20" /><br /></td> </tr> <tr> <td>Confirm password:</td><td><input name="passwordc" type="password" value="<?php echo $password; ?>" maxlength="20" /><br /></td> </tr> <tr> <td><input name="submit" type="submit" value="Register" /></td> </tr> </form> </table> </div> </div> </body> </html> I was also wondering if anyone could suggest a way for me to do my date of birth field. I wanted to have a dropdown menu for each value, but I'm not sure how to do that, or how that data would be retrieved by php. Also I was wondering if there was any way that I could query the database after the user types in their username and either display a check mark next to it, or an "already taken" message before they submit the form. I was googling it, and found a bunch of stuff about how you could do it with javascript, but nothing on php. Thanks in advance for any help you may give me. I really appreciate it, and would love to have this page working soon. Until then, it's time to go work on my account page .
  10. Hmm, that's a good point. I don't know why I even put it in there honestly, I think I was having a syntax error using mkdir() so I just made it move to the directory first instead of debugging it thanks for the suggestion, I really appreciate your input. I'll go ahead and take it out, and if I can't get it to auto create the dated folders in the right place without it, I'll just put another chdir() at the end of the function to have it move back.
  11. Max, I think you missed the point xD, but thanks anyway! Kicken, I can't believe I overlooked that! It's always best to have another eye around for things like this. So, that brings me to wondering, if you think I should rewrite this function to make it work, or just separate the directory part altogether and make it another function? I was considering separating them before, but not for that reason, I hadn't even noticed that. I was just going to do it so I could use the directory function for other tasks. Thank you so much! Update: By the way, I just used moved the curly braces around a little (separated the directory portion from the fopen/ect. portion) and it worked just as it was supposed to, I'd still like your opinion though. I'm actually considering just making a log class to do it all. I think that might be an even better option, because I plan to do more with it.
  12. Hello everyone, I've been working on a function for a logfile for my site that logs when users login. I have it working sort of, but the problem I'm having is very annoying, and makes the logfile useless.. Anyway, basically what I have set up is a script that automatically makes a folder for each month, and a new logfile every day, and it (is supposed to) logs all the logins in the file that day then makes a new file the next day. My problem is after the initial creation and the first login log, it will no longer update. I haven't tried to make it delete and write a new line, because I just assumed that would work since the first line will work. It just won't append anything else to the file the next time a user logs in. The code should work, as far as I know it's coded correctly for what I want it to do. It's just not appending for whatever reason. I've made two functions, one using fopen/fwrite/fclose and the other uses file_put_contents($with, FILE_APPEND) and neither append as their supposed to. The data is just the same every time(it doesn't change at all). The permissions on the folders/files are 0666 (I'm using windows 7 and it won't let me chmod to 0777, but 0666 should work too) and since it creates/writes the first time, I don't see why it wouldn't be allowed to append to the file, so I doubt it's a permissions issue. Please take a look at my code and tell me what you think I should do to make this work the way I need it to. function log_action($action, $message="") { if(isset($action)) { $dt = time(); $datetime = strftime("%m-%d-%Y %I:%M:%S", $dt); $date = strftime("%m-%d-%Y", $dt); $dirname = strftime("%m-%Y", $dt); $logfile = SITE_ROOT.DS.'logs'.DS.$dirname.DS."logfile_".$date.".txt"; $data = strftime("%m-%d-%Y %I:%M:%S", $dt)." | Login: {$message} logged in.\r\n"; chdir(SITE_ROOT.DS.'logs'); if(!is_dir($dirname)) { mkdir(SITE_ROOT.DS.'logs'.DS.$dirname); //if($handle = fopen($logfile, "a")) { //$content = fwrite($handle, strftime("%m-%d-%Y %I:%M:%S", $dt)." | Login: {$message} logged in.\r\n"); //fclose($handle); //} else { //$message = "The log file could not be accessed."; //} if(file_put_contents($logfile, $data, FILE_APPEND)) { } else { echo "logfile could not be accessed"; } } return $message; } } I know it's pretty sloppy, but there's both codes with the fopen/fwrite/fclose portion commented out. That's the method I'd prefer to use, but I figured I'd show you both so you can see how it should go. I'm also aware there's more variables than necessary, but I was troubleshooting and made them so the code could be changed easier. I appreciate your replies in advance! Thanks. P.S. On my login page I have $action set to my login confirmation variable which is set when a user successfully logs in, and $message is set to their username, so it displays correctly in the output. I plan to make this a lot more versatile in the future, but I'd like to have it working first .
  13. My fault, I misunderstood, not the other way around . I thought putting the 0 in single quotes would make mysql consider it a string instead of an integer. As for Pikachu's(and I notice also Psycho) suggestion, I have the specific variable (menu_name(the only one that isn't an integer on the form)) trimmed on a separate part of the code, but thanks for the suggestion. I also have a separate function for max length. I am curious though, that <1 would allow the integer 0 and forbid the field from being blank as well correct? I never thought to do it that way and the trim on it already should stop the user from submitting a blank field with a couple spaces as psycho suggested. These are all great ideas, and I appreciate all the support. I got it working with the single quotes, so thank you Psycho for that, and thanks everyone else for your alternate suggestions. I'm slowly learning there is more than one way to do almost everything with PHP.
  14. I think you both misunderstand the reason I have behind this. I want 0 (false) to be an allowable answer. I know the form is flawed as it's used to validate both a string field, and radio buttons for whether or not the page is visible. So on is true, and off is false. before I added this piece to my code. If I were to select the visibility to be off, it wouldn't query mysql because php considered it an empty value therefore not sending it along with the rest of the query. It used to look like. $required_fields = array('menu_name', 'position', 'visible'); foreach($required_fields as $fieldname) { if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]))) { $errors[] = $fieldname; } } When I added && $_POST[$fieldname] != 0) it would allow me to send the false back to the database without any issues, but it also would let me send a blank field. Maybe this clears up my question a bit. Also, could you explain the difference between != and !==. I don't think !== is what I'm looking for because the way you said it, it should be referring to a string, but that's not why this code is in place. Thanks again.
  15. Hello, I'm new to this forum, and PHP, so don't go too rough on me lol. I'm learning from tutorials, and everywhere I look the code for "Not equal" is != correct? Well in my script, I'm trying to make required fields with 0 being an exception (for radio buttons) and for some reason when I put in != my form lets me leave the field blank and I get the "form successfully updated" message I'm supposed to get when the form is updated. Here's the code $required_fields = array('menu_name', 'position', 'visible'); foreach($required_fields as $fieldname) { if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) { $errors[] = $fieldname; } } However if I switch them around to =! I can no longer leave the field blank, and get the error message as I'm supposed to. I'm not sure if this is a PHP update since the tutorial was made or what, but I'd like to have it cleared up because, !empty and !isset work as they're supposed to, and me being new to this, I'm not sure if coding it as =! will act differently on the client end than I had planned. If that is in fact how not equal is supposed to be coded though, I'd like to know to clear up this confusion lol. Thanks in advance for any replies. I appreciate your help!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.