View Full Version : get table information into a function.
thesavior
07-23-2006, 08:01 PM
okay my table is formatted like:
name value
and i have a bunch of rows, example:
Site Name Power Tutorial Manager
What i want to be able to do, is simply do something like:
$config("Site Name"); and it would return Power Tutorial Manager.
What would be the easiest way to do this? Lets say im already connected to my database.
You could either fill an array with all pairs (this is just like outputting each row, and you know how to do that), and then access one with:
$site_name=$config['site name'];
or you could have a function that gets one at a time, when you call it:
$site_name=getConfigVal('site name');
which would do the SQL SELECT query when it is called.
thesavior
07-23-2006, 09:09 PM
okay, i like this method:
$config['site name'];
But how would i get that array automatically filled from the database?
In a very similar way to how you iterate over results to display them.
thesavior
07-23-2006, 11:23 PM
...like...
As5a5sIn5
07-23-2006, 11:37 PM
I'm not exactly sure what your looking for, but I think you may want this.
//Connected to db already
$query1 = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_assoc($query1)){
$site = $row[name];
$config[$site] = $row[value];
}
echo $config['site name'] //this will return the corresponding "value"
thesavior
07-23-2006, 11:40 PM
that will only work for sitename though, i have about 200 or so things i want to be able to call without having to do a query everytime i want to call it.
As5a5sIn5
07-23-2006, 11:46 PM
Wait, is it not all in one table?
thesavior
07-24-2006, 12:13 AM
it is all in one table, but using your function, i use this:
<?php echo $config['site_name'];?>
and it is empty. site_name is in the name column and its corresponding "value" is "Power Tutorial Manager"
However when i use your code, it is empty.
*nvm* I got it to work.
Brandoe85
07-24-2006, 12:19 AM
Keeping and searching an array of 200 elements, or doing a simple query with a where clause. I'm leaning toward doing the query...but glad you have it working. :)
If all 200 (or a sufficiently large number) are going to be used, it makes sense to do one query, as an array-index is far quicker than a DB query.
If you know which ones are going to be needed on any particular page, then performance could be increased by restricting the query.
As5a5sIn5
07-24-2006, 08:55 AM
Yes. And he did specify he wanted one query. I was only following his specifictations :(
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.