PHP help editing latest news functionality


#1

Hello all,

This looks like an excellent community and I hope that I can help out as well as get a few answers when needed. My question is about a custom-coded latest-news plugin written by someone else. I don’t know if it is generic, a copy of something else or what. It works good! but when I go to add a new news item to the list under Settings, it adds the item to the bottom of the list. The news is displayed on my homepage in short form and in full format on the primary news page.

I need new items to be added to the TOP of the list instead or in reverse, so that in all the containers where it resides, the latest news item is always at the top of the list. I know nothing about php but I am looking to learn. I plan on taking some basic classes on css and php to upgrade my archaic html skills, but I’m currently studying other subjects. Regardless, if this has a simple fix, I would be very interested in knowing what is needed and where/why.

Here is the core of the php that I am finding in the plugin: There are three php files, This one says active, the other two say inactive so I get the impression they are not in use. Let me know if I should post their contents as well.

Appreciate any help with this! My sincere thanks in advance.

Labeled active: latest_news.php
[php]*/
ob_start();
register_activation_hook(file, latest_news_install);

function latest_news_install() {

global $wpdb;

$table_name = $wpdb->prefix . "latest_news";

if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {

    $sql = "CREATE TABLE IF NOT EXISTS " . $table_name . " (
                 `id` int(10) NOT NULL AUTO_INCREMENT,
                 `title` varchar(255) NOT NULL,
                 `description` text NOT NULL,
                 `date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
                 PRIMARY KEY (id)
             );";

    mysql_query($sql) or die(mysql_error());
}

}

if( !function_exists(‘wp_latest_news_init’) ) {

function wp_latest_news_init() {

    add_shortcode( 'wp_latest_news', 'shortcode_latest_news');
}

}
if( !function_exists(‘shortcode_latest_news’) ) {

function shortcode_latest_news() {

   // extract(shortcode_atts(array('limit' => '5', ), $atts));

    global $wpdb;      

    $user_count = $wpdb->get_results("SELECT * FROM wp_latest_news ORDER BY date ASC LIMIT 4");
    
    foreach ($user_count as $output) {
        $htmlOutput[] = "<li><b><a href='".  get_permalink(287)."?news_id=".$output->id."'>" . $output->title . "</a></b></li>";

    }
    //echo $htmlOutput;
    //echo "<pre>"; print_r($user_count); echo "</pre>";
    foreach ($htmlOutput as $out) {
        echo $out;
    }
    
    return $htmlOutput;

}

}

/* add_action(‘init’, ‘wp_latest_news_init’ ) is used for call init function for create shortcode */
add_action(‘init’, ‘wp_latest_news_init’ );
add_action(‘admin_menu’, ‘wp_latestnews_links’ );

function wp_latestnews_links() {

add_submenu_page( 'options-general.php', __( 'Latest News ' ), __( 'Latest News ' ), 5 , 'latest_news_settings', 'latest_news_settings' );

}

// function latest_news_settings() {
// global $wpdb;
// if (isset($_POST[“submit”])) {
// $sql=“INSERT INTO “.$wpdb->prefix . “latest_news(title,description) VALUES (’”.$_POST[‘news_title’].”’,’”.$_POST[‘description’]."’) ";
// //$sql=$wpdb->insert( $wpdb->latest_news, array( ‘title’ => $_POST[‘news_title’] ,‘description’ => $_POST[‘description’] ) );
// mysql_query($sql) or die(mysql_error());
// }
// include ‘Latest_news_setting.php’;

function latest_news_settings() {
global $wpdb;
$action = $_REQUEST[“action”];
$id = $_REQUEST[‘id’];

$sql = "SELECT * from ".$wpdb->prefix."latest_news ";
$results = $wpdb->get_results($sql);
//echo "<pre>";print_r($results);
//$post_arr = query_posts("");
//echo "<pre>";print_r($post_arr);exit;
switch ($action) {
    case 'add' :
        include('news_form.php');
        break;
    case 'save' :
        if(isset($_POST['cancel'])) {
            include('latest_news_list.php');
        } else if(isset($_POST['add'])) {

            $wpdb->query("insert into ".$wpdb->prefix."latest_news (`title`,`description`) VALUES ('".$_REQUEST['news_title']."','".$_REQUEST['description']."')");

            //header("Location: http://localhost:81/Bravo/wp-admin/edit.php?page=slider_images");
            header("location:?page=latest_news_settings");
        }
        break;

    case 'edit' :
        $sql = "SELECT * from ".$wpdb->prefix."latest_news where id = '".$id."' ";
        $results_arr = $wpdb->get_results($sql);
        //echo "<pre>";print_r($results_arr);
        include('news_form.php');
        break;

    case 'update' :
        if(isset($_POST['cancel'])) {
            include('news_form.php');
        } else if(isset($_POST['edit'])) {
            //echo $id;
            $wpdb->query("update ".$wpdb->prefix."latest_news set title = '".$_REQUEST['news_title']."', description = '".$_REQUEST['description']."' where id='".$id."'");
            header("location:?page=latest_news_settings");
        }
        break;

    case 'delete' :

        $wpdb->query("delete from ".$wpdb->prefix."latest_news where id='".$id."'");

        header("location:?page=latest_news_settings");
        break;

    default:
        include('latest_news_list.php');
        break;
}

}
?>[/php]


#2

Well, this is a short answer… You have the data pulled with “ORDER BY ASC”…
This means ordered by ascending dates. So, yesterday comes first, tomorrow last.
Try it by DESC instead descending, so Today is first, yesterday is next… Should work!