Jump to content

ignace

Moderators
  • Posts

    6,457
  • Joined

  • Last visited

  • Days Won

    26

Everything posted by ignace

  1. <script type="text/javascript"/> <-- remove the /
  2. It might help posting some actual code
  3. ignace

    Using &

    It instructs PHP not Apache
  4. @salathe like you said GlobIterator is a glob wrapper. Thus if it doesn't exist you can create it yourself: if (!class_exists('GlobIterator')) { class GlobIterator extends FilesystemIterator implements Iterator, Traversable, SeekableIterator, Countable { //.. } } Assuming FilesystemIterator does exists the manual does not point out when which component was added to the SPL.
  5. Well that's the problem. I don't have any login for their current host as the hosting company was also the one responsible for the creation of their website.
  6. Hi, A new client with an existing website has asked me to move his website. So I contacted the hosting company and send them the server's DNS of the new host however apparently they wanna play hardball. Any advice?
  7. function get_directory_contents($directory, $sorting_order = 0, $context = null) { $files = array(); $directory = rtrim($directory, '\/') . DIRECTORY_SEPARATOR; if ($scan =@ scandir($directory, $sorting_order, $context)) { foreach ($scan as $file) { $files[] = $directory . $file; } } return $files; } function print_file_contents($file) { print file_get_contents($file); } array_map('print_file_contents', get_directory_contents('.'));
  8. It isn't actually so hard to implement. You have one class for each decision (Red, Blue, Yellow). This works only if the logic behind each decision remains the same if the logic differs then my solution can become your problem and I would advise not to use it. Because the logic for each decision handler varied in only a few parts of it's code have I created a general method in the base class protected function _handleDecision($expected, $decision) { if ($expected !== $decision) { null === $this->_successor ? $this->_gameOver() : $this->_successor->handleDecision($decision); return false; } return true; } By adding a new variable $expected I could now pass my color (eg 'Red') and by returning a boolean value (wether or not my class could handle it) If it returned true the current class would be the one responsible and thus it could execute it's logic. if ($this->_handleDecision('Red', $decision)) { $this->_handleRed(); } Ofcourse was this not my original code but through code refactoring were I able to create a nice clean solution. My code is not without flaw though but I'll blame that to the late hour protected function _handleRed() { echo 'You picked red.'; } protected function _handleBlue() { echo 'You picked blue.'; } protected function _handleOrange() { echo 'You picked orange.'; } protected function _handleYellow() { echo 'You picked yellow.'; } This code should have been in his respective class instead of the base class.
  9. IMO you would be best suited using a Chain of Responsibility as a decision may return multiple times in an application while the logic may remain the same. I also added some extra's like _getSession() and _getBreadCrumbs() for usage in your application. <?php abstract class DecisionTree { protected $_successor = null; protected $_session = null; public function __construct(DecisionTree $dt = null) { $this->_successor = $dt; } protected function _getSession() { if (null === $this->_session) { $this->_session = new ArrayObject(&$_SESSION, ArrayObject::ARRAY_AS_PROPS); // Works in PHP<5.3 } return $this->_session; } protected function _getBreadCrumbs() { $session = $this->_getSession(); if (!$session->offsetExists('breadCrumbs')) { $session->breadCrumbs = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS); } return $session->breadCrumbs; } protected function _gameOver() { echo 'You lost.'; } protected function _handleRed() { echo 'You picked red.'; } protected function _handleBlue() { echo 'You picked blue.'; } protected function _handleOrange() { echo 'You picked orange.'; } protected function _handleYellow() { echo 'You picked yellow.'; } protected function _handleDecision($expected, $decision) { if ($expected !== $decision) { null === $this->_successor ? $this->_gameOver() : $this->_successor->handleDecision($decision); return false; } return true; } abstract public function handleDecision($decision); } /////////////////////////////////////////////////////////////////////////////// // CONCRETE CLASSES /////////////////////////////////////////////////////////////////////////////// class Red extends DecisionTree { public function handleDecision($decision) { if ($this->_handleDecision('Red', $decision)) { $this->_handleRed(); } } } class Blue extends DecisionTree { public function handleDecision($decision) { if ($this->_handleDecision('Blue', $decision)) { $this->_handleBlue(); } } } class Orange extends DecisionTree { public function handleDecision($decision) { if ($this->_handleDecision('Orange', $decision)) { $this->_handleOrange(); } } } class Yellow extends DecisionTree { public function handleDecision($decision) { if ($this->_handleDecision('Yellow', $decision)) { $this->_handleYellow(); } } } /////////////////////////////////////////////////////////////////////////////// // LOGIC /////////////////////////////////////////////////////////////////////////////// $l = new Red(new Blue(new Yellow(new Orange()))); print 'Act One: Orange<br />'; $l->handleDecision('Orange'); print '<br />'; print 'Act Two: Blue</br />'; $l->handleDecision('Blue'); print '<br />'; print 'Act Three: Lost!<br />'; $l->handleDecision('Purple'); print '<br />'; Outputs: Act Orange You picked orange. Act Blue You picked blue. Act Losing You lost. It always issues a Deprecated warning if you are using PHP 5.3.0 due to the &$_SESSION
  10. It's not possible using PHP. You can use JavaScript for it though: <a href="#" onclick="javascript:showForm();">Show Form</a> <form id="someForm" style="display: none;" action="" method="post"> <label for="inputField">Label Name</label> <input type="text" name="inputField" id="inputField" /> </form> <script type="text/javascript"> function showForm() { var someForm = document.getElementById('someForm'); if ("block" == someForm.style.display) { someForm.style.display = "none"; } else { someForm.style.display = "block"; } } </script>
  11. function my_echo($s) { echo $s, '<br />'; } array_map('my_echo', scandir('.'));
  12. if (isValidImage(..)) { ... } else { echo 'wrong image'; }
  13. Don't execute that query in you application it's supposed to execute at a 5 min interval using cron
  14. UPDATE users SET from = to, to = now() WHERE ID_user = $ID_User
  15. typo: while($row = mysql_fetch_assoc()) should be: while($row = mysql_fetch_assoc($result)) use trigger_error() instead of die()
  16. I disagree it should be: require_once('Chargeable.php'); class ShopProduct implements Chargeable { public function getPrice() { return 23; } } Otherwise he will have to add include_once('interface.php'); everytime he wants to use the ShopProduct class
  17. @Gomesh usage of global variables is wrong much like the function die() they are considered to be bad practice.
  18. $Building = new Building(); $Building->Upgrade(); $upgrade = $Building->get_upgrade();
  19. In PHP there is no location.href. location.href is from JavaScript which is a client-side language while PHP is a server-side language it's two different worlds. It's like the Earth and Mars
  20. @ym_chaitu function addInterface($interface, $description = 'No Description', $input = 'No Input Service-Policy', $output = 'No Output Service-Policy') { return "$interface, $description, $input, $output"; } function appendCsvLine($line, &$lines) { $lines = $lines . $line . PHP_EOL; } function appendNoneEmpty(&$args, &$lines) { if (sizeof($args)) { $line = call_user_func_array('addInterface', $args); appendCsvLine($line, $lines); $args = array(); } } function getIdentifier($line) { return current(str_word_count($line, 1)); } $csvLines = ''; $args = array(); $lines = file('string2csv.txt'); foreach ($lines as $line) { $line = trim($line, " \r\n\t"); $identifier = getIdentifier($line); switch (strtolower($identifier)) { case 'interface': appendNoneEmpty($args, $csvLines); $args[] = $line; break; case 'description': case 'service-policy': $args[] = $line; break; } } appendNoneEmpty($args, $csvLines); echo $csvLines; Solved it! Now it doesn't matter if you give Interface, interface, iNTERFACE or iNtERfACe
  21. What do you want to achieve with this code? Get the closing and opening hours of a store? +-----------+--------------------------------------------+ | day | opening_hours | +-----------+--------------------------------------------+ | Monday | 8.30 till 11.30 and 13.00 till 17.00 | | Tuesday | 8.30 till 11.30 and 13.00 till 17.00 | | Wednesday | 8.30 till 11.30 and 13.00 till 17.00 | | Thursday | 8.30 till 11.30 and 13.00 till 17.00 | | Friday | 8.30 till 11.30 and 13.00 till 17.00 | | Saturday | 8.30 till 11.30 | +-----------+--------------------------------------------+ Keep it simple let the user define the closing and opening hours.
  22. I had that same problem and added $args[] = $line to the case 'interface' weird your know getting that what are your specs?
  23. @ym_chaitu You do have one point: So I re-wrote my code to: function addInterface($interface, $description = 'No Description', $input = 'No Input Service-Policy', $output = 'No Output Service-Policy') { return "$interface, $description, $input, $output"; } function appendCsvLine($line, &$lines) { $lines = $lines . $line . PHP_EOL; } function appendNoneEmpty(&$args, &$lines) { if (sizeof($args)) { $line = call_user_func_array('addInterface', $args); appendCsvLine($line, $lines); $args = array(); } } function getIdentifier($line) { return current(str_word_count($line, 1)); } $csvLines = ''; $args = array(); $lines = file('string2csv.txt'); foreach ($lines as $line) { $line = trim($line, " \r\n\t"); $identifier = getIdentifier($line); switch ($identifier) { case 'interface': appendNoneEmpty($args, $csvLines); $args[] = $line; break; case 'description': case 'service-policy': $args[] = $line; break; } } appendNoneEmpty($args, $csvLines); echo $csvLines; And it now returns: interface Vlan1, description Management LAN, No Input Service-Policy, No Output Service-Policy interface Vlan41, description Hamiltons, No Input Service-Policy, No Output Service-Policy interface Vlan65, description Somelan, service-policy input 2Mbps, service-policy output 2Mbps interface Vlan66, description HSomelan, No Input Service-Policy, No Output Service-Policy interface Vlan70, description Somelan, No Input Service-Policy, No Output Service-Policy interface Vlan71, description Somelan, service-policy input 4Mbps, service-policy output 4Mbps interface Vlan72, description Somelan, service-policy input 4Mbps, service-policy output 4Mbps interface Vlan73, description Somelan, service-policy input 6Mbps, service-policy output 6Mbps interface Vlan74, description Somelan, No Input Service-Policy, No Output Service-Policy interface Vlan75, description Somelan, service-policy input 20Mbps, service-policy output 20Mbps interface Vlan76, description Somelan, No Input Service-Policy, No Output Service-Policy interface Vlan77, description Somelan, service-policy input 2Mbps, service-policy output 2Mbps
  24. @ ym_chaitu I tried the input from the OP which is: interface Vlan1 description Management LAN interface Vlan41 description Hamiltons interface Vlan65 description Somelan service-policy input 2Mbps service-policy output 2Mbps interface Vlan66 description HSomelan interface Vlan70 description Somelan interface Vlan71 description Somelan service-policy input 4Mbps service-policy output 4Mbps interface Vlan72 description Somelan service-policy input 4Mbps service-policy output 4Mbps interface Vlan73 description Somelan service-policy input 6Mbps service-policy output 6Mbps interface Vlan74 description Somelan interface Vlan75 description Somelan service-policy input 20Mbps service-policy output 20Mbps interface Vlan76 description Somelan interface Vlan77 description Somelan service-policy input 2Mbps service-policy output 2Mbps And my script returned: interface Vlan1,description Management LAN interface Vlan41,description Hamiltons interface Vlan65,description Somelan,service-policy input 2Mbps,service-policy output 2Mbps interface Vlan66,description HSomelan interface Vlan70,description Somelan interface Vlan71,description Somelan,service-policy input 4Mbps,service-policy output 4Mbps interface Vlan72,description Somelan,service-policy input 4Mbps,service-policy output 4Mbps interface Vlan73,description Somelan,service-policy input 6Mbps,service-policy output 6Mbps interface Vlan74,description Somelan interface Vlan75,description Somelan,service-policy input 20Mbps,service-policy output 20Mbps interface Vlan76,description Somelan interface Vlan77,description Somelan,service-policy input 2Mbps,service-policy output 2Mbps Which is as far as I can tell what the OP asked for.
  25. Here I tried this one and it worked for me: $csvLines = ''; $csvLine = ''; $lines = file('string2csv.txt'); foreach ($lines as $line) { $line = trim($line, "\r\n\t"); $words = str_word_count($line, 1 /* mode 1: returns an array containing all the words found inside the string */); $identifier = $words[0]; switch ($identifier) { case 'interface': $csvLines .= $csvLine . PHP_EOL; $csvLine = $line; break; case 'description': case 'service-policy': $csvLine .= ',' . $line; break; } } $csvLines .= $csvLine; echo $csvLines; Returns: interface Vlan207, description Some customer interface Vlan214, description Some customer, service-policy input 4Mbps, service-policy output 4Mbps interface Vlan218 interface Vlan219 interface Vlan221, description Some customer interface Vlan223, description Some customer interface Vlan236, description Some customer, service-policy input 4Mbps, service-policy output 4Mbps That being said I favor cags code.
×
×
  • 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.