We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4971
    • 964 Posts
    I have a Listbox-Multiple TV which options are loaded via an @SELECT with all the available players in the website (it is a sports club website) to be added to a team.

    Once all the players are selected I want to process the output of the TV so I have
    a list of players linked to their individual player pages.

    So I am trying this code:

    <?php
    $tvPlayers = $modx->getTemplateVarOutput(array("tmPlayers"),'[*id*]',$published=1);
    $players = explode('||',$tvPlayers[tmPlayers]);
    
    for ($i=0; $i<count($players); $i++) {
        $player = explode(', ',$players[$i]);
        $fixedPlayer = $player[1] . " " . $player[0];
        $document = $modx->db->select("id", "modx_site_content", "pagetitle = '" . $fixedPlayer . "'");
        $output .= $document . ",";
    }
    return $output;
    ?>


    I get the players selected into $tvPlayers (they are listed as "Lastname, Firstname") that are outputed as a delimited list using "||" as the delimiter.

    Then I explode them into $players and invert the name as "Firstname Lastname" because that is the way the pagetitle of the individual player name is set.

    Then I do a SELECT to find the "id" of the page of each player that I will input into Ditto to have a list of linked players (could not do it with the Grid widget).

    Questions:

    1) The $document SELECT gives me the following output:

    Resource id #17,Resource id #18,Resource id #19,Resource id #20, 


    instead of what I want which is something like this:
    44,21,35,39,
    which are the page id’s.

    Where are these coming from? the Resource id #x?
    What’s wrong on my query?
    This query works fine in phpMyAdmin:
    SELECT `id` FROM `modx_site_content` WHERE `pagetitle`= 'Brenda Stevens'
    and gives 39.


    TIA for your help...

    [SOLUTION]
    Do a select, then a getRow (or getValue if you only want the first element) and then get the
    result in an array that could be associative, numeric or both...

    <?php
    $tvPlayers = $modx->getTemplateVarOutput(array("tmPlayers"),'[*id*]',$published=1);
    
    $players = explode('||',$tvPlayers[tmPlayers]);
    
    for ($i=0; $i<count($players); $i++) {
        $player = explode(', ',$players[$i]);
        $fixedPlayer = $player[1] . " " . $player[0];
    
        $res = $modx->db->select("id", "modx_site_content", "pagetitle = '" .  $fixedPlayer . "'");
        $row = $modx->db->getRow($res);
        $output .= $row['id'] . ", ";
    }
    
    return $output;
    ?>
      Website: www.mercologia.com
      MODX Revo Tutorials:  www.modxperience.com

      MODX Professional Partner
      • 12151
      • 115 Posts
      greetings, the select method returns a recordset, not a value, so for returning a single value you should either use:

      $query = "SELECT id FROM modx_site_content WHERE pagetitle = '".$modx->db->escape($fixedPlayer)."'";
      $output = $modx->db->getValue($query);
      


      or you can return the first row into an array:

      $rs = $modx->db->select("id", "modx_site_content", "pagetitle = '" . $fixedPlayer . "'");
      $arrResult = $modx->db->getRow($rs);
      $output = $arrResult["id"];
      


      if you need a multi row recordset returned into an array:

      $rs = $modx->db->select($query);
      $arrResult = $modx->db->makeArray($rs);
      


      hope this helps smiley

        • 4971
        • 964 Posts
        thanks a lot highlander...
        I finally discover the same after digging in the docs
        the three getValue, getRow and makeArray functions are
        not defined in the new wiki, had to dig in the old one...
          Website: www.mercologia.com
          MODX Revo Tutorials:  www.modxperience.com

          MODX Professional Partner