This is the current code in page.php
We are having problems with it since we use Couch to log data and so we end up having 100+ queries as the page name is e.g keakie-visited-the-homepage-101. The current code iterates from 1 to 100 until it matches a unique number, meaning there are 100 extra queries to the database. A better solution could be
Is there anything wrong with this? I'm not that experienced with PHP
- Code: Select all
if( $name=='' && $title!='' ){
$name = $FUNCS->get_clean_url( $title );
// verify the name does not already exist
$unique = false;
$unique_id = 1;
$orig_name = $name;
while( !$unique ){
$rs = $DB->select( K_TBL_PAGES, array('id'), "page_name='" . $DB->sanitize( $name ). "' and NOT id=" . $DB->sanitize( $this->id ) . " and template_id='" . $DB->sanitize( $this->tpl_id ). "'" );
if( !count($rs) ){
$unique = true;
}
else{
$name = $orig_name . '-' . $unique_id++;
}
}
$this->_fields['k_page_name']->store_posted_changes( $name );
}
We are having problems with it since we use Couch to log data and so we end up having 100+ queries as the page name is e.g keakie-visited-the-homepage-101. The current code iterates from 1 to 100 until it matches a unique number, meaning there are 100 extra queries to the database. A better solution could be
- Code: Select all
if( $name=='' && $title!='' ){
$name = $FUNCS->get_clean_url( $title );
// verify the name does not already exist
$unique = false;
$unique_id = 1;
$orig_name = $name;
while( !$unique ){
$rs = $DB->select( K_TBL_PAGES, array('id'), "page_name='" . $DB->sanitize( $name ). "' and NOT id=" . $DB->sanitize( $this->id ) . " and template_id='" . $DB->sanitize( $this->tpl_id ). "'" );
if( !count($rs) ){
$unique = true;
}
else {
$rs = $DB->raw_select( 'SELECT MAX(page_name) as max FROM ' . K_TBL_PAGES . ' WHERE page_name LIKE "' . $DB->sanitize( $name ) . '%"' );
$largest_id = $rs[0]['max'];
$exploded_largest_id = explode("-", $largest_id);
$last_id = end($exploded_largest_id);
$last_id++;
if (is_numeric($last_id)) {
$name = $orig_name . '-' . $last_id;
} else {
$name = $orig_name . '-' . $unique_id;
}
}
}
$this->_fields['k_page_name']->store_posted_changes( $name );
}
Is there anything wrong with this? I'm not that experienced with PHP