Jump to content

ToonMariner

Members
  • Posts

    3,342
  • Joined

  • Last visited

Everything posted by ToonMariner

  1. Hi guys n gals, I have a requirement to generate 250,000 unique codes... These codes are for a tracking service and will be stored in the database (table field has a unique index). Now I am working on the assumption this must work on the command line as the script will take a long time to complete... here is the salient pat of my code... <?php ... function appendToCode( &$val , $key , $append ) { $val = $val . $append; } $chars = "ABCDEFGHJKLMNPQRSTUVWXYZ"; $charend = strlen ( $chars ); $date = date ( 'md' ); $month = $date[0].$date[1]; $year = $date[2].$date[3]; $datecode = $chars[$month] . $chars[$year[0]] . $chars[$year[1]]; $codes = array(); $no_codes = isset( $_GET['no_codes'] ) ? $_GET['no_codes'] : 250000; $codecount = 0; do { for ( $j = $codecount ; $j < $no_codes ; $j++ ) { $trackcode = NULL; for ( $i = 0 ; $i < 7 ; $i++ ) { $trackcode .= $chars[rand( 0 , $charend - 1 )]; echo ( time() - $start ) . PHP_EOL; } $codes[] = $trackcode . $date; } //$t = count($codes); //echo 'Codes Generated: ' . $t . PHP_EOL; //echo 'Last Code: ' . $codes[--$t] . PHP_EOL; $codes = array_unique( $codes ); $codecount = count ( $codes ); //echo 'Unique Codes: ' . $codecount . PHP_EOL; // the following statement prevents infinite loop in dev mode... if ( (time() - $start) > 150 ) { break; } } while ( $codecount < ($no_codes) - 1 ); ... The last characters of the code relate to the date - as these codes will only be developed every now and then - not a frequent occurrence and will allow a check on the database to see if codes ending in that 3 character string exist... What I am asking is, is there a more efficient method of performing this operation. Any tips would be most welcome... Cheers peeps Toon xxx
  2. html_entity_decode is your 'friend' in this instance - notwithstanding any detrimental effects
  3. new DB is that the pear DB class? which database - mysql, mssql db2 oracle? fact is your query fails that is why you have no results to loop over...
  4. the clue IS in the error - 'on a non-object'... the query returns an object that can be traversed - as the query failed no object was returned hance when you try to do anything with said object you get an error
  5. means the query didn't return anything. Either print the error or echo out the query and copy it to your db admin app and run the query - it will tell you what is wrong.
  6. That's why I suggested check boxes NOT radio buttons. If you go back and look at the code I posted - it allows you to select as many options as you wish and you can alter the string glue on implode to ' AND ' if you only want results where the search term appears in all the fields specified. Have a look at bitwise operators and you will see how those if statements allow all search options to be included. Try the code and echo out the query - you should be pleasantly surprised.
  7. you may benefit from replacing the select box with radio buttons (you woul donly need 2 for your current functionality - adding a third option would mean 6 options in a select drop down but still just 3 for check boxes...) trick here is to use bitwise operators (using three options to demonstrate)... search options <?php define ('SEARCH_SHOE' , 1); define ('SEARCH_BAG' , 2); define ('SEARCH_HAT' , 4); .... ?> markup <form method="get" ...> <fieldset> <legend>Directory Search</legend> <label for="searchterm">Find:</label> <input type="text" name="searchterm" /> <div class="search-options"> <label><input type="checkbox" name="searchoption[]" value="<?php echo SEARCH_SHOE; ?>" /> Shoes</label> <label><input type="checkbox" name="searchoption[]" value="<?php echo SEARCH_BAG; ?>" /> Bags</label> <label><input type="checkbox" name="searchoption[]" value="<?php echo SEARCH_HAT; ?>" /> Hats</label> </div> <input type="submit" name="submitbttn" value="Go" /> </fieldset> </form> search code <?php function addTerm (&$term,$key) { $term .= "LIKE '%" . $_GET['searchterm'] . "%'"; } $search = array_sum ( $_GET['searchoption'] ); $fields = array(); if ( $seach & SEARCH_SHOE ) { $fields[] = '`Sbrand`'; } if ($search & SEARCH_BAG ) { $fields[] = '`Bbrand`'; } if ( $search & SEARCH_HAT ) { $fields[] = '`Hbrand`'; } array_walk($field,'addTerm'); $qry = "SELECT * FROM `directory_table` WHERE " . implode(' OR ' , $field); echo $qry; // for testing - you can copy and paste to check query runs OK. $result = mysql_query($qry); ?> Hopefully that code will be a little more robust and maintainable for you...
  8. problem was one of the bindValues not returning an error - sorted now cheers mate
  9. So is there something special I have to do with PDO to make what I am trying to achieve work?
  10. Yes it is available. The second insert fails however due to the fact that the lastinsert_id (which is correctly returned and assigned to the column having a foreign key reference to the first table) won't actually exist until the transactions are committed. So when the second insert runs - it looks to see if that value exists in the field the foregin key references. As it doesn't actually exist yet the query cannot execute successfully.
  11. Yeah I bind the last insert id to the user_id in the second query BUT as the first insert has not been committed the second query fails on foreign key constrains - that user_id doesn't actually exist yet...
  12. I don't think there is a difference in tems of efficiency - this is more to do with scope of methods/members
  13. Think I may be taking the wrong approach to this but here goes... I have a PDO transaction - first query insert a new user into the database - the second query inserts profile data about that user into a profile table. the user_id on the location table is a foreign key to the userID on the user table. here's the code <?php $userQry = " INSERT INTO `users` ( `username`, `local_email`, `domain_email`, `password` ) VALUES ( :username, :localemail, :domain, SHA1(:password) ) "; $addressQry = " INSERT INTO `user_location` ( `user_id`, `city_id`, `user_address` ) VALUES ( :userid, :city, :address ) "; $this->beginTransaction(); $user = $this->prepare ( $userQry ); $user->bindValue ( ':username' , $_POST['regusername'] , PDO::PARAM_STR ); $user->bindValue ( ':localemail' , $email[0] , PDO::PARAM_STR ); $user->bindValue ( ':domain' , $domain_id , PDO::PARAM_INT); $user->bindValue ( ':password' , PW_SALT . $_POST['reguserpassword'] , PDO::PARAM_STR ); $insertUser = $user->execute(); $locale = $this->prepare ( $addressQry ); $locale->bindValue ( ':userid' , $this->lastInsertId () , PDO::PARAM_INT ); $locale->bindValue ( ':city' , $_POST['regcity'] , PDO::PARAM_INT ); $locale->bindValue ( ':address' , $_POST['regaddress'] , PDO::PARAM_STR ); $insertLocale = $locale->execute(); var_dump ($locale->errorInfo () ); /*if ( $insertLocale->rowCount() > 0 ) { $this->commit (); return true; }*/ $this->rollBack (); return false; ?> now you will see that I want to check if the last query run executed - know that won't work because the query was not committed so no rows will actually be affected. The problem is the foreign key reference - as the user is not really inserted the second insert will fail as the foreign key referenced does not exist. bit stuck on how to approach this now - the foregin key constraint has to stay but I also want the transaction so I can roll back if anything goes wrong... any help much appreciated
  14. OK but if I added the json.so to the extensions that would work too?
  15. which pear /usr/bin/pear So I think that's already installed. does php have to be compiled with pear OR can I just add extension = json.so to the config file (or rather add the json.ini with that command in the php.d directory)?
  16. NEIL I LOVE YOU... I think that all went well find -name *json* reported json.reg in NOW is that --without-pear in the configuration command shown in phpinfo going to hurt? or do I just need to add the json.so to the modules in php.ini?
  17. Hey peeps. Complete linux command line noob here...... Working on clients site - they have dedicated host with 1and1 and I am trying to install the json extension because the server has php 5.1.6 and client does not want to updgrade in case it kills his other sites (assured him it won't but he is the boss). server is running centos (i think v5) pear was not installed so I ran: yum install pear that was fine and 'which pear' reveals that its in usr/bin/pear... happy with that then I tried to install json... which pecl /usr/bin/pecl pecl install json downloading json-1.2.1.tgz ... Starting to download json-1.2.1.tgz (17,780 bytes) ......done: 17,780 bytes 11 source files, building running: phpize Configuring for: PHP Api Version: 20041225 Zend Module Api No: 20050922 Zend Extension Api No: 220051025 building in /var/tmp/pear-build-root/json-1.2.1 running: /tmp/tmp50rtMu/json-1.2.1/configure checking for egrep... grep -E checking for a sed that does not truncate output... //bin/sed checking for gcc... no checking for cc... no checking for cc... no checking for cl... no configure: error: no acceptable C compiler found in $PATH See `config.log' for more details. ERROR: `/tmp/tmp50rtMu/json-1.2.1/configure' failed so that didn't go to well... when I run phpinfo() on the site the configure demand has ''--without-pear' which I reckon will hurt me... What on earth do I need to do - all the stuff I have tried i got from googling stuff and I'm now stuck. Any help will be VERY much appreciated.
  18. yeah couple of typos on copying (removed bits and bobs etc etc) found the issue... guess who's php file containing the class had an empty line at the beginning? - that's right MINE... fookin dime bar...
  19. Hi redarrow... All pretty good - got forced into working for myself back in May - quite liking it OK code class Array2XML { private $XMLArray; private $doc; public function __construct () { $this->doc = new DOMDocument(); $this->doc->strictErrorChecking = true; $this->doc->formatOutPut = true; $this->doc->ignoreWhiteSpace = false; } private function createXML ( $arr , $node = NULL , $key = NULL) { if ( is_null ( $node ) ) { $node = $this->doc->createElement('root'); $this->doc->appendChild ( $node ); } foreach ($arr as $k => $v) { if ( is_array ( $v ) ) { $item = $this->doc->createElement($k); foreach ($v as $k2 => $v2) { $this->createXML($v2 , $item , $k2); } $node->appendChild($item); } else { if ( is_string ( $k ) ) { $item = $this->doc->createElement($k); $text = $this->doc->createTextNode ($v); $item->appendChild($text); $node->appendChild( $item ); } else { $item = $this->doc->createElement($key); $text = $this->doc->createTextNode ($v); $item->appendChild($text); $node->appendChild( $item ); } } } } public function retrunAsString () { $this->createXML($this->XMLArray); return $this->doc->saveXML (); } public function setArray($XMLArray){ if ( is_array($XMLArray) && count($XMLArray) != 0 ) { $this->XMLArray = $XMLArray; } return false; } } ?> (its a work in progress!!!) here is the array serialized: a:2:{s:5:"field";s:9:"regregion";s:4:"data";a:2:{s:5:"value";a:4:{i:0;s:4:"5392";i:1;s:4:"5391";i:2;s:4:"5390";i:3;s:4:"5389";}s:7:"display";a:4:{i:0;s:7:"England";i:1;s:16:"Northern Ireland";i:2;s:8:"Scotland";i:3;s:5:"Wales";}}} just call with $obj = new ArrayToXML(); $obj->setArray($arr)' // that serialized effort... echo $obj->returnAsString(); strangely trying to view in browser issues error stating there is whitespace before xml declaration!!! Bit flumoxed on that one....
  20. Hi Guys, should be a quick 'un... Not found anything on this and looked a fair bit. Creating an xml file with DOMDocument and saving to sting using DOMDocument::saveXML(); the result is passed back to an ajax call but fails as the xml has an empty line prior to the <?xml ...?> declaration. Anyone encountered this and how to combat it? (must be a better way than just loading string into array and shifting first element.) cheers peeps.
  21. if you can see why it doesn't work you will know that you need to assign values to the array held in $_SESSION['parts'] BEFORE you start using them.
  22. list(,$node) = $sxml->xpath('/config/media/content/video'); see if that works
  23. its very simple - you have $_SESSION['parts'] = array(); this sets that session variable to an empty array. Between that line and the first attempt to access the QTY element of a non existent array there is no code to populate the parts array.
×
×
  • 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.