$html = array();
$sqltourtypes = 'SELECT * FROM tourtypes ORDER BY nTourTypeID ASC';
$sqltours = 'SELECT * FROM tours WHERE nTourTypeID = ? ORDER BY _kpnID ASC';
$tourtypes = $this->db->query($sqltourtypes)->result();
for($i = 0; $i < count($tourtypes); $i++){
$html[] = '<li><a href="#">'.$tourtypes[$i]->_kftDescription.'</a>';
$tours = $this->db->query($sqltours,array($tourtypes[$i]->nTourTypeID))->result();
if(count($tours)>0){
$html[] = '<ul>';
for($ia = 0; $ia < count($tours); $ia++){
$html[] = '<li>'.$tours[$ia]->tDescription.'</li>';
}
$html[] ='</ul></li>';
}else {
$html[] = '</li>';
}
}
return implode('',$html);
下面的代碼我最近不得不改用Laravel框架。我無法讓我的查詢在Laravel中工作。基本上我有兩個表,tourtypes和旅遊。 _kftDescription用於列出ul標籤下的巡視類型,tDescription用於將特定組下的巡迴名稱列爲li標籤。
嘗試轉換查詢時,我總是收到錯誤。任何人都可以建議如何從CodeIgniter實現我的代碼?當nTourTypeID是「1」時,它們屬於遊覽類型「遊輪」。希望是有道理的。
更新:我的應用程序\ HTTP \控制器\ BookingsController.php文件看起來像這樣
namespace App\Http\Controllers;
use App\Models\Bookings;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Validator, Input, Redirect ;
class BookingsController extends Controller {
public function index()
{
$tourTypes = collect(DB::table('tourtypes')->orderBy('nTourTypeID')->get())
->map(function ($item) {
$item->tours = DB::table('tours')->where('nTourTypeID', $item->nTourTypeID)->get();
return $item;
});
return view('bookings', compact('tourTypes'));
}
和預訂的路線是這樣的(我的路線是預訂我不有路線導覽):
Route::get('bookings','[email protected]');
最後\ resources \ views \ bookingings \ index.blade.php文件看起來像是th是:
@extends('layouts.app')
@section('content')
{{--*/ usort($tableGrid, "SiteHelpers::_sort") /*--}}
@if(count($tourTypes))
<ul>
@foreach($tourTypes as $tourType)
<li>
<a href="#">{{ $tourType->_kftDescription }}</a>
@if(count($tourType->tours))
<ul>
@foreach($tourType->tours as $tour)
<li>{{ $tour->tDescription }}</li>
@endforeach
</ul>
@endif
</li>
@endforeach
</ul>
@endif
我仍然得到錯誤
未定義的變量:tourTypes(查看: d:\ XAMPP \ htdocs中\預訂\資源\意見\預訂\ index.blade.php)
當我寫
$tourTypes = DB::table('tourtypes')->orderBy('nTourTypeID', 'ASC')->get();
print_r($tourTypes);
打印
照亮\支持\集合對象([項目:保護] =>數組( [0] => stdClass的對象([nTourTypeID] => 1 [_kftDescription] => 遊輪[_kftColourID] => 003399 )1 => stdClass Object( [nTourTypeID] => 2 [_kftDescription] => 4WD [_kftColourID] =>)[2] => stdClass對象([nTourTypeID] => 3 [_kftDescription] =>珍珠農場 [ _kftColourID] => 00ccff)
因此,該查詢正在工作,但無法打印ul和li標籤,其值爲using;
@if(count($tourTypes))
<ul>
@foreach($tourTypes as $tourType)
<li>
<a href="#">{{ $tourType->_kftDescription }}</a>
@if(count($tourType->tours))
<ul>
@foreach($tourType->tours as $tour)
<li>{{ $tour->tDescription }}</li>
@endforeach
</ul>
@endif
</li>
@endforeach
</ul>
@endif
你會得到什麼樣的錯誤?是否有錯誤訊息?如果是這樣,它說什麼? – waka
首先我得到'未定義的屬性:Illuminate \ View \ Engines \ CompilerEngine :: $ db(View:C:\ XAMPP \ htdocs \ bookings \ resources \ views \ bookings \ index.blad e.php)''然後改變'$ tourtypes = $ this-> db-> query($ sqltourtypes) - > result();'to'$ tourtypes = DB :: table('tours') - > get();'現在我得到這個eror:'ErrorException在e1cff7d5e9e0d2f02a08975fab510e441de69bc5.php第39行:未定義的屬性:stdClass :: $ _ kftDescription只是無盡的不同的錯誤。我提到https://laravel.com/docs/4.2/queries,但無法運行查詢 – hijacker83
爲什麼要使用框架,如果你不使用給定的工具? laravel的正確方法是定義模型和關係。控制器代碼會簡單得多。並且HTML代碼應該移到模板中。 –