Jump to content

monkey_05_06

Members
  • Posts

    55
  • Joined

  • Last visited

    Never

Everything posted by monkey_05_06

  1. Update: I tried just using a for loop instead: for ($i = 0, $set = $sets[$i]; $i < count($sets); $i++, $set = (($i < count($sets)) ? $sets[$i] : NULL)) { It produced exactly the same results. Why? What in processing this command is causing the $sets array to be modified? Edit: Update Mark II - SOLVED! I was being an idiot. In a <b>PRIOR</b> foreach loop I was doing this: <?php foreach ($sets as &$set) { // ... } ?> So $set was still set as a reference to $sets[4]. D'oh! Simply calling unset($set); after I was done with using it by reference resolved the issue!
  2. Hey freaks, long time no see. Not that I was ever very active anyway... So basically I'm doing this: <?php echo "before foreach: " . $sets[4]["scriptName"] . "<br />\n"; foreach ($sets as $set) { echo "before: " . $sets[4]["scriptName"] . "<br />\n"; ?> <input type = "checkbox" name = "include_<?php echo $set["scriptName"]; ?>" <?php $checked = TRUE; if (!isset($_POST["include_{$set["scriptName"]}"])) $checked = FALSE; $query = "UPDATE `users` SET `include_{$set["scriptName"]}` = \"" . (($checked) ? "1" : "0") . "\" WHERE `UID` = \"{$userID}\""; @mysql_query($query); if ($checked) echo "checked "; ?>/> Include <?php echo $set["friendlyName"]; ?>?<br /> <?php if ($checked) print_set($set); echo "after: " . $sets[4]["scriptName"] . "<br />\n"; } ?> And the output I'm getting is very worrying to me. The echo prior to the foreach statement prints the proper value. However as soon as the first "before" statement is being printed, $sets[4]["scriptName"] is immediately set equivalent to $set["scriptName"]. This happens every loop, meaning that by the time it reaches $sets[4], the last index that $sets[4]["scriptName"] is now reading as $sets[3]["scriptName"]. Why is foreach doing this? I've never encountered this before, and as I said it happens immediately when the foreach block is entered. Just in case you were wondering, my print_set function doesn't have any access to the global $sets array, it only takes the parameter for the set to print out.
  3. Because it's 3:15 AM [here]. The more important question is why I would be awake. I guess I just figured it would be easier for me to keep the connection open than having to reopen it. I've got all the pertinent data regarding connecting to MySQL available to both pages...it was really just a matter of lack of experience with PHP...and being extremely tired. I understand now why that line of thinking doesn't really make sense.
  4. Hmmm...actually I can think of a way to work around it now that I know it can't be done. Thanks!
  5. I'm working on a script where I would like a MySQL link identifier to persist over a redirect to another URL. I've tried using session variables to persist the identifier: <?php session_start(); $conn = @mysql_connect("localhost", "username", "pass"); $_SESSION["conn"] = $conn; header("Location: $url"); ?> The variable persists the redirect, but the identifier seems to be getting modified. I know for a fact that it is a valid MySQL link identifier prior to the redirect, however after the redirect it is invalid. Is there any explanation for this???
  6. Ah thank-you. That's exactly what I needed. The function is so descriptively named... I'm sure this has probably been brought up before, sorry for not searching 'round the forums first. I was just being lazy.
  7. When a function returns it simply means that the function stops executing and the script returns to wherever the function was called from. In your example, you've defined the function, but you're never actually calling it. I assume that this is the action script for an HTML form you're using? The solution is, as suzzane said, to use header to redirect...but you should probably also look into how functions work. You don't seem to completely understand the concept.
  8. Is there any function that can be used to interpret a char as an int? AFAIK PHP doesn't distinguish between chars and strings (a char is simply a string with a length of 1 (right?)), so I haven't found/can't think of a way to do this. I imagine I might not be coming across very clear right now, so let me try to explain what I want. I want to convert a char, say 'A' to it's appropriate integer value, i.e., 65. Everything I've tried as far as conversion has simply led to the chars/strings casting to 0. Thanks for any help with this. - monkey
  9. O.O Why the hell is it doing that? I need to take a look at it but my schedule just shut down big time. In other words, I finally got a job! It sucks 'cause now I don't have countless hours on end to spend working on these things...but it's good 'cause now I'll have money. *sigh*
  10. Apparently what I wrote about JS not recognizing the form is an issue with FF (1.5, I'm not sure about 2.0). Seeing as in that snippet I never set a JS var called fullname, that snippet isn't meant to be a standalone example. It should submit the form and then output...something...though I'm not sure what considering the JS var is never set. Possibly an error. Or the fact that the JS var is never set might make it crash. I don't know enough about JS to be sure. [code]<?php   if (!isset($_POST["fullname"])) {   ?> <form action = "" name = "myform" method = "POST">   FORM <!-- Without some output JS won't recognize your form - FF 1.5 -->   <input type = "hidden" name = "fullname" /> </form> <script language = "JavaScript">   var fullname = "John Doe";   document.myform.fullname.value = fullname;   document.myform.submit(); </script> <?php     }   else {     $fullname = $_POST["fullname"];     echo $fullname;     echo $fullname;     }   ?>[/code]
  11. Okay it could just be Firefox 1.5. I don't know if this is any different in FF 2.0. And I'm unfamiliar with "the .forms collection" to which you refer. As I already mentioned, I don't use JS unless it's necessary...and I got it working I just was unsure if this was perhaps a bug. In any case, thanks for the responses.
  12. header requires a full path be sent. Or at least it's supposed to. So: [code]if (isset($_SERVER["HTTP_HOST"])) $host = $_SERVER["HTTP_HOST"]; else $host = $_SERVER["SERVER_NAME"]; $uri = dirname($_SERVER["PHP_SELF"]); if ($uri === "\\") $uri = "/"; if ($uri[strlen($uri) - 1] !== "/") $uri .= "/"; header("Location: http://" . $host . $uri . "include.php?page=newsconfirm");[/code] Also, AFAIK the header function is case-insensitive in regards to the HTTP header choice (i.e., "Location" === "location"). But I could be wrong about this, so it's probably safest just to use "Location".
  13. I was actually hoping someone would solve my challenge mathematically. Oh well. :P Orio your code doesn't...decode properly.
  14. You can use JS to submit a form: [code]<?php   if (!isset($_POST["fullname"])) {   ?> <form action = "<?php echo $_SERVER["PHP_SELF"]; ?>" name = "myform" method = "POST">   FORM <!-- Without some output JS won't recognize your form -->   <input type = "hidden" name = "fullname" /> </form> <script language = "JavaScript">   document.myform.fullname.value = fullname;   document.myform.submit(); </script> <?php     }   else {     $fullname = $_POST["fullname"];     echo $fullname;     echo $fullname;     }   ?>[/code]
  15. You're asking someone to write a script that uses the mysql_connect function? Or what are you asking? [b][EDIT:][/b] You do of course realize that I was joking about "the dreadful extensions"? I just wanted to see if anyone would actually take the time to work it out without these pre-built functions.
  16. [code]<?php   function fib($which) {     static $fibs = array();     $which = intval($which);     if (isset($fibs[$which])) return $fibs[$which];     $neg = (($which < 0) && (($which % 2) === 0));     if ($which < 0) $which *= -1;     if (($which === 0) || ($which === 1)) return $which;     $rval = fib($which - 2) + fib($which - 1);     $rval = ($neg ? $rval * -1 : $rval);     $fibs[($neg ? $which * -1 : $which)] = $rval;     return $rval;     }   for ($i = 0; $i < 40; $i++) echo fib($i) . (($i % 10) === 0 ? "<br />" : " ");   ?>[/code] At 40 levels of recursion this was starting to run extremely slowly...timing out in fact...storing the previously called Fibonacci numbers in a static array sped it up immensely. [b]Next Challenge:[/b] I want to see someone write a function to get the day of the week without using any calendar functions. Basically I want a form where I can enter any date and it will tell me the day of the week. If requesting the same thing again seems drab or whatever anybody who feels compelled to set a different challenge feel free. This is just something I'd like to see someone work out in PHP (without using those dreadful extensions that do all the work for them :P).
  17. I think he was saying to do basically the same thing...but only use php/GD functions (no custom functions)...? I'm actually working on something else right now (plus I just did the last challenge) though the Fibonacci numbers sounds fun. I could use a refresher on scripting that (I've done the same thing in C++ but it's been a while). :)
  18. i.e., the [color=green]document.formname[/color] variable isn't set. So if I have a form like this: [code]<form name = "hiddenForm" action = "">   <input type = "hidden" name = "one" value = "1" />   <input type = "hidden" name = "two" value = "2" /> </form>[/code] Then the [color=green]document.hiddenForm[/color] variable won't be set. Changing it to something even as basic as: [code]<form name = "hiddenForm" action = "">   A   <input type = "hidden" name = "one" value = "1" />   <input type = "hidden" name = "two" value = "2" /> </form>[/code] Will register the variable.
  19. [quote author=obsidian link=topic=119164.msg489835#msg489835 date=1166731226]All my learning of the aforementioned languages is from [b]loads[/b] of reading and even more [b]trial and error[/b] ;)[/quote] Isn't that what self-taught [i]really[/i] means....? ;)
  20. [code]<?php function ftp_dir($ftp_stream, $dir, $rmode=0755, $tmode=FTP_BINARY) {   // ftp_stream is...the ftp stream (i.e., the resource returned by ftp_connect)   // dir is the directory to upload   // rmode is the read mode (like the values you set when using chmod)   // tmode is the transfer mode (FTP_ASCII or FTP_BINARY only)   if ((!is_dir($dir)) || (($tmode !== FTP_ASCII) && ($tmode !== FTP_BINARY))) return FALSE;   if (!@ftp_chdir($ftp_stream, $dir)) {     if (!@ftp_mkdir($ftp_stream, $dir)) return FALSE;     ftp_chdir($ftp_stream, $dir);     }   $rmode = intval($rmode, 8);   $dir_files = scandir($dir);   for ($i = 2, $dir_size = sizeof($dir_files); $i < $dir_size; $i++) {     ftp_put($ftp_stream, $dir_files[$i], $dir . "/" . $dir_files[$i], $tmode);     ftp_chmod($ftp_stream, $rmode, $dir_files[$i]);     }   return TRUE;   } ?>[/code] It can't be so hard if I can figure it out! :P [b][EDIT:][/b] 2 posts while I figured it out but still... Hmmm....totally n00b'd right here. Can someone please help me figure out why I can't delete these files...? :o (I'm not that familiar with read permissions so I may have set them wrong (0755)...but....) [i]Modified version of the above:[/i] [code]<?php function ftp_dir($ftp_stream, $dir, $rmode=0755, $tmode=FTP_BINARY, $ormode=array(), $otmode=array()) {   if ((!is_dir($dir)) || (($tmode !== FTP_ASCII) && ($tmode !== FTP_BINARY)) || (!is_array($ormode)) ||   (!is_array($otmode))) return FALSE;   if (!@ftp_chdir($ftp_stream, $dir)) {     if (!@ftp_mkdir($ftp_stream, $dir)) return FALSE;     ftp_chdir($ftp_stream, $dir);     }   $rmode = intval($rmode, 8);   $dir_files = scandir($dir);   for ($i = 2, $dir_size = sizeof($dir_files); $i < $dir_size; $i++) {     if (isset($ormode[$dir_files[$i]])) $ormode[$i] = $ormode[$dir_files[$i]];     if (isset($otmode[$dir_files[$i]])) $otmode[$i] = $otmode[$dir_files[$i]];     if ((isset($otmode[$i])) && (($otmode[$i] !== FTP_BINARY) && ($otmode[$i] !== FTP_ASCII))) $otmode[$i] = $tmode;     ftp_put($ftp_stream, $dir_files[$i], "./" . $dir . "/" . $dir_files[$i], (isset($otmode[$i]) ? $otmode[$i] : $tmode));     ftp_chmod($ftp_stream, (isset($ormode[$i]) ? intval($ormode[$i], 8) : $rmode), $dir_files[$i]);     }   return TRUE;   } ?>[/code] I've added two optional parameters, [color=green]$ormode[/color] and [color=green]$otmode[/color]. Both are arrays designed to override the [color=green]$rmode[/color] and [color=green]$tmode[/color] parameters respectively. That way you can set file permissions/transfer mode on a file-per-file basis. [code]<?php $conn = @ftp_connect($host); if ((!$conn) || (!@ftp_login($conn, $user, $pass))) die("Error connecting to FTP."); ftp_dir($conn, $dir, 0755, FTP_BINARY, array("robots.txt" => 0600), array("robots.txt" => FTP_ASCII)) or die("Error uploading directory."); // uploads all files in DIR in FTP_BINARY mode and CHMODs them to mode 0755 EXCEPT robots.txt which is uploaded in FTP_ASCII mode and CHMODed to mode 0600 ftp_close($conn); ?>[/code] [b]Next Challenge:[/b] Do that thing that makeshift_theory said since I can't think of anything right now! ;)
  21. [code]<?php   function is_prime($num) {     if ($num === 0) return FALSE;     for ($i = 2, $root = (int)sqrt($num); $i <= $root; $i++) if (($num % $i) === 0) return FALSE;     return TRUE;     }   function print_primes_bold($text) {     $text = (string)$text;     for ($i = 0, $text_len = strlen($text); $i < $text_len; $i++) {       if (is_prime($i)) echo "<b>{$text[$i]}</b>";       else echo $text[$i];       }     }   ?>[/code] Next challenge: Write a script that takes a date (Day, Month, Year) and then tells the user what day of the week it is (i.e., Sunday, Monday, Tuesday, etc.). (Hint: Set a seed date/day-of-week combination and then find the offset of the entered date)
  22. I, whenever possible, don't like to use JavaScript due to personal preference. However when attempting to retrieve the end-user's browser dimensions I was left with no choice as PHP is completely server-side. One thing that I noticed that actually caused SEVERAL problems for me is JavaScript's registration of HTML forms. Since the only thing I needed to do via JavaScript was retrieve (since this can't be done in PHP) and then post (to transfer the data from JS to PHP) the dimensions, my code was pretty simplistic. My form contained nothing but two elements, one for width, and one for height, both of which were hidden. I didn't need the user to input anything into the form, it was all taken care of by the JavaScript. Or so I thought. In some of my primary testing I realized that without screen output my script wasn't working properly. Well that was simple enough to fix. It was however later when I removed the screen output...then the problems began. I tried everything I could think of to figure out why JavaScript wasn't registering the form (which I determined via JS alerts). All the tutorials I'd read said I was doing nothing wrong...but my form just wasn't being registered!!! WHY ME??? So finally I caved in and copied a form from one of the tutorials. It worked exactly as it should have. ARG!!! That's when it occurred to me that this form had textual output as well as visible form elements. What would happen if I tried removing the text and changed the elements to hidden? AHA! JavaScript was no longer registering the form! Simply reinstating the textual output in [i]my[/i] form was enough to get it working again. I figured that others might be able to gain something from this knowledge...but there's also a question here...is this a bug in JavaScript? Or is it by design that "empty"/"non-visible" forms aren't registered? The latter wouldn't really make much sense to me...but I figured it could make for an interesting conversation piece.
  23. The old method uses only CSS and so it has to put DIVs on top of the TABLE to create the illusion of rounded corners. These DIVs must have a background color set manually, so that if you have a background image for that area, you cannot see it behind the rounded corners. The newer methods use JavaScript which I'd rather not do :P. Also I've never liked relying on other people's coding. I'd just much rather do things myself. That way I learn...and if something gets screwed up 1) it's my fault, and 2) I have to either learn how to fix it or work around it. ;)
  24. There's also -khtml-opacity that should be taken into consideration: [code]filter: alpha(30); -moz-opacity: 0.30; opacity: 0.30; -khtml-opacity: 0.30;[/code] That will ensure your opacity settings get applied to all browsers.
  25. Just to see what's going on...why not throw a die in there somewhere... i.e.: [code]if (getext($filename) === ".jpg") die("JPEG"); else if (getext($filename) === ".gif") die("GIF"); else if (getext($filename) === ".png") die("PNG");[/code] I'm telling you...you really need to use triple equal signs here. You said it didn't make a difference, but you should still change it.
×
×
  • 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.