dongpou1935 2018-02-28 10:08
浏览 56

MVC模型如何在多个视图之间传输数据

Hello im trying to create a semi MVC project and im new to this so what ive done so far and what ill explain as my problem here may be completely bad on itself so sorry if that is the case.

  • View 1 tables.phtml
  • View 2 Calc.phtml
  • View 3 Result.phtml
  • Controller Tables.php
  • Model Tables.php

On my first view is an Form where you input 2 numbers to generate an multiplication table and it puts this in an variable called $aTempArray it looks like this:

Array ( 
  [0] => Array ( [0] => 10 [1] => 6 [2] => 60 )
  [1] => Array ( [0] => 7 [1] => 6 [2] => 42 ) 
  [2] => Array ( [0] => 10 [1] => 6 [2] => 60 )
  [3] => Array ( [0] => 7 [1] => 6 [2] => 42 ) 
  [4] => Array ( [0] => 2 [1] => 6 [2] => 12 ) 
  [5] => Array ( [0] => 1 [1] => 6 [2] => 6 ) 
  [6] => Array ( [0] => 10 [1] => 6 [2] => 60 )
  [7] => Array ( [0] => 6 [1] => 6 [2] => 36 ) 
  [8] => Array ( [0] => 3 [1] => 6 [2] => 18 ) 
  [9] => Array ( [0] => 5 [1] => 6 [2] => 30 )
)

On my second view it generates a form based on the array like this:

10 * 6 = ... <(this is a textbox)
7 * 6 = ... <(this is a textbox)

So when the user submits this second form on the second view I want it to OR send the $aTempArray, OR generate a new one like this:

Array ( 
 [0] => Array ( [0] => 10 [1] => 6 ['answer'] => ... ) 
 [1] => Array ( [0] => 7 [1] => 6 ['answer'] => ... ) 
)

However, I have no clue on how to send the $aTempArray becouse on the second submit it is empty becouse of the new controller/model that gets generated(or just reset becouse it reloads), and also I dont have a specific idea on how to get the sum data back when I submit the form on the second page. (the 10 * 6 for example becouse this is just text at this point.)

Ive tried a lot and found some things that do make this work but I want to know if there are better ways to do this.

  • 1 is Saving the array in a session variable for example works but I want to avoid using sessions.
  • 2 is making hidden input fields where I save the sums numbers without the answer like this: <input type="hidden" name="rebuildArray" value="10,6/7,6 etc">

And to recreate the array I do something like:

  1. Str_split on /
  2. Put in array
  3. str_split on ,
  4. Put this in arrays inside the main array.
  5. Foreach over every item in the array
  6. if((currentsubarray[0] * currentsubarray[1]) == answersarray[currentpos]) Answer is correct else answer is not correct

So can someone say if one of these ways is a semi correct way to do it or give me a push in a direction on how to so this correctly in an MVC stylish way.

And again, I just started with MVC so if this is completely wrong in general im sorry.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程