English 中文(简体)
如何将“如果情况”改为“itch”。 方法
原标题:How to convert `if condition` to `switch` method

如何在<条码>上写这一<条码>。

if( $k !=  pg_id  && $k !=  pg_tag  && $k !=  pg_user  )
{
     $result = $connection->run_query($sql,array(...));
}

......

switch($k)
{
case  pg_id : false;
case  pg_tag : false;
case  pg_user : false;
default:
   $result = $connection->run_query($sql,array(...));

}

http://www.un.org。

令我感到担忧的是,我以前没有说明这一点,下面是我想如何使用它。

$editable_fields = array(
     pg_id ,
     pg_url ,
     pg_title ,
     pg_subtitle ,
     pg_description ,
     pg_introduction ,
     pg_content_1 ,
     pg_content_2 ,
     pg_content_3 ,
     pg_content_4 ,
     pg_backdate ,
     pg_highlight ,
     pg_hide ,
     pg_cat_id ,
     ps_cat_id ,
     parent_id ,
     tmp_id ,
     usr_id 
);

$sql_pattern = array();

foreach( $editable_fields as $key )
{
    if( $key !=  pg_id  && $key !=  pg_tag  && $key !=  pg_user   ) $sql_pattern[] = "$key = ?";
}

你可以看到,我在那里重复了情况:

if( $key !=  pg_id  && $key !=  pg_tag  && $key !=  pg_user   )

在某些时候,这一增长可能很长。

最佳回答

(从我相信的先前一个问题来看——)。 A. 导 言 更新数据库表格的简捷径:

$editable_fields = array(
   pg_url  ,
   pg_title  ,
  ...
);
/* These are the fields we will use the values of to match the tuple in the db */
$where_fields = array(
   pg_id  ,
   pg_tag  ,
   pg_user  ,
  ...
);

$form_values = array();
$sql_pattern = array();
foreach( $editable_fields as $k ){
  if( $k !=  pg_id 
      && isset( $_POST[$k] ) ){
    $form_values[$k] = $_POST[$k];
    // NOTE: You could use a variant on your above code here, like so
    // $form_values[$k] = set_variable( $_POST , $k );
    $sql_pattern[] = "$k = ?";
  }
}
$where_values = array();
$where_pattern = array();
foreach( $where_fields as $k ){
  if( isset( $_POST[$k] ) ){
    $where_values[$k] = $_POST[$k];
    // NOTE: You could use a variant on your above code here, like so
    // $form_values[$k] = set_variable( $_POST , $k );
    $where_pattern[] = "$k = ?";
  }
}

$sql_pattern =  UPDATE root_pages SET  .implode(   ,   , $sql_pattern ).  WHERE  .implode(   ,   , $where_pattern );

# use the instantiated db connection object from the init.php, to process the query
$result = $connection->run_query($sql_pattern,array_merge(
    $form_values ,
    $where_values
    ));
问题回答

使用<条码><>条码/代码>,防止今后出现下列情况:

switch($k)
{
case  pg_id :
case  pg_tag :
case  pg_user :
  // any match triggers this block; break causes no-op
  break;
default:
  $result = $connection->run_query($sql,array(...));
}

我不敢肯定,为什么你希望为此使用开关声明。

如果可以读取,你可以尝试:

if($k !=  pg_id  &&
   $k !=  pg_tag  &&
   $k !=  pg_user )
{
  $result = $connection->run_query($sql,array(...));
}

转换条件可能更快(假设表),比照容易阅读。 如果情况相同,而改善的辛迪加则在每一条件下使用 cur:

switch ($k)
{
 case  pg_id :
 case  pg_tag :
 case  pg_user : {
 break;
 }
 default: {
   $result = $connection->run_query($sql,array(...));
 }
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

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

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签