Jump to content

Parameterized template


nalply
 Share

Recommended Posts

I have an external database with a list of items, each one identified by an id. I would like to integrate this database into the CMS.

www.example.com/external-database/item/<id>

I have created a page www.example.com/external-database/item, but not the dependent pages.

If link to the ids, Processwire tells me that they don't exist. Can I use the id urls without creating the pages? The id urls should use the parent url as template and use <id> as a parameter.

In short, a parameterized template.

Link to comment
Share on other sites

Hi nalply, and welcome to Processwire and the forums (even though it's your second post ;) )

The external db is not build on Processwire?

But the url "www.example.com/external-database/item/<id>" is served by processwire, right?

In your template you want to get the item from the external db which was provided in the url (<id>)?

So when I get you right you could use WireDatabasePDO like so

$urlSegment1 = $sanitizer->int($input->urlSegment1);

$customDb = new WireDatabasePDO('mysql:host=localhost;dbname=EXTERNAL_DATABASE_NAME', 'USER', 'PASS');
$query = $customDb->prepare('SELECT column FROM table WHERE id=:id');
$query->execute(array(':id' => $urlSegment1));

$result = $query->fetchAll(PDO::FETCH_OBJ);

foreach ($result as $r) {
    echo $r->column;
}

So for this template you need to enable url segments in backend to get it working

and of course you need to adjust the connection and the query.

But thats basically what you put in the template file, e.g. calles "external-database"

Saludos

Can

  • Like 3
Link to comment
Share on other sites

Thanks for the detailed answer.

Yes, the external database (however not MySQL) is not build on PW. Yes, the URL is served by PW.

Now I know how to proceed. What I needed was the word «url segment».

The external db is not build on Processwire?

But the url "www.example.com/external-database/item/<id>" is served by processwire, right?

In your template you want to get the item from the external db which was provided in the url (<id>)?

So when I get you right you could use WireDatabasePDO like so

$urlSegment1 = $sanitizer->int($input->urlSegment1);

$customDb = new WireDatabasePDO('mysql:host=localhost;dbname=EXTERNAL_DATABASE_NAME', 'USER', 'PASS');
$query = $customDb->prepare('SELECT column FROM table WHERE id=:id');
$query->execute(array(':id' => $urlSegment1));

$result = $query->fetchAll(PDO::FETCH_OBJ);

foreach ($result as $r) {
    echo $r->column;
}

So for this template you need to enable url segments in backend to get it working

and of course you need to adjust the connection and the query.

But thats basically what you put in the template file, e.g. calles "external-database"

Saludos

Can

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...