Forum for discussing general topics related to Couch.
3 posts Page 1 of 1
I'm using the couch calendar, but I need to populate it with data from another table (fortunately in the same database). I got it to work, but it loads very slowly. I think it's because I connect and disconnect to the database for each day of the month. I thought I'd be smart and pull the first part of the code (to connect) out of the days loop, but I get an error. Also, since couch is already logging into the database, do I even need to do it here again?

This code works:
Code: Select all
<cms:calendar >
    <table class="calendar_big">
        <tr>
            <th colspan="7"><cms:date k_calendar_date format='F Y' /></th>
        </tr>
        <tr>
            <cms:repeat count='7'>
            <td class="months_heading"><cms:zebra 'Su' 'M' 'T' 'W' 'Th' 'F' 'S'/></td>
            </cms:repeat>
        </tr>

        <cms:weeks>
            <tr>
            <cms:days >
              <cms:if k_timeline_position='present'>
                   <cms:set tdclass='today' />
              <cms:else />
                   <cms:set tdclass='' />
              </cms:if>
              <cms:if k_position='current_month' >
                   <td class='<cms:show tdclass />' >
              <cms:else />
                   <td class='other_month'>
              </cms:if>
            
           <cms:show k_day />
            <br />
            <cms:php>
            $servername = 'mysql.wwwwwwww.com';
            $username = 'xxxxxxx';
            $password = 'yyyyyy';
            $dbname = 'zzzzzzz';

            $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
            if ($conn->connect_error) {
               die("Database csr_scheule Connection failed: " . $conn->connect_error);
            }
            $sql = 'SELECT * FROM csr_schedule WHERE idate = "<cms:show k_date />"';
            $result = $conn->query($sql);
            if ($result->num_rows > 0) {  // output data of each row
               while($row = $result->fetch_assoc()) {
                  echo $row["idesc"];
               }
            }
            $conn->close();
            </cms:php>
           </td>
            </cms:days>
            </tr>
        </cms:weeks>
    </table>
</cms:calendar>


But if I split my php/mysql code, I get this error:
Code: Select all
Fatal error: Call to a member function query() on a non-object in /home/uniguy/skintown.com/couch/tags.php(2866) : eval()'d code on line 3


Here is the code where I get the error:
Code: Select all
<cms:calendar >

   <cms:php>
      $servername = 'mysql.wwwwwwww.com';
            $username = 'xxxxxxx';
            $password = 'yyyyyy';
            $dbname = 'zzzzzzz';

      $conn = new mysqli($servername, $username, $password, $dbname);
      // Check connection
      if ($conn->connect_error) {
         die("Database csr_scheule Connection failed: " . $conn->connect_error);
      }
   </cms:php>
    <table class="calendar_big">
        <tr>
            <th colspan="7"><cms:date k_calendar_date format='F Y' /></th>
        </tr>
        <tr>
            <cms:repeat count='7'>
            <td class="months_heading"><cms:zebra 'Su' 'M' 'T' 'W' 'Th' 'F' 'S'/></td>
            </cms:repeat>
        </tr>

        <cms:weeks>
            <tr>
            <cms:days >
              <cms:if k_timeline_position='present'>
                   <cms:set tdclass='today' />
              <cms:else />
                   <cms:set tdclass='' />
              </cms:if>
              <cms:if k_position='current_month' >
                   <td class='<cms:show tdclass />' >
              <cms:else />
                   <td class='other_month'>
              </cms:if>
            
           <cms:show k_day />
            <br />
            <cms:php>
            $sql = 'SELECT * FROM csr_schedule WHERE idate = "<cms:show k_date />"';
            $result = $conn->query($sql);
            if ($result->num_rows > 0) {  // output data of each row
               while($row = $result->fetch_assoc()) {
                  echo $row["idesc"];
               }
            }
            </cms:php>
           </td>
            </cms:days>
            </tr>
        </cms:weeks>
    </table>
   <cms:php>
      $conn->close();
   </cms:php>
</cms:calendar>
Hi,

The problem is that PHP variables within <cms:php> block are in local scope by default (like in PHP functions).

To use the $conn object created within one block in another block(s), we need to declare it as 'global'.

I think the following amended code (in three blocks) should work -
Code: Select all
<cms:php>
  global $conn;
  $servername = 'mysql.wwwwwwww.com';
        $username = 'xxxxxxx';
        $password = 'yyyyyy';
        $dbname = 'zzzzzzz';

  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn->connect_error) {
     die("Database csr_scheule Connection failed: " . $conn->connect_error);
  }
</cms:php>
>

Code: Select all
<cms:php>
global $conn;
$sql = 'SELECT * FROM csr_schedule WHERE idate = "<cms:show k_date />"';
$result = $conn->query($sql);
if ($result->num_rows > 0) {  // output data of each row
   while($row = $result->fetch_assoc()) {
      echo $row["idesc"];
   }
}
</cms:php>

Code: Select all
<cms:php>
  global $conn;
  $conn->close();
</cms:php>

Hope it helps.
Perfect! That works great. Now on to a related problem regarding integrating php into couch. On the page I defined above, I display a calendar containing the data from a table that's maintained by another system. I just got a copy of the php page that does the editing and I'd like to make it available to administrators. I put it in my site and put a link to it on the above page. The entire page is just one big chunk of php code, so I replaced the opening and closing php tags as follows:
Code: Select all
<?php require_once( '../couch/cms.php' ); ?>
<cms:php>
// My php code including Mysql Ajax Table Editor
...
...
...
</cms:php>
<?php COUCH::invoke(); ?>


This unfortuately messes something up in the page and I get a JSON error and my returned data is scrambled with weird formatting:
Image.jpg
Image.jpg (151.23 KiB) Viewed 7945 times

The page works fine if I just use the <? and ?> wrapping around it without cms. There may be some incompatibility between couch and the Ajax Table Editor?
3 posts Page 1 of 1