0

我有一个用于预约的 joomla 组件,并且我有用于预约开始日期的复选框...我的问题是我一次只能预约一个,我希望能够选中多个框,因此这些框的值可以保存在mysql中,当我选中多个复选框时,只有最后一个选中的复选框保存在数据库中...

这是来自joomla组件的代码,我认为必须对其进行调整,所以如果可以的话,请帮助大家...

这是复选框的代码...

$timetableHTML .= '<td class="timeSlot timeFree" ><input type="checkbox" name="appointment[]" value="'.$startKey.'" onclick="changeTimes(\''.$calendar->min_duration.'\',\''.$startKey.'\',\''.$endKey.'\')"/></td>';

这是组件控制器中的保存功能...

function save() {
    global $app;
    JRequest::checkToken() or jexit( 'Invalid Token' );

    $db =& JFactory::getDBO();
    $row =& JTable::getInstance('appointments', 'Table');
    $post   = JRequest::get( 'post',4 );
    if (!$row->bind( $post )) { JError::raiseError(500, $row->getError() ); }
    for ($i=1;$i<=10;$i++) {
        if (is_array($row->{'field'.$i})) $row->{'field'.$i} = implode('|',$row->{'field'.$i}); $row->{'field'.$i} = strip_tags($row->{'field'.$i});
    }
    if (!$row->check()) { JError::raiseError(500, $row->getError() ); }
    if (!$row->store()) { JError::raiseError(500, $row->getError() ); }
    $row->checkin();

            if ($this->config->emails){
                $this->notifyOwner(array($row->id));
                $this->notifyAppointee(array($row->id));
            }

            $url = JRoute::_('index.php?option=com_jxtcappbook'.(JRequest::getInt( 'pop', 0) ? '&view=complete&tmpl=component' : ''));
    $this->setRedirect($url ,JText::_( 'Termin je zakazan!'.$pop ));
}

我用谷歌搜索了一下,我想我需要用数组设置 jrequest::get,对吗?

4

1 回答 1

0

假设 Joomla >1.6。由于您使用JRequest了合理的数量:

$appointments = JRequest::getVar('appointments', null, 'post', 'array');

或者更好,因为这将在 3.0 后被弃用,您可以使用 JInput:

$jinput = JFactory::getApplication()->input;
$appointments = $jinput->post->get('appointments', 'null', 'ARRAY');

清理输入并添加到数据库:

foreach (array_keys($appointments ) as $element) {
    $myappointments[] = $db->quote($element);
}


// construct query using implode and use comma separator
$myappointments = implode(',', $myappointments );
$db =& JFactory::getDBO();
$query  = $db->getQuery(true);
$query = sprintf('UPDATE $db->quoteName(#__mytable) SET $db->quote(...) WHERE $db->quote(...) IN (%s)', $myappointments );
$db->setQuery($query);
$db->query();

你明白了……

编辑(根据您的评论):

我仍然不知道你想要达到什么目的。所以我很难提供方向。我会再试一次。我猜你想把从表单中检查的值放入数据库中。

//this pulls out the values in an array of all the things that have been "checked" (selected in the checkbox)
$jinput = JFactory::getApplication()->input;
$appointments = $jinput->post->get('appointments', 'null', 'ARRAY');

//**This is not code you need** I just want to illustrate what you are getting.
//This is looping through the values of the checkboxes to see what you have
for($i=0; $i < count($appointments); $i++){
    echo "Selected " . $appointments[$i];
}

我之前提供的代码向您展示了如何获取值并将其存储到数据库中。我无法对数据库给出说明,因为我不知道数据库结构。

于 2013-02-28T01:57:51.690 回答