vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   how to UPDATE multiple inputs (https://vborg.vbsupport.ru/showthread.php?t=57301)

cinq 09-22-2003 01:50 AM

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. :D

Velocd 09-22-2003 02:24 PM

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.

filburt1 09-22-2003 02:36 PM

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...

Dean C 09-22-2003 03:31 PM

Then how would we get round this one?

Velocd 09-22-2003 05:40 PM

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.

Dean C 10-06-2003 06:15 PM

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?

Admin 10-07-2003 12:15 PM

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.

TECK 10-17-2003 12:59 AM

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? :D

cinq 10-17-2003 02:26 AM

Quote:

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

:D

Will try some of the code provided here, thanks guys :)

Dean C 10-17-2003 09:10 AM

Quote:

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

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 :)


All times are GMT. The time now is 03:22 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01179 seconds
  • Memory Usage 1,763KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_code_printable
  • (6)bbcode_php_printable
  • (4)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete