Fork me on GitHub

pnVarCleanFromInput Question  Bottom

  • Hi,

    In pnAPI.php, the last few lines from pnVarCleanFromInput are:

    Code

    // Return vars
        if (func_num_args() == 1) {
            return $resarray[0];
        } else {
            return $resarray;
        }



    Why is it that when there is only 1 arugument you can only get 1 character returned? Sometimes that isn't valid. Would it hurt anything to return the whole $resarray?

    Thanks,
    Grant
  • I can't speak for the original author of the API but changing the bahaviour now would break backwards compatability. What your missing is the bahaviour of the list function and the concept of a string. The example module uses the code list ($var1, $var2) = pnVarCleanFromInput('var1', 'var2'). And as you've observed an array is returned and var1 and var2 resolve correctly.

    If we reduce this down to one variable i.e. list ($var1) = pnVarCleanFromInput('var1') we only get the first character of of the variable back. Why? The list function expects an array. Conceptually a string can be thought of (and treated practically) as an array of characters hence we only get the first character.

    The solution is not to use list when passing only one parameter to pnVarCleanFromInput i.e. $var1 = pnVarCleanFromInput('var1').

    -Mark

    --
    Visit My homepage and Zikula themes.
  • Thanks! You da man. That took me a while to track down, and I was just using dummy variables at the time to get 2 arguments.

    Grant
  • No problem - I did the same thing myself when first starting coding with PN (seems an age ago now...).

    -Mark

    --
    Visit My homepage and Zikula themes.
  • Hey ..
    I have this problem too, only one character i returned. I checked and yepp, I had the list function on only on variable, however
    $genre = pnVarCleanFromInput('genre'); did not fix the prob.. anyone got a clue where to check next?
  • Jeanie

    Hey ..
    I have this problem too, only one character i returned. I checked and yepp, I had the list function on only on variable, however
    $genre = pnVarCleanFromInput('genre'); did not fix the prob.. anyone got a clue where to check next?


    What are you expecting to be returned? Maybe if you could post a little bit more of your code I can see what is happening.
  • I want only one word back, for example Action, Humor, General. I'm pretty much a newbie...

    function xxx_genresapi_create($args)
    {

    extract($args);

    if ((!isset($genre))) {
    pnSessionSetVar('errormsg', _MODARGSERROR3);
    return false;
    }

    if (!pnSecAuthAction(0, 'xxx::', "$genre::", ACCESS_ADD)) {
    pnSessionSetVar('errormsg', _MODULENOAUTH);
    return false;
    }

    $dbconn =& pnDBGetConn(true);
    $pntable =& pnDBGetTables();

    $Genrestable = &$pntable['xxx_genres'];
    $Genrescolumn = &$pntable['xxx_genres_column'];

    $nextId = $dbconn->GenId($Genrestable);

    $genre = pnVarPrepForStore($genre);

    $SQL = "INSERT INTO $Genrestable(
    $Genrescolumn[genreid],
    $Genrescolumn[genre])
    VALUES (
    '".(int)$nextId."',
    '".$genre."'
    )";
    $dbconn->Execute($SQL);

    if ($dbconn->ErrorNo() != 0) {
    pnSessionSetVar('errormsg', _EXAMPLECREATEFAILED);
    return false;
    }

    $genreid = $dbconn->PO_Insert_ID($Genrestable, $Genrescolumn['genreid']);

    pnModCallHooks('item', 'genrescreate', $genreid, 'genreid');
    return $genreid;
    }

    xxx_Genres_create.php
    function xxx_genres_create($args)
    {

    $genre = pnVarCleanFromInput('genre');

    extract($args);

    if (!pnSecConfirmAuthKey()) {
    pnSessionSetVar('errormsg', pnVarPrepHTMLDisplay(_BADAUTHKEY));
    pnRedirect(pnModURL('xxx', 'genres', 'view'));
    return true;
    }

    if (!pnModAPILoad('xxx', 'genres')) {
    pnSessionSetVar('errormsg', pnVarPrepHTMLDisplay(_LOADFAILED23));
    pnRedirect(pnModURL('xxx', 'genres', 'view'));
    return true;
    }

    $genreid = pnModAPIFunc('xxx',
    'genres',
    'create',
    array('genre' => $genre));

    if ($genreid) {
    // Success
    pnSessionSetVar('statusmsg', pnVarPrepHTMLDisplay(_EXAMPLECREATED));
    }

    pnRedirect(pnModURL('xxx', 'genres', 'view'));

    return true;
    }
  • Jeanie,


    It's hard to tell for sure without actually using the module myself, but I re-ordered some of the code and put in a couple debug statements to print out the information to the screen. This might help with the debugging. Be sure to take the echo and print_r statements out of your final code. I use a lot of echo and print_r statements during debugging.

    Code

    function xxx_genresapi_create($args)
    {

      if ((!isset($genre))) {
        pnSessionSetVar('errormsg', _MODARGSERROR3);
        return false;
      }

      if (!pnSecAuthAction(0, 'xxx::', "$genre::", ACCESS_ADD)) {
        pnSessionSetVar('errormsg', _MODULENOAUTH);
        return false;
      }

      $genre = pnVarPrepForStore($genre);
      // Debug
      echo "Genre: $genre<br>";
     
      extract($args);
      // Debug
      print_r($args);
     
      $dbconn =& pnDBGetConn(true);
      $pntable =& pnDBGetTables();

      $Genrestable = &$pntable['xxx_genres'];
      $Genrescolumn = &$pntable['xxx_genres_column'];

      $nextId = $dbconn->GenId($Genrestable);

      $sql = "INSERT INTO $Genrestable($Genrescolumn[genreid],$Genrescolumn[genre])
                 VALUES ('"
    .(int)$nextId."',
                         '"
    .$genre."')";

      $dbconn->Execute($sql);

      if ($dbconn->ErrorNo() != 0) {
        pnSessionSetVar('errormsg', _EXAMPLECREATEFAILED);
        return false;
      }

      $genreid = $dbconn->PO_Insert_ID($Genrestable, $Genrescolumn['genreid']);

      pnModCallHooks('item', 'genrescreate', $genreid, 'genreid');
      return $genreid;
    }


    function xxx_genres_create($args)
    {

     

      if (!pnSecConfirmAuthKey()) {
        pnSessionSetVar('errormsg', pnVarPrepHTMLDisplay(_BADAUTHKEY));
        pnRedirect(pnModURL('xxx', 'genres', 'view'));
        return true;
      }

      if (!pnModAPILoad('xxx', 'genres')) {
        pnSessionSetVar('errormsg', pnVarPrepHTMLDisplay(_LOADFAILED23));
        pnRedirect(pnModURL('xxx', 'genres', 'view'));
        return true;
      }

      $genre = pnVarCleanFromInput('genre');
      // Debug
      echo "Genre: $genre<br>";

      extract($args);
      // Debug
      print_r($args);


      $genreid = pnModAPIFunc('xxx',
                  'genres',
                  'create',
                  array('genre' => $genre));

      if ($genreid) {
        // Success
        pnSessionSetVar('statusmsg', pnVarPrepHTMLDisplay(_EXAMPLECREATED));
      }

      pnRedirect(pnModURL('xxx', 'genres', 'view'));

      return true;
    }


    Grant
  • Hey :) Thanks for the help so far!

    When I run the script now I get
    Genre: xxx
    Array ( )

    This means that theres no info in the array right? or am I completely blank here? Most likely I am :D

    Jeanie
  • 0 users

This list is based on users active over the last 60 minutes.