0

私はPHPとmySQLベースのウェブサイトに取り組んでいます。管理パネルの[設定]ページを設定しようとしています。このページで、サイトを管理するためのさまざまな設定を入力できます。設定は、要件に応じて最大100(またはそれ以上)の範囲になります。したがって、100列を作成する(そして将来さらに列を追加する必要がある場合は増やす)代わりに、データを行単位の形式で格納し、列からフェッチする場合と同じように値をフェッチします。

参照:

私は、最も人気のあるブログツール「Wordpress」でそのような機能の同様の実際の実装を見つけました。参考までに、私が話しているのは「wp_options」テーブルです(私が間違っていることを訂正してください)

例:

これが私がそのようにしようとしていること(そしてその理由)の簡単な例です:

--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.

上からわかるように、私は非常に少数のオプションをリストしました、そしてそれらは数が増えています。(記録のために、Wordpressのインストールではwp_optionsテーブルに902行があり、重複するoption_nameは表示されませんでした)。ですから、Wordpressと同じ動作原理を適用して設定の成長に対応すれば、私は元気になっていると感じています。また、すべての設定をDBに保存したら、すべての設定を取得して、DBにエントリが存在するフォームのそれぞれのフィールドに入力するようにします。

私のコードトライアルの1つ:

--
-- 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

上記のクエリを実行すると、次のような2つのPHP通知も受け取ります。

未定義のインデックス:option_name

未定義のインデックス: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.

4

2 に答える 2

4

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>';
}
于 2010-01-04T05:55:53.290 に答える
1

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']

于 2010-01-04T05:46:58.817 に答える