Evaluate the php multi-array combination writing method to the database

Source: Internet
Author: User
Php multi-array combination writing method this post was last edited by wjm_hi at 15:55:24

I made a score management system.

// Subject
$ Xk = array ();

// Grade
$ Nj = array ();

// Class
$ Bj = array ();

There are three arrays, which are generated based on the formula * grade * class, and then written to the database.
For example:
When count ($ xk) is 2, two subjects are selected.
When count ($ nj) is 3, three grades are selected.
When count ($ bj) is 7, it indicates that 7 grades are selected.
2*3*7 data records should be inserted into the database. how can this problem be achieved?


Reply to discussion (solution)

Php multi-cycle, and then write the data subscript loop to the database. The PHP infrastructure is poor. please help me.

$ Xk = Array ("1", "2 ");
$ Nj = Array ("1", "2", "3 ");
$ Bj = Array ("1", "2", "3", "4", "5", "6", "7 ");
$ Xk = count ($ xk );
$ Nj = count ($ nj );
$ Bj = count ($ bj );
$ Test = "$ xk * $ nj * $ bj ";
Echo $ test;
Exit;
?>

Dizzy, wrong understanding. ,

What should I do?

$ Xk = Array ("1", "2 ");
$ Nj = Array ("1", "2", "3 ");
$ Bj = Array ("1", "2", "3", "4", "5", "6", "7 ");
$ Xk = count ($ xk );
$ Nj = count ($ nj );
$ Bj = count ($ bj );
$ Test = "$ xk * $ nj * $ bj ";
Echo $ test;
Exit;
?>

How can I write an INSERT INTO statement?


Insert into 'examinfo' ('NJ ', 'BJ', 'xk') VALUES ($ nj, $ bj, $ xk ), $ xk * $ nj * $ bj should be inserted after VALUES. how can this problem be solved?

Find the code to calculate Cartesian product in the essence area

Write a stupid method.
$ Xk = Array ("1", "2 ");
$ Nj = Array ("1", "2", "3 ");
$ Bj = Array ("1", "2", "3", "4", "5", "6", "7 ");
$ Arr = array ();
For ($ I = 0; $ I For ($ j = 0; $ j For ($ k = 0; $ k $ Arr [] = array ($ xk [$ I], $ nj [$ j], $ bj [$ k]);
}
}
}
Foreach ($ arr as $ row ){
// Insert data
}

Is this complicated? Can I use?

For ($ I = 0; $ I

$ Xk = Array ("1", "2"); $ nj = Array ("1", "2", "3 "); $ bj = Array ("1", "2", "3", "4", "5", "6", "7 "); $ arr = array (); for ($ I = 0; $ I
  
  

Write a stupid method.
$ Xk = Array ("1", "2 ");
$ Nj = Array ("1", "2", "3 ");
$ Bj = Array ("1", "2", "3", "4", "5", "6", "7 ");
$ Arr = array ();
For ($ I = 0; $ I For ($ j = 0; $ j For ($ k = 0; $ k $ Arr [] = array ($ xk [$ I], $ nj [$ j], $ bj [$ k]);
}
}
}
Foreach ($ arr as $ row ){
// Insert data
}

This seems feasible. how can I write an SQL statement to insert data in foreach?

$ Xk = Array ("1", "2"); $ nj = Array ("1", "2", "3 "); $ bj = Array ("1", "2", "3", "4", "5", "6", "7 "); $ arr = array (); for ($ I = 0; $ I
     
    


Can I insert // data to echo the call method.

$ Xk = Array ("1", "2"); $ nj = Array ("1", "2", "3 "); $ bj = Array ("1", "2", "3", "4", "5", "6", "7 "); $ arr = array (); for ($ I = 0; $ I
       
      

I want this effect. I want to INSERT INTO 'examinfo' ('NJ ', 'BJ', 'xk') VALUES ($ nj, $ bj, $ xk) put it in the // Insert data location. how can I write it.

$ Xk = Array ("1", "2"); $ nj = Array ("1", "2", "3 "); $ bj = Array ("1", "2", "3", "4", "5", "6", "7 "); $ arr = array (); for ($ I = 0; $ I
        
        

$ Xk = Array ("1", "2"); $ nj = Array ("1", "2", "3 "); $ bj = Array ("1", "2", "3", "4", "5", "6", "7 "); $ arr = array (); for ($ I = 0; $ I
           
          


Thank you very much, but this SQL statement can be optimized. INSERT INTO 'examinfo' ('NJ ', 'BJ', 'xk') VALUES ($ nj, $ bj, $ xk) this statement is always in a loop. if insert into 'examinfo' ('NJ ', 'BJ', 'xk') VALUES (1, 1, 1 ), SQL statements such as (1, 1, 2), (1, 1, 3) are perfect.

Dizzy. Dude, why don't you think about it. You cannot expect others to do everything well for you. others can only give you ideas ..

Dizzy. Dude, why don't you think about it. You cannot expect others to do everything well for you. others can only give you ideas ..


Well, with this idea, I can continue. thank you. I have thought about optimization. thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.