English 中文(简体)
(PHP & mySQL) Treat rows as columns
原标题:
  • 时间:2010-01-04 05:38:10
  •  标签:
  • php
  • mysql
  • row

I am working on PHP & mySQL based website. I am trying to setup the Settings page in administration panel where I can enter different settings to manage the site. The settings can range upto 100 (or even more) depending upon the requirements. So instead of making 100 columns (and increase if I have to add more columns in future), I want to store the data in row wise format and fetch the values as if I am fetching it from columns.

REFERENCE:

I found a similar real life implementation of such feature in the most popular blogging tool Wordpress . For reference, it is the wp_options table that I am talking about.(Please correct me I am wrong)

EXAMPLE:

Here s a quick example of what (& why) I am trying to do it that way:

--Table settings

P.KEY   option_name      option_value
1       site_name        XYZ site inc.
2       siteurl          http://www.xyz.com
3       slogan           Welcome to my XYZ site
4       admin_email      admin@xyz.com
5       mailserver_url   mail.xyz.com
6       mailserver_port  23
..... etc.

As you can see from above, I have listed very few options and they are increasing in number. (Just for the records, my installation of Wordpress has 902 rows in wp_options table and I did not see any duplicate option_name). So I have the feeling that I am well off if I apply the same working principle as Wordpress to accomodate growth of the settings. Also I want to do it so that once I save all the settings in DB, I want to retrieve all the settings and populate the respective fields in the form, for which the entries exist in DB.

ONE OF MY CODE TRIALS:

--
-- Table structure for table `settings`
--

CREATE TABLE IF NOT EXISTS `settings` (
  `set_id` tinyint(3) NOT NULL auto_increment,
  `option_name` varchar(255) NOT NULL,
  `option_value` varchar(255) NOT NULL,
  PRIMARY KEY  (`set_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--
-- Dumping data for table `settings`
--

INSERT INTO `settings` (`set_id`, `option_name`, `option_value`) VALUES
(1,  site_name ,  XYZ site inc. ),
(2,  slogan ,  Welcome to my XYZ site );

$result = mysql_query("SELECT option_name, option_value FROM settings");
$defaults = array( option_name ,  option_value );


while( list($n, $v) = mysql_fetch_array($result) )
{
 $defaults[ option_name ] .= $n;
 $defaults[ option_value ] .= $v;
}

echo  $defaults[ option_name ]. --- .$defaults[ option_value ]. <br /> ;

//The above code gives me the following Output:
//site_nameslogan---XYZ site inc.Welcome to my XYZ site

When I run the above query, I also receive 2 PHP Notices that says:

Undefined index: option_name

Undefined index: option_value

I would appreciate any replies that could show me the PHP code to retrieve the options successfully and eliminate the Undefined index issues as well. Also, like I mentioned earlier, I want to retrieve all the existing settings and populate the respective fields in the form when I visit the settings page next, after storing the data.

Thanks fly out to all in advance.

最佳回答

PHP gives you warning because $defaults[ option_name ] and $defaults[ option_value ] are not being initialized before they are used in .= operation.

So just put

$defaults[ option_name ] =   ;
$defaults[ option_value ] =   ;

before the loop and warning will go away.

The rest of the code is completely correct, although you don t have to have set_id column at all since every setting will have unique name, that name (option_name column) can be used as primary key.

Another thing that you can improve your code, is to use $defaults differently, like so

$defaults[$n] = $v;

Then you can use every setting on its own without looking through two huge strings.

$site_url = $defaults[ site_url ];
foreach ($defaults as $name => $value) {
   echo $name,   =  , $value,  <br> ;
}
问题回答

This should do the trick:

$defaults = array( option_name  => array( ),  option_value  => array( ) );

while( list($n, $v) = mysql_fetch_array($result) )
{
 $defaults[ option_name ][] = $n;
 $defaults[ option_value ][] = $v;
}

Then in your view iterate over $defaults[ option_name ] and $defaults[ option_value ]





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签