Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions

Reply
 
Thread Tools Display Modes
  #1  
Old 09-22-2003, 01:50 AM
cinq's Avatar
cinq cinq is offline
 
Join Date: Oct 2002
Posts: 1,398
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default how to UPDATE multiple inputs

I wish to display order of certain categories for eg.

Cat 1 , order : 1
Cat 2 , order : 2

( Runs thru the catorder table to display category names in ASC order of catorders. )

The orders are displayed as input boxes, which can be changed in value , say to reverse the order, i will put 2 in Cat 1's order input box and vice versa.

At the end of the form, i will click on a Submit button and the categories will have their orders updated accordingly ( i have a table column for Category order )

How should i go about doing this ?

Thanks for any help rendered.
Reply With Quote
  #2  
Old 09-22-2003, 02:24 PM
Velocd's Avatar
Velocd Velocd is offline
 
Join Date: Mar 2002
Location: CA University
Posts: 1,696
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
$fetch_cats $DB_site->query("SELECT id,title,order FROM `categories` ORDER BY order DESC");

while (
$category $DB_site->fetch_array($fetch_cats))
{
  echo 
$category['title'] . '   ';
  echo 
'<input type="text" name="cat_order['.$category['id'].']" value="'.$category['order'].'" />';

Then the POST process portion:

PHP Code:
$fetch_cats $DB_site->query("SELECT id,title,order FROM `categories`");

while (
$category $DB_site->fetch_array($fetch_cats))
{
    
$DB_site->query("UPDATE `categories` SET order=$_POST[cat_order][$category[id]] WHERE id=$category[id]");

This should work. You might get a parse error or two since I compiled this very quickly, but it shouldn't be hard to work around.
Reply With Quote
  #3  
Old 09-22-2003, 02:36 PM
filburt1 filburt1 is offline
 
Join Date: Feb 2002
Location: Maryland, US
Posts: 6,144
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Velocd
PHP Code:
$fetch_cats $DB_site->query("SELECT id,title,order FROM `categories` ORDER BY order DESC");

while (
$category $DB_site->fetch_array($fetch_cats))
{
  echo 
$category['title'] . '&nbsp;&nbsp;&nbsp;';
  echo 
'<input type="text" name="cat_order['.$category['id'].']" value="'.$category['order'].'" />';

Then the POST process portion:

PHP Code:
$fetch_cats $DB_site->query("SELECT id,title,order FROM `categories`");

while (
$category $DB_site->fetch_array($fetch_cats))
{
    
$DB_site->query("UPDATE `categories` SET order=$_POST[cat_order][$category[id]] WHERE id=$category[id]");

This should work. You might get a parse error or two since I compiled this very quickly, but it shouldn't be hard to work around.
Very bad idea to query in a loop...
Reply With Quote
  #4  
Old 09-22-2003, 03:31 PM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Then how would we get round this one?
Reply With Quote
  #5  
Old 09-22-2003, 05:40 PM
Velocd's Avatar
Velocd Velocd is offline
 
Join Date: Mar 2002
Location: CA University
Posts: 1,696
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

There aren't many fast ways around it.

The method I provided is the exact vBulletin uses to update the order of forums in the Admin Cp:

PHP Code:
// ###################### Start do order #######################
if ($_POST['do'] == 'doorder')
{
    
globalize($_POST, array('order'));

    if (
is_array($order))
    {
        
$forums $DB_site->query("SELECT forumid,displayorder FROM " TABLE_PREFIX "forum");
        while (
$forum $DB_site->fetch_array($forums))
        {
            
$displayorder intval($order["$forum[forumid]"]);
            if (
$forum['displayorder'] != $displayorder)
            {
                
$DB_site->query("
                    UPDATE " 
TABLE_PREFIX "forum
                    SET displayorder = 
$displayorder
                    WHERE forumid = 
$forum[forumid]
                "
);
            }
        }
    }

    
build_forum_permissions();

    
define('CP_REDIRECT''forum.php?do=modify');
    
print_stop_message('saved_display_order_successfully');

The only main difference is this:
PHP Code:
            $displayorder intval($order["$forum[forumid]"]);
            if (
$forum['displayorder'] != $displayorder
Which is a check to ensure that it's not quering and updating a row when it has yet to be modified.

You could probably apply the same to the code above.
Reply With Quote
  #6  
Old 10-06-2003, 06:15 PM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I feel ashamed to be bumping a reasonably old thread but i'm ecountering a similar issue at the moment. I want to update multiple rows in a database with one query. Any solution?
Reply With Quote
  #7  
Old 10-07-2003, 12:15 PM
Admin's Avatar
Admin Admin is offline
Coder
 
Join Date: Oct 2023
Location: Server
Posts: 1
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Supposedly this might work:
Code:
UPDATE table SET order = IF(id = 1, 1, IF(id = 2, 2, 3)) WHERE id IN (1, 2, 3);
But I doubt it.
Reply With Quote
  #8  
Old 10-17-2003, 12:59 AM
TECK's Avatar
TECK TECK is offline
 
Join Date: Nov 2001
Location: Canada
Posts: 4,182
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Chen
Supposedly this might work:
Code:
UPDATE table SET order = IF(id = 1, 1, IF(id = 2, 2, 3)) WHERE id IN (1, 2, 3);
But I doubt it.
Whoot? You are still alive?
Reply With Quote
  #9  
Old 10-17-2003, 02:26 AM
cinq's Avatar
cinq cinq is offline
 
Join Date: Oct 2002
Posts: 1,398
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by TECK
Whoot? You are still alive?


Will try some of the code provided here, thanks guys
Reply With Quote
  #10  
Old 10-17-2003, 09:10 AM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by TECK
Whoot? You are still alive?
He's around all the time in the vBulletin.org chat TECK

Btw cinq - the best way I found of doing this was via vb3's method which VelocD highlighted
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:41 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04963 seconds
  • Memory Usage 2,280KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_code
  • (6)bbcode_php
  • (4)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete