Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Integrating existing database into WordPress template pages WordPress

Hi there,

I am moving a PHP-driven site to WordPress using the Karma theme. The old (current) site has has these 2 pages:

[[LINK href="http://www.wickman-group.com/stock/"]][[/LINK]]
[[LINK href="http://www.wickman-group.com/stock/detail.php?id=00451"]][[/LINK]]

They have special PHP code in the <head> and <body> tags to connect to an external database but I need to have them populate 2 WordPress pages instead.

I assume I need to create 2 new template pages (with a unique header.php file) but am not sure how to do that.

Any help is much appreciated. Thanks!

- Paul

Answers (4)

2011-09-12

ej_emman answers:

hello pmycroft,

I can do this. I just pm you my details.

2011-09-12

Duncan O'Neill answers:

You can dictate which header file is called in using the name ( slug ) of your pages. For example, if you have a page with a slug of about, it will look for, and use, header-about.php if that file is in the child-theme folder first, or the parent theme folder second.

2011-09-12

Gabriel Reguly answers:

Hi Paul,

I have just done this to a client, it is fairly simple.


$other_db = new wpdb('user','password','database','server');
$query = 'select * from table where condition';
$row = $other__db->get_row( $query, ARRAY_A ); // for a single row result
$result = $other_db->get_results( $query, ARRAY_A ); // for a result set with more than one row
if ( sizeof( $result ) ) {
foreach ( $result as $row ) {
echo $row['column_one'] . ' ' . $row['column_two'];
}
}



No need to have a special header.php, for the title you can set it with this:

function title_from_query() {
return 'gabriel' . ' | ';
}
add_filter( 'wp_title', 'title_from_query' );


So you only need to create 2 template files, add your queries and HTML and you are set.

Regards,
Gabriel


Gabriel Reguly comments:

Hi Paul,

To create your template files just upload 2 files to the server with a special comment inside them, and your code (PHP and HTML )

<?php
/**
* Template Name: Stock
*
*
*/
?>


<?php
/**
* Template Name: Stock Details
*
*
*/
?>


Let me know if you need more help.

Regards,
Gabriel


pmycroft comments:

Hi Gabriel,

I understand how to create the 2 template files but don't understand why the header.php is the same. Doesn't it need to contain specific information regarding the database.

This is in the current page 1:

<blockquote><?php
// set up the database connection [$db_name=, $connection=; $db= mysql_select_db($db_name, $connection);]
include( "/home/2/8/d/26771/26771/secure/sql.php" );

?></blockquote>

then the table with the results is in the <body>.

Maybe you can assist?

- Paul

2011-09-13

Abdessamad Idrissi answers:

Hey Paul,

to solve your question the right way, we will break it into 2 simple parts:

1. Create this two template pages by inserting in your "functions.php" file this code:


// an array where you declare pages setting
// don't forget to create acutale files inside your theme; first.php and second.php
$custom_pages = array (
array(
'post_title' => 'My first page title',
'post_name' => 'first_page_slug',
'_wp_page_template' => 'first.php'
),
array(
'post_title' => 'My second page title',
'post_name' => 'second_page_slug',
'_wp_page_template' => 'second.php'
)
);

// launch page creation process
// note that you can call this function on certain cases
// here I call it only when we are in admin.
// (note that you must visit the admin once in order to create this templates)
if (is_admin()) :
test_create_pages( $custom_pages );
endif;

function test_create_pages( $custom_pages )
{
if( empty($custom_pages) ) return;

global $wpdb;

foreach ($custom_pages as $page )
{
// Prepare the page
$my_page = array(
'post_status' => 'publish',
'post_type' => 'page',
'post_author' => 1,
'post_name' => $page["post_name"],
'post_title' => $page["post_title"]
);

// check if the page doesn't exist already
$sql = "SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '". $page["post_name"] ."' LIMIT 1";

$wpdb->get_results($sql);

if($wpdb->num_rows == 0) {

// Insert the page into the database
$page_id = wp_insert_post($my_page);

// Assign the page template to the new page
update_post_meta($page_id, '_wp_page_template', $page["_wp_page_template"]);
}
}
}



2. Open the two files and edit them to suite the needs of your script. To simplify things for you, go to your existing "header.php" page and copy it inside your created pages "first.php" and "second.php" then you can use the existing "single.php" to copy the rest of code to your pages and edit it to suite your needs.
Note that if you don't have "header.php" or "single.php" in your current theme, copy them from the default wordpress theme directory "twentyten".

Hope it helps.. bon chance :)