PHP - Moved: Any Optimization Wizards Around?
This topic has been moved to PHP Freelancing.
http://www.phpfreaks.com/forums/index.php?topic=346599.0 Similar Tutorialsi have a random no. of directories and files being created using the mkdir and touch functions and the newly created files are copying a pre-made template.. the links of the pages which were being created had %20 signs between the spaces.. i wanted to change them with "-" soo i used the following code.. but it gives errors.. as invalid arguments in mkdir and touch and as well as copy function can anyone help? here is the code while ($row = mysql_fetch_assoc($result1) ) { $hosting = explode(',', $row['host']); $linking = explode(',', $row['links']); $parts = explode(',', $row['link_no']); $no_host = count($hosting); //miniusing the no of loops to make it accurate $count_host = count($hosting); $count_links = count($parts); $current_k = 0; $sum = 0; $parts_href = ''; $new = null; $updated_part = null; for($i=0; $i<($count_host-1); $i++ ) { $j = $i+1; $hos = str_replace(":","",$hosting[$i]); $path = "$base_folder/{$hos}"; echo "<br />{$hos}"; echo "<br />{$path}"; mkdir($path); for($k=$current_k; $k<($count_links); $k++) { for($f=0; $f<$parts[$current_k]; $f++ ) { $for_touch = "$base_folder/{$hos}/".$linking[$sum].".php"; $for_touch = str_replace(" ","-",$for_touch); touch("$for_touch"); $new_seo = "{$part_href}/{$hos}/".$linking[$sum].".php"; $new_seo = str_replace(" ","-",$new_seo); $new = "$new_seo"; $updated_part = $updated_part.$new; $updated_part = $updated_part.","; //copy function $old_file = "$for_touch"; $new_file = "{$part_page}/part".$sum.".php"; $new_file = str_replace(" ","-",$new_file); $new_file = "$new_file"; copy($new_file,$old_file); $sum++; } $query_href = 'update movies set href_parts = \''.$updated_part.'\''; mysql_query($query_href) or die('COULD NOT EXECUTE THE QUERY FOR PARTS HREF'); echo "<br />{$updated_part}"; $sum = $sum; $current_k = $current_k+1; break; } } PLEASE HELP ME! Hi all. I was looking for some tips on code optimization and came across a few resources. However, I know nothing as of yet about code optimization so I'm not sure how legitimate these resources are. Some confirmation would be very helpful along with any personal tips you may have. Thanks in advance! http://www.wmtips.com/php/tips-optimizing-php-code.htm http://www.chazzuka.com/blog/?p=58(some redundancy from the first link is included here) Some Questions I Thought Up (Warning: may be ridiculous questions): 1) Does excess white space (returns, space, tabs, etc.) in files affect performance for a large scale application (For an application that's ~500 files)? 2) Does directory structure affect performance (e.g. 3 directories deep versus 10 directories deep)? Possibly more to come I am trying to optimize my website for speed as much as possible. However it is heavily database driven. Are there any ways to speed up each page request? Also I am closing each MySql connection after every page load. Here is my database class, is that a good idea? <?php //For changes, see: http://www.php.net/manual/en/mysqli.connect.php class Database{ var $mysqli, $result, $q, $affectedRows; function __construct($host, $user, $pass, $db){ $this->connect($host, $user, $pass, $db); } function connect($host, $user, $pass, $db){ $this->mysqli = new MySQLi($host, $user, $pass, $db); if(mysqli_connect_error()){ //Add Line to error handling system here... echo "Internal Site Error - Cannot Continue!"; exit; } } function clean(){ $str = $this->q; $str = @trim($str); if(get_magic_quotes_gpc()){ $str = stripslashes($str); } $this->q = mysqli_real_escape_string($this->mysqli, $str); } function execute($query, $mode = MYSQLI_STORE_RESULT){ $this->q = $query; $this->clean(); $result = $this->mysqli->query($query, $mode); if(is_object($result) && $result instanceof MySQLi_Result){//if result is a object and is part of the mysqli class? $this->result = $result; $this->affectedRows = $this->result->num_rows; }else $this->affectedRows = $this->mysqli->affected_rows; return $this; } function fetchRow(){ return $this->result->fetch_assoc(); } function fetchAll(){ /*$row = $this->result->fetch_all($mode); See manual for the mode under mysqli_result::fetch_all //return !empty($row) ? $row : array();//if not empty return row, else return an array? */ $row = array(); while($f = $this->fetchRow()){ $row[] = $f; } return !empty($row) ? $row : array(); } function numRows(){ return $this->affectedRows; } function delete($table, $where){ return $this->execute("DELETE FROM ".$table." WHERE ".$where); } function deleteAll($table){ return $this->execute("TRUNCATE ".$table); } function update($table, $set, $where){ return $this->execute("UPDATE ".$table." SET ".$set." WHERE ".$where); } function select($table, $select = "*", $where = NULL, $cap = ""){ if(is_null($where) || empty($where)) return $this->execute("SELECT ".$select." FROM ".$table." ".$cap); else return $this->execute("SELECT ".$select." FROM ".$table." WHERE ".$where." ".$cap); } function lastId(){ return $this->mysqli->insert_id; } function resetInc($table, $inc){ $this->execute("ALTER TABLE ".$table." AUTO_INCREMENT = ".$inc); } function error(){ return @mysqli_error($this->mysqli). " <strong><font color=\"red\">QUERY</font>: ".$this->q."</strong>"; } function close(){ @mysqli_close($this->mysqli); } function __destruct(){ $this->close(); } } $db = new Database(DB_HOST, DB_USER, DB_PASS, DB_DB); ?> I am using a PHP include file for my site, which includes validation functions used throughout out the site. Like string type, alphanumeric values, etc. Its completely customized for the types of user inputs the site takes. The problem is, each page uses only about 2-3 functions from the whole list of 16. One of the pages gives a memory_get_usage() of 748KB. If I remove the Inluded file, it goes down by 200KB. While the include file is of 4KB and the test page is 2KB. I am looking for a method to optimize the memory allocation. I dont want to write the code separately for each file.Is there a method to optimize for uncalled functions? Thanks in Advance. Thank you for looking into this! I need to use result from mysql query twice or more on a page Should I do this: $sql = mysql_query("SQL"); while ($row = mysql_fetch_assoc($sql)) CODE ... while ($row = mysql_fetch_assoc($sql)) CODE2 OR should I keep result of a query in an array and use FOREACH instead? My concern is performance. Can someone please double check my search query to make sure it is the minimum and most efficient coding. My goal is an exact match search of field1 from my database table and list field1, field2, field3, field4 and field5 if the part number is in the database or no results if it is not. The query works as is but I have a feeling it could be done more efficiently or professionally and minimum work for my poor little server. Thanks in advance. Code: [Select] <?php //connect to the database include("./databaseconnect.php"); //get query $q=$_GET['q']; //convert query to uppercase & remove all spaces and special charactars $q=strtoupper(preg_replace("/[^A-Za-z0-9]/","",$q)); //if blank query if ($q == "") { echo "You did not enter a search term"; } else { //exact match only query $query = "SELECT * FROM ".$dbtable." WHERE field1 like \"$q\""; $result = mysql_query($query); //if query returns no results if(mysql_num_rows($result)==0) { echo "<span class=\"noresults\">There were no results for your search</span>"; //display database results } else { while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<span class=\'results\'>SEARCH RESULTS</span><br /> <span class=\'list\'>Part Number: ".$row['field1']."<br /> Manufacturer: ".$row['field2']."<br /> Cost per unit: ".$row['field3']."<br /> Warehouse Location: ".$row['field4']."<br /> Quantity Available: ".$row['field5']."<br /> Notes: ".$row['field6']."<br /> </span>"; } } } ?> Hi I am presently writing a code which does the following steps given a site 1) get some keywords (written a function for it) 2) search google for these keywords (I have used curl and getURL functions for these) 3) perform keyword search in the first page of googles results. Presently I have used curl and curl_multi_getcontent but when i run the code it consumes 100% of my server. My server will not respond in this case if someone else pings. I have been trying to do pcntl_fork, but could you let me know how to optimize this code by threading and forking. I am a newbe in PHP please let me know the structure of how to invoke multiple threads and process the same. If you have someother suggestions please let me know that too. Hi Guys and Gals, I run a simple fansite for some of my fellow MTG Tactics players at www.onlinegamekey.com I recently made a Booster Page value page he http://www.onlinegamekey.com/booster-packs.php And it seems the page loads really slow, but I kind of understand that since its doing a lot of math and dealing with a lot of data. I am wondering if there is a way to possibly optimize the page and querys in such a way that it doesn't take so long to load. I'm basically using 4 queries to get the value of each pack and 1 function to average the query data. And I add them up as I go. This is my function to find average values Code: [Select] ///AVERAGE FUNCTION function calculate_average($arr) { $count = count($arr); //total numbers in array foreach ($arr as $value) { $total = $total + $value; // total value of array numbers } $average = ($total/$count); // get average value return $average; } These are the queries to get the Average Values Code: [Select] <?php ///SETTING THE DAY $today = date("Y/m/d"); ///ALTERING DAY QUERY TIME (20 Days of Data) $minusday = mktime(0,0,0,date("m"),date("d")-20,date("Y")); ///SETTING THE DAY FOR THE QUERIES $queryday = date("Y/m/d",$minusday); ///DATA SET FOR BOOSTER PACK 1 //GET THE MYTHIC AVERAGE VALUE $sqlMythic=mysql_query("SELECT AVG( ab.Price_Per ) AS Aaverage FROM auctions AS ab INNER JOIN cardlist AS c ON c.card_name = ab.Card_Name WHERE ab.Day >= '$queryday' AND c.rarity = 'Mythic Rare' AND c.set = '1' GROUP BY c.card_name, ab.Card_Name") or die; while ($row=mysql_fetch_array($sqlMythic)) { $MythicValues[]=$row['Aaverage']; } ///MYTHIC VALUE IS 10% OF AVERAGE VALUE $Mythic = calculate_average($MythicValues) * .1; //GET THE RARE AVERAGE VALUE $sqlRare=mysql_query("SELECT AVG( ab.Price_Per ) AS Aaverage FROM auctions AS ab INNER JOIN cardlist AS c ON c.card_name = ab.Card_Name WHERE ab.Day >= '$queryday' AND c.rarity = 'Rare' AND c.set='1' GROUP BY c.card_name, ab.Card_Name") or die; while ($row=mysql_fetch_array($sqlRare)) { $RareValues[]=$row['Aaverage']; } ///RARE VALUE IS 90% OF AVERAGE RARE VALUE $Rare = calculate_average($RareValues) *.9; ///TOTAL CONTRIBUTION TO PACK 100% IS 10% MYTHIC + 90% RARE $raremythic= $Mythic + $Rare; ///GET THE UNCOMMON AVERAGE VALUE $sqlUncommon=mysql_query("SELECT AVG( ab.Price_Per ) AS Aaverage FROM auctions AS ab INNER JOIN cardlist AS c ON c.card_name = ab.Card_Name WHERE ab.Day >= '$queryday' AND c.rarity = 'Uncommon' AND c.set='1' GROUP BY c.card_name, ab.Card_Name") or die; while ($row=mysql_fetch_array($sqlUncommon)) { $ucValues[]=$row['Aaverage']; } ///UNCOMMON VALUE IS VALUE * 3 $Uncommon = calculate_average($ucValues); $urm= ($Uncommon * 3) + $raremythic; ///GET THE COMMON VALUE $sqlCommon=mysql_query("SELECT AVG( ab.Price_Per ) AS Aaverage FROM auctions AS ab INNER JOIN cardlist AS c ON c.card_name = ab.Card_Name WHERE ab.Day >= '$queryday' AND c.rarity = 'Common' AND c.set='1' GROUP BY c.card_name, ab.Card_Name") or die; while ($row=mysql_fetch_array($sqlCommon)) { $cValues[]=$row['Aaverage']; } ///COMMON VALUE IS VALUE * 10 $common = calculate_average($cValues); //TOTAL SET 1 VALUE IS ALL VALUES ADDED UP $curm= ($common * 10) + $urm; ?> But I'm not real sure where the strain is coming from. The page seems to load really slow, and I'm not sure if I should just try and write all these queries as a separate function then have it write the data to a separate table, or if there is another way to optimize this code. I'm still have a lot to learn so I would appreciate any thoughts or ideas you folks can offer me. Many thanks This is a two parter... mostly a discussion as I am currently not employing the purpose of these "things"
I am creating an autoparsing webapp that has unlimited use... whatever a person can think of
It accesses camera, microphone, gyro/accelerometer, flash etc... mostly it takes in data and does something to it according to the parsing tool
I'm not saying this is new, in fact I spent a while using Touch Develop which is a scripting "thing" by Microsoft, the problem was lag
That is another thing that concerns me, without web access the web-app is useless right? So I'm wondering if it is possible to copy your current setup and either translate it to the mobile languages like Java, C#/XML, Objective C or somehow a platform independent alternative
Anyway...
I'm not sure if I can access front end code, like <div class="whatever"> safely using injection
Well injection you just bind parameters but what if the incoming string is literally malicious ?
Also as far as autoparsing optimization goes, what I mean by that is
I intended to create a character by character comparison, obviously or at least to me, starting with easier stuff first like
for example a link is entered
http://www.something.com
then the autoparser compares each character one at a time from left to right
|1|2|3|4|5|6|7|8|9|10|
|h|t|t|p|:|/|/|w|w|w|...etc...
But I would check for existing formats starting with the shortest first and also checking from right and left, eg. .mp4 is obvious as a file type
I'll have more once I actually know what I need just looking to discuss I suppose... sorry if that is not appropriate feel free to delete this thread
In the future the users who have modified their personal accounts would benefit from an "AI" thing that is specific to their personalities based on what they have enabled
Edited by greenace92, 28 December 2014 - 09:37 AM. This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=342919.0 This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=313579.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=343318.0 This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=315910.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=316254.0 This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=319767.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=342987.0 This topic has been moved to PHP Freelancing. http://www.phpfreaks.com/forums/index.php?topic=349322.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=309960.0 This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=356760.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=328753.0 |