gevans Posted November 5, 2009 Share Posted November 5, 2009 Hey guys, I'm updating a CMS to allow for re-ordering content. My first step is to add the `order` field to my db table. After that's done I want to run a query to fill in the new field. The table after adding the order field; CREATE TABLE IF NOT EXISTS `pages` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(44) NOT NULL, `url` varchar(44) NOT NULL, `parent_id` int(11) NOT NULL, `order` tinyint(1) NOT NULL, `active` varchar(1) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=89 ; For each `parent_id` I want to increment the value in `order`. So for the 6 pages with a `parent_id` of 2 I want the `order` values to run from 1 to 6. Is there a simple way to do this via a SQL without any PHP loops etc? Cheers, gevans Quote Link to comment https://forums.phpfreaks.com/topic/180444-solved-adding-an-order-field/ Share on other sites More sharing options...
jazzman247 Posted November 5, 2009 Share Posted November 5, 2009 You code block is being skipped because the table already exist. Try this... ALTER TABLE `pages` ADD `order` tinyint(1) NOT NULL; Quote Link to comment https://forums.phpfreaks.com/topic/180444-solved-adding-an-order-field/#findComment-951944 Share on other sites More sharing options...
gevans Posted November 5, 2009 Author Share Posted November 5, 2009 I already have the `order` field in the table. I'm looking to populate the new field Quote Link to comment https://forums.phpfreaks.com/topic/180444-solved-adding-an-order-field/#findComment-951955 Share on other sites More sharing options...
fenway Posted November 18, 2009 Share Posted November 18, 2009 You can use user variables to mimic row numbering. Quote Link to comment https://forums.phpfreaks.com/topic/180444-solved-adding-an-order-field/#findComment-960091 Share on other sites More sharing options...
gevans Posted November 18, 2009 Author Share Posted November 18, 2009 Sorry, forgot to close this thread. I just wrote a small php script to update the db, cheers Quote Link to comment https://forums.phpfreaks.com/topic/180444-solved-adding-an-order-field/#findComment-960217 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.