-
Posts
327 -
Joined
-
Last visited
-
Days Won
3
Everything posted by objnoob
-
Confused about how __set() and __get() methods work
objnoob replied to eldan88's topic in PHP Coding Help
__get() and __set() magic methods provide a means of manipulating object properties at run-time that are not predefined members of the object's class. Edit: There are also __unset() and __isset() magic methods that can also be defined to handle unset() and isset() calls on non-predefined properties. <?php class myClass { public $aPredefinedProperty; private $arrayForNonPredefinedProperties = array(); // __set will be called when we attempt to assign a value to non predefined property. function __set($name, $value){ $this->arrayForNonPredefinedProperties[$name] = $value; } // __get will be called when we attempt to access a non predefined property function __get($name){ return (isset($this->arrayForNonPredefinedProperties[$name])) ? $this->arrayForNonPredefinedProperties[$name] : null; } } $objInstance = new myClass(); $objInstance->aPredefinedProperty = 'a value'; # __set is not called, property is predefined echo $objInstance->aPredefinedProperty; # __get is not called, property is predefined $objInstance->magicTime = 'woo. thank goodness we have __set'; # __set is called, property is not predefined echo $objInstance->magicTime . ' and __get magic methods in our class'; # __get is called, property is not predefined -
Why does this always give same result, even when table is empty?
objnoob replied to CGTroll's topic in PHP Coding Help
i stand corrected, thanks. has it always been like that? it's been awhile since i've last used mysqli extension but i can remember the manual warning about using == false to check for execution errors like it was yesterday. -
If you're going to be running a lot of data pulls based on the date, you would benefit tremendously by adding an index for the theDate column.
-
You should do this............ ALTER TABLE your_table ADD COLUMN theDate DATE default null; -- adds a column to the table that is of date datatype UPDATE your_table SET theDate = STR_TO_DATE(theUKDate); -- updates the table setting the new column to the correct date in the correct format ALTER TABLE your_table DROP COLUMN theUKDate; -- drops your column that is storing the date in the UK format which is useless Now you're storing the date properly in the standard proper format. When you need to query and wish to return the date in UK format you can use DATE_FORMAT function.... SELECT DATE_FORMAT(theDate, '%d/%m/%Y') as 'UK Date' FROM your_table;
-
Why does this always give same result, even when table is empty?
objnoob replied to CGTroll's topic in PHP Coding Help
i'm not sure what you mean. -
s/he should figure out sooner or later that CHAR(), VARCHAR() are not meant for dates. anyhow, my main intention was to offer an efficient alternative solution.
-
Why does this always give same result, even when table is empty?
objnoob replied to CGTroll's topic in PHP Coding Help
simple. $sql = "UPDATE .... "; $cnt = mysqli->query($sql); if( ! $cnt ) { # false positive when 0 records are affected } -
SELECT ..... WHERE DATE_ADD(dateColumn, INTERVAL 30 DAY) = CURDATE(); or you can just match................... dateColumn to 30 days ago. which is better for those indices. SELECT .... WHERE dateColumn = DATE_SUB(CURDATE(), INTERVAL 30 DAY);
-
google is my only friend... http://stackoverflow.com/questions/14902554/issue-with-php-function-the-fastcgi-process-exceeded-configured-activity-timeo
-
Why does this always give same result, even when table is empty?
objnoob replied to CGTroll's topic in PHP Coding Help
Make a habit of checking for mysqli_query errors using the === operator.. $rs = mysqli->query($sql); if($rs === false) { /* error */ } -
i wouldn't use strtotime to validate a supported date/time format. I would first validate the date/time format is supported, then use strtotime() to handle the conversion. if i needed to allow for custom date/time formats not supported by strtotime(), I would validate custom format is valid, convert to a strtotime() supported date/time format, then use strtotime() to handle conversion. except, i really wouldn't. i'd parse out the date/time parts and use mktime().
-
That's not a PHP array. That looks like JSON, and it also appears to be more than 1 object which is invalid if evaluated as a single entity. Ensure that the tracking objects be contained in a single javascript array/object... [ {"tracking":[{"waybill":"TCG3267158","booking_no":"94887"}]} , {"tracking":[{"waybill":"TCG3240376","booking_no":"94870"}]} ] Use PHP's buildin json_decode() function to convert the JSON string to a PHP array. Now as a PHP array, you can loop through the tracking elements.... $javascript_array = '[ {"tracking":[{"waybill":"TCG3267158","booking_no":"94887"}]},{"tracking":[{"waybill":"TCG3240376","booking_no":"94870"}]}]'; $tracking_list = json_decode($javascript_array); foreach($tracking_list as $track) { # SANITIZE TRACKING DATA HERE $sql = "UPDATE waybill SET waybill = '{$track['tracking']['waybill']}' WHERE booking_no = '{$track['tracking']['booking_no']}' AND waybill IS NULL"; $update=mysql_query($sql) or die(mysql_error()); } Be sure to sanitize the values going into the database before using them in your UPDATE statement.
-
http://en.wikipedia.org/wiki/Loopback way easier.
-
I would choose a single naming convention for my class files. Then I can....... function __autoload($className) { include $path_to_class_directory . '/' . $className . '.class.php'; } Anyways, You can (if you insist) ....... function __autoload($className) { $extensions = array(".php", ".class.php", ".inc"); $paths = explode(PATH_SEPARATOR, get_include_path()); # ######################################### // ADD CLASSES DIR TO THE $paths ARRAY # ######################################### $paths[] = '/documentRoot/php/classes'; $className = str_replace("_" , DIRECTORY_SEPARATOR, $className); foreach ($paths as $path) { $filename = $path . DIRECTORY_SEPARATOR . $className; foreach ($extensions as $ext) { if (is_readable($filename . $ext)) { require_once $filename . $ext; break; } } } }
-
To test email locally, you need to locally install a mail server on your machine. You can use Apache's James project for this. http://james.apache.org/server/
-
Need help displaying variables through functions
objnoob replied to eldan88's topic in PHP Coding Help
You have a few options... You can work with global variables (not recommended), you can accept function parameters as references instead of values, or you can return an array of values. Here is how to work with references.... <?php // my variables that will be referenced $variable1 = 'a string'; $var2 = 13; // a number $var3 = new someObject(); // my function that will accept parameters by reference and not by value function myFunctionUsesReferences(&$param1, &$param2, &$param3){ $param1 = 'a new string'; $param2 = 14; $param3->setId(45); } // the arguments $variable1, $var2, $var3 are passed to the function by reference and not by value myFunctionUsesReferences($variable1, $var2, $var3); -
Somethings you'll need to keep in mind... to randomize results while enforcing results requires you to store the 'randomizing' procedure (not truly random) from which you can offset or to at least store the previously presented results to exclude the next time around . Generally speaking, pagination is not random. If the filtering specifications or sort change, you can no longer guarantee what has or hasn't been displayed to the user. We must select records using the same filter and sort procedures. When the filter(s) and sort have been established, we can introduce a limit and offset to control portions.
-
Passing an argument as opening and closing tag
objnoob replied to eldan88's topic in PHP Coding Help
when you define a function, your own function, aka a user defined function, you declare any parameters (data) your function needs in order to execute and perform its duty. the parameter names you assign in your function's declaration can be used throughout your function. function myFunction($parameter_1, $parameter_2){ echo 'can use $parameter_1 once: ' . $parameter_1; echo '<br />can use $parameter_1 twice: ' . $parameter_1; echo '<br />can even change the value of $parameter_1...'; $parameter_1 = 'I\'ve changed'; echo '<br />my new value is....' . $parameter_1; } the user defined function above expects 2 paramters, so I must provide 2 arguments when calling function... $argument_1 = 'Hello'; $argument_2 = 'goodbye'; myFunction($argument_1, $argument_2); So..... if i want to create a function that uses requires a tag name, I declare the function with a parameter to accept the tag name. function createNode($nodeName, $children){ if($children){ echo "<{$nodeName}>"; foreach($children as $child) createNode($child['nodeName'], $child['children']); echo "</{$nodeName}>"; }else{ echo "<{$nodeName} />"; // no children; self closing tag } } in my user defined function above for creating nodes, i use the node name parameter 3 times .... if the node has children it is used to create the open and close tags, otherwise it is used to create a self closing tag ( no children ) -
Display all databases in dropdown menu.
objnoob replied to prathameshkakade's topic in PHP Coding Help
The result set (records returned), generated by your query "SHOW DATABASES", has already been looped through to display the databases. while($record = mysqli_fetch_assoc($result_set)) { echo $record['Database']."<br />"; } You now need to explicitly set the pointer to the first record (row) of the result set to loop through it again to create the select box. mysqli_data_seek($result_set,0); // set pointer to first record because already at end of records // now can loop through records (result set) again because we set to first record while($record = mysqli_fetch_assoc($result_set)) { echo "<option>{$record['Database']}</option>"; } As Josh pointed out, after fetching all of the records to display the databases, the function mysqli_data_seek() will be used to set the internal pointer of the result set to the first record. This will allow you to loop through the result set again starting from the first record, or any record for that matter specified by mysqli_data_seek() s offset parameter, to create the select box.