We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34178
    • 212 Posts
    I have a database "workshops" containing a list of Workshop-Events (inclusive the zip-codes of the workshops-adresses). I was going to fill-up this database with values of a second database "allzips" which contains values of latitude and longitude for every zip-code. Unfortunately the following code doesn´t work. Does anybody know what I did wrong or is there maybe a better way to realize this challenge?

    <?php
    $datetoday = date("Y-m-d");
    
    $path = $modx->getOption('core_path') . 'components/technique/';
    $result = $modx->addPackage('technique',$path . 'model/','tec_');
    
    $dbworkshop = $modx->getCollection('workshops', array('date:>=' => $datetoday)); // To get all workshops which will take place in the future
    
    foreach($dbworkshop as $single) {
    $zip = $single->get('zip'); // read the zip-code of every object of the workshop-databese
      
    $db2 = $modx->getObject('allzips', array('plz' => $zip)); // get the object for the certain zip-code
    $latitude = $db2->get('latitude'); //get the latitude for the zip-code
    $longitude = $db2->get('longitude');  //get the longitude for the zip-code
    
    $single->set('latitude',$latitude); // set the latitude
    $single->set('longitude',$longitude); // set the longitude
    } //end of foreach
    
    $dbworkshop->save(); //save the workshope-database now containing longitude and latitude for each zip-code
    
    return;
    [ed. note: lettis last edited this post 14 years, 11 months ago.]
      • 31946
      • 116 Posts
      Hey!
      Your variables $dbloadgeodaten and $einzeldaten are never initialized or set?

      Try this.


      foreach($dbworkshop as $single) {
      $zip = $single->get('zip'); // read the zip-code of every object of the workshop-databese
         
      $db2 = $modx->getObject('allzips', array('plz' => $zip)); // get the object for the certain zip-code
      if(is_object($db2) && ($db2)){
        $latitude = $db2->get('latitude'); //get the latitude for the zip-code
        $longitude = $db2->get('longitude');  //get the longitude for the zip-code
        $single->set('latitude',$latitude); // set the latitude
        $single->set('longitude',$longitude); // set the longitude
        $single->save();
      }
      } //end of foreach
        • 34178
        • 212 Posts
        Thanks a lot for this first help. You are completely right. The two variables have not been named correctly.

        Unfortunately the two variables ($longitude and $latitude) are still not added to the database "workshops". I tried to add $dbworkshop->save(); after the the foreach-circle and also to not add this save-tag. But both doesn`t work.

        Do you have any idea how to save the two variables to every row of the workshop-database?

        Maybe it is better to use normal php-code instead of modx-code for this purpose? Or is this challenge also posible with xpdo?
          • 1778
          • 659 Posts
          Not sure of that, but I remember I've read sowewhere something sounding like your problem...
          Did you try to add the PREFIX of the 'target' table in the 'set' action something like :
          $single->set('mytableprefix_latitude', $latitude);
          


          Hope this helps a bit
          Cheers
            • 34178
            • 212 Posts
            Thanks anso. I will try it. Do you think I have to add after each set-tag:

            $single->save();

            or after foreach has ended:

            $dbworkshop->save();
              • 34178
              • 212 Posts
              Unfortunately I does not work. I tried the tablename with and without the prefix in the modx-database:

              $single->set('mymodx_db2_latitude', $latitude);

              and also:

              $single->set('db2_latitude', $latitude);

              But both doesn´t work.

              Does anybody have an idea what else I could try?
                • 16345
                • 1 Posts
                Have you defined a correct shema for both tables?

                You should first try to get/set some sample content on both tables?
                  • 34178
                  • 212 Posts
                  I updated the schema right before running the script. I also checked the values by adding

                  echo $latitude . $longitude;


                  in the foreach-loop which resulted in the correct values suggesting the schema is correct.

                  But there was something which made me wonder: When adding print_r($single) in the foreach-loop I got a really big output which looked like the complete content of our modx-database. Is this output correct or is this maybe also an error?

                  You should first try to get/set some sample content on both tables?

                  Ok I changed the first set-tag into :

                  $single->set('zip_db2_latitude','1');


                  But it didn´t work. Unfortunately there was nothing added to the table.
                    • 18373 ☆ A M B ☆
                    • 3,141 Posts
                    Can you paste your schemas? If you are getting the right values from $db2->get('latitude'), but $single->set('latitude',$latitude) doesn't add anything, I suppose the problem is in there. Either you're setting it on a wrong column or there may be an error in there.

                    You will need to call the save method on the object you are changing. In this case you are changing $single, so you will need to call $single->save(). You would put that in your for each.

                    $dbworkshop->save(); //save the workshope-database now containing longitude and latitude for each zip-code

                    That's wrong. $dbworkshop is an array of objects, not an object itself. It doesn't have any methods you can use. So get rid of that.

                    An object (in your case $single) represents a row in the database table, when you use $single->set(fieldname,value) you are only working with the object but changes have not yet been made in the underlying database. You will need to call save() on it to change the database.
                      Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                      Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                      • 31946
                      • 116 Posts
                      Quote from: lettis at Oct 28, 2011, 07:59 AM

                      But there was something which made me wonder: When adding print_r($single) in the foreach-loop I got a really big output which looked like the complete content of our modx-database. Is this output correct or is this maybe also an error?

                      That happens because $single is an object. If you want to get the columns and their values,
                      print it out with the toArray method like this:

                      print_r($single->toArray());