-
Posts
4,704 -
Joined
-
Last visited
-
Days Won
179
Everything posted by kicken
-
Glad to hear your doing well and learning. I notice references to stargate stuff in several posts, makes me curious what your working on. Big fan of the franchise, just finished watching sg-1 (again lol).
-
Why data type REAL returns almost-correct values for decimals?
kicken replied to benphp's topic in Microsoft SQL - MSSQL
Not all software treats floats the same way. Some software may use more or less bits to represent the value, or may have different levels of precision/rounding. There's a whole slew of problems/quirks one has to deal with when working with floating point values. That is why it is generally recommended you avoid them when possible. -
<?php $handle = fopen("test.txt",'a'); //Writing a new line. $result = fwrite($handle,"\nMy name is sonu"); if($result) { echo "Done"; } else { echo "error"; } fclose($handle); ?>
-
Finding percentage difference to assign as $discount !
kicken replied to Steve1957's topic in PHP Coding Help
string(0) "" indicates that your variable has no value. It is an empty string, which when converted to a number is zero. Your variables appear to not be getting the values you think they should. Somewhere in your code prior to this you are not assigning them correctly, or not passing them to the page correctly. You will need to start looking back through the code to find out where the error is. Move the var_dump statements around your code at different places and use them to "trace" the code by seeing what the values of certain variables are at different points in the script. PHP does not care too much about the actual type of the variables. Having them be strings is ok, PHP will convert them to numbers when it needs to in order to do the math. You just have to ensure that the value consists of only digits and a decimal point. Any non-numeric characters such as a $ prefix or comma separator will cause PHP to fail to convert the value. -
"\n" represents a new line. Just write that you where you want it to be.
-
Here is a function I use that closes up tags. You could adapt it to also count the 800 non-tag characters and use it for extracting your summary. function fixupHtml($html){ static $noClosers = array( 'input', 'br', 'link', 'base' ); $tagStack = array(); $len=strlen($html); $pos = 0; while (($pos=strpos($html, '<', $pos)) !== false){ $pos++; $isEnding = false; $isSelfClose = false; $foundTagEnd= false; for ($i=$pos; !$foundTagEnd && $i<$len; $i++){ $ch = $html[$i]; switch ($ch){ case "/": $isEnding = true; $isSelfClose = !($i==$pos); break; case '>': case " ": case "\r": case "\n": $foundTagEnd=true; break; default: } } $tagEnd = strpos($html, '>', $pos); if ($tagEnd === false){ $foundTagEnd = false; } if ($foundTagEnd){ $i--; $tag = substr($html, $pos, $i-($pos)); if (!$isEnding){ if (!in_array($tag, $noClosers)){ array_push($tagStack, $tag); } } else if (!$isSelfClose){ $tag=ltrim($tag, '/'); $tslen = count($tagStack); if ($tagStack[$tslen-1] == $tag){ array_pop($tagStack); } else { //Try and find it earlier in the stack $found=false; for ($i=$tslen-1; !$found && $i>=0; $i--){ if ($tagStack[$i] == $tag){ unset($tagStack[$i]); $tagStack = array_values($tagStack); $found=true; } } if (!$found){ //Bad end tag found. Lets remove it. $tagStart = $pos-1; $endOfTag = strpos($html, '>', $tagStart); $html = substr($html, 0, $tagStart).substr($html, $endOfTag+1); } } } } else { $html = substr($html, 0, $pos-1); } } while (count($tagStack) > 0){ $tag = array_pop($tagStack); $html .= '</'.$tag.'>'; } return $html; }
-
Look at the manual for fopen to see what the different modes do. 'w' is: In otherwords, it erases the entire contents of the file when you open it. There are other modes that allow writing which do not do this, such as 'r+' or 'a'. That said, there is no 'insert' mode where you can write data to the beginning (or middle) of a file without overwriting the data which is there. If you want to do that you have to read in all the existing data to a variable, truncate the file, write the new data, then re-write the old data. You can append data to the end of a file without having to touch the old data first. Use mode 'a' and then just fwrite the new data to put it at the end.
-
Finding percentage difference to assign as $discount !
kicken replied to Steve1957's topic in PHP Coding Help
Steve, The question being asked was do your variables ($price and $listprice) contain a numeric value such as 25.20 or do they contain your formatted value such as '$25.20'. PHP cannot do math on string values, it has to convert them to a number first. If your value contains non-numeric characters such as '$' or ',' then this conversion will fail and PHP will use 0, causing your math to come out incorrect. When we ask you to verify or show us what your variables contain as a value, what we want you to do is echo them out just prior to your usage of them so that you can see what value they have at that particular point in time. One of the better methods to use for this is the var_dump function. In your case, you'd modify your code to look like this: var_dump($listprice); var_dump($price); $discount = ((($listprice - $price) / $listprice) * 100); var_dump($discount); $content = str_replace("{discount}", $discount, $content); then load your page. You'd notice some extra output which would show you the exact values of those variables at that moment. -
PHP has serialize and unserialize functions that will convert variables to/from string form. In your case though, why not just json_encode the array and store that in the database?
-
You can do a regex replace of white-space. Though that'd remove it from any string values if you have them. $out = print_r($arr, true); $out = preg_replace('/\s+/', '', $out); edit: why are you storing the output of print_r in a database?
-
Array_multisort with a variable amount of arrays
kicken replied to longphant's topic in PHP Coding Help
If you have 5.3 or better and can use closures: function getCompareFn($keys){ return function($a, $b) use ($keys) { foreach ($keys as $k){ if ($a[$k] < $b[$k]){ return -1; } else if ($a[$k] > $b[$k]){ return 1; } } return 0; }; } $arr=array( array('name' => 'a', 'group' => 'a', 'order' => 2), array('name' => 'b', 'group' => 'a', 'order' => 1), array('name' => 'c', 'group' => 'b', 'order' => 5), array('name' => 'd', 'group' => 'c', 'order' => 3), array('name' => 'e', 'group' => 'b', 'order' => 4), array('name' => 'f', 'group' => 'c', 'order' => 6) ); usort($arr, getCompareFn(array('group', 'order'))); print_r($arr); -
Regulate "Image Size" and "Image Dimensions"?
kicken replied to doubledee's topic in PHP Coding Help
Only if you feel it necessary. If your only going to use resized copies and not the original anywhere than dimensions is less of a concern probably as you'll have them standardized. Even if you do use the original, checking dimensions is only necessary if you feel it to be. For example if you had a photo gallery people may want/need to upload large dimension images. Pretty much, it all boils down to do you think it is necessary, and if so what do you feel is a reasonable dimension/size for user's images. I personally haven't limited peoples image uploads beyond raw filesize in anything I have done recently. Even if it's just for a profile photo or similar i usually give the user the ability to crop/edit their upload as necessary so I store the original un-altered file somewhere then make copies to use on the site where necessary. -
Correct
-
You only need to check the width and height if you want to enforce a maximum/minimum size. If you don't care what size the image is you do not need to check.
-
You could not change the DB at all and just specify it in the query. Table names can be prefixed with the DB name allowing you to work across multiple DB's at once. INSERT INTO slow_execution.query_execution_time ...
-
If you don't already have some central function you run all your queries through, you'll just have to scatter your code with some calls to a timing function. I use this function to help to timing of my code when I find something is slow: function markTime($finish=false, $lbl=null){ static $startTime=null; static $points=array(); if ($finish){ if ($startTime==null){ $startTime = microtime(); trigger_error('Timer not initiated. Call with no arguments to start timer.'); } $endTime = microtime(1); if ($lbl == null){ $lbl = count($points); } $points[] = array($lbl, $endTime); $tally = array(); $lastPoint = $startTime; foreach ($points as $point){ $fromStart = number_format($point[1]-$startTime, 3); $fromPrev = number_format($point[1]-$lastPoint, 3); $tally[] = array($point[0], $fromStart, $fromPrev); $lastPoint = $point[1]; } $totalTime = number_format($endTime-$startTime, 3); echo '<pre> Total time: '.$totalTime.'; tracked '.count($points).' points Time track:'; foreach ($tally as $pointInfo){ echo ' Point: '.$pointInfo[0].' From start: '.$pointInfo[1].' From prev: '.$pointInfo[2]; } echo '</pre>'; $startTime = null; } else if ($startTime == null){ $startTime = microtime(1); $points = array(); } else { if ($lbl == null){ $lbl = count($points); } $points[] = array($lbl, microtime(1)); } } As an example usage: <?php markTime(false, 'Begin loop'); foreach (range(1, 1000) as $i){ //... } markTime(true, 'End Loop');
-
Is the 'score' column a VARCHAR? If so that is wrong, you need to make it a numeric type such as INT.
-
Running a value through intval() is good enough, no need to call mysql_real_escape_string on it. As an integer it can only contain digits 0-9, anything invalid would just become 0.
-
As mentioned above, the problem is likely because your WHERE condition depends on a column in the table which is null in the case of no join being made. You could try adding OR timestamp IS NULL to the condition, see if that helps. If not, maybe try moving the timestamp>'2012-02-15' condition from the WHERE clause to your ON clause.
-
Remove your group by and just look at the results: select stats.terminal, id from stats right join (select distinct terminal from stats) as terminais on stats.terminal=terminais.terminal where timestamp>'2012-02-15' Do you see the rows for what you want, but with a NULL value for the id column? If not then that is why they are not counted and you need to fix the query so they are displayed. When they are displayed then re-apply the group by.
-
You need to convert the column to zero before the count is applied, not after. select stats.terminal, count(coalesce(id,0)) edit: that would probably give you a 1 not 0 like you want. Using SUM and a CASE statement would probably work. select stats.terminal, SUM(CASE WHEN id IS NULL THEN 0 ELSE 1 END)
-
Functions being called multiple times when header is set
kicken replied to patawic's topic in PHP Coding Help
Firefox had (has? dunno if it's been fixed) an issue where when you loaded an image directly it would request it twice. Once to render the image in the browser, and again to use it as a favicon in the URL bar. Could be that your seeing. The best way to figure out if it is multiple requests is to use a tool that logs requests and see how many are made. I prefer using Fiddler2 for this. -
Use the function imagesavealpha to tell GD to save complete alpha channel information to the image. Otherwise it only does single-color transparency such as what GIFs use.
-
You probably don't have to specify it if your just using standard port 80. I'm guessing that is just an example that shows how to set it in case your using an alternate port.
-
GoDaddy will kill scripts that use too much resources or that persist for too long. You may be running into this problem.