URL Parameter With Array - PHP List Plugin

Discussion in 'Community' started by tiagovareta, Jan 12, 2018.

  1. tiagovareta

    tiagovareta Member

    Level: Standard
    Hi!

    I have a PHP List button, to open a link (form).
    When I click the button to open the link/form, I want to pass a parameter, with the id of the selected records.

    I use this code:
    $app = JFactory::getApplication();
    $ids = $app->input->get('ids', array(), 'array');
    $id = array_shift($ids);
    $app->redirect('index.php?option=com_fabrik&view=form&formid=36&id_linha=' . $id);

    It works, but it does not pass the array with the id of the selected registers, it passes only 1 id.

    How can I pass an array with the various records selected?
     
  2. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    Not sure what you mean. It's doing exactly what you are asking it to do. You get the $ids array, then you array_shift() the first value out of the array, and use that.

    http://php.net/manual/en/function.array-shift.php

    If you need to pass all the ids, as (say) a comma separated list ...

    Code (Text):

    $app = JFactory::getApplication();
    $ids = $app->input->get('ids', array(), 'array');
    $app->redirect('index.php?option=com_fabrik&view=form&formid=36&id_linha=' . implode(',', $ids));
     
    -- hugh
     
  3. tiagovareta

    tiagovareta Member

    Level: Standard
    Hi Cheesegrits!
    Thank you one more time!!! That's what I wanted!

    Now clicking the button opens the link / form, and passes the selected ids!

    But in this form that opens, I have a field that does a query to the databases, to get the contact of the id that passed in the URL.
    The code is as follows:

    $myInput = JFactory::getApplication()->input;
    $myParam = $myInput->get('id_linha','');
    $db = FabrikWorker::getDbo();

    $contactoassessor = "SELECT contacto_ctt FROM tb_contacto WHERE id_contacto_geral_ctt = '$myParam'";
    $db->setQuery($contactoassessor);
    $contacto_assessor = $db->loadResult();
    return $contacto_assessor;

    It was working, when the URL passed only 1 id selected.
    Now the URL passes several ids separated by ";" (as intended), but now I can not separate the ids, to make the query.

    Example:
    If I pass the URL the ids: 1; 2; 3; 4; 5; 6
    When calling the variable to do the query: $myParam = $myInput->get('id_linha','');
    Set the value: "1; 2; 3; 4; 5; 6"
    I intended to receive the ids separately, and then make the query ($contactoassessor = "SELECT contacto_ctt FROM tb_contacto WHERE id_contacto_geral_ctt = '$myParam'";), for each id.
     
  4. tiagovareta

    tiagovareta Member

    Level: Standard
    When calling the variable passed by the URL, for the field, the values get together:
    $myInput = JFactory::getApplication()->input;
    $myParam = $myInput->get('id_linha','');

    return $myParam;

    Image: http://prntscr.com/hzmeag
     
  5. troester

    troester Well-Known Member Staff Member

    Level: Standard
    You can't do this if $myParam is something like 1,2,3
    Use
    WHERE id_contacto_geral_ctt IN ( $myParam)
     
  6. tiagovareta

    tiagovareta Member

    Level: Standard
    Hi Troester!
    Thanks for the answer!

    When calling the variable passed by the URL, for the field, the values get together: http://prntscr.com/hzmeag
    I use the code in the field:
    $myInput = JFactory::getApplication()->input;
    $myParam = $myInput->get('id_linha','');
    return $myParam;

    That is, in the URL has ";", when calling the variable it loses ";", to do the query the ids passed by the url have to maintain ";".
     
  7. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    Are you using a semicolon in the implode() code I gave you instead of a comma?

    If so, you probably can't do that, as ; is a reserved character and musn't be used in URL's unencoded.

    -- hugh
     
  8. tiagovareta

    tiagovareta Member

    Level: Standard
    Hi Cheesegrits! I already signed :)

    When I click the button, I pass the values as I want and separated by commas: http://prntscr.com/i16j59

    When I call the variable in a field:
    $myInput = JFactory::getApplication()->input;
    $myParam = $myInput->get('id_linha','');
    return $myParam;

    Remove the commas by getting an integer: http://prntscr.com/i16khh

    The idea is to make a sql query to the selected ids.
     
  9. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    You'll probably need to use the 'raw' filter type ...

    Code (Text):

    $myParam = $myInput->get('id_linha','', 'raw');
     
    ... as by default it'll use 'text' which I'm pretty sure only allows alphanumeric and - or _, so it'll strip out commas.

    Be careful about making sure you sanitize anything you fetch with 'raw' before using in a query.

    -- hugh
     
  10. tiagovareta

    tiagovareta Member

    Level: Standard
    Hi Cheesegrits!

    Many thanks for the reply! When we call the variable "id_line", it is already separated by commas: http://prntscr.com/i1likk

    But now, with each id that the variable passes, I have to do this query:
    $contactoassessor = "SELECT contacto_ctt FROM tb_contacto WHERE id_contacto_geral_ctt IN($myParam) AND tipo_contacto_ctt = 1 ORDER BY date_time DESC LIMIT 1";
    $db->setQuery($contactoassessor);
    $contacto_assessor = $db->loadResult();
    return $contacto_assessor;

    The idea is to collect the "contacto_ctt" field of all the ids, and put in a field separated by ";".
    I have been to see and there is the function "explode" to separate the ids, the issue is that I will have to use a loop to run all the ids (which the variable has) and I do not know how to do it. Do I really have to use the "explode"? Can the array read be a foreach?

    I ask for your help in this situation! I have to deliver the project tomorrow ...
     
  11. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    The "IN($myParam)" query will return a result set with all the ids, in multiple rows. Use loadColumn() instead of loadResult(), which returns an array of a single columns values from the result set, and implode that ...

    Code (Text):

    $contacto_assessor = $db->loadColumn();
    return implode(';', $contacto_assessor);
     
    -- hugh
     
  12. tiagovareta

    tiagovareta Member

    Level: Standard
    Hi Cheesegrits!!!

    Thank you very much for this help! You are the best!!!
     
  13. Sophist

    Sophist Moderator Staff Member

    Level: Community
    Cheesegrits certainly is!!!!!!
     
  14. tiagovareta

    tiagovareta Member

    Level: Standard
    Hi Cheesegrits!!!
    Hi Cheesegrits!

    I thought that this situation was already resolved, but here is a detail that will not let it work!

    When I use this query:
    $contactoassessor = "SELECT contacto_ctt FROM tb_contacto WHERE id_contacto_geral_ctt IN($myParam) AND tipo_contacto_ctt = 1 ORDER BY date_time DESC";
    $db->setQuery($contactoassessor);
    $contacto_assessor = $db->loadColumn();
    return "'".implode("', '", $contacto_assessor)."'";

    I want to populate a field "ids_das_linhas_selecionadas_sms_grp", with the "contact_ctt" of the ids selected! With the format: ( '+351912643315', ) So far so good, it's working!
    The question is I have a php script (form pulgin: http://prntscr.com/i24b7p), which will read this field to put in an array:
    $numbers = [$formModel->getElementData('tb_envio_sms_grupo___ids_das_linhas_selecionadas_sms_grp')];

    But this does not work, because it does not assume as an array but a single value (maybe string).

    To test I also tried to put the numbers in variable in the field "ids_das_linhas_selecionadas_sms_grp", and it does not work.
    $numeros = "'+351912643315', '+351964650705', '+351939795220'";
    return $numeros;

    However if you put the numbers in the array directly in plugin form (for testing) it works:
    $numbers = ['+351912643315', '+351964650705', '+351939795220'];

    I assume that by using the field "ids_das_linhas_selecionadas_sms_grp", the variable "$ numbers" takes on a string.
    If you put the numbers directly into the form's pulgin, assume the array.

    How can I pass the field value "ids_das_linhas_selecionadas_sms_grp", as array for variable $numbers?
     
  15. tiagovareta

    tiagovareta Member

    Level: Standard
    Hi Cheesegrits!

    I think I have solved the situation! I put the following code:
    $numeros_selecionados = $formModel->getElementData('tb_envio_sms_grupo___ids_das_linhas_selecionadas_sms_grp');
    $numbers = explode(',', $numeros_selecionados);
     

Share This Page