Zikula: A Flexible Open Source Content Management System
home | forum | contact us

Dizkus

Bottom
displaying data back help needed.
  • Posted: 17.07.2005, 21:43
     
    sunadmn
    rank:
    Helper Helper
    registered:
     October 2004
    Status:
    offline
    last visit:
    01.03.06
    Posts:
    437
    Hey all in my weekend rush to hack together a quick and dirty module for a demo tomorrow I am running into a brick wall and need the advise of you all. I have written a couple of functions to do some DB gathering and I want to take the return and display it, but for some reason I can't get what I need back so I wanted to see if there was something I can do to get what I need.

    Let me start by explaining the DB setup:

    simple three column table setup like so:

    pnChange_table

    pnChangecolumn rid
    pnChangecolumn key
    pnChangecolumn value

    the insert function simply grabs everything stored in $_POST and loops through it and does an insert of the form element as the KEY and the form element name as the rid the value is set to that of the form element, so if form element is cm_name that becomes the rid and element cm_date becomes the key and the contents become value. This works great and is very simple to do, but now I want to display the data for given rid back and I have stumbled upon an issue below is my code for the user and userapi calls made:

    user

    Code

    function pnChange_user_gather($args) {


        // Load API.
        if (!pnModAPILoad('pnChange', 'user')) {
            return pnVarPrepHTMLDisplay(_LOADFAILED);
        }

        $cm_id = pnVarCleanFromInput('rid');

        extract($args);


        // The API function is called.
        $items = pnModAPIFunc('pnChange',
                              'user',
                              'get',
                              array('cm_id'  => $cm_id));

    //print_r($items);
    //die;


        if (!$items) {
            return pnVarPrepHTMLDisplay(_pnEquipmentFAILED);
        }


       $pnRender =& new pnRender('pnChange');

       $pnRender->caching=false;


        // Assign the items to the template
        $pnRender->assign('pnChangeitems', $items);


        // Return the output that has been generated by this function
        return $pnRender->fetch('pnChange_user_cmdisplay.htm');



    }


    userapi

    Code

    function pnChange_userapi_get($args)
    {
        extract($args);

    //print_r($args);
    //die;

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

        $pnChangetable  = &$pntable['pnChange_requestor_data'];
        $pnChangecolumn = &$pntable['pnChange_requestor_column'];


        $sql = "SELECT $pnChangecolumn[rid],
                       $pnChangecolumn[key],
                       $pnChangecolumn[value]
                FROM $pnChangetable
                WHERE $pnChangecolumn[rid] = '$cm_id'"
    ;



           $result =& $dbconn->Execute($sql);


           if ($dbconn->ErrorNo() != 0) {
               print($dbconn->ErrorMsg());
           }

       for(; !$result->EOF; $result->MoveNext()) {

          $data = $result->fields;

    //      $items[] = $data[2];

          $items[] = array(
                           $data[1],
                           $data[2],
                           $data[3]);


    //print_r($data);
    //print_r($items);

       }

    //print_r($items);
    //die;

        $result->Close();

        // Return the items
        return $items;



    }


    Now the issue I have is when I build my $item I am getting only Array in return if I set it up like this:

    Code

    $items[] = array(
                           $data[1],
                           $data[2],
                           $data[3]);

    Yet if I do this I return data back to the template:

    Code

    $items[] = $data[2];


    it seems that either I have an issue with building the $items array or I have really missed something in my code and for the life of me am not sure where to go from here,also below is what I am doing in my template file.

    Template

    Code

    <!--[include file="pnChange_user_menu.htm"]-->
    <br /><br />
    <div class="pn-title"><!--[pnml name="_pnChangeVIEW"]--></div>
    <br />
    <div class="pn-normal" style="width:100%;">
    <table style="text-align:center;width:100%;" border="0">

      <tr>
          <th><!--[pnml name="_pnChangeRID"]--></th>
      </tr>

      <!--[section name="pnChangeitems" loop=$pnChangeitems]-->

      <tr>
          <td><!--[$pnChangeitems[pnChangeitems]|pnvarprephtmldisplay]--></td>
      </tr>

      <!--[/section]-->

    </table>
    </div>


    Thanks all
    -SUNADMN
  • Posted: 18.07.2005, 01:39
     
    cannibus
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    20.09.08
    Posts:
    401
    what happens when u print $data?

    u use this:
    $data = $result->fields;

    but the example module uses something to this effect, not sure if thats the problem
    list($rid, $key, $value) = $result->fields;
  • Posted: 18.07.2005, 01:48
     
    sunadmn
    rank:
    Helper Helper
    registered:
     October 2004
    Status:
    offline
    last visit:
    01.03.06
    Posts:
    437
    this is due to the nature of the way PN builds it's list array in order to build in that fashion you would have to have 100% static content which I do not I have a 100% dynamic form elements can be added on the fly, but I did however solve this problem by doing this below:

    Code

    $items[] = array(
                           $data[1],
                           $data[2],
                           $data[3]);


    Then in my template I just referanced the assocative array element like this and that works:

    Code

    <td><!--[$pnChangeitems[pnChangeitems].1|pnvarprephtmldisplay]--></td>


    -SUNADMN
  • Posted: 18.07.2005, 01:58
     
    jediping
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    12.03.07
    Posts:
    387
    I dislike sections, because they make my head spin, but don't you need something after the pnChangeitems[pnChangeitems] in this bit of code below?

    Code

    <td><!--[$pnChangeitems[pnChangeitems]|pnvarprephtmldisplay]--></td>


    I mean, right now, it would just return Array, rather than the content of the data array. I think. Can you do this with a foreach loop? Those make looping so much easier!
  • Posted: 18.07.2005, 02:02
     
    sunadmn
    rank:
    Helper Helper
    registered:
     October 2004
    Status:
    offline
    last visit:
    01.03.06
    Posts:
    437
    Yeah the loop in my section takes care of going over the array and there is also a for loop in the API function to help clear things up a bit.


    -SUNADMN
  • Posted: 18.07.2005, 02:14
     
    jediping
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    12.03.07
    Posts:
    387
    Oh, sorry, I mean doing a foreach loop instead of the section loop. Smarty's foreach loop is similar to PHPs, and therefore a lot easier for me to grasp than the section loop.
  • Posted: 18.07.2005, 06:06
     
    csteelatgburg
    rank:
    Helper Helper
    registered:
     October 2002
    Status:
    offline
    last visit:
    23.01.07
    Posts:
    192
    I'm not 100% on this, but I'm pretty sure there's an error in your SQL statement:

    Code

    $sql = "SELECT $pnChangecolumn[rid],
                       $pnChangecolumn[key],
                       $pnChangecolumn[value]
                FROM $pnChangetable
                WHERE $pnChangecolumn[rid] = '$cm_id'"
    ;


    In my experience, this should read:

    Code

    $sql = "SELECT $pnChangecolumn[rid],
                       $pnChangecolumn[key],
                       $pnChangecolumn[value]
                FROM $pnChangetable
                WHERE $pnChangecolumn[rid] = '"
    . $cm_id . "'";


    This is because if you have a string between single quotes, PHP won't interpret the string.

    Also, it's good practice to run your variables through pnVarPrepForStore($variable) when inserting into the database. This will take care of any illegal strings.

    There might be something else in there that I didn't see, that's just the first thing that jumped out at me. I always have problems with my SQL statements, so they're the first place that I look.
  • Posted: 18.07.2005, 15:54
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723
    Keep in mind that in your template, this:


    ...shouldn't work...no brackets allowed!

    However, for your items array, (another brackets issue!) where you have $items[] ... try removing the brackets and using just $item and see what you come up with then?

    --
    Photography | PHP | Other
  • Posted: 18.07.2005, 15:57
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723

    csteelatgburg

    I'm not 100% on this, but I'm pretty sure there's an error in your SQL statement:

    Code

    $sql = "SELECT $pnChangecolumn[rid],
                       $pnChangecolumn[key],
                       $pnChangecolumn[value]
                FROM $pnChangetable
                WHERE $pnChangecolumn[rid] = '$cm_id'"
    ;


    This is because if you have a string between single quotes, PHP won't interpret the string.

    Actually, the above works out ok... Reason being that the entire string is encased in double-quotes. The single quotes are specifying the match string, rather than being concatenated in the string. All the same, csteelatgburg's method will work too.

    --
    Photography | PHP | Other
  • Posted: 18.07.2005, 19:42
     
    jediping
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    12.03.07
    Posts:
    387

    Quote

    Keep in mind that in your template, this:


    ...shouldn't work...no brackets allowed!


    Actually, for the section loops, brackets are allowed. I think it's the only place where they work.
  • Posted: 18.07.2005, 20:02
     
    alarconcepts
    rank:
    Professional Professional
    registered:
     April 2004
    Status:
    offline
    last visit:
    21.01.08
    Posts:
    2723
    Nice...thanks for the tip!

    --
    Photography | PHP | Other
  • Posted: 18.07.2005, 21:03
     
    jediping
    rank:
    Helper Helper
    registered:
     November 2004
    Status:
    offline
    last visit:
    12.03.07
    Posts:
    387
    The section loops are going out of style for me, because I can't quite graps them. I try to use a foreach loop when I can get away with it. Then I don't have to remember brackets and such. :)

Extensions Moderation

Main Menu

Extensions Database

Documentation

Development

Login

Donate to Zikula