owzim Posted May 29, 2013 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
MatthewSchenker Posted May 29, 2013 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
arjen Posted May 29, 2013 Posted May 29, 2013 $pages->count("selector") is supposed to use less overhead and therefore more efficient.
Soma Posted May 29, 2013 Posted May 29, 2013 Oh, another owzim question $val = $pages->find("template=mytempl, sort=-counter, limit=1")->first->counter + 1; 6
owzim Posted May 29, 2013 Author 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.
Harmster Posted May 29, 2013 Posted May 29, 2013 Are you using the field as a unique value? If so I woulnd't use these methods...
owzim Posted May 29, 2013 Author 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.
SteveB Posted June 2, 2013 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.
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