Community & Support

Traditional mySQL tables and queries in Drupal

My company is currently migrating it's system from standard HTML/PHP/mySQL to Drupal. One page that I can't figure out how to move is a page that pulls future and past content titles from a table, checks the list against the current week (arbitrarily defined) and outputs the result. However, I'm not sure how to re-implement this in Drupal. Should I use standard mySQL queries, or the Drupal functions? The table and titles will need to be manually generated, not updated through the Drupal system, so I don't know if the Drupal functions can select tables other than the tables Drupal creates.

To make it more clear what I'm doing, here's the code we were using before:

<?php
$weekNumber
= 3;

// connect to DB and grab titles.
   
$link = mysql_connect(/* connection information snipped out */)
        or
trigger_error('Could not connect: ' . mysql_error());
   
mysql_select_db('DBname') or trigger_error('Could not select database');
   
// Pull titles from DB.
   
$titleSQL="SELECT id,talkTitle FROM interim_site_content ORDER BY id";
   
$titleQuery = mysql_query($titleSQL) or trigger_error('Query failed: ' . mysql_error().'<br>'.$titleSQL);
   
mysql_close($link);

// Go through titles and create two arrays.  One of upcoming, one of previous.
$previous = array();
$next = array();
while (
$titleResult = mysql_fetch_array($titleQuery))
    {
       
// If the excerpt is from a past week, add to previous
       
if($titleResult['id']<$weekNumber&&$titleResult['id']>=$weekNumber-6)
        {
           
array_push($previous,$titleResult['talkTitle']);
        }
       
// If the distinction is from a future week, add to next
       
if($titleResult['id']>$weekNumber&&$titleResult['id']<=$weekNumber+6)
        {
           
array_push($next,$titleResult['talkTitle']);       
        }
    }
// If there are no previous, provide appropriate statement   
if(empty($previous))
{
   
array_push($previous,'No Prior Talks');
}

$previous = array_reverse($previous);

// Content begins here

$content = '<p class="pageTitle">
        Upcoming Talks
        &nbsp;<br>
        &nbsp;<br>
        &nbsp;<br>
    </p>
   
        <p class="pageContent" style="margin-bottom:20px;">
            Upcoming
        </p>
        '
;
// Add all upcoming talks
foreach ($next as $talkTitle)
{
   
$content.='<p class="titleList">'.$talkTitle.'</p>';
}

$content.=    '&nbsp;<br>
            </p>

        <p class="pageContent" style="margin-bottom:20px;">
                Previous Weeks
        </p>

            '
;
// Add all previous talks
foreach ($previous as $talkTitle)
{
   
$content.='<p class="titleList">'.$talkTitle.'</p>';
}
print
$content;
?>

Could someone give me some idea about how to proceed with turning this into a Drupal page?

Comments

A couple of approaches. One

A couple of approaches.

One option is to convert code to a module

  • Need to implement hook_menu()
  • Need to implement menu callback, this could be pretty much the code above
  • If tables are not in Drupal database see: http://drupal.org/node/18429
  • Should convert mysql calls to Drupal db equivalents
  • Could break logic up into collect data and theming

Another is to write a module that defines the tables for views 2

A third option is to import

A third option is to import your data and turn it into nodes.
How are you planning to edit/add new talk items in the future?

Suggestions

Hi,
if you wants to use your original code:

1. Go to admin page,
2. Click on "create content" and click "Page" to start your test content (you can use other content type or create your own content type when you are more familiar with drupal)
3. copy and paste all your code into the Body section of the "Page" content type, remember to check "PHPcode" as the input format and click submit
4. Go back to "Admin" page, click "Content", or if you know the node id for the content you've just create, type it in your URL such as www.yourweb.com/node/yourid (if you install in the root directory)
5. Start with a simple php code such as hello world, and you will get the idea how it works pretty fast

Good Luck

Choong Hoong Liew

Choong Hoong Liew

nobody click here