We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,453 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Need some quick advice for designing a dynamic website

Hello again DaniWeb!

I'm designing a dynamic website in PHP, and it's structured so that each page creates the necessary objects (based on what content is being viewed) which get the right information to display. Smarty then organizes this content and makes it look good.

The problem is, I have most of these objects creating a new database object, so I end up with multiple database connections which is unnecessary. To solve this, I'd rather not have to make a $db object and then use the global keyword in every single object to pull that variable in, so is there a better way?

Thanks a lot, and let me know if I could be designing this any better. I've never done anything like this before.

2
Contributors
3
Replies
5 Hours
Discussion Span
3 Years Ago
Last Updated
4
Views
crh0872
Newbie Poster
18 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Why are you creating a new db object every time?
Just create it one (e.g. in an include file) and the just reference it. Just use the link_id reference, although if you just have one, you don't need to refer to it explicitly. Close the connection if you really need to at the end of your last sql call.

diafol
Keep Smiling
Moderator
10,675 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

Okay so I'll just declare the object at the beginning of every page, but then how do I reference it from another object? The global keyword?

crh0872
Newbie Poster
18 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I like to avoid using globals if possible. If you're just using vanilla functions - the DB object should be available without the need to use global. If you're using classes, it may be more complicated. You mention using Smarty - I haven't used it for a couple of years, so I can't quite remember the pitfalls, but I can't imagine that it would present an obstacle as you sort out your variables in php before assigning them/setting the block to the html template.

I'm no expert I'm afraid - perhaps if you posted some key elements of your code, it would help.

diafol
Keep Smiling
Moderator
10,675 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.2944 seconds using 2.67MB