owzim Posted May 29, 2013 Share Posted May 29, 2013 Is there a neat an straightforward PW way of incrementing a value of a field other than getting all pages with that template an figuring out the highest value through a loop or something? In this case it should not be a regular auto increment but a number one higher than the highest of the already inserted pages. 1 Link to comment Share on other sites More sharing options...
MatthewSchenker Posted May 29, 2013 Share Posted May 29, 2013 Greetings, Interesting coincidence... I was just this morning trying to figure out a neat way to do this. In my case, I am building a site with property listings, and I want to auto-generate the listing numbers. I was thinking of just tying into the page ID. Would be interested in other ideas... Thanks, Matthew Link to comment Share on other sites More sharing options...
arjen Posted May 29, 2013 Share Posted May 29, 2013 $pages->count("selector") is supposed to use less overhead and therefore more efficient. Link to comment Share on other sites More sharing options...
Soma Posted May 29, 2013 Share Posted May 29, 2013 Oh, another owzim question $val = $pages->find("template=mytempl, sort=-counter, limit=1")->first->counter + 1; 6 Link to comment Share on other sites More sharing options...
owzim Posted May 29, 2013 Author Share Posted May 29, 2013 Oh, another owzim question $val = $pages->find("template=mytempl, sort=-counter, limit=1")->first->counter + 1; Alrite, selectors ftw, thanks Soma. Link to comment Share on other sites More sharing options...
Harmster Posted May 29, 2013 Share Posted May 29, 2013 Are you using the field as a unique value? If so I woulnd't use these methods... Link to comment Share on other sites More sharing options...
owzim Posted May 29, 2013 Author Share Posted May 29, 2013 Are you using the field as a unique value? If so I woulnd't use these methods... No, just as a value to be incremented. Link to comment Share on other sites More sharing options...
SteveB Posted June 2, 2013 Share Posted June 2, 2013 Sometimes I setup a table for counters... CREATE TABLE counters ( count int(10) unsigned NOT NULL DEFAULT '0' , name char(10) NOT NULL , PRIMARY KEY (name) ); And a function like this: function bumpCount($mysqli, $name){ if($result = $mysqli->query("UPDATE counters SET count = LAST_INSERT_ID(count+1) WHERE name = \"$name\"")){ $count = $mysqli->insert_id; if($count>0) return $count; //normal if($result = $mysqli->query("INSERT INTO counters (count, name) VALUES (1, \"$name\")")) return 1; //initialize new counter } return false;} And use it like this: $mysqli = new mysqli('host', 'user', 'password', 'database');echo 'The counter is: ' . bumpCount($mysqli, 'foo'); If anything goes wrong it returns false. New counters are setup automatically when you use a new name. First number issued is 1. The usual disclaimers about code examples apply. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now