我正在嘗試保存一個可變數量的「cols」對象。列數等於標題數量。這是之前的代碼是什麼樣子:我如何動態地填充對象屬性?
if(isset($_POST['submit'])){
$sub = new Sub();
$sub->product_id = $_POST['product_id'];
$sub->col1 = $_POST['col1'];
$sub->col2 = $_POST['col2'];
$sub->col3 = $_POST['col3'];
$sub->col4 = $_POST['col4'];
$sub->col5 = $_POST['col5'];
$sub->col6 = $_POST['col6'];
$sub->col7 = $_POST['col7'];
$sub->col8 = $_POST['col8'];
$sub->col9 = $_POST['col9'];
$sub->col10 = $_POST['col10'];
$sub->col11 = $_POST['col11'];
$sub->col12 = $_POST['col12'];
$sub->col13 = $_POST['col13'];
$sub->col14 = $_POST['col14'];
$sub->col15 = $_POST['col15'];
這是我希望它看起來:
if(isset($_POST['submit'])){
$sub = new Sub();
$sub->product_id = $_POST['product_id'];
$i = 0;
foreach($headers as $header){
$i++ ;
$sub->col.$i = $_POST['col'.$i];
}
如何傳遞變量$我到對象的屬性? $ sub - >(col。$ i)? $ sub - >(col {$ i})?請幫我想出解決辦法=)謝謝
我同意,列類型應該是一個數組。 – slik
好酷 - 這絕對有更多的意義。 –
現在我必須努力將$ sub>列傳遞給我的類並編寫一個函數,根據標題計數拉動標題計數並添加/刪除表中的字段...對嗎? –