RCCWP_Application::AddColumnIfNotExist PHP Method

AddColumnIfNotExist() public static method

public static AddColumnIfNotExist ( $db, $column, $column_attr = "VARCHAR( 255 ) NULL" )
    public static function AddColumnIfNotExist($db, $column, $column_attr = "VARCHAR( 255 ) NULL")
    {
        global $wpdb;
        $exists = false;
        if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $db)) == $db) {
            $sql = "SHOW columns from {$db}";
            $table_colums = $wpdb->get_results($sql);
            foreach ($table_colums as $table_colum) {
                if ($table_colum->Field == $column) {
                    $exists = true;
                    break;
                }
            }
            if (!$exists) {
                $sql = $wpdb->prepare("ALTER TABLE {$db} ADD {$column} {$column_attr}");
                $wpdb->query($sql);
            }
        }
    }

Usage Example

Exemplo n.º 1
0
    public static function Content($customGroup = null)
    {
        // add the new expanded column, if it's not there already (Traversal)
        RCCWP_Application::AddColumnIfNotExist(MF_TABLE_PANEL_GROUPS, "expanded", $column_attr = "tinyint after duplicate");
        global $mf_domain;
        $customGroupName = $customGroupDuplicate = $customGroupExpanded = "";
        if (isset($_GET['custom-write-panel-id'])) {
            $customWritePanelId = $_GET['custom-write-panel-id'];
        }
        if (isset($_POST['custom-write-panel-id'])) {
            $customWritePanelId = $_POST['custom-write-panel-id'];
        }
        if ($customGroup != null) {
            $customGroupName = $customGroup->name;
            $customGroupDuplicate = $customGroup->duplicate;
            $customGroupExpanded = $customGroup->expanded;
        }
        ?>
		<?php 
        if ($customWritePanelId) {
            ?>
  			<input type="hidden" name="custom-write-panel-id" value="<?php 
            echo $customWritePanelId;
            ?>
">
		<?php 
        }
        ?>

		<table class="form-table" width="100%" border="0" cellspacing="0" cellpadding="6">
		<tbody>

		<tr valign="top">
			<th scope="row"  align="right"><?php 
        _e('Name', $mf_domain);
        ?>
:</th>
			<td><input name="custom-group-name" id="custom-group-name" size="40" type="text" value="<?php 
        echo $customGroupName;
        ?>
" /></td>
		</tr>

		<tr>
			<th scope="row" align="right"><?php 
        _e('Duplication', $mf_domain);
        ?>
:</th>
			<td><input name="custom-group-duplicate" id="custom-group-duplicate" type="checkbox" value="1" <?php 
        echo $customGroupDuplicate == 0 ? "" : "checked";
        ?>
 />&nbsp;<?php 
        _e('The group can be duplicated', $mf_domain);
        ?>
</td>
		</tr>

		<tr>
			<th scope="row" align="right"><?php 
        _e('Show as Expanded', $mf_domain);
        ?>
:</th>
			<td><input name="custom-group-expanded" id="custom-group-expanded" type="checkbox" value="1" <?php 
        echo $customGroupExpanded == 0 ? '' : ' checked="checked" ';
        ?>
 />&nbsp;<?php 
        _e('Display the full expanded group editing interface instead of the group summary', $mf_domain);
        ?>
			  <br /><small><?php 
        _e('Note: the group can still be collapsed by the user, this just determines the default state on load');
        ?>
</td>
		</tr>

		</tbody>
		</table>
		<br />

		<?php 
    }
All Usage Examples Of RCCWP_Application::AddColumnIfNotExist