Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6210453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:09:44+00:00 2026-05-24T06:09:44+00:00

How can I write this if condition in switch method? if( $k != ‘pg_id’

  • 0

How can I write this if condition in switch method?

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

to…?

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

}

EDIT:

Sorry I think I didn’t make it clear earlier, below is how I want to use it,

$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 = ?";
}

as you can see I repeated the condition there –

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

and it may grow long at some point.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:09:45+00:00Added an answer on May 24, 2026 at 6:09 am

    (Borrowing from a previous question which I believe spawned this one – A short-cut to update a table row in the database?)

    $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
        ));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I write this in such a way that $counter value is 1
How can I write this code more cleanly/concisely? /// <summary> /// Creates a set
If I'm using a Hashtable , I can write code like this: object item
So I know I can write my own HTML-encoding function like this: function getHTMLEncode(t)
Eg. can I write something like this code: public void InactiveCustomers(IEnumerable<Guid> customerIDs) { //...
In Oracle, you can write: update t set xml = updateXML(xml, '/a/b/text()', 'gaga') This
Can someone write some sample code to explain this concept? I know what a
According to this site I can simply write $user = getlogin(); but the group
How can I write a query similar to this one in LINQ to SQL
Can I write to the end of a 5GB file in Java? This question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.