2014-09-22 12 views
0

我想使用我們所稱的C中的結構,但使用PHP,我知道在這裏他們被稱爲類,但我需要使用該類數組對於選擇標籤名,我這樣做如何使用PHP類數組作爲選擇標記的名稱HTML

<?php 

class info_subject{ 

    public $code_su; 
    public $time_su; 
    public $selecction_su; 
} 

$subjects= new info_subjects(); 

$i=0; 

//THE DATABASE CONNECTION WORKS FINE, I IGNORED CODING ABOUT DATABASE BECAUSE THAT'S NOT THE 
//PROBLEM, JUST FOCUS IN THE STATEMENT OF THE ARRAYS IN THE TAGS NAMES PLEASE 

while($line = pg_fetch_array($result, null, PGSQL_NUM))//getting some stuff from postgrest 
{ 

     echo "$line[0]";//I am printing this 
     echo "$line[1]";//I am printing this 

     //here i am creating selects in every loop with some options, and i want to save the 
     //result of the selection in the field code_su of the array of classes 
     echo "<select name=$subjects[$i]->code_su>"; 
     echo "<option value='hola'>hola</option>"; 

     //here i am creating checkbox in every loop, and i want to save the 
     //result of the checkbox in the field selection_su of the array of classes 
     echo "<input type='checkbox' name=$subjects[$i]->selection_su>"; 

    $i++; 
    } 


?> 

的問題是,它不工作,我想我犯了一個錯誤,在的輸入和選擇名字的聲明,就像我以前說過,我需要一個類數組。

+0

嘗試使用:echo「 seleccion_as。」\「>」 – 2014-09-22 06:39:48

+0

@oPi只有英文plz – Justinas 2014-09-22 06:41:38

+0

@Justinas多數民衆贊成什麼即時消息告訴他關於增值稅名稱; – 2014-09-22 06:42:30

回答

0

問題是你沒有正確添加變量(也沒有引號)。試着用:

echo "<select name=\"".$asignaturas[$i]->codigo_as . "\">"; 

echo "<input type='checkbox' name=\"".$asignaturas[$i]->seleccion_as."\">" 

問候。

0

1)你是呼應HTML錯誤(缺少'echo "<select name='{$asignaturas[$i]->codigo_as}'>";

2)您$asignaturas不是一個數組。它只有一個班級。像這樣使用它:echo $asignaturas->coding_as;

3)(旁註)按標準,類名是CamelCases,它的名稱與文件名相同。

0

看起來好像你正在嘗試使用類本身作爲一個無法完成的數組。

放在一個構造函數定義一些在這裏您變量:

class info_asignatura{ 

    public $codigo_as; 
    public $periodo_as; 
    public $seleccion_as; 

    function __construct(){ 
     $this->seleccion_as = array(); 
    } 

} 

改變此聲明:

echo "<input type='checkbox' name=$asignaturas->seleccion_as[$i]>"; 

雖然,我擔心這會不會爲你做的伎倆。因爲每次加載此頁面時,seleccion_as將在構造類時定義爲一個數組。這將覆蓋先前聲明的任何內容。

你需要什麼來獲得你的目標是爲你的代碼實現會話。

相關問題