Hopefully this will help someone...
If you are looking at creating SEO friendly URLS, you need to do this:
Code: Select all
<?php
// For help on using hooks, please refer to https://bigprof.com/appgini/help/working-with-generated-web-database-application/hooks
function news_init(&$options, $memberInfo, &$args){
return TRUE;
}
function news_header($contentType, $memberInfo, &$args){
$header='';
switch($contentType){
case 'tableview':
$header='';
break;
case 'detailview':
$header='';
break;
case 'tableview+detailview':
$header='';
break;
case 'print-tableview':
$header='';
break;
case 'print-detailview':
$header='';
break;
case 'filters':
$header='';
break;
}
return $header;
}
function news_footer($contentType, $memberInfo, &$args){
$footer='';
switch($contentType){
case 'tableview':
$footer='';
break;
case 'detailview':
$footer='';
break;
case 'tableview+detailview':
$footer='';
break;
case 'print-tableview':
$footer='';
break;
case 'print-detailview':
$footer='';
break;
case 'filters':
$footer='';
break;
}
return $footer;
}
function news_before_insert(&$data, $memberInfo, &$args){
return TRUE;
}
function news_after_insert($data, $memberInfo, &$args){
update_seo_friendly_url($data);
return TRUE;
}
function news_before_update(&$data, $memberInfo, &$args){
return TRUE;
}
function news_after_update($data, $memberInfo, &$args){
update_seo_friendly_url($data);
return TRUE;
}
function news_before_delete($selectedID, &$skipChecks, $memberInfo, &$args){
return TRUE;
}
function news_after_delete($selectedID, $memberInfo, &$args){
}
function news_dv($selectedID, $memberInfo, &$html, &$args){
}
function news_csv($query, $memberInfo, &$args){
return $query;
}
function news_batch_actions(&$args){
return array();
}
function update_seo_friendly_url($data){
$seo_friendly = trim($data['google_title']);
$seo_friendly = strip_tags($seo_friendly);
$seo_friendly = html_entity_decode($seo_friendly, ENT_QUOTES, "UTF-8");
$seo_friendly = strtolower($seo_friendly);
$seo_friendly = preg_replace('/\s+/', '-', $seo_friendly);
$query = sql("SELECT seo_friendly_url FROM news group by seo_friendly_url having count(*) >= 2", $eo);
$num_rows = db_num_rows($query);
if ($num_rows >=2) {
$seo_friendly = $seo_friendly."-".$data['selectedID'];
}
sql("UPDATE news SET seo_friendly_url = '{$seo_friendly}'
WHERE id={$data['selectedID']}", $eo);
}
I did first generate a micro time number at the end of the url when a similar record was found but it does not make sense.....if you keep changing the number each time, Google will need to reindex the url again and again so instead I linked it to the id of the record, this way it will always be the same one whatever happens.
I have seen some people using $i++ to add a number to the url is a similar record is found, but again, this wont make sense for an SEO purpose.
So now on update or insert, the first google_title field will copy itself to the seo_friendly_url field while being cleaned up and modifyed to soemthing like word1-word2-word3, then if a duplicate title is found, the id of the article will show at the end of the url.
Feel free to change this code and repost better solutions here as there are many ways of doing this.....
Thank again Ahamad for the selectedID, I scratched my head on that one:-)