Chubichan Posted January 28, 2009 Share Posted January 28, 2009 I have this at the top of all my pages that will pull from my database. <?php include 'config.php'; include 'opendb.php'; $query = mysql_query("SELECT * FROM cms"); while($row = mysql_fetch_array($query)) { include 'variables.php'; } ?> This is the variables.php file <?php $home = nl2br($row['home']); $field01 = nl2br($row['field01']); $field02 = nl2br($row['field02']); $field03 = nl2br($row['field03']); $field04 = nl2br($row['field04']); $field05 = nl2br($row['field05']); $mot = nl2br($row['team']); $sta = nl2br($row['sta']); $pf = nl2br($row['form']); $add = nl2br($row['additionalforms']); $fi = nl2br($row['financial']); $loc = nl2br($row['location']); $con = nl2br($row['contact']); $ms = nl2br($row['mission']); $about = nl2br($row['aboutus']); $dis = nl2br($row['disclaimer']); $addr = nl2br($row['address']); $addr02 = nl2br($row['address02']); $new = nl2br($row['newuser']); $newpass = nl2br($row['newpass']); ?> config.php and opendb.php aren't of any concern and work just fine. Any ideas why this would not be working? Am I abusing include? Quote Link to comment https://forums.phpfreaks.com/topic/142730-can-you-use-include-to-pull-in-a-file-containing-variables/ Share on other sites More sharing options...
shlumph Posted January 28, 2009 Share Posted January 28, 2009 I'm not sure why you want to do this. If it did work - it'd make things harder for you. And, it's not the best idea to have an include/require in the middle of a loop. Quote Link to comment https://forums.phpfreaks.com/topic/142730-can-you-use-include-to-pull-in-a-file-containing-variables/#findComment-748189 Share on other sites More sharing options...
Chubichan Posted January 28, 2009 Author Share Posted January 28, 2009 I'm not sure why you want to do this. If it did work - it'd make things harder for you. And, it's not the best idea to have an include/require in the middle of a loop. Maybe there is a better way to do what I want to do. I want to update my variables in one spot. I want to have one file that I can stick all that stuff in and reference to it. If there is not a way to do it then that is cool, but perhaps there is a way. So I am asking. Quote Link to comment https://forums.phpfreaks.com/topic/142730-can-you-use-include-to-pull-in-a-file-containing-variables/#findComment-748190 Share on other sites More sharing options...
Philip Posted January 28, 2009 Share Posted January 28, 2009 Why not just have the following on the included page: <?php $query = mysql_query("SELECT * FROM cms"); while($row = mysql_fetch_array($query)) //... ?> As stated, it's very bad practice including files inside a loop. Especially considering each new row you go through, you'll overwrite the variables. Quote Link to comment https://forums.phpfreaks.com/topic/142730-can-you-use-include-to-pull-in-a-file-containing-variables/#findComment-748193 Share on other sites More sharing options...
trq Posted January 28, 2009 Share Posted January 28, 2009 I don't see why you need a loop with that design. Are you expecting more than one row? You shouldn't be, because your only going to get the values of the last row stored in your variables. Anyway, theres probably not too much wrong with your approuch if only for the fact that your poisoning your application namespace with variables. Seems to me the problem lies in your database design. I'm really not sure why you need all those different fields. In fact, if all you are doing is storing key / value pairs it is likely better to store tham that way. eg; database schema: CREATE TABLE settings ( id INT NOT NULL AUTO_INCREMENT, k VARCHAR(255), v VARCHAR(255), PRIMARY KEY (id) ); Now your code. <?php include 'config.php'; include 'opendb.php'; $config = array(); if ($result = mysql_query("SELECT k, v FROM settings")) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_array($query)) { $config[$row['k']] = $row['v']; } } } ?> Now you have an array ($config) which holds all your settings. Its still not a great solution, but it seem more logical than your approuch. Quote Link to comment https://forums.phpfreaks.com/topic/142730-can-you-use-include-to-pull-in-a-file-containing-variables/#findComment-748201 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.