Insert value on update or insert

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
bambinou
Veteran Member
Posts: 163
Joined: 2013-02-01 15:09

Insert value on update or insert

Post by bambinou » 2017-08-08 09:48

Hello,

I am a php programmer but I have a little bit of trouble to understand how to simply insert data in a table.

Here is what I would like to achieve:

1)When the user update, or insert data in a form, I would like to take 1 field from that form and copy it in another row.

2)This is my table structure:
Table: news

field1: google_title
field2: seo_friendly_url


At the moment, in the hook folder I have created a new function:

Code: Select all

    function update_seo_friendly_url($data){

        sql("UPDATE news
             SET seo_friendly_url = '{$data['google_title']}'
             WHERE id={$data['$id']}", $eo);

    }
Then called this function here:

Code: Select all

	function news_after_update($data, $memberInfo, &$args){

        update_seo_friendly_url($data);

		return TRUE;
	}

and here:

Code: Select all

function news_after_insert($data, $memberInfo, &$args){

        update_seo_friendly_url($data);

		return TRUE;
	}

I would have expected this to work but it is not, any idea why please?

I am simply trying to copy over the data from the field google_title to seo_friendly_url, when I get this going, I will add a new function that rewrite the url in a friendly way but the most important for me is to get the first part going.


Thank you!

bambinou
Veteran Member
Posts: 163
Joined: 2013-02-01 15:09

Re: Insert value on update or insert

Post by bambinou » 2017-08-09 09:42

Anyone able to help on this please?

Thank you.

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Insert value on update or insert

Post by a.gneady » 2017-08-09 12:27

Hmm ... try using

Code: Select all

$data['selectedID']
instead of

Code: Select all

$data['id']
in update_seo_friendly_url().
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

bambinou
Veteran Member
Posts: 163
Joined: 2013-02-01 15:09

Re: Insert value on update or insert

Post by bambinou » 2017-08-09 13:20

Hi Ahmad,

I edited my reply, I had a spelling mistake in my code, all good now! It is working.

I will post an SEO friendly url code in this thread for others as soon as I manage to crack it.

Thanks again!

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Insert value on update or insert

Post by a.gneady » 2017-08-09 13:29

You're welcome :)
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

bambinou
Veteran Member
Posts: 163
Joined: 2013-02-01 15:09

Re: Insert value on update or insert

Post by bambinou » 2017-08-09 16:07

Strange, I posted what I did but it did not save in the forum

This is what I did in the end:

Code: Select all

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);

    }




	function news_after_insert($data, $memberInfo, &$args){

        update_seo_friendly_url($data);

		return TRUE;
	}


	function news_after_update($data, $memberInfo, &$args){

        update_seo_friendly_url($data);

		return TRUE;
	}
	



What I did here:
$seo_friendly = $seo_friendly."-".$data['selectedID'];

Is add the id of the row so it is always the same one otherwise if I add $i++ it will add unknown numbers on duplicate which will cause Google to always reindex the urls.

Hope this helps someone.

In the front end generate all your links in your <a> tags by pulling the articles seo names, then if a crawler find an article that uses the old dynamic links, simply add a code if the seo name is not available.

xbox2007
Veteran Member
Posts: 129
Joined: 2016-12-16 16:49

Re: Insert value on update or insert

Post by xbox2007 » 2018-01-28 15:56

thanks a lot

i try and test this code and other code , its work , but only after press Save now !!!!!

any way to make value on ( field2: seo_friendly_url ) show once insert date into field1: google_title ?????

R Tammam
Veteran Member
Posts: 113
Joined: 2017-08-26 15:35

Re: Insert value on update or insert

Post by R Tammam » 2018-02-20 05:00

Hello xbox
Regarding to your question , you should make an ajax request to get the data will be updated , then use javascript to update it

Post Reply