Results 1 to 2 of 2

Thread: POST From MIN to MAX

  1. #1
    Join Date
    May 2010

    POST From MIN to MAX

    Hey all,
    I'm currently working on a new feature for a website. Basically, we use a calendar system, and i'm trying to create a method to "one-click signup" to every event.

    To make things simple, the table is called "table_name", and the field i want to work with will be "field_name".

    I've got most of the code and theory down. However, the crucial part, to avoid simply creating loads of database fields and thus putting loads of strain on the server, is detecting the current minimum (SELECT MIN) and the current maximum (SELECT MAX) from field_name.

    Here's the 2 queries i'd be wanting to run (it's extracted from PHP, so forgive any syntax errors);
    SELECT MAX(field_name) FROM table_name WHERE field_name > '115'
    SELECT MIN(field_name) FROM table_name WHERE field_name > '115'

    What i want to do is for every value between 115 and the maximum, i want it to run a specific query. Basically, i guess i want to assign a variable to field_name so that i can post this value to the database along with a bunch of other values that will be going to other fields in the same table of the same DB.

    It's hard to explain what i want to achieve without being able to pop out my brain and show you what i'm thinking. I've only started actually working with SQL and PHP at any sort of decent level a few days ago.

    Here's the first few lines of the sort of code i'd be using (table names and most field names edited for obvious reasons), which has been tested and works, but might also help explain my situation;

    $sql="INSERT INTO table_name (field_name,field_2,field_3,field_4,field_5,field_6,field_7,timestamp,field_9)
    This would go on right up until a field_name was a really high number, but the problem is that going above the current max in "field_name" may cause errors at user level (and then they'd complain i broke it, etc). Additionally, it's a lot of copy-pasting of lines, as well as a lot of queries to run, which causes unnecessary server load. It's also not completely futureproof because eventually, we'll get to a field_name value of 1001, and i'd have to update the code yet again for the next 1000.

    I really just want it to find out the max value in field_name, assign a variable i can paste into the query above (e.g. "(VAR,'1','0','0','1','2','','1274920279','4')", where "VAR" would represent the variable), and then run that query potentially saving myself a lot of time, and moreover making it cleaner code.

    I've no problem making it work for a single ID in "field_name" as it can be manually written without working on a procedure for the variable, but making it a variable within a query is something i'm not so sure about.

    As there's multiple users using this system, i need to actually develop this before i can push it out to a more live environment, and as such, i can adapt the code to work with other fields such as the one representing the user ID (in this case, field_name isn't the user ID, it's the event ID).

    I'm confident what i'm after can be done with around 10 lines including the query execution itself.

    As said earlier, i'm fairly new to PHP and SQL, but i've got numerous years in Website Design itself (working with Joomla! CMS, and SMF Forum currently), as well as more experience in Desktop Application development (and around 8 or so years of computer tech/support/system building and optimisation). I feel that learning "the backbone of the web" so to speak would be beneficial to my skillset, and may help me in progressing my career.

    Sorry for the lengthy post.
    Any ideas on how i can achieve what i'm after?

  2. #2
    Join Date
    Mar 2010
    Quote Originally Posted by Syn89 View Post
    .......What i want to do is for every value between 115 and the maximum, i want it to run a specific query.....
    Unfortunately my attention span isn't what it used to be when reading looooooong posts, but hopefully just taking the above extract will at least help you head in the right direction.

    PHP Code:
    SELECT MAX(field_nameFROM table_name WHERE field_name '115';
    $rs mysql_query($query$conn);
    $row=mysql_fetch_assoc($rs)) {
    $query 'your query for each field_name';
    $rs mysql_query($query$conn);

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.16128 seconds
  • Memory Usage 2,848KB
  • Queries Executed 13 (?)
More Information
Template Usage (35):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (1)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (2)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (2)postbit
  • (2)postbit_onlinestatus
  • (2)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates