$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代码应该移到模板中。 –