Hybride Posted June 6, 2009 Share Posted June 6, 2009 Am trying to get a new layout working, the only problem is... connecting to the database. Here's how it works: I have one page layout that has all of the variables on it. All other pages are only the variables set, which means they have to be set before sent off to the main page layout. The problem is, I can't connect to the database since the information in the main page is required beforehand... which I can't do. I am trying to avoid making the connection manually on every page or linking it during a separate file, if at all possible. Here's an example of what I mean: <? include "include/incdb.hyb"; $metakey = "metastuff"; $metadesc = "metadescstuff."; $metatitle = "title of the page"; $type = "index"; $submenuhl = "updates"; $subtxt = "text text text"; $subtxtq= mysql_query("SELECT * FROM updates ORDER BY id DESC LIMIT 7") OR die(mysql_error()); if ($subtxtq) { while ($row = mysql_fetch_array($subtxtq, MYSQL_ASSOC)) { $subtxt = "<p><b> $row[date] :: </b><i> $row[title] </i> - $row[body] </p><br /><hr /><br />"; } } include "header.php"; ?> And on the HTML, they are just the PHP page's variables within the layout (that way, I only update one page.) So in order for this to work, the variables have to be set before sending to the header, but the db connection requires the header to be set beforehand to work. Any way around this? Also, if you noticed in the piece of code, I have a while loop going on. Am also trying to get each entry within the loop to be set to $subtxt. Is the only way to do it is with an array or some other way? Quote Link to comment https://forums.phpfreaks.com/topic/161151-2-questions-php-to-database-and-database-loop/ Share on other sites More sharing options...
trq Posted June 6, 2009 Share Posted June 6, 2009 but the db connection requires the header to be set beforehand to work. Any way around this? You will need to reorganise your code more logically. Is the only way to do it is with an array or some other way? An array would be the best method. Quote Link to comment https://forums.phpfreaks.com/topic/161151-2-questions-php-to-database-and-database-loop/#findComment-850401 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.