duanhao7786 2013-04-02 16:12
浏览 54

WordPress + jQuery + PHP + MySQL:根据总计数减去上传限制

I built simple review media system for WordPress site. I am limiting number of upload through jQuery where button changes the state to disable when user reaches to the limit. Let's say 4 images is the limit.

I have created custom table called wp_review_media and using hook inserting data of id, post_id, user_id, image_name, status

Database Image

Currently system doesn't check if user has already posted images with the limited amount and each time he visits allows again to add images. Now here I want to restrict and count each time when user click on add more button that how many upload limits remains. First time users can use current jQuery limit as no record in database for that user. But once he submit the form than it should check how many images user has been uploaded for hat past.

I have done everything but stuck on this count system to check with the database. Here are all my code what I wrote.

Upload Form:

<form id="file-upload" action="" method="POST" enctype="multipart/form-data">
    <div id="image-uploader-box" class="group">           
        <div id="forms" class="add-photo-fields">                    
            <input type="button" id="add-photo-button" class="add-photo-button" value="Add Photo"/>
            <input type="submit" value="Upload" />
        </div>
    </div> 
</form>

WP Localize Script:

if(!is_admin()){   
    $photo_num = ($rm_options['max_image_count'] - 1); // Here you fetch the option data.    
    // Enqueue your script
    wp_enqueue_script( 'rm-javascript', RM_URL.'js/rm-scripts.js', array('jquery'), 1, true );   
    // Set the data you want to pass to your script here
    $data = array( 'photo_button_cnt' => $photo_num );    
    // Localize the script, the 'photo_button_cnt' value will now be accessible as a property in the global 'add_photo_button' object.
    wp_localize_script( 'rm-javascript', 'add_photo_button', $data );
}

jQuery Code:

jQuery(document).ready(function(){

    $cnt = 0;

    jQuery('#add-photo-button').click(function(){    

    if($cnt == add_photo_button.photo_button_cnt ){ 
        jQuery('#add-photo-button').prop('disabled', true);
        jQuery('#add-photo-button').addClass('disabled');
    }
    $cnt++;
        var current_count = jQuery('input[type="file"]').length;
        //var next_count = current_count + 1;

        jQuery('#file-upload').prepend('<p><input type="file" name="photo[]" /></p>');    

    });

});

Image count method:

public function user_image_count()
{
    global $wpdb, $post, $current_user;
    get_currentuserinfo();
    $image_count = $wpdb->get_var("SELECT COUNT(user_id) AS total_images FROM {$wpdb->prefix}review_media WHERE post_id=$post->ID AND user_id=$current_user->ID GROUP BY post_id");

    return $image_count;        
}

I hope this would be enough information for what help I am looking for. Please let me know if you guyz need any further information.

Trillion thanks :)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度