We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32337
    • 28 Posts
    There’s no documentation for getRow, getRecordCount, or makeArray. I want to query a table and if it returns anything, increment a value, otherwise create an entry. I thought I had it working earlier but discovered that it always created a new entry, so clearly I don’t know what I’m doing.

    Please, please, please, please someone just show me how to do this. I’m all for figuring things out for myself, but I can’t devote days to trial and error and I’m too clueless about PHP and MySQL in general to know what I should be doing.

    In steps, how do I:

    1) when user submits form check a specified table for a row where the values match those submitted by the user.

    2) if a matching row exists, add 1 to a field called ct

    3) if it doesn’t exist, create it and add a field called ct with a value of 1

    #3 is the only one I can get to work.

    Please, how do I do the rest of it?

    I’ve tried almost everything, looked everywhere, and still haven’t a clue. Below is what I thought was the solution. It wasn’t.
      • 32337
      • 28 Posts
      Trying to convert a portion of a program written by someone else to use the MODx API without any previous PHP experience is frustrating, to say the least, but for a while I thought I had it. However, what this does is to always add a new row.

      <?php
      function ecards(&$fields) 
      	{
      		global $modx;
      		$fields['card'] = $_SESSION['card'];
      		$check = array();
      		$check = $modx->db->getValue( $modx->db->select("count(*) as ct", "modx_senders", "'firstname = $fields[firstName]' && 'lastname = $fields[lastName]' && 'email = $fields[email]'"));
      		if($check) {
      			$modx->db->update( "ct = (ct+1)", "modx_senders", "'firstname = $fields[firstName]' && 'lastname = $fields[lastName]' && 'email = $fields[email]'");
      		} else {
      			$senders = array();
      			$senders[firstname] = $fields[firstName];
      			$senders[lastname] = $fields[lastName];
      			$senders[email] = $fields[email];
      			$senders[ct] = '1';
      			$dbQuery = $modx->db->insert($senders, 'modx_senders');
      		}
      	}
      ?>
        • 32337
        • 28 Posts
        OK. After far more time than I care to say, I’ve managed to do something that really does work. What I’d still very much like to know is, is this the "modx way" to do what I’m doing, or is there a more compact approach?

        <?php
        function ecards(&$fields) 
        	{
        		global $modx;
        		$check = $modx->db->query("SELECT count(*) as ct FROM modx_senders WHERE firstname = '$fields[firstName]' AND lastname = '$fields[lastName]' AND email = '$fields[email]'");
        		if ($r = $modx->db->makeArray($check)) {
        			if ($r[0]['ct']) {
        				// update the count for this user
        				$sql = "UPDATE modx_senders SET ct = (ct+1) WHERE firstname = '$fields[firstName]' AND lastname = '$fields[lastName]' AND email = '$fields[email]'";
        			} else {
        				// insert a new record, and set the count to 1
        				$sql = "INSERT INTO modx_senders SET firstname = '$fields[firstName]',lastname = '$fields[lastName]',email = '$fields[email]',ct = 1";
        			}
        			$modx->db->query($sql);
        		}
        	}
        ?>
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          If it works, it’s the MODx way!

          You could use $modx->db->select(), but the parameter list is a bit arcane, and it all boils down to just being a wrapper for PHP’s mysql_query() anyway, so it doesn’t make that much difference.

          To be honest, I often revert to using bare PHP mysql_whatever() function calls just to avoid the extra overhead of the MODx wrapper functions. They are primarily to make it trivial to add support for other database engines by creating a new dbapi file, but usually I’m not overly concerned about it; at least not for my own custom usage.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org