Community @ The Turning Gate

Support community for TTG plugins and products.

NOTICE

The Turning Gate's Community has moved to a new home, at https://discourse.theturninggate.net.

This forum is now closed, and exists here as a read-only archive.

  • New user registrations are disabled.
  • Users cannot create new topics.
  • Users cannot reply to existing topics.

You are not logged in.

#1 2015-05-31 01:58:45

gengl
Member
From: Lexington, SC
Registered: 2013-11-24
Posts: 80
Website

Pulling images from a database

Is there a way to use phplugins to connect to an existing online database and dynamically populate a gallery with images from the database? I've been struggling with using the ttg_user_load hook to connect the database, and then the ttg_grid_top hook to populate the gallery, but am having no luck with it.

Offline

#2 2015-05-31 03:43:33

gengl
Member
From: Lexington, SC
Registered: 2013-11-24
Posts: 80
Website

Re: Pulling images from a database

Additional note - I'm attempting this with a TTG Stage page that is , except for header, navigation, and footer, a blank page with the block active and the grid disabled.

Offline

#3 2015-05-31 04:41:58

rod barbee
Moderator
From: Port Ludlow, WA USA
Registered: 2012-09-24
Posts: 17,830
Website

Re: Pulling images from a database

what sort of php code have you come up with so far?


Rod 
Just a user with way too much time on his hands.
www.rodbarbee.com
ttg-tips.com, Backlight 2/3 test site

Offline

#4 2015-05-31 04:57:56

gengl
Member
From: Lexington, SC
Registered: 2013-11-24
Posts: 80
Website

Re: Pulling images from a database

This works in an older website that is at http://65kahoks.com/missing.php - and I've tried to escape all the problem areas when applying it to phplugins - but not getting very far. This is the code to connect to the database and retrieve the data for the page - -

function ttg_head_end ( $style, $path ) {
    if (G_PATH == 'MISSING') {   
        echo '
   
             <?php require_once (../\'includes/config.inc.php\');
                     require_once (MYSQL);
                     ?>

                     <?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "\'" . $theValue . "\'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "\'" . doubleval($theValue) . "\'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "\'" . $theValue . "\'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_Names_Pics = 20;
$pageNum_Names_Pics = 0;
if (isset($_GET[\'pageNum_Names_Pics\'])) {
  $pageNum_Names_Pics = $_GET[\'pageNum_Names_Pics\'];
}
$startRow_Names_Pics = $pageNum_Names_Pics * $maxRows_Names_Pics;


$q = "SELECT DISTINCT classmates.lastName, classmates.firstName, classmates.middleName, classmates.marriedName, class_pic_small.pic_loc AS small_pic, class_pic_large.pic_loc, activities.activity FROM ((classmates INNER JOIN class_pic_small ON classmates.id = class_pic_small.classmates_id) INNER JOIN class_pic_large ON classmates.id = class_pic_large.classmates_id) INNER JOIN activities ON classmates.id = activities.classmates_id ORDER BY classmates.lastName, classmates.firstName, classmates.middleName";
$query_limit_Names_Pics = sprintf("%s LIMIT %d, %d", $q, $startRow_Names_Pics, $maxRows_Names_Pics);
$Names_Pics = mysqli_query($dbc, $query_limit_Names_Pics) or die(mysqli_error());
$row_Names_Pics = mysqli_fetch_assoc($Names_Pics);

if (isset($_GET[\'totalRows_Names_Pics\'])) {
  $totalRows_Names_Pics = $_GET[\'totalRows_Names_Pics\'];
} else {
  $all_Names_Pics = mysqli_query($dbc, $q);
  $totalRows_Names_Pics = mysqli_num_rows($all_Names_Pics);
}
$totalPages_Names_Pics = ceil($totalRows_Names_Pics/$maxRows_Names_Pics)-1;

$queryString_Names_Pics = "";
if (!empty($_SERVER[\'QUERY_STRING\'])) {
  $params = explode("&", $_SERVER[\'QUERY_STRING\']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Names_Pics") == false &&
        stristr($param, "totalRows_Names_Pics") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Names_Pics = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Names_Pics = sprintf("&totalRows_Names_Pics=%d%s", $totalRows_Names_Pics, $queryString_Names_Pics);
   
?>

        ';
        return false;

} // END

Last edited by gengl (2015-05-31 08:10:31)

Offline

#5 2015-05-31 05:10:33

rod barbee
Moderator
From: Port Ludlow, WA USA
Registered: 2012-09-24
Posts: 17,830
Website

Re: Pulling images from a database

<?php require_once (./\'includes/config.inc.php\');

is that file on your server in that location? (because TTG galleries don't have an "includes" folder)


Rod 
Just a user with way too much time on his hands.
www.rodbarbee.com
ttg-tips.com, Backlight 2/3 test site

Offline

#6 2015-05-31 05:15:06

gengl
Member
From: Lexington, SC
Registered: 2013-11-24
Posts: 80
Website

Re: Pulling images from a database

The code for writing to the page is below - but I'm getting no connection to the database evidently, and receiving some weird results on screen. Using the ttg_canvas_top hook was the only way I could get anything to appear on screen.


function ttg_canvas_top( $style, $path ) {
     if (G_PATH == 'MISSING') {       
        echo '

<p align="center">This page will display the missing Class of 1965 members. Press the close button above when you are finished. Click on a photo to view a larger image.</p>
   
    <?php do { ?>
  <div class="figure">
    <div class="photo">
      <a href="<?php echo $row_Names_Pics['pic_loc']; ?>" rel="lightbox[blum]" title="<?php echo $row_Names_Pics['firstName']; ?> <?php echo $row_Names_Pics['middleName']; ?> <?php echo $row_Names_Pics['lastName']; ?> <?php echo $row_Names_Pics['marriedName']; ?> <br />
<?php echo $row_Names_Pics['activity']; ?>"><img src="<?php echo $row_Names_Pics['small_pic']; ?>"></a>      </div>
    <p><?php echo $row_Names_Pics['firstName']; ?> <?php echo $row_Names_Pics['middleName']; ?> <?php echo $row_Names_Pics['lastName']; ?></p>
    </div>
  <?php } while ($row_Names_Pics = mysqli_fetch_assoc($Names_Pics)); ?>
  <div class="clear">  </div>
 

  ';
        return false;
} // END

Offline

#7 2015-05-31 05:16:48

gengl
Member
From: Lexington, SC
Registered: 2013-11-24
Posts: 80
Website

Re: Pulling images from a database

Yes, the includes folder is above my visible web domain to secure the connection details. (in a level above public_html)

Offline

#8 2015-05-31 06:22:48

rod barbee
Moderator
From: Port Ludlow, WA USA
Registered: 2012-09-24
Posts: 17,830
Website

Re: Pulling images from a database

./includes/config.inc.php

this is a relative url that will look for the path to the file starting in the folder that the page is already in. And while I'm certainly not an expert at this, I don't believe it can look outside the public_html folder anyway.
I'm thinking your includes/ folder should be in the root of the site. Then the path to it would be:

/includes/config.inc.php

And I don't see any code that connects to the database, unless that's in the config.inc.php file.

The code for writing to the page is below - but I'm getting no connection to the database evidently, and receiving some weird results on screen. Using the ttg_canvas_top hook was the only way I could get anything to appear on screen.

You should probably be hooking into ttg_block_top

code hooked into ttg_head_end won't appear on the page as it's in the html <head> tag.

the page code you're using also uses deprecated html. For example :  <p align="center"> is deprecated. Probably should be using <p style="text-align: center;"> instead

You've also got a number of classes in the page html code. Have you included those in your custom css file?

You know, and easier way of doing this might be to just use an i-frame to replace either the block or the grid content and just insert that entire page.

But if you want those images to appear in a TTG like gallery, you're going to have to do a lot of re-writing of the html.


Rod 
Just a user with way too much time on his hands.
www.rodbarbee.com
ttg-tips.com, Backlight 2/3 test site

Offline

#9 2015-06-01 20:35:37

gengl
Member
From: Lexington, SC
Registered: 2013-11-24
Posts: 80
Website

Re: Pulling images from a database

Okay - could not get anything to work using phplugins, but since this will not be a normal gallery and is hidden from autoindex, I tried modifying the index folder directly. I published a CE4 Stage gallery as a stand-alone named "MISSING".
To connect with the database, I placed my code directly after this part of index.php -  http://65kahoks.com/MISSING/


if (function_exists('ttg_user_load')) {
    $void = ttg_user_load( TTG_COMP, TTG_ROOT );
}

?>


<?php
ob_start();
require_once ('../../includes/config.inc.php');
require_once (MYSQL1);


$q = "SELECT DISTINCT classmates.lastName, classmates.firstName, classmates.middleName, classmates.marriedName, classmates.missing, classmates.deceased, class_pic_small.pic_loc AS small_pic, class_pic_large.pic_loc, activities.activity FROM ((classmates INNER JOIN class_pic_small ON classmates.id = class_pic_small.classmates_id) INNER JOIN class_pic_large ON classmates.id = class_pic_large.classmates_id) INNER JOIN activities ON classmates.id = activities.classmates_id WHERE classmates.missing=1 ORDER BY classmates.lastName, classmates.firstName, classmates.middleName";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
$row_rs_Missing = mysqli_fetch_assoc($r);
$totalRows_rs_Missing = mysqli_num_rows($r);

?>

That makes a good connection to my database, which is still located above the public_html folder on my server, and is thus outside of hacker's reach for security purposes.

I also added a couple of links to CSS stylesheets immediately before the <!DOCTYPE html> declaration. I had to rework the CSS to more closely resemble the ttg format.

To display the data I added the following code in the <div id="primary-content" class="grid_12 collapse clearfix"> area -

<p style="text-align: center;">We do not have current contact information on these classmates. If you know where or how we can contact them, please <a href="mailto:glen_england@65kahoks.com">email</a> us.</p>




    <?php do { ?>
        <div class="figure">
        <div class="photo">
          <a href="<?php echo $row_rs_Missing['pic_loc']; ?>" rel="lightbox[missing]" title="<?php echo $row_rs_Missing['firstName']; ?> <?php echo $row_rs_Missing['middleName']; ?> <?php echo $row_rs_Missing['lastName']; ?> <?php echo $row_rs_Missing['marriedName']; ?>"><img src="<?php echo $row_rs_Missing['small_pic']; ?>" width="105" height="145" /></a>
      </div>
      <!-- /photo -->
      <p><?php echo $row_rs_Missing['firstName']; ?> <?php echo $row_rs_Missing['middleName']; ?> <?php echo $row_rs_Missing['lastName']; ?> <?php echo $row_rs_Missing['marriedName']; ?></p>
      </div>
      <!-- /figure -->
      <?php } while ($row_rs_Missing = mysqli_fetch_assoc($r)); ?>


This successfully prints the images and title tags. Not sure yet how well it will translate to a tablet or phone...........

Last edited by gengl (2015-06-01 21:05:57)

Offline

#10 2015-06-01 21:41:15

rod barbee
Moderator
From: Port Ludlow, WA USA
Registered: 2012-09-24
Posts: 17,830
Website

Re: Pulling images from a database

So you got it to work?

I tried modifying the index folder directly.

be aware that if you ever re-export this particular Stage page (like when there are updates, and Matt regularly updates stuff) that your changes will be overwritten.

I also added a couple of links to CSS stylesheets immediately before the <!DOCTYPE html> declaration. I had to rework the CSS to more closely resemble the ttg format.

This may or may not work, but it's definitely the wrong place to put the links to the stylesheets. Those should follow the other stylesheets that get declared in the <head> tag. If using phplugins for this, you'd use the ttg_head_end hook.
(see Custom CSS)


Rod 
Just a user with way too much time on his hands.
www.rodbarbee.com
ttg-tips.com, Backlight 2/3 test site

Offline

#11 2015-06-02 00:01:45

gengl
Member
From: Lexington, SC
Registered: 2013-11-24
Posts: 80
Website

Re: Pulling images from a database

Okay, thanks Rod.

I'll revisit this thing after a bit. I guess the delimiting issue with using phplugins is just really giving me fits. I definitely want to get it figured out - for exactly the reasons you mention. Think I'll start back with moving the stylesheets via plugins and just take it a step at a time.

Offline

Board footer

Powered by FluxBB