• Resolved Spencer Hill

    (@ws5f3dj7)


    Heya,
    So I can’t figure out why this code deletes the last record in the database instead of the one the user selects, can someone help?

    <?php
    
    // GLOBALS
    // GLOBALS
    $table_name = $wpdb->prefix . "tpc_slideshow"; // THIS VARIABLE IS USE TO CREATE A TABLE IN THE DATABASE CALLED WP_TPC_SLIDESHOW
    
    $plugin_path = WP_PLUGIN_URL . '/' . str_replace(basename( __FILE__),"",plugin_basename(__FILE__));
    
    // CREATE NEW SLIDESHOW
    // CREATE NEW SLIDESHOW
    $create_new_slideshow_button = $_POST['create-a-new-slideshow'];
    $delete_this_slideshow_button = $_POST['delete-this-slideshow'];
    $update_this_slideshow_button = $_POST['update-this-slideshow'];
    
    $id = $_POST['id'];
    $title = $_POST['title'];
    $category = $_POST['category'];
    $max_slides_to_display = $_POST['max-slides-to-display'];
    $theme = $_POST['theme'];
    
    if (isset($create_new_slideshow_button)) {
    
    	global $title;
    	global $category;
    	global $max_slides_to_display;
    	global $theme;
    
    	$wpdb->insert( $table_name, array( 'title' => $title, 'category' => $category, 'max_slides_to_display' => $max_slides_to_display, 'theme' => $theme) );
    
    	$successfully_added_message = '<div class="updated"><p>' . $title . ' has been added.</p></div>';
    
    } elseif (isset($update_this_slideshow_button)) {
    
    	global $id;
    	global $title;
    	global $category;
    	global $max_slides_to_display;
    
    	$wpdb->update( $table_name, array( 'title' => $title, 'category' => $category, 'max_slides_to_display' => $max_slides_to_display), array( 'id' => $id ) );
    
    	$successfully_updated_message = '<div class="updated"><p>' . $title . ' has been updated.</p></div>';
    
    } elseif (isset($delete_this_slideshow_button)) {
    
    	global $title;
    	global $id;
    
    	$wpdb->query("DELETE FROM $table_name WHERE id = $id");
    
    	$successfully_deleted_message = '<div class="updated"><p>' . $title . ' has been deleted.</p></div>';
    }
    
    $get_categories = mysql_query("SELECT * FROM  <code>wp_terms</code> LIMIT 0 , 30") or die(mysql_error());
    $get_slideshows = mysql_query("SELECT * FROM  <code>wp_tpc_slideshow</code>") or die(mysql_error());
    
    function tpc_slideshow_create_database() {
    	global $wpdb;
    	global $tpc_slideshow_version;
    	$table_name = $wpdb->prefix . "tpc_slideshow"; // THIS VARIABLE IS USE TO CREATE A TABLE IN THE DATABASE CALLED WP_TPC_SLIDESHOW
    
    	if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
    
    		$sql = "CREATE TABLE " . $table_name . " (
    			id mediumint(3) NOT NULL AUTO_INCREMENT,
    			title VARCHAR(200) NOT NULL,
    			category VARCHAR(200) NOT NULL,
    			theme VARCHAR(200) NOT NULL,
    			max_slides_to_display mediumint(3) NOT NULL,
    			UNIQUE KEY id (id)
    		);";
    
    		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    		dbDelta($sql);
    
    		add_option("tpc_slideshow_version", $tpc_slideshow_version);
    
       }
    }
    
    register_activation_hook(__FILE__,'tpc_slideshow_create_database'); // THIS TELLS WORDPRESS TO EXECUTE THIS FUNCTION UPON THE USER ACTIVATING THE PLUGIN.
    
    // CHECK TO SEE WHAT VERSION OF THIS PLUGIN IS CURRENTLY INSTALLED.
    $installed_version = get_option( "tpc_slideshow_version" );
    
    // IF THE INSTALLED VERSION IS NOT THE SAME AS THE VERSION NOTED IN THIS CODE THAN UPDATE THE DATABASE
    if( $installed_version != $tpc_slideshow_version ) {
    
    	$sql = "CREATE TABLE " . $table_name . " (
    		id mediumint(3) NOT NULL AUTO_INCREMENT,
    		title VARCHAR(200) NOT NULL,
    		category VARCHAR(200) NOT NULL,
    		theme VARCHAR(200) NOT NULL,
    		max_slides_to_display mediumint(3) NOT NULL,
    		UNIQUE KEY id (id)
    	);";
    
    	require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    	dbDelta($sql);
    
    	update_option( "tpc_slideshow_version", $tpc_slideshow_version );
    }
    
    // CREATE AN ADMINISTRATION PAGE BENEATH THE "SETTINGS" PANEL IN WORDPRESS
    // CREATE AN ADMINISTRATION PAGE BENEATH THE "SETTINGS" PANEL IN WORDPRESS
    function tpc_slideshow_add_options_page() {
    
    	add_options_page('TPC Slideshow Options', 'TPC Slideshow', 'manage_options', 'tpc-slideshow', 'tpc_slideshow_admin_page');
    
    	wp_enqueue_style('style', '/wp-content/plugins/tpc-slideshow/themes/back-end.css', '', '1.0', 'screen');
    
    }
    
    add_action('admin_menu', 'tpc_slideshow_add_options_page'); // USES THE tpc_slideshow_add_options FUNCTION TO ADD THE ADMINISTRATION PAGE
    
    // THIS IS THE CODE THAT IS DISPLAYED ON THE ADMIN PAGE
    // THIS IS THE CODE THAT IS DISPLAYED ON THE ADMIN PAGE
    function tpc_slideshow_admin_page() {
    
    	global $successfully_added_message;
    	global $successfully_deleted_message;
    	global $successfully_updated_message;
    	global $get_categories;
    	global $get_slideshows;
    	global $title;
    
    	echo '
    		<div class="wrap tpc-slideshow">
    
    			' . $successfully_deleted_message . $successfully_added_message . $successfully_updated_message . '		
    
    			<h2>The Portland Company Slideshow</h2>
    			<p>This plugin allows back end users to create multiple slideshows that display posts from multiple categories that front end users can cycle through via a previous and next button or by selecting the position indicator icon.</p>
    
    			<br />
    
    			<form action="" method="post">
    				<p><i>Slides are ordered by the date they were created, if you would like to change their order you must visit the "Posts" section of WordPress and edit the date there.</i></p>
    			<br />
    
    			<h3>Create a New Slideshow</h3>
    			<table class="widefat">
    				<thead>
    				<tr>
    					<th style="width: 200px;">Enter a title for your slideshow:</th>
    					<th>Select a category to display articles from:</th>
    					<th>What is the maximum number of slides you want displayed?</th>
    					<th>What theme would you like this slideshow to use?</th>
    					<th></th>
    				</tr>
    				</thead>
    
    				<input type="hidden" name="id" value="' . $row['id'] . '" />
    
    				<tbody class="plugins">
    					<tr>
    						<td><input type="text" name="title" value="" /></td>
    						<td>
    							<select name="category">
    	';
    
    								while ($row = mysql_fetch_array( $get_categories )){
    									echo '<option value="' . $row['name'] . '">' . $row['name'] . '</option>';
    								}
    
    	echo '
    							</select>
    						</td>
    						<td><input type="text" name="max-slides-to-display" value="" maxlength="3" /></td>
    						<td>
    							<select name="theme">
    								<option></option>
    	';
    								if ($handle = opendir('../wp-content/plugins/tpc-slideshow/themes/')) {
    
    									while (false !== ($file = readdir($handle))) {
    										if ( $file != '.' && $file != '..' && $file != 'index.html' && $file != '.DS_Store' ) {
    											echo '<option value="' . $file . '">' . str_replace('-', ' ', ucwords($file)) . '</p>';
    										}
    									}
    
    									closedir($handle);
    								}
    
    	echo '
    							</select>
    						</td>
    						<td style="text-align: right"><button name="create-a-new-slideshow" class="button-primary">Create</button></td>
    					</tr>
    				</table>
    			</form>
    
    			<form name="edit-delete" action="" method="post">
    
    			<br />
    
    			<h3>Edit an Existing Slideshow</h3>
    
    			<table class="widefat">
    				<thead>
    				<tr>
    					<th style="width: 200px;">Title</th>
    					<th>Category</th>
    					<th>Max Slides to Display</th>
    					<th>Shortcode</th>
    					<th>Theme</th>
    					<th></th>
    					<th></th>
    				</tr>
    				</thead>
    	';
    
    	echo "<h1>ID: " . $_POST['id'] . "</h1>";
    
    				while ($show_slideshows = mysql_fetch_array( $get_slideshows )){
    					echo '
    						<tr>
    							<td>
    								<input type="text" name="id" value="' . $show_slideshows['id'] . '" />
    								<p><input type="text" name="title" value="' . $show_slideshows['title'] . '" /></p>
    							</td>
    							<td>
    							<select name="category">
    								<option value="' . $show_slideshows['category'] . '">' . $show_slideshows['category'] . '</option>
    							</select>
    							</td>
    							<td><p>' . $show_slideshows['max_slides_to_display'] . '</p></td>
    							<td><p class="code">[tpc_slideshow title="' . $show_slideshows['title'] . '"]</p></td>
    							<td>
    							<select name="theme">
    								<option>' . str_replace('-', ' ', ucwords($show_slideshows['theme'])) . '</option>
    	';
    								if ($handle = opendir('../wp-content/plugins/tpc-slideshow/themes/')) {
    
    									while (false !== ($file = readdir($handle))) {
    										if ( $file != '.' && $file != '..' && $file != 'index.html' && $file != '.DS_Store' && $file != $row['theme'] ) {
    											echo '<option value="' . $file . '">' . str_replace('-', ' ', ucwords($file)) . '</p>';
    										}
    									}
    
    									closedir($handle);
    								}
    
    	echo '
    							</select>
    							</td>
    							<td style="text-align: right"><button title="update-this-slideshow" class="button-primary">Update</button></td>
    							<td style="text-align: right"><button name="delete-this-slideshow" class="button-secondary">Delete</button></td>
    						</tr>
    					';
    				}
    
    	echo '
    				</tbody>
    			</table>
    			</form>
    
    			<br />
    			<br />
    			<br />
    			<br />
    
    			<div id="sm_rebuild" class="postbox">
    				<h3 class="hndle"><span>Shortcode</span></h3>
    				<div class="inside">
    					<p>To display a slideshow on a particular page, copy the following "shortcode" and paste it into the page you want it to display on:</p>
    					<p class="code">[tpc_slideshow title="<i><b>name-of-your-slideshow</b></i>"]</p>
    				</div>
    			</div>
    
    			<br />
    
    			<h3>Developer Notes</h3>
    
    			<div id="sm_rebuild" class="postbox">
    				<h3 class="hndle"><span>PHP Code</span></h3>
    				<div class="inside">
    					<p>If you are a developer you may use the following PHP code:</p>
    					<p class="code"><?php tpc_notifier_code("<i><b>title-of-your-slideshow</b></i>"); ?></p>
    				</div>
    			</div>
    
    			<div id="sm_rebuild" class="postbox">
    				<h3 class="hndle"><span>Styling and Themeing</span></h3>
    				<div class="inside">
    					<p>If you want to change the default style of the slideshow by adding or creating your own, you may deposit it into the <span class="code">wp-content/plugins/tpc-slideshow/themes</span> directory. You must also change the URL to the stylesheet in <span class="code">wp-content/plugins/tpc-slideshow/tpc-slideshow.php</span> within the <span class="code">tpc_slideshow_externals()</span> PHP function.</p>
    					<p>If you choose to create a custom theme it must be valid by ensuring these steps are taken:</p>
    					<p>1. The theme must be placed in the themes directory using all lowercase, no special characters, dashes instead of spaces or underscores (Ex. example-theme).</p>
    					<p>The stylesheet must be in the root of the directory and names "styles.css".</p>
    				</div>
    			</div>
    
    			<div id="sm_rebuild" class="postbox">
    				<h3 class="hndle"><span>Roadmap</span></h3>
    				<div class="inside">
    					<p>For those who are fans of this plugin here is a <i>"roadmap"</i> of our plans to enhance this plugin:</p>
    					<p>1. Implement a theme manager allows back end users to upload new themes or navigate an activate existing themes.</p>
    					<p>2. Implement the ability to add multiple slideshows.</p>
    
    					<br />
    
    					<p>Have an idea or feature request? <a href="mailto:[email protected]">Tell us!</a></p>
    				</div>
    			</div>
    
    		</div>
    	';
    
    }
    
    // THIS CODE IS TO BE INCLUDED IN THE HEAD OF THE TEMPLATE
    // THIS CODE IS TO BE INCLUDED IN THE HEAD OF THE TEMPLATE
    function tpc_slideshow_externals() {
    
    	global $get_themes;
    
    	// GET THE PLUGIN DIRECTORY
    	global $plugin_path;
    
    	echo '
    		<link rel="stylesheet" type="text/css" href="' . $plugin_path . 'themes/default/styles.css">
    
    		<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    		<script type="text/javascript" src="' . $plugin_path . 'scripts/slideshow.js"></script>
    	';
    }
    
    add_action('wp_head', 'tpc_slideshow_externals'); // THIS ADDS THE CODE WITHIN THE tpc_slideshow_externals FUNCTION INTO THE HEAD OF THE WEBSITES THEME.
    
    // THIS CODE IS TO BE INCLUDED WHEREVER THE SHORTCODE OR PHP IS PLACED.
    // THIS CODE IS TO BE INCLUDED WHEREVER THE SHORTCODE OR PHP IS PLACED.
    function tpc_slideshow_front_end($title) {
    
    	extract(shortcode_atts(array(
    		'title' => $title,
    	), $title));
    
    	global $table_name;
    
    	$result = mysql_query("SELECT * FROM $table_name WHERE title = '$title'");
    
    	while($row = mysql_fetch_array($result)) { $category = $row['category']; }
    
    	// IF THE TITLE IN THE SHORT CODE OR FUNCTION DOESNT EXIST IN THE DATBASE THEN DONT
    	if (mysql_num_rows(mysql_query("SELECT * FROM $table_name WHERE title = '$title'"))) {
    
    	echo '
    		<div id="pageContainer">
    			<div id="slideshow">
    				<div id="slidesContainer">
    	';
    
    		query_posts('category_name=' . $category);
    
    		while (have_posts()) : the_post();
    
    			echo '<div class="slide">';
    				the_content();
    			echo '</div>';
    
    		endwhile;
    
    	echo '
    				</div>
    			</div>
    		</div>
    	';
    
    	}
    
    }
    
    add_shortcode('tpc_slideshow', 'tpc_slideshow_front_end'); // GENERATES THE SHORTCODE [tpc_slideshow]

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Why does this code delete the LAST record, need help…’ is closed to new replies.