2011-05-02 70 views
1

我对此很新,但我会尽我所能地尽我所能解释我的问题。我有两个文件 - 一个名为edit_captions_form.php,其中包含一个名为edit_form的类,它列出了要成为表单一部分的元素。元素的数量是动态决定的,它们是使用循环从数据库中添加的。循环计数器是从sql查询返回并在url中传递的行数。这是代码的相关部分:将一个PHP页面的值传递给另一个的问题

class edit_form extends moodleform { 

function definition() { 

    $numRows = $_GET['numRows']; 


    if(isset($numRows)){ 
     echo "yes" .$numRows; } 
     else 
      echo "no"; 
//result: yes and the value of $numRows 


    $mform = & $this->_form; 
       . 
      //some irrelevant code 
       . 

    $mform->addElement('header', 'editcaptionsheader', $editcaptionsheader .$title); 
       . 
       //some more irrelevant code 
       . 
    $captionResult = mysql_query($captionQuery); 
    $captionsArray = array(); 

    $textFieldAttributes = "size=\"10\" value=\"\""; 

    while ($row = mysql_fetch_array($captionResult)) { 
     array_push($captionsArray, $row); 


    } 


    for ($i = 0; $i < $numRows; $i++) { 

     $startName = "start_" . $i; 
     $startValue = $captionsArray[$i]['start_time']; 
     $endName = "end_" . $i; 
     $endValue = $captionsArray[$i]['end_time']; 
     $captionName = "caption_" . $i; 
     $captionValue = $captionsArray[$i]['caption_text']; 
     $captionIdName = "id_".$i; 
     $captionIdValue = $captionsArray[$i]['caption_id']; 


     // print_object($captionsArray); 


     $mform->addElement('hidden', $captionIdName, $captionIdResult); 
     $mform->addElement('text', $startName, $editstarttimeTrans, $textFieldAttributes); 

     $mform->setDefault($startName, $startValue); 
     $mform->addElement('text', $endName, $editendtimeTrans, $textFieldAttributes); 
     $mform->setDefault($endName, $endValue); 


     //text area to contain caption text 
     $mform->addElement('htmleditor', $captionName, "$editcaptiontextTrans: ", 'wrap="virtual" rows="4" cols="40"'); 
     $mform->setDefault($captionName, $captionValue); 

     //echo "<button type = \"button\", name = \"edit_caption\", onclick = 'updateRecordInDatabase($referenceId, $start, $end, $text)'>Save</button>"; 
     $buttonarray = array(); 
     $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('savechanges')); 
     $buttonarray[] = &$mform->createElement('reset', 'resetbutton', get_string('revert')); 
     $buttonarray[] = &$mform->createElement('cancel'); 
    //add_action_buttons($buttonarray[0] = true); 
     $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); 
     $mform->closeHeaderBefore('buttonar'); 

第二文件称为edit_captions.php并创建的edit_form一个实例,并尝试从表单元素提取值,并再次使用一个循环与来自数导出的计数器的行。这是代码的相关部分:

require_once("edit_captions_form.php"); 

$id = $_GET['id']; 
$numRows = $_GET['numRows']; 

echo " the number of rows: " .$numRows; 
//$numRows doesn't print here 

$pageForm = new edit_form(); 

if($pageForm->is_cancelled()) 
{ 
redirect elsewhere 

} 

else if($fromform = $pageForm->get_data()) 
{ 


for ($i=0; $i<$numRows; $i++) 
{ 
$elementNumber = 3+(5*$i);//3, 8, 13, 18 

$elementArray = $pageForm->_form->_elements; 
$timeAtts = $elementArray[$elementNumber]->_attributes; 
$captionStartTime = $timeAtts['value']; 
$elementNumber = $elementNumber +1; 

$elementArray = $pageForm->_form->_elements; 
$endTimeAtts = $elementArray[$elementNumber]->_attributes; 
$captionEndTime = $endTimeAtts['value']; 
$elementNumber = $elementNumber +1; 

$elementArray =$pageForm->_form->_elements; 
$captionText = $elementArray[$elementNumber]->_value; 

} 

现在,如果我在实际的行数为两个环路计数器硬编码,所有的数组元素的值被存储在$elementArray并且可以被检索,所以在这方面一切都很好。问题是无论我做什么,edit_captions.php文件中的$ numRows变量都没有值,所以循环不执行我无法获取数据。

我想我将有机会获得这一点,因为我用require_once(edit_captions_form.php)我试着使用MySQL查询通过$_GET从函数获取,而不是价值,我想魔术方法__set()__get(),虽然我听不太懂我在做什么,这可能是为什么没有工作,我试图访问$numRows,就好像它是一个edit_form类的变量,但我不认为这是因为它在函数definition()中。我试图在define()之前将它作为一个变量添加,但是这打破了整个表单。我也尝试使用global关键字,但这也没有效果。

再次,道歉,如果我没有解释得很好,但我想不出一个更简洁的方式来陈述问题。我一直试图整理三天,并且无法做到这一点。如果有人能向我解释为什么我不能在第二个文件中访问$numRows循环,我将不胜感激。或者如果这是不可能的,请把我从痛苦中解脱出来,让我知道!

+0

如果你的代码说'回声“行数”。$ numRows行; // $ numRows不在这里打印“这是否意味着你根本没有输出,或者'$ numRows'变量中什么都没有?什么是确切的URL,包括发生这种情况时显示的参数? – Simon 2011-05-02 22:07:59

+0

嗨西蒙,对不起,这是有点误导,我打算在发布问题之前添加更多细节。当显示表单时,echo语句正常打印,url为edit_captions.php?id = 7&numRows = 8。不过,当我点击“保存更改”按钮时,页面会重定向几秒钟到另一个页面,该页面会在重定向到另一个页面之前通知用户编辑已完成。编辑完成页面上的url是edit_captions.php,并在此页面上输出“行数:”,但$ numRows变量中没有任何内容。我希望这有点清楚。 – YodaWren 2011-05-02 23:01:21

回答

3

您的问题是“通知其他页面”。

它不流通GET变量的edit_captions.php因此没有它可以访问:)

+0

谢谢麦克拉肯 - 这很有道理,我有点无视那个页面。我会看看我能做些什么。 – YodaWren 2011-05-03 10:17:30

+0

我摆脱了其他页面,并在表单上放置了一个隐藏元素,并将行号传递给另一个文件,并测试了两个表单都具有相同的值,并且他们解决了一些问题,我想说谢谢那。还没有得到它的工作,但我认为我更接近。 – YodaWren 2011-05-03 20:06:23

相关问题