We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25586
    • 105 Posts
    Hi everyone,

    I followed the "Using Custom Database Tables in your 3rd Party Components" tutorials which is working great! I can add a Project in the database by running the snippit.

    My snippit looks like this

    $projects = $modx->newObject('Project');
    $projects->fromArray(array(
        'name' => 'myTestProjectName',
    ));
    $projects->save();


    Now I have a problem. How do I get a form to submit a new Project?

    Also: On an other page I’m outputting a table with projects. But everything is processed by a snippit including the html output. How can I use a chunk tot do this?

    When everything is working I will share the snippit with you so everyone can manage projects with MODx smiley

    Thanks,

    Rogier
      • 25586
      • 105 Posts
      Oke I made some progress with my snippit!

      I figured out I can get a chunk to display with the code:
      $projects = $modx->getCollection('Project');
      
      foreach ($projects as $project) {
      	if ($i % 2 != 0)
          $rowTpl = $modx->getChunk($tplRow);  
        else
          $rowTpl = $modx->getChunk($tplRow);
      	echo $rowTpl;
      }


      But how do I get the ’name’ from the Projects in the [[+name]] placeholder of my chunk?

      Thanks!

      EDIT:

      I got it to work!!! With the following code.

      $projects = $modx->getCollection('Project');
      foreach ($projects as $project) {
      	$name = $project->get('name');
          $rowTpl = $modx->getChunk($tplRow,array(
      	'name' => $name,   
      ));


      Is this the correct way to handle SQL data?

      The only thing left for now is the form problem. Damn I’m proud of myself as I have never worked with PHP before xD
        • 2611
        • 394 Posts
        Your second snippet seems okay. Why not enter the get(’name’) directly instead
        of assigning it to a variable first? Plus there is 1 left over comma after the
        ’name’ value.


        $projects = $modx->getCollection('Project');
        foreach ($projects as $project) {
                $rowTpl = $modx->getChunk($tplRow, array('name' => $project->get('name')));
        }


        Also if you would want more data in the chunk (the whole project for instance)
        you could use toArray to assign all keys as placeholders.

        $projects = $modx->getCollection('Project');
        foreach ($projects as $project) {
                $rowTpl = $modx->getChunk($tplRow, $project->toArray());
        }
          Follow me on twitter: @b03tz
          Follow SCHERP Ontwikkeling on twitter: @scherpontwikkel
          CodeMaster
          • 25586
          • 105 Posts
          Thanks b03tz for the help! The toArray code cleaned my code quite a bit!

          Do you know how to get the <input id="name"> "name" into the database on form submitting? Have been playing with it the whole day but can’t seem to figure it out sad

          I know I have to work with:
          $projects = $modx->newObject('Project');
          $projects->fromArray(array(
              'name' => $name,
          ));
          $projects->save();


          I can use the toArray function here aswell but for now I will go one step at the time ^^ handy little trick smiley

          Any help will be appreciated,

          Rogier
            • 25586
            • 105 Posts
            Ahhh I got it working! Don’t know if it’s the proper way to do so, maby some tips?

            This is what I got:

            $newProjectTpl = $modx->getChunk($newProjectTpl);  
            
            $name = $_POST["name"];
            
            if (empty($name)) {
                echo $newProjectTpl;
            } else {
            $projects = $modx->newObject('Project');
            $projects->fromArray(array(
                'name' => $name
            ));
            $projects->save();
            echo 'Done!';
            }


            Any tips? Ahhh I’m so happy it’s working! xD

            Rogier
              • 3749
              • 24,544 Posts
              Glad you got it working. Have you considered using Formit with a version of your snippet as a hook? That would give you some nice validation options.
                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
                • 25586
                • 105 Posts
                Quote from: BobRay at May 17, 2010, 05:52 PM

                Glad you got it working. Have you considered using Formit with a version of your snippet as a hook? That would give you some nice validation options.

                Perhaps thats a nice and easy way to validate and email the just created project, I’ll look in to that.