We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18367
    • 834 Posts
    Hopefully, someone will chime in here with a working example of what that chunk should look like. 
    Since I haven't done it, I don't want to guess and get you off on the wrong foot.


    I sincerely hope so ’cos I"m lost already.
      Content Creator and Copywriter
      • 3749
      • 24,544 Posts
      It’s easier than it sounds.

      Here are the basic steps:

      1. Create the document that you want to display the data on.

      2. In that document, put a call to your snippet (created in step 3) where you want the data to show up. Your snippet call will look like this:

      [!MyDataDisplaySnippet!]

      3. In the manager, selecte Resources | Manage Resources - select the snippet tab and click on new snippet.
      Name the snippet MyDatDisplaySnippet
      Paste OpenGeek’s snippet code above into the snippet.
      Edit the name of the data table and change RowChunkName to MyRowChunk
      Save it.

      4. Click on the Chunk tab and select New Chunk.
      Call it MyRowChunk
      For Now, just put this line in it: <p>My Row Chunk did this.</p>. Later, you can add the html to display the row.
      Save it.

      5. When you visit the page, you should get as many rows as there are in the DB table (although you won’t see any data yet).

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 4310
        • 2,310 Posts
        Sorry BobRay but
        $modx->placeholders= array_merge($modx->placeholders, $row);

        causes this error
        Error: 	array_merge() [function.array-merge]: Argument #1 is not an array

        any thoughts?
        EDIT I’ve found an error in the DB that I’m going to look at. I may have been too hasty!
          • 4310
          • 2,310 Posts
          The data is fine but I’ve been renaming columns whilst developing an import via csv module and haven’t updated the query to reflect the new column names.
          I’ll have another look tomorrow.
            • 4310
            • 2,310 Posts
            Okay I’m using a standard unmodified table in the DB.
            My chunk "test" is :
            <table><tr><td>[+id+]</td><td> </td><td>[+category+]</td></tr></table>

            and the snippet is :
            <?php
            $output= '';
            $rowChunkName= 'test';
            $sql= 'SELECT * FROM `modx_categories`';
            $rs= $modx->db->query($sql);
            if ($rs) {
                foreach ($modx->db->getRow($rs) as $row) {
                    $modx->placeholders= array_merge($modx->placeholders, $row);
                    $output.= $modx->parseChunk($rowChunkName, $modx->placeholders, '[+', '+]');
                }
            }
            return $output;
            ?>

            The error message is :
            « PHP Parse Error »
             
            PHP error debug
              Error: 	array_merge() [function.array-merge]: Argument #1 is not an array	 
              Error type/ Nr.: 	Warning - 2	 
              File: 	C:\wamp\www\dev\manager\includes\document.parser.class.inc.php(769) : eval()'d code	 
              Line: 	8

            I can’t see what’s wrong!?
            (Running PHP 5.2.5 as part of a Wampserver 2.0 xp set up)
              • 18367
              • 834 Posts
              OK, I’ve put my own php script in a new snippet and I get this error

              Parse error: parse error, unexpected ’/’ in C:\wamp\www\modx-0961p2\manager\includes\document.parser.class.inc.php(769) : eval()’d code on line 2

              The script is quite long so I won’t post it here unless you really want it.
                Content Creator and Copywriter
                • 18367
                • 834 Posts
                OK,

                I keep answering my own questions here, and that’s not because of any extra smarts on my part, quite the opposite.

                I keep stumbling and changing things till I get something to work.

                Anyway, my parse error was caused by not having the full path to my connection file.

                IE: Part of my new snippet read:
                <?php
                require_once(’Connections/dbConnect.php’); ?>

                When I changed it to

                <?php
                require_once(’C:\wamp\www\modx-0961p2\assets\templates\acwa/Connections/dbConnect.php’); ?>

                My database renders beautifully on my page, and I’m very happy.

                As we say down here, "It’s more arse than class" ’cos I’m a total php noob.

                I’d write a tute on this, except I’m not entirely sure what I’ve done, only that I’ve got it to work.

                Still if anyone has any questions on how I did it, I’ll try and answer them.

                PS I wasn’t using the script pasted above, but one from another source.



                  Content Creator and Copywriter
                  • 22303 MODX Staff
                  • 10,725 Posts
                  The MODX_BASE_BATH constant is your friend. You can use it to make portable code, i.e.
                  <?php
                  require_once(MODX_BASE_PATH . 'assets/templates/or/wherever/you/put/Connections/dbConnect.php');
                  ?>

                  base_path is also available as a config variable, i.e.
                  <?php
                  require_once($modx->config['base_path'] . 'assets/templates/or/wherever/you/put/Connections/dbConnect.php');
                  ?>

                  is an alternative approach that will have more meaning in future versions, as this can be configured differently for each "context". But that’s is way TMI for now.

                  I suggest taking a look at other snippet source code to see examples of how they are coded. It will help immensely.
                    • 10076
                    • 1,024 Posts

                    PS I wasn’t using the script pasted above, but one from another source.

                    Could you post the script etc you áre using? I might want to use that,

                    thanks, Frank.
                      • 18367
                      • 834 Posts
                      Frank,

                      I’ll post you my script if you tell me how to style wrappers in Wayfinder. I know you’ve messed around with WF.

                      See my post here:

                      http://modxcms.com/forums/index.php/topic,25887.0.html

                      Anyway, here’s the snippet. If I recall I got the original php from a tutorial for Dreamweaver. There are more notes at the bottom of this post which you’ll need to read.


                      <?php
                      require_once(’C:\wamp\www\modx-0961p2\assets\templates\acwa/Connections/dbConnect.php’); ?>
                      <?php
                      mysql_select_db($database_dbConnect, $dbConnect);
                      $query_List_2 = "SELECT * FROM brood___seed_stock_suppliers";
                      $List_2 = mysql_query($query_List_2, $dbConnect) or die(mysql_error());
                      $row_List_2 = mysql_fetch_assoc($List_2);
                      $totalRows_List_2 = mysql_num_rows($List_2);
                      ?>

                      <table border="0" align="center">
                      <tr>

                      <td>Company Name</td>
                      <td>Contact Name</td>
                      <td>Phone</td>
                      <td>Fax</td>
                      <td>Mobile</td>
                      <td>Email</td>
                      <td>Business Address</td>
                      <td>Postal Address</td>
                      <td>Postcode</td>
                      <td>State</td>
                      <td>Town City</td>
                      <td>Website</td>
                      <td>Country</td>
                      <td>species</td>
                      <td>Availability</td>
                      <td>Species Common Name</td>
                      <td>Activate</td>
                      <td>Priority</td>
                      <td>logo</td>
                      </tr>
                      <?php do { ?>
                      <tr>

                      <td><?php echo $row_List_2[’Company Name’]; ?></td>
                      <td><?php echo $row_List_2[’Contact Name’]; ?></td>
                      <td><?php echo $row_List_2[’Phone’]; ?></td>
                      <td><?php echo $row_List_2[’Fax’]; ?></td>
                      <td><?php echo $row_List_2[’Mobile’]; ?></td>
                      <td><?php echo $row_List_2[’Email’]; ?></td>
                      <td><?php echo $row_List_2[’Business Address’]; ?></td>
                      <td><?php echo $row_List_2[’Postal Address’]; ?></td>
                      <td><?php echo $row_List_2[’Postcode’]; ?></td>
                      <td><?php echo $row_List_2[’State’]; ?></td>
                      <td><?php echo $row_List_2[’Town City’]; ?></td>
                      <td><?php echo $row_List_2[’Website’]; ?></td>
                      <td><?php echo $row_List_2[’Country’]; ?></td>
                      <td><?php echo $row_List_2[’species’]; ?></td>
                      <td><?php echo $row_List_2[’Availability’]; ?></td>
                      <td><?php echo $row_List_2[’Species Common Name’]; ?></td>
                      <td><?php echo $row_List_2[’Activate’]; ?></td>
                      <td><?php echo $row_List_2[’Priority’]; ?></td>
                      <td><?php echo $row_List_2[’logo’]; ?></td>
                      </tr>
                      <?php } while ($row_List_2 = mysql_fetch_assoc($List_2)); ?>
                      </table>
                      <?php
                      mysql_free_result($List_2);
                      ?>
                      ?>




                      Some notes:

                      For some reason MODx always puts in an extra ?> Don’t know why, but it doesn’t seem to affect the result.

                      Line 2 (in red) obviously refers to my local server and another php file required to make it all work. Put yours where you see fit. Also see Open Geeks comments above for a more elegant writing of this line.

                      The other file is attached. I put it in this folder /Connections/dbConnect.php cos that’s what was written in the original php and I’m way too inexperienced to want to change too many things.

                      Line 5 is obviously my database, insert yours here.

                      The html and table tags could obviously be done better. I’m not even sure if they should be in the snippet, but I’m still playing with MODx (This is day 4) and I’m happy just to have something working.

                      The names of the table columns you will have to change to match your own dbase columns names of course.

                      Snippet call is simply

                      [!hatcheries!]

                      cos it’s a list of fish hatcheries.

                      The end result is simple display of what’s in the db.

                      See the screenshot I posted earlier for how it looks. Sorry can’t post a link as it’s on my desktop server.

                      Obviously I want to manipulate the final display further to make it more "sexy" but for now this will do.

                      If anyone wants to take it and run with it, please do but post your results back here.

                      Hope this helps

                      Ahh Poop, I can’t upload php files thru this forum. (Duh)


                      Here’s the other dbConnect.php file

                      <?php
                      # FileName="Connection_php_mysql.htm"
                      # Type="MYSQL"
                      # HTTP="true"
                      $hostname_dbConnect = "localhost";
                      $database_dbConnect = "hatcheries";
                      $username_dbConnect = "root";
                      $password_dbConnect = "";
                      $dbConnect = mysql_pconnect($hostname_dbConnect, $username_dbConnect, $password_dbConnect) or trigger_error(mysql_error(),E_USER_ERROR);
                      ?>


                      Wow, this is more like a tute than a post.






                        Content Creator and Copywriter