Jump to content

.josh

Staff Alumni
  • Posts

    14,780
  • Joined

  • Last visited

  • Days Won

    43

Everything posted by .josh

  1. @kicken that first word boundary won't work, because " /issue.." has 2 non-word chars in a row (the space and then forward slash). IOW it has to be [non-word char]\b[word char]
  2. close. A character class will match any one character (or not, if negated). So your attempt will match 2 characters total. All you have to do is combine [a-fh-z] Alternatively, you can do this: (?!g)[a-z]. This uses a negative lookahead to make sure "g" isn't the next character, then matches any character a-z. So while the character class itself will match for "g", the negative lookahead ensures "g" isn't actually there to match. This *might* be more readable to you.
  3. Yes, you gave the php code. I can read your code just fine. But clearly it's not doing what it's supposed to be doing, seeing as how you are on this forum telling us it doesn't work. But without explaining what you expect the code to be doing, we can't really do anything beyond pointing out syntax errors, which any monkey can do by paying attention to whatever the php error handler screams about. And since it's pretty clear that this is only part of your code, there could be any number of other things adversely affecting the code you posted, that we would have no idea about because you didn't post all of your code. We aren't psychic. So, the best way to move forward is for you to a) post relevant code (which you presumably did), b) explain what it is the code is supposed to be doing, c) explain what it is doing instead, because we don't know the whole picture.
  4. Yes. I've given you everything you need and then some. If you want to insist on doing it like that, then apply some critical thinking to pick the relevant parts of the code out and adapt it to your own.
  5. <form action="" method="POST"> <input type="hidden" name="id" value="1234" /> number 1:<input type="number" name="number[1]" value="<?php echo $_POST['number'][1];?>"/> <br> number 2:<input type="number" name="number[2]" value="<?php echo $_POST['number'][2];?>"/> <br> number 3:<input type="number" name="number[3]" value="<?php echo $_POST['number'][3];?>"/> <br> number 4:<input type="number" name="number[4]" value="<?php echo $_POST['number'][4];?>"/> <input type="submit" value="submit"/> </form> <?php if ($_POST) { // trim any whitespace $numbers = array_map('trim',$_POST['number']); // filter out numbers not entered in $numbers = array_filter($numbers); // if there aren't 4 numbers, echo out error if ( count($numbers) != 4 ) { echo "Please fill in form above?"; // there are 4 numbers, echo out results } else { // for each number... foreach ($numbers as $num) { // cast to integer just in case someone alters your form's input type $num = (int) $num; echo str_repeat($num.' ',abs($num)) . '<br/>'; } } } ?> Okay so this is a better way to write the script (but there are more improvements that could be made). 1) notice in the input fields, 2 changes. First is instead of using individual input names like "number1" "number2" etc. You should use an array. This will allow you to loop through the input or use built-in functions to loop through it, instead of having to evaluate each one individually. the 2nd thing is the input field value. You were previously using $number1, $number2, etc.. which you never actually define anywhere. If this was working for you, that must mean you have register globals turned on and that is BAD. Whoever is in charge of your server needs to turn off register globals, as this is a HUGE security risk. 2) look how i replaced your form validation. Since the input fields are put into an array instead of individual variables, you can significantly decrease the code by using functions that will walk through the array, and also loop through the array. 3) finally is the part that actually outputs the repeated numbers. a simple loop made possible by using an array for your form fields. First thing to note is that I cast the value to an integer type. This is to keep php from throwing an error if someone alters your form to submit something other than a number (very easy to do). Also note that I added abs around how many times to repeat the number in str_repeat(). This is because currently your form allows you to enter in negative numbers, and you can't repeat a string negative number of times. So abs() will give the absolute value, so for example if you enter in -4, it will repeat -4 four times.
  6. No, there are no other ways to filter it out except via the query or in the loop.
  7. Okay so there are a number of improvements that can be made to your existing script, but I see a difference between what you're asking for and what you're actually doing. Are you asking to just add the 4 numbers up and show the total, or are you wanting to repeat each number n times? In other words, let's say the user enters in 4 numbers: 2, 4, 5, 6 Do you want to give the user back this: 17 (the total) Or do you want to give the user back this: 2 2 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 Those are 2 different things...
  8. I'm not sure I understand what you mean. Do you mean 4 input fields where you can enter in 4 numbers, and then repeat those 4 numbers how ever many times the number is? Be more specific about what you are wanting to do. Show what code you do have, etc.
  9. it would be helpful if you explained what it is supposed to be doing and what it is not doing in less abstract/arbitrary terms. Saying "It works in one place but not another" isn't helpful. Saying "switch languages in interface" means nothing to anybody except you. What is the code physically supposed to be doing, altering a variable? show a "before", expected "after" and reality "after", etc.
  10. use a loop to count from $min (e.g. 1) to $max (e.g. 10). Or if it's a list of arbitrary numbers, loop through those instead. Use str_repeat for the repetition n times.
  11. what have you tried?
  12. Sweet.. Yeah it's only the same when you're actually assigning something. Example $k=$k+1; // <-- this increments value of $k because you are assigning $k+1 back to $k $k++; // <-- this is shorthand for the above $k+=1; // <-- also shorthand for the above ++$k; // *sort of the same. Those are all the same. But $k+1 all by itself doesn't actually assign a new value to $k. *sort of the same - Although $k++ and ++$k both increment $k, there is a subtle difference. $k++ increments after the reference to the $k is used, whereas ++$k increments before the reference to $k is used. Consider this: $x = 5; $y = 5; echo 'x: '.$x.'<br/>'; // output: 5 echo 'y: '.$y.'<br/>'; // output: 5 $a = ++$x; // this will first increment $x and then assign the value of $x to $a $b = $y++; // this will first assign the value of $y to $b and then increment $y echo 'x: '.$x.'<br/>'; // output: 6 echo 'y: '.$y.'<br/>'; // output: 6 echo 'a: '.$a.'<br/>'; // output: 6 echo 'b: '.$b.'<br/>'; // output: 5
  13. oh also, i failed to point something out from first response: foreach ( $textarray as $k => $char ) { if ( $char == $string && array_key_exists($k++, $textarray) ) { if (isset($textarray[$k++])) { $tempArr[]=$textarray[$k++]; } } } This: $textarray[$k++] - don't do that. Instead use $textarray[$k+1] The difference is that $k++ increments $k which is screwing with your loop, seeing as how that's part of the foreach iterator. $k+1 on the other hand just takes the current value +1 instead of actually assigning a new value to $k.
  14. basically the name of the game here is to make sure your variables, including array element references, are initialized and/or set before attempting to use them.
  15. okay so overall, these notices are telling you that you are attempting to use an index of an array that doesn't (yet) exist. So for this one: So this one looks to be coming from first iteration of your loop, that attempts to use $tempArr before you've initialized it. I see that in the bottom of your script you have $tempArr=array(); which both serves to wipe the current value and initialize it. Well you don't do that for your very first iteration. So add that to somewhere at the top of your script just before your loop. Or else move that line to inside your loop to pop when it's first used, instead of popping it after it's first used. IOW pop it at the beginning of each iteration instead of the end so that it's initialized in the beginning.
  16. This sounds like gmail's built-in warding against images. It's actually common in a lot of email providers, to prevent display of images unless the visitor clicks a button to enable them (or sets it to allow in their email settings). The reason is because in order to show images in an email, the image must be downloaded to the user's browser or email client (e.g. outlook). Images can possibly contain viruses or malware. More common though is that companies usually use images in emails as a way to track when users read the emails they send out. Well you know how much everybody likes having their activities tracked. Anyways, the point is that it's likely not a problem with your code, but a "problem" with gmail's (and other email providers) policy about displaying images in the email.
  17. foreach ( $textarray as $k => $char ) { if ( $char == $string && array_key_exists($k++, $textarray) ) { $tempArr[]=$textarray[$k++]; } On the last iteration of the foreach loop, you will get that error, since $textarray[$k++] won't exist. For example, let's say $textarray has 10 elements (0-9). Well on the last iteration of that loop, you are attempting to assign $textarray[10] to $tempArr[] which doesn't exist. I'm not sure what the actual intent of the logic is, so I don't know if this will work for you logic-wise, but for instance to get rid of that error, you would do something like this: if (isset($textarray[$k++])) $tempArr[]=$textarray[$k++]; This will cause the code to NOT assign a new element to $tempArr on last iteration of the foreach loop, though. So like I said, I dunno if that messes with the overall goal, so maybe you need to do something else. Point is that you are attempting to access an element of $textarray that doesn't actually exist.
  18. Is there any reason why you can't just do it like this? if ($someone !='john') $someone = 'mary'; $layout=' <h2 class="title"><a href="#">Title</a></h2> <p class="meta">bla bla bla by <a href="#">'.$someone.'</a></p> <div class="entry"> </div>'; echo $layout; Basically the point is that when you assign the value to $layout, php has already parsed the string and made the assignment. So the only way to change it is to either a) Assign the value to $layout again (the whole string including the var, like you initially did). This isn't very efficient. At best it's double coding. b) Do a search/replace of the name on $layout. In principle, this is how template engines work. Basically instead of using $someone in your $layout assignment, you'd use a unique placeholder, something like "[[someone]]" and then you'd use something like str_replace to replace "[[someone]]" with $someone. This isn't a bad method if you're actually trying to make a full template engine, but it's a bit overkill otherwise. c) Re-order your logic to have the final value of $someone when you assign it to $layout in the first place. This is the most efficient method and what you should go for (and what I did in the example above), if you aren't trying to make a full template engine out of this. On a sidenote, you should look into HEREDOC syntax for multi-line string assignment. It's a lot cleaner and easier to read than how you're currently assigning the value to $layout. For example: $layout=<<<EOS <h2 class="title"><a href="#">Title</a></h2> <p class="meta">bla bla bla by <a href="#">{$someone}</a></p> <div class="entry"> </div> EOS; Notice how in this syntax, I do not have to break out of quotes to insert a variable. Instead, I wrap the variable in curly brackets {}. You don't even have to do that in this specific example, since php understands from the surrounding content that $someone is the full var name (because $someone< isn't a valid variable name). But I like to use {} regardless, because for example if you wanted to do $someoneblahblah and really wanted php to parse for the variable $someone, well it's going to look for a variable called $someoneblahblah. So you can disambiguate with the curly brackets by doing {$someone}blahblah. In any case, point is that you don't have to worry about breaking out of quotes for vars or escaping quotes or anything, since <<<EOS and EOS; act as the string delimiter.
  19. also, nothing in your posted code is ajax.
  20. so is that entire js block actually being echoed out by php and $url is a php var? or is $url a js var? If it's php then you can do this: document.location = location.href+\"{$url}lats=\" + lat + \"&lons=\" + lon; if $url is actually a js var at that point, you can do this: document.location = location.href+$url+\"lats=\" + lat + \"&lons=\" + lon;
  21. just swap the order in your variable assignment... var dateTime = (currentDate.getMonth()+1) + '/' + currentDate.getDate() + '/' currentDate.getFullYear();
  22. Yes, taking out the return false; will allow you to go to the link.. however, as mentioned, what will happen is when you click the link, your function will execute, but then the browser is going to redirect to the myPage.html url, since you clicked on a link that has a target url. So at best you will only see it appear for all of a fraction of a second, before you are redirected. Why are you wanting to do this? I don't really see the point in doing this from a usability PoV. I don't really see the point in changing something on the page like that, when it's going to immediately be wiped and new page loaded. Now here's a question.. ARE you wanting it to only show the date onclick, or are you wanting it to show on page load so that it's there underneath the link before the link is actually clicked? I'm just trying to figure out what it is you're actually trying to do here, because as-is, it makes no sense to do this.. especially when you turn around and mention throwing a cookie into the mix which at face value has nothing to do with showing the current date underneath a link..
  23. From a top-level PoV, there is no difference, those terms are interchangeable in the sense that both at some point in time offer a way for a visitor to purchase something, vs. a website where you do not purchase something. Note that "purchase" does not necessarily mean purchasing a physical item, nor does it necessarily mean purchasing with real money. Now, beyond that, differentiating the 2 terms is highly subjective/opinionated, big gray line and all that, but it is fairly common to accept nisroc's definition - that a "shopping cart" site is usually a site that has all kinds of stuff on it, and "purchasing" something is just one part of that site. Versus an "e-commerce" site being a site that focuses almost exclusively on trying to sell you something. For example, phpfreaks.com focuses on providing coding help and tutorials etc.. but as a side thing, you can purchase a "supporter" subscription that will among other things, remove ads from your visiting experience here (without having to use something like adblock, that is). But since we don't focus on trying to sell people stuff, it would be inaccurate to put us in the same bucket as say, bestbuy.com, which focuses almost exclusively on selling you something. So a site like bestbuy.com, walmart.com, etc. would be a good examples of an "e-commerce" site, because they focus almost exclusively on trying to sell you something, and pretty much anything else on their site is secondary. So the main takeaway here is that both offer a way for someone to buy something, but the major difference between the two is how much focus is put on getting someone to buy something.
  24. 1) You have "smartquotes" in various places of your code (inside your link and your function). It should be regular single or double quotes. Perhaps this was just some copy/paste fiasco to the forums, but if those smartquotes are actually in your code, it's going to throw a js error and not work in general. 2) Your onclick just shows a reference to myFunction. It's not actually calling it. You need to do onclick="myFunction()" to have it actually invoke the function on click. 3) You have a target url in your link's href attribute. Even if you get the date to show under the link, at best you will see it for all of a fraction of a second before you get redirected to myPage.html. If you do not want this to happen, you have to do something to keep it from happening. For example, remove the href attribute, or return false in the onclick to prevent the redirect. 4) There are a number of things wrong with the date methods you are using. 4.a) The most immediate thing is that you have a typo: it should be .getFullYear() not .GetFullYear() (lowercase 'g'). 4.b) The 2nd issue is that the way you have it now, you will get (today) the following: "1/0/2014". The "1" is from your .getDay() method, which returns an integer value for the day of the week. The day of the week starts on Sunday "0". Today is Monday "1". I suspect what you really want is .getDate() which returns the actual date (e.g. "13" for today). 4.c) The 3rd issue is that the "0" portion (the month) also shows that .getMonth() returns the month, starting at 0. So if you want to show the more recognizable "1" for January convention, you need to +1 it. 5) Overall, your function is creating a date object and creating a formatted date based on that date object, but it isn't actually doing anything to display the date anywhere. Here is an example of how to make it display the date under the link. This example also stops the redirect from happening, though you didn't specify what you are trying to make happen overall: <!DOCTYPE html > <head> <script type='text/javascript'> function myFunction() { var currentDate = new Date(); var dateTime = currentDate.getDate() + '/' + (currentDate.getMonth()+1) + '/' + currentDate.getFullYear(); document.getElementById('currentDate').innerHTML = dateTime; } </script> </head> <body> <a href ='myPage.html' onclick="myFunction();return false;">test</a> <br/> <span id='currentDate'></span> </body> </html> So to reiterate the notes: 1) Don't use smartquotes in js. They don't work. Period. 2) The function without the () after it is just a reference. To actually call the function you have to have the () after it. 3) Is situational and depends on what your overall goal is. But in general, "Make the date show under the link when clicked" doesn't really jive with the link redirecting you to its target when it is clicked. So in my example, I added return false; to the onclick to prevent the browser from redirecting to myPage.html when the link is clicked. 4) Understand the date methods and what they actually return. 5) Setting javascript values does not by itself put things on your page. You need to set your page up to actually display it. There are many ways to do this. In this example, I added a "placeholder" span tag under the link, and in the function I change its contents.
  25. pffft... real coders develop on production.
×
×
  • 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.