Jump to content

objnoob

Members
  • Posts

    327
  • Joined

  • Last visited

  • Days Won

    3

Everything posted by objnoob

  1. It will create a variable name $array and the value of this variable would be an array. I'm quite certain.... Ex: <input type='text' name='array[]' value='value 1' /> <input type='text' name='array[]' value='value 2' /> <input type='text' name='array[]' value='value 3' /> On submission: <? php foreach($_POST['array'] as $value) echo $value.'<br />'; ?> value 1 value 2 value 3 On submission: <? php foreach($_POST as $var => $value) $$var = $value; if(isset($array)) foreach($array => $value) echo $value.'<br />'; ?> value 1 value 2 value 3
  2. Hi, Guy! This is pretty simple. 1. The error message are being stored in $errormessage, however this is not defined until the form is submitted. We'll need to define the error array even when the form as not been submitted, albeit, empty. 2. Now that we define an array to hold any errors, we can check it when ever! Since we can check it whenever.... it makes the form reusable (on initial display, on error display) I have not tested this code. <?php session_start(); # start session $pid = 1000; # whatever this is $submit = false; # form has not been submitted ( even so, we don't know yet until we check ) $errormessage = array(); # need something to hold errors if(isset($_POST['submit'])){ # check if form was submitted by means of clicking the submit button... #since we only have 1 submit button, the value of the button 'Upload It' is moot! $submit = true; # yes, the form as been submitted if( ! isset($_FILES['uploaded_file']) ){ $errormessage[] = 'Yikes! My HTML skills suck, and I have no clue!'; }else{ if(($errId = $_FILES['uploaded_file']['error'])){ # oops, there was an error! $upload_errors is an array of php upload error codes and messages $upload_errors = array( 0=>'There is no error, the file uploaded with success', 1=>'The uploaded file exceeds the upload_max_filesize directive in php.ini', 2=>'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', 3=>'The uploaded file was only partially uploaded', 4=>'No file was uploaded', 6=>'Missing a temporary folder' ); $errormessage[] = $upload_errors[$errId]; # get store the php error message in our $errormessage array }else{ $fileName = $_files['uploaded_file']['name']; $ext = substr($fileName, strrpos($fileName, '.')); if( ! move_uploaded_file($_FILES['uploaded_file']['tmp_name'], '/path/to/perm_file_location/'.$pid.$ext) ){ # error storing the uploaded file permanently! $errormessage[] = 'Could not permanently store the uploaded file! Perhaps a permissions issue'; } } } } # if form not submitted, or form submission has errors if( ! $submit || $errormessage ){ # print the form to browser # foreach error message, print the message. if there are none.... none will print! foreach($errormessage as $msg){ echo "<div style='color:red'>{$msg}</div>"; } ?> <form enctype="multipart/form-data" class="clearfix" action="" method="post"> Choose your file here: <input name="uploaded_file" type="file"/><br /><br /> <input type="submit" name="submit" value="Upload It" class="" /> </form> <?php }else{ # submitted, and no errors!! echo '<div style="color:green">Nom Nom Nom! We got your file and it was delicious! Send more, Thanks!</div>'; }
  3. Jazzy Firefox toolbar configuration you have there, sir. :]
  4. This code is not the only code that does the job correctly. And, it looks like a rats nest. You should clean that up some. And, you should define the functions show_totals and add_totals. And, you should comment out ALL of the comments appropriately. Your code is far from correct.
  5. you can do this.... which makes a multidimensional array $assets = array(); while ($row = mysql_fetch_array($result)) { $assets[$row['Type_A']][$row['Type_B']][] = $row['Name]; } Then you can do: foreach($assets as $type => $makes){ echo $type . '<br />'; foreach($makes as $make=>$series){ echo ' ' . $make. '<br />'; foreach($series as $model){ echo ' ' . $model. '<br />'; } } }
  6. Right! We're not making an array for every song, we're making an array of every song (that matched the glob pattern).
  7. Well, if you don't want to make an array of every song.... you better delete $Songs = glob($SongsFolder."/*.mp3"); because that's exactly what it's doing
  8. Just don't dynamically create the form using this line. $formhtml="<form action="sometext" method="POST">";
  9. $songs = array('song a','song b','song c'); # the songs in order a, b, c shuffle($songs); # mix up the songs! PROBABLY not in order any more echo $songs[0]; # get the first song from the $songs array, which is PROBABLY not 'song a' since the songs were PROBABLY mixed up. ## PROBABLY IS A VERY IMPORTANT WORD HERE
  10. preg_match()'s 3 parameter is passed by reference. you need to make sure you declare it so the function can refer to it. $match = array(); # declared, initialized, and referable preg_match('%image-carousel">.+?jpg%i', $html, $match); echo $match[0]; Edit: Nevermind, I guess you don't have to. I forget how easy PHP really is :]
  11. parent_id column in your comments table is all you really need. ie, comment 4 has parent comment 2, comment 2 has parent comment 1. comment 3 has parent comment null (null is mind blowing) You don't need any extra tables to accomplish this. Comment 1 Comment 2 Comment 4 Comment 3
  12. Solutions! I like! One could also postback to self. ie, <html><head></head><body> <?php # comments page $user = null; # assume no user is signed in $showForm = false; # whether to show the comments form if ( ! isset($_POST['btnComments'])) { $showForm = true; # comments form not submitted, lets show it.... but before, lets check some other stuff if( isset($_SESSION['user']) ){ $user = $_SESSION['user']; # check, set $user if signed in }elseif( isset($_POST['btnLogin']) ){ require '/var/www/scripts/login-process.php'; # not signed in, but clicked to log in. validate this attempt by including the proper script }else{ echo "Hey buddy! Sign in if you have an account!: <form method=post><input name=login /><input type=password name=password /><input type=submit name=btnLogin /></form><br />"; } }else{ // process comments form, user submitted comments $errors = array(); // store any errors in this array } if($showForm || $errors): ?> <form method=post> YADDA YADDA </form> <?php else: ?> <p>Thanks for those comments, buddy!</p> <?php endif; ?> </body> </html>
  13. And, when you decide to move your files around.... it becomes your problem. Why code problems, when you can code solutions!?
  14. you create a function that accepts the brand. function isBrandOkay($brand){ switch(strtolower($brand)){ case 'laptop': return true; case 'computer': return true; } return false; } $thisBrand = 'computer'; echo $thisBrand . ((isBrandOkay($thisBrand)) ? ' is okay!' : ' is <b>not</b> okay');
  15. You append [] to the input element's names making the value an array of values. <?php # on form create while( $row = msyqli_fetch() ) { # 1 or more expression not complete. echo '<input type="checkbox" name="id[' . $row['id'] . ']" value="1" /> input type="text" name='weight[' . $row['id'] . ']' value /> <br />'; } ?> <!-- creates something like this --> <input type="checkbox" name="id[MYSQL_RECORD_ID_HERE]" value="1" /> input type="text" name='weight[MYSQL_RECORD_ID_HERE]' value /> <br /> <input type="checkbox" name="id[NEXT_RECORD_ID_HERE]" value="1" /><input type="text" name='weight[NEXT_RECORD_ID_HERE]' value /> <br /> <input type="checkbox" name="id[LAST_RECORD_ID_HERE]" value="1" /><input type="text" name='weight[LAST_RECORD_ID_HERE]' value /> <?php # on postback foreach($_POST['id'] as $record_id => $nothing_useful){ $sql = "update tOrders set weight=" . (float)$_POST['weight'][$record_id] . "where order_id =".(int)$record_id; } ?>
  16. You should always redirect using a 303 after any successful login, otherwise a few clicks on the back button could be used to sign in. Also the from_reg whatever whatever is too specific. You could generalize that "FROM" and use it for all sorts of different stuff like tracking users' click trends. Also, getting this from value defined by means of a hidden form input is bad practice. 1. it's now user data. and 2. if it's a path, users can redirect themselves anyplace. Using the $_SERVER super variable to capture any referrer URL, and validating it's part of your site, and ensuring it's a suitable location for the user is, imo, just much better. Thanks!
  17. if( $loginSuccess ){ header ('HTTP/1.1 303 See Other'); header ('Location: /'); }
  18. Sorry, I made a mistake in the code. This will work: while($min < 2){ # while minute less than 2 } Also, for this code to work one would need to call this set_time_limit function before sleep(60); set_time_limit(0); # allow the script to run indefinitely
  19. Since PHP's userspace output buffering allows you to nest output buffers, we must ensure to end output buffering on all and any buffers currently capturing output. Also, PHP uses output buffering by default so a call to flush() must be made to force output to the browser. <?php while( ob_get_level() ){ # while ob_get_level() returns a value other than 0 echo ob_get_clean(); # echo the contents of the current output buffer and disable that buffer. } # now we've disabled all output buffers started. echo 'We can echo this, flush, and sleep! Now, Let\'s wait 2 minutes!'; flush(); # force this output to browser $min = 0; while($min > 2){ echo '<br />We are at minute '.$min++; flush(); sleep(60); } echo '<br />And we\'re done!';
  20. once you get the hang of it, you never forget. like riding a bike.
  21. A leading and trailing single quote would help too. See in red below. $sql = "INSERT IGNORE INTO " .$table. "VALUES ('" . implode("','", $data) . "')";
  22. try single quotes?
  23. STOP NOW! Use mysqli_ and you'll be headed in a great direction! See the big fat warning? Big Fat MySQL Extension Warning This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_connect() PDO::__construct() USE MySQLi INSTEAD
×
×
  • 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.