簡體   English   中英

從Joomla中的MySQL數據庫填充PHP下拉列表

[英]Populate a PHP Dropdown List from MySQL Database in Joomla

我是一個非常不熟練的php程序員,我做了一些asp.net編程,但是從來沒有php。

我需要添加一個下拉列表,其中包含來自mysql數據庫中表的值。 我手動創建了一個表格訓練:id訓練日期小時openseats

而且我需要在下拉列表中顯示此日期和小時,因此一旦用戶clikc提交了該日期和時間,就將其存儲到名為jos_jquarks_users_acknowledge的表中

您能幫我如何簡化下拉菜單嗎?

碼:

{source}
<!-- You can place html anywhere within the source tags -->
<?php 
// If the constant _JEXEC is not defined, quit now.
// This stops this script from running outside of the system.
defined( '_JEXEC' ) or die( 'Restricted access' );
?>

<?php

$user = JFactory::getUser(); 
$id = $user->get('id'); 
$name = $user->get('name');
$username = $user->get('username'); 
$department = $user->get('department');

$vardate = date("Y-m-d H:i:s");

$acknowledge = 1;
$courseTitle = $mainframe->getPageTitle();

$courseDate = ;
$courseHour =;


/***************************************/

$db = &JFactory::getDBO();

$query = "
INSERT INTO
`jos_jquarks_users_acknowledge`
(
course_name,
user_id,
employeeNumber,
department,
name,
acknowledge,
timeStamp,courseDate,
courseHour
)
VALUES
(
'{$courseTitle}',
'{$id}',
'{$username}',
'{$department}',
'{$name}',
'{$acknowledge}',
'{$vardate}',
'{$courseDate}',
'{courseHour}'

 )";

$db->setQuery($query);

$db->query();


if($db->getErrorNum()) { 
JError::raiseError( 500, $db->stderr()); 
}

?>

<form name="quiz_info" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> 

<?php echo JText::_('Do you want to enroll into the course?') ; ?>

<? $queryCourses="SELECT training_id,training,trainingDate FROM training"; ?>

$result = mysql_query ($queryCourses); 
echo "<select name=courseDates value=''>Date</option>"; 
// printing the list box select command 

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt 
echo "<option value=$nt[id]>$nt[training]</option>"; 
/* Option values are added by looping through the array */ 
} 
echo "</select>";//Closing of list box

<input id="proceedButton" name="proceedButton" value="Acknowledge" type="submit" />

<input type="hidden" name="layout" value="default" /> <?php echo JHTML::_( 'form.token' ); ?>

</form>

{/source} 

組件控制器/任務功能:

public  function ShowData()        {
    $course_id = JRequest::getVar('id');

    $db = JFactory::getDBO();
                $query = $db->getQuery(true);
                $query->select('*');
                $query->from('#__tablename');
                $query->where('courseid =\'' . $course_id.'\'');
                $db->setQuery((string)$query);

$data=$db->loadObjectList();

    $option = '<option value="0">choose...</option>';
     foreach ( $data  as $row)  {

     $option .= '<option value="' . $row->candidateid . '">' . $row->firstname .' '. $row->lastname. '</option>';

    }
    echo json_encode(array('options' =>$option));
jexit(); 

 }

阿賈克斯:

從某個組合(選項選定的值)中搜索並在其他組合框中列出

$("select#searchcombo").change(function(){

        $("select#listingcombo").html("<option>wait...</option>");

        var id = $("select#searchcombo option:selected").attr('value');           
     var url='index.php?option=com_example&task=methodName&format=json';
    var dat = {'id':id};
            $.ajax({ 
                type: "POST", 
                cache: false, 
                url: url, 
                 data: dat,  
                dataType: "json", 
                success: function(data) {           
            //alert(data['options']);
        $("select#listingcombo").html(data['options']);
    }, error:function(xhr, status, data) {
            //alert("Status "+status + xhr.responseText );
    }
            }); 

      }); 

很難知道從哪里開始嘗試回答。 上面的代碼中有多少是實際的,而在演示問題時出於演示目的有多少? 我假設您不會在每次頁面加載時將數據插入數據庫中。 我假設您不會用'jos_'前綴對數據庫表名稱進行硬編碼。 實際執行此操作時,應使用不帶引號的'#__'。

返回錯誤的數據庫查詢是否需要終止腳本並引發服務器500錯誤? 我認為檢測並攔截錯誤並進行適當處理會更好。

標簽之后有單詞date,然后是--即關閉從未打開過的選項標簽。 我假設您打算以某種方式標記選擇項-最好使用實際標簽來執行此操作,而不是在選擇項中填充虛擬選項。

如果您不將數組鍵用引號引起來,則可能會生成警告-這樣做$ nt ['training']而不是$ nt [training]

您可能需要檢索關聯數組而不是標准序數數組。

然后,您在Joomla的“外部”運行數據庫查詢。 您已經檢索了數據庫對象-應該使用它,而不是直接運行mysql_query。

代替這個:

$queryCourses="SELECT training_id,training,trainingDate FROM training"; ?>
$result = mysql_query ($queryCourses);

您可能需要做更多這樣的事情

// http://docs.joomla.org/How_to_use_the_database_classes_in_your_script#loadAssoc.28.29

$queryCourses="SELECT training_id,training,trainingDate FROM training"; ?>
$db->setQuery($queryCourses);
$db->query();

$num_rows = $db->getNumRows();
if(! $num_rows){ 
    // return or die or something - there ar no results
}
while($nt = $db->loadAssoc()){

在這條線上

echo "<option value=$nt['id']>$nt['training']</option>"; 

您可能應該這樣做:

echo "<option value=\"{$nt['id']}\">{$nt['training']}</option>"; 

因為整行都用引號引起來,所以用大括號清楚地描述了變量,並記住在value參數周圍加上引號,以防您決定在其中添加其他變量並包含空格等。

從您的描述中,您想向用戶顯示日期-您可能想添加一個額外的變量-在...部分中可能是兩個,也許是這樣的:

echo "<option value=\"{$nt['id']}\">{$nt['training']} {$nt['trainingDate']}</option>"; 

我確定還有其他事情我想念的-我已經假設您的數據庫查詢大致正確並且可以返回結果,但是那里應該有足夠的指針來使您走上正確的軌道。

最終-構建/填充選擇列表時,您可以構建數據結構並獲取Joomla的jHTML類來為您完成繁重的工作。 我不知道有時候,過多的抽象是否比滾動自己的工作會產生更多的工作-但是,可以選擇了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM