Osclass forums

Development => Development => Topic started by: Sophia_OS on October 09, 2018, 07:18:07 am

Title: Replace word
Post by: Sophia_OS on October 09, 2018, 07:18:07 am
Hey,
I want to replace the data input of custom-fields.
For example, when a user put "red" for custom-field1, i want to add "-color" to that and make it: "red-color"
Custom-field1-value = custom-field1-Value . "-color"
And be multiple to do the same thing for all my 10 custom-fields.
Please help me to do this,
Thanks,
Title: Re: Replace word
Post by: Sophia_OS on February 05, 2019, 12:19:03 pm
Is there a solution for this?
Title: Re: Replace word
Post by: Aficionado on February 05, 2019, 12:42:33 pm
Is there a solution for this?

You have bumped 5 topics until now with the same old useless "Is there any solution for this".

Please understand that bumping doesn't help anybody, especially you.

Please respect our community, that we are trying to hard to keep alive.

Title: Re: Replace word
Post by: WEBmods on February 05, 2019, 11:56:41 pm
There is a solution to this.  :o

Is there a solution for this?

You have bumped 5 topics until now with the same old useless "Is there any solution for this".

Please understand that bumping doesn't help anybody, especially you.

Please respect our community, that we are trying to hard to keep alive.



Bump actually helped a quite of times because I didn't saw some topics (like this one) before...
Title: Re: Replace word
Post by: WEBmods on February 06, 2019, 12:00:57 am
Code: [Select]
<?php
function wm_item_post_replace_meta_value($item) {
    
$field_id 5// CUSTOM FIELD ID
    
$item_id $item['pk_i_id']; // Get item ID.
    
$dao = new DAO(); // Intialise data access object.
    
$query sprintf('UPDATE %st_item_meta SET s_value = "red_color" WHERE fk_i_item_id = %s AND fk_i_field_id = &s AND s_value = "red"'DB_TABLE_PREFIX$item_id$field_id); // Update.
    
$dao->dao->query($query);
}
osc_add_hook('posted_item''wm_item_post_replace_meta_value');

Regards.
Title: Re: Replace word
Post by: Aficionado on February 06, 2019, 12:08:42 am

Bump actually helped a quite of times because I didn't saw some topics (like this one) before...

Well if you encourage bumping of topics, then i do not know what else to say.
Title: Re: Replace word
Post by: WEBmods on February 06, 2019, 12:20:37 am

Bump actually helped a quite of times because I didn't saw some topics (like this one) before...

Well if you encourage bumping of topics, then i do not know what else to say.

I don't say it's okay but I don't either say it's bad. Sometimes it helps and sometimes it frustrates all us who try to help.

Regards.
Title: Re: Replace word
Post by: Sophia_OS on February 07, 2019, 10:45:30 pm
Code: [Select]
<?php
function wm_item_post_replace_meta_value($item) {
    
$field_id 5// CUSTOM FIELD ID
    
$item_id $item['pk_i_id']; // Get item ID.
    
$dao = new DAO(); // Intialise data access object.
    
$query sprintf('UPDATE %st_item_meta SET s_value = "red_color" WHERE fk_i_item_id = %s AND fk_i_field_id = &s AND s_value = "red"'DB_TABLE_PREFIX$item_id$field_id); // Update.
}
osc_add_hook('posted_item''wm_item_post_replace_meta_value');

Regards.

Thank you very much Patrick,
Im on my phone these days. I will test it and post the result after i reach my pc.
Regards,
Title: Re: Replace word
Post by: Sophia_OS on February 07, 2019, 10:56:19 pm

Bump actually helped a quite of times because I didn't saw some topics (like this one) before...

Well if you encourage bumping of topics, then i do not know what else to say.

I don't say it's okay but I don't either say it's bad. Sometimes it helps and sometimes it frustrates all us who try to help.

Regards.

This guy dosnt have a problem with bumping. He has a problem with me. And i dont know why. Several times in the past i asked him not to reply to my posts but he dosnt listen.
Title: Re: Replace word
Post by: Sophia_OS on February 08, 2019, 08:58:26 pm
Code: [Select]
<?php
function wm_item_post_replace_meta_value($item) {
    
$field_id 5// CUSTOM FIELD ID
    
$item_id $item['pk_i_id']; // Get item ID.
    
$dao = new DAO(); // Intialise data access object.
    
$query sprintf('UPDATE %st_item_meta SET s_value = "red_color" WHERE fk_i_item_id = %s AND fk_i_field_id = &s AND s_value = "red"'DB_TABLE_PREFIX$item_id$field_id); // Update.
}
osc_add_hook('posted_item''wm_item_post_replace_meta_value');

Regards.

@patrick
would you please edit this code to:
replace multiple custom-fields values?
And also could put custom-fields values in $query by variable? and get html custom-fields values?
By the way, i tested this code but didn't work.
thanks
Title: Re: Replace word
Post by: Sophia_OS on March 06, 2019, 03:52:01 am
Code: [Select]
<?php
function wm_item_post_replace_meta_value($item) {
    
$field_id 5// CUSTOM FIELD ID
    
$item_id $item['pk_i_id']; // Get item ID.
    
$dao = new DAO(); // Intialise data access object.
    
$query sprintf('UPDATE %st_item_meta SET s_value = "red_color" WHERE fk_i_item_id = %s AND fk_i_field_id = &s AND s_value = "red"'DB_TABLE_PREFIX$item_id$field_id); // Update.
}
osc_add_hook('posted_item''wm_item_post_replace_meta_value');

Regards.

@patrickFromCroatia
thank you but your code does not work!
Title: Re: Replace word
Post by: WEBmods on March 06, 2019, 11:05:42 am
Sorry, I forgot one line. Check the edited post.

Regards.
Title: Re: Replace word
Post by: Sophia_OS on March 08, 2019, 11:35:19 am
Code: [Select]
<?php
function wm_item_post_replace_meta_value($item) {
    
$field_id 5// CUSTOM FIELD ID
    
$item_id $item['pk_i_id']; // Get item ID.
    
$dao = new DAO(); // Intialise data access object.
    
$query sprintf('UPDATE %st_item_meta SET s_value = "red_color" WHERE fk_i_item_id = %s AND fk_i_field_id = &s AND s_value = "red"'DB_TABLE_PREFIX$item_id$field_id); // Update.
    
$dao->dao->query($query);
}
osc_add_hook('posted_item''wm_item_post_replace_meta_value');

Regards.

did not work!
i put custom-field id (Table: oc_t_meta_fields -> pk_i_id) in here: ($field_id = 5;) and put it in functions.php but did not work!
what i need is when i put for example "blue" in custom-field, it stores in the database "blue+color". it adds 'color' to the end of the word that we enter in.

Title: Re: Replace word
Post by: WEBmods on March 08, 2019, 08:16:22 pm
Typo again...
Give this a try.

Code: [Select]
<?php
function wm_item_post_replace_meta_value($item) {
    
$field_id 5// CUSTOM FIELD ID
    
$item_id $item['pk_i_id']; // Get item ID.
    
$dao = new DAO(); // Intialise data access object.
    
$query sprintf('UPDATE %st_item_meta SET s_value = "red_color" WHERE fk_i_item_id = %s AND fk_i_field_id = %s AND s_value = "red"'DB_TABLE_PREFIX$item_id$field_id); // Update.
    
$dao->dao->query($query);
}
osc_add_hook('posted_item''wm_item_post_replace_meta_value');

Regards.
Title: Re: Replace word
Post by: Sophia_OS on March 09, 2019, 03:22:57 am
Typo again...
Give this a try.

Code: [Select]
<?php
function wm_item_post_replace_meta_value($item) {
    
$field_id 5// CUSTOM FIELD ID
    
$item_id $item['pk_i_id']; // Get item ID.
    
$dao = new DAO(); // Intialise data access object.
    
$query sprintf('UPDATE %st_item_meta SET s_value = "red_color" WHERE fk_i_item_id = %s AND fk_i_field_id = %s AND s_value = "red"'DB_TABLE_PREFIX$item_id$field_id); // Update.
    
$dao->dao->query($query);
}
osc_add_hook('posted_item''wm_item_post_replace_meta_value');

Regards.

This is the same code that you updated up there!
I tested this and didn't work!  :-\
Title: Re: Replace word
Post by: Sophia_OS on March 20, 2019, 12:31:36 pm
@patrickFromCroatia
I tested your code but it really didn't work!
I edited my first post for clarifying my question!
Thanks,
Title: Re: Replace word
Post by: WEBmods on March 21, 2019, 05:22:16 pm
I will try the code on my demo site.

One suggestion: please don't expect EVERYTHING from others, try enabling PHP debugging, SQL debugging, testing, looking at tutorials etc. That way you will learn and it will be better than just copying the code.

Regards.
Title: Re: Replace word
Post by: WEBmods on March 21, 2019, 05:30:57 pm
Just tried the code. It worked.

http://148.251.207.178/osclass/plugins/demo/index.php?page=item&id=102

I selected "red" and "red_color" was saved in the database.

Regards.
Title: Re: Replace word
Post by: Sophia_OS on March 21, 2019, 07:44:20 pm
Just tried the code. It worked.

http://148.251.207.178/osclass/plugins/demo/index.php?page=item&id=102

I selected "red" and "red_color" was saved in the database.

Regards.

Not like that. Please reread my first post. I edited it!
I dont want just replace red with red_color. I want to add "_color" to the value of custom-field. And also for multiple custom-fields not just one. Please read my first post again. Its edited!
Thank you Patrick,
Title: Re: Replace word
Post by: Sophia_OS on March 26, 2019, 11:33:43 pm
Just tried the code. It worked.

http://148.251.207.178/osclass/plugins/demo/index.php?page=item&id=102

I selected "red" and "red_color" was saved in the database.

Regards.

Not like that. Please reread my first post. I edited it!
I dont want just replace red with red_color. I want to add "_color" to the value of custom-field. And also for multiple custom-fields not just one. Please read my first post again. Its edited!
Thank you Patrick,

@patrickFromCroatia
Patrick, did you read this post?
Title: Re: Replace word
Post by: WEBmods on March 28, 2019, 04:44:13 pm
Look, I can help but I can't write all the code. You have a lot of questions and expect people to write all the code for you. There's a job section for that. I work on a lot of paid projects and I don't have time, can't and don't want to do everything in cases like this...

Anyways, make an array of colors and use foreach to replace each one of them, or look if there is a SQL query to replace more stuff, I have no idea. Not sure how fast is this, but it probaby can be optimized...

Code: [Select]
<?php
function wm_item_post_replace_meta_value($item) {
    
$field_id 5// CUSTOM FIELD ID
    
$item_id $item['pk_i_id']; // Get item ID.
    
$dao = new DAO(); // Intialise data access object.
    
$colors = array('red''blue'); // List of colors.
    
foreach($colors as $color) {
        
$query sprintf('UPDATE %st_item_meta SET s_value = "%s-color" WHERE fk_i_item_id = %s AND fk_i_field_id = %s AND s_value = "%s"'DB_TABLE_PREFIX$color$item_id$field_id$color); // Update.
        
$dao->dao->query($query);
    }
}
osc_add_hook('posted_item''wm_item_post_replace_meta_value');

Regards.
Title: Re: Replace word
Post by: Sophia_OS on March 28, 2019, 11:20:27 pm
Look, I can help but I can't write all the code. You have a lot of questions and expect people to write all the code for you. There's a job section for that. I work on a lot of paid projects and I don't have time, can't and don't want to do everything in cases like this...

Anyways, make an array of colors and use foreach to replace each one of them, or look if there is a SQL query to replace more stuff, I have no idea. Not sure how fast is this, but it probaby can be optimized...

Code: [Select]
<?php
function wm_item_post_replace_meta_value($item) {
    
$field_id 5// CUSTOM FIELD ID
    
$item_id $item['pk_i_id']; // Get item ID.
    
$dao = new DAO(); // Intialise data access object.
    
$colors = array('red''blue'); // List of colors.
    
foreach($colors as $color) {
        
$query sprintf('UPDATE %st_item_meta SET s_value = "%s-color" WHERE fk_i_item_id = %s AND fk_i_field_id = %s AND s_value = "%s"'DB_TABLE_PREFIX$color$item_id$field_id$color); // Update.
        
$dao->dao->query($query);
    }
}
osc_add_hook('posted_item''wm_item_post_replace_meta_value');

Regards.

@patrickFromCroatia
Thank you very much, this works now! but there is one problem! i want to add '-color' to custom-field's value! i want to get the value of custom-field and add '-color' to it!
please see this code:
Code: [Select]
<?php
function wm_item_post_replace_meta_value($item) {
    
$field_id 5;
    
$item_id $item['pk_i_id'];
    
$dao = new DAO();
    
$cf_value Params::getParam('meta[24]'); //Not working! i can not get the value of custom-field!
$query sprintf('UPDATE %st_item_meta SET s_value = "%s-color" WHERE fk_i_item_id = %s AND fk_i_field_id = %s'DB_TABLE_PREFIX$cf_value$item_id$field_id);
$dao->dao->query($query);
}
osc_add_hook('posted_item''wm_item_post_replace_meta_value');
Title: Re: Replace word
Post by: WEBmods on March 29, 2019, 12:28:01 am
I'm on phone, but try Params::getParam('meta')[24]

Regards.
Title: Re: Replace word
Post by: Sophia_OS on March 29, 2019, 02:53:21 am
I'm on phone, but try Params::getParam('meta')[24]

Regards.

Thank you very much Patrick!