We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10226
    • 412 Posts
    I thought I was getting somewhere when I hit another wall. I have my query in a snippet and I need to filter the results by the document id. So I added a WHERE clause to my query. I tried it with manually setting the doc id in the query so I could see if my statement works, and it did.

    Next I use the snippet in the template (the template is made of chunks). It shows me the doc id by way of echo $thispage; and it tells me the doc id, but does not pull in the data, tried with every combo of cached / uncached and the result is the same, shows me the doc id, and the data matches for doc id, but it does not show.

    So I am thinking that I need some fancy syntax I have yet to learn?

    <?php
    $id = $modx->documentIdentifier;
    echo $id; //just for debugging
    $result = $modx->db->query( 'SELECT...
    // long nasty query 
    ...WHERE
      (problem.is_here = "$id")' );
    
    while($row = $modx->db->getRow( $result )) {
    // presenting data...
    }
    ?>


    No idea..

    Thanks!
      • 10226
      • 412 Posts
      I think this is confusing everyone. Simply here is what I need.

      how would I implement the current document id into a query on the modx databse?

      If someone can show me that, I can go from there.

      Thanks
        • 3749
        • 24,544 Posts
        I think we need to see the whole query. smiley
          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
          • 10226
          • 412 Posts
          ok well here is the snippet. it is fully working outside of mod using php - of course I have modx-ified it to my best abilities.

          I moved the data into the modx db just to make it that much easier.

          well, this is the more simple query, but not much different from the other.

          <?php
          // do we know where we are? yes
          $id = $modx->documentIdentifier;
          echo $modx->documentIdentifier;
          // do it
          $result = $modx->db->query( 'SELECT * FROM animals left join species on animals.species = species.species_idx WHERE
          //  (animals.animal_id = "28")' ); this works when I tell it the id - the same id that $id displays
          (animals.animal_id = "$id")' );
          // that gives me no result, no error
          echo "<table border='1'>";
          echo "<tr> <th>id</th> <th>name</th> <th>species</th> <th>bin</th> </tr>";
          while($row = $modx->db->getRow( $result )) {
          	echo "<tr><td>"; 
          	echo $row['animal_id'];
          	echo "</td><td>"; 
          	echo $row['aname'];
          	echo "</td><td>";
          	echo $row['species'];
          	echo "<br/>";
          	echo $row['subspecies'];
          	echo "</td><td>"; 
          	echo $row['bin'];
          	echo "</td></tr>"; 
          } 
          
          echo "</table>";
          ?>


          I tried with diff combos of ` ’ and " probably missed the right one, or something odd is happening.

          Thanks!
            • 20413
            • 2,877 Posts
            Try this:
            $id = $modx->documentObject['id'];


            I’m no programmer! I’m a rookie programmer! tongue
              @hawproductions | http://mrhaw.com/

              Infograph: MODX Advanced Install in 7 steps:
              http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

              Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
              http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
              • 10226
              • 412 Posts
              Quote from: mrhaw at Aug 09, 2009, 06:56 PM


              $id = $modx->documentObject['id'];


              That "works" too, either way I always get the doc id, but I just got curious and echoed the $result and I get the good ole
              Resource id #17
              I ran into that error on a few other posts, but none of them helped me much.

              Is there any way to print the query it is actually sending? That might shed some light. I tried logging on mysql but I got nothing, then I installed mytop and it broke my perl install, so I had to clean up that mess lol.

              Hope I can get this sorted out, I don’t really want to do this with javascript!
                • 5340
                • 1,624 Posts
                try this

                $id = $modx->documentIdentifier;
                $sql= 'SELECT * FROM animals left join species on animals.species = species.species_idx WHERE animals.animal_id =' . $id;


                  • 10226
                  • 412 Posts
                  That works!
                  but I have ()’s in my other query...

                  <?php
                  $sql= 'SELECT 
                    master_ev.ev_date,
                    animals.aname,
                    species.species,
                    species.subspecies,
                    events_ref.ev_name,
                    master_ev.events_idx,
                    notes.note,
                    weight.weight,
                    foodinv.food_type
                  FROM
                   master_ev
                   INNER JOIN animals ON (master_ev.animal_id=animals.animal_id)
                   INNER JOIN species ON (animals.species=species.species_idx)
                   INNER JOIN events_ref ON (master_ev.events_idx=events_ref.event_id)
                   LEFT OUTER JOIN notes ON (master_ev.ev_id=notes.ev_id)
                   LEFT OUTER JOIN weight ON (master_ev.ev_id=weight.ev_id)
                   LEFT OUTER JOIN length ON (master_ev.ev_id=length.ev_id)
                   LEFT OUTER JOIN feedings ON (master_ev.ev_id=feedings.ev_id)
                   LEFT OUTER JOIN foodinv ON (feedings.perm_id=foodinv.perm_id)
                  WHERE
                    (animals.animal_id =' . $id')';
                  
                  // unexpected T_CONSTANT_ENCAPSED_STRING
                  $result = $modx->db->query($sql);
                  echo "<table border='1'>";
                  echo "<tr> <th>date</th> <th>animal</th> <th>species</th> <th>event</th> </tr>";
                  
                  while($row = $modx->db->getRow( $result )) {
                  	echo "<tr><td>"; 
                  	echo $row['ev_date'];
                  	echo "</td><td>"; 
                  	echo $row['aname'];
                  	echo "</td><td>";
                  	echo $row['species'];
                  	echo "</td><td>";
                  		switch ( $row['events_idx'] ) {
                  			case 8:
                  				echo $row['note'];
                  				break;
                  			case 5:
                  				echo "weight ",$row['weight'],"oz";
                  				break;
                  			case 6:
                  				echo "length ",$row['length'],"in";
                  				break;
                  			case 1:
                  				echo $row['food_type'];		
                  				break;
                  			default:
                  				echo $row['ev_name'];		
                  		}	
                  	echo "</td></tr>"; 
                  } 
                  
                  echo "</table>";
                  ?>


                  Sorry! I’m really bad with the ticks, back-ticks and quotes lol
                    • 5340
                    • 1,624 Posts
                    change

                    (animals.animal_id =’ . $id’)’;

                    to

                    (animals.animal_id =’ . $id . ’)’;
                      • 10226
                      • 412 Posts
                      SUPER.. wait for it...

                      ...AWESOME laugh

                      I had actually tried that, but then after you suggested it, I realized I had commented out the $id = $modx->documentIdentifier; on that query.

                      Thanks smiley

                      By chance, can you explain why putting the query into an array works, while my original method does not?