ติดปัญหา ทำอย่างไงจะให้ Query ข้อมูลได้รวดเร็วยิ่งขึ้น

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network

Moderators: mindphp, ผู้ดูแลกระดาน

makup
PHP Super Member
PHP Super Member
Posts: 397
Joined: 05/10/2020 10:02 am

ติดปัญหา ทำอย่างไงจะให้ Query ข้อมูลได้รวดเร็วยิ่งขึ้น

Post by makup »

มีการส่งค่า Form มา showAirflight.blade.php

Code: Select all

<thead class="thead-dark">    
    <form class="" method="get" name="search" action="/searchAirflight/search">

        <tr>
          <th scope="col">ลำดับ</th>
          <th scope="col">
             <select name="name" id="name"  >

      <option value="0" selected="selected">---- เลือกทั้งหมด ----</option>
      <?php $number = 1; ?>
  
      @foreach($airport as $airports)
      <option value="{{$airports->id}} " {{ ( $airports->id == $flight_1['name']) ? 'selected' : '' }}
      >{{$number}}<?php echo ".";?>{{$airports->name}} </option>
      <?php $number++; ?>
      @endforeach
    
    </select>


            <br><br>ชื่อที่ตั้งสนามบิน</th>
          <th scope="col">

            <select name="name_airline" id="name_airline">
      <option value="0" selected="selected">---- เลือกทั้งหมด ----</option>
       <?php $number = 1; ?>
     @foreach($airline as $airlines)
      <option value="{{$airlines->id}}" {{ ( $airlines->id == $flight_1['name_airline']) ? 'selected' : '' }}
        >{{$number}}<?php echo ".";?>{{$airlines->name_airline}}</option>
      <?php $number++; ?>  
      @endforeach
    </select>

            <br><br>ชื่อสายการบิน</th>
          <th scope="col">

             <select name="path" id="path">
      <option value="0" selected="selected">---- เลือกทั้งหมด ----</option>
      <?php $number = 1; ?>
     @foreach($airport as $airports)
      <option value="{{$airports->id}}" {{ ( $airports->id == $flight_1['path']) ? 'selected' : '' }}
    >{{$number}}<?php echo ".";?>{{$airports->name}}</option>
    <?php $number++; ?>  
     @endforeach
    </select>

            <br><br>สนามบินปลายทาง</th>
          <th scope="col">วันที่</th>
          <th scope="col">เวลาออกเดินทาง</th>
          <th scope="col">ราคา</th>
          <th scope="col">รุ่นเครื่องบิน</th>
          <th scope="col">แก้ไข</th>
          <th scope="col">

              <button type="submit" name="submit" class="btn btn-primary" name="submit">ค้นหา</button>
            <br><br>ลบ</th>
        </tr>
        </form>      
      </thead>
 
และส่งเข้าไปที่ Controller_show_airflight.php วิ่งที่ไป Function search()
90.JPG
90.JPG (72.14 KiB) Viewed 276 times
ตัวอย่าง Code : Controller_show_airflight.php

Code: Select all

public function search(Request $request)
    {



          $name = $request->name;
          $name_airline = $request->name_airline;
          $path = $request->path;
          $flight_1 = $request->all();
         
        

        $flight = Airflight::where('id_airport' , '=' , "{$name}")
        ->OrWhere('id_airline' , '=' , "{$name_airline}")
        ->OrWhere('path' , '=' , "{$path}")
        ->paginate(5);
       
       
       
        return view("showAirflight" )
        ->with('flight', $flight)
        ->with('flight_1', $flight_1)   
        ->with('airport',Airport::all())
        ->with('airline',Airline::all())
        ->with('flight_path',Airflight::all())

        ;                                               

    }
 
User avatar
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
Posts: 25892
Joined: 22/09/2008 6:18 pm
Contact:

Re: ติดปัญหา ทำอย่างไงจะให้ Query ข้อมูลได้รวดเร็วยิ่งขึ้น

Post by mindphp »

Code: Select all

  $flight = Airflight::where('id_airport' , '=' , "{$name}")
        ->OrWhere('id_airline' , '=' , "{$name_airline}")
        ->OrWhere('path' , '=' , "{$path}")
        ->paginate(5);
เช็คก่อนว่า คนค้นหา เขาค้นหา ส่วนไหนเข้ามาก่อน เอามาทำเป็นคำสั่ง SQL เพื่อค้นหา
ไม่จำเป็นต้อง ใช้ or สำหรับข้อมูลที่ไม่เกี่ยวข้อง
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
makup
PHP Super Member
PHP Super Member
Posts: 397
Joined: 05/10/2020 10:02 am

Re: ติดปัญหา ทำอย่างไงจะให้ Query ข้อมูลได้รวดเร็วยิ่งขึ้น

Post by makup »

mindphp wrote: 30/10/2020 7:50 pm

Code: Select all

  $flight = Airflight::where('id_airport' , '=' , "{$name}")
        ->OrWhere('id_airline' , '=' , "{$name_airline}")
        ->OrWhere('path' , '=' , "{$path}")
        ->paginate(5);
เช็คก่อนว่า คนค้นหา เขาค้นหา ส่วนไหนเข้ามาก่อน เอามาทำเป็นคำสั่ง SQL เพื่อค้นหา
ไม่จำเป็นต้อง ใช้ or สำหรับข้อมูลที่ไม่เกี่ยวข้อง
รับทราบครับ
makup
PHP Super Member
PHP Super Member
Posts: 397
Joined: 05/10/2020 10:02 am

Re: ติดปัญหา ทำอย่างไงจะให้ Query ข้อมูลได้รวดเร็วยิ่งขึ้น

Post by makup »

thatsawan wrote: 30/10/2020 7:48 pm echo $flight ; ดูคำสั่ง SQL หน่อย
ใช้ echo ไม่แสดงผล , ต้องใช้ print_r ครับ

Illuminate\Pagination\LengthAwarePaginator Object ( [total:protected] => 7 [lastPage:protected] => 2 [items:protected] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\Airflight Object ( [connection:protected] => mysql [table:protected] => airflights [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [id] => 58 [id_airport] => 12 [id_airline] => 9 [path] => 13 [date] => 2020-10-10 [timeout] => 22:56:00 [price] => 200.00 [model_aircraft] => AH-78 [created_at] => 2020-10-30 11:53:23 [updated_at] => 2020-10-30 11:53:23 ) [original:protected] => Array ( [id] => 58 [id_airport] => 12 [id_airline] => 9 [path] => 13 [date] => 2020-10-10 [timeout] => 22:56:00 [price] => 200.00 [model_aircraft] => AH-78 [created_at] => 2020-10-30 11:53:23 [updated_at] => 2020-10-30 11:53:23 ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [classCastCache:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [timestamps] => 1 [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) [1] => App\Airflight Object ( [connection:protected] => mysql [table:protected] => airflights [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [id] => 59 [id_airport] => 12 [id_airline] => 10 [path] => 13 [date] => 2020-10-11 [timeout] => 20:55:00 [price] => 1000.00 [model_aircraft] => AH-78 [created_at] => 2020-10-30 11:53:58 [updated_at] => 2020-10-30 11:53:58 ) [original:protected] => Array ( [id] => 59 [id_airport] => 12 [id_airline] => 10 [path] => 13 [date] => 2020-10-11 [timeout] => 20:55:00 [price] => 1000.00 [model_aircraft] => AH-78 [created_at] => 2020-10-30 11:53:58 [updated_at] => 2020-10-30 11:53:58 ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [classCastCache:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [timestamps] => 1 [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) [2] => App\Airflight Object ( [connection:protected] => mysql [table:protected] => airflights [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [id] => 61 [id_airport] => 14 [id_airline] => 10 [path] => 13 [date] => 2020-10-04 [timeout] => 21:58:00 [price] => 1000.00 [model_aircraft] => AH-52 [created_at] => 2020-10-30 11:55:05 [updated_at] => 2020-10-30 11:55:05 ) [original:protected] => Array ( [id] => 61 [id_airport] => 14 [id_airline] => 10 [path] => 13 [date] => 2020-10-04 [timeout] => 21:58:00 [price] => 1000.00 [model_aircraft] => AH-52 [created_at] => 2020-10-30 11:55:05 [updated_at] => 2020-10-30 11:55:05 ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [classCastCache:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [timestamps] => 1 [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) [3] => App\Airflight Object ( [connection:protected] => mysql [table:protected] => airflights [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [id] => 62 [id_airport] => 13 [id_airline] => 10 [path] => 13 [date] => 2020-10-10 [timeout] => 21:58:00 [price] => 1000.00 [model_aircraft] => AH-52 [created_at] => 2020-10-30 11:55:43 [updated_at] => 2020-10-30 11:55:43 ) [original:protected] => Array ( [id] => 62 [id_airport] => 13 [id_airline] => 10 [path] => 13 [date] => 2020-10-10 [timeout] => 21:58:00 [price] => 1000.00 [model_aircraft] => AH-52 [created_at] => 2020-10-30 11:55:43 [updated_at] => 2020-10-30 11:55:43 ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [classCastCache:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [timestamps] => 1 [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) [4] => App\Airflight Object ( [connection:protected] => mysql [table:protected] => airflights [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [id] => 63 [id_airport] => 13 [id_airline] => 10 [path] => 14 [date] => 2020-10-23 [timeout] => 20:58:00 [price] => 5000.00 [model_aircraft] => AH-52 [created_at] => 2020-10-30 11:56:12 [updated_at] => 2020-10-30 11:56:12 ) [original:protected] => Array ( [id] => 63 [id_airport] => 13 [id_airline] => 10 [path] => 14 [date] => 2020-10-23 [timeout] => 20:58:00 [price] => 5000.00 [model_aircraft] => AH-52 [created_at] => 2020-10-30 11:56:12 [updated_at] => 2020-10-30 11:56:12 ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [classCastCache:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [timestamps] => 1 [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [perPage:protected] => 5 [currentPage:protected] => 1 [path:protected] => http://127.0.0.1:8000/searchAirflight/search [query:protected] => Array ( ) [fragment:protected] => [pageName:protected] => page [onEachSide] => 3 [options:protected] => Array ( [path] => http://127.0.0.1:8000/searchAirflight/search [pageName] => page ) )
Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “Programming - PHP”

Who is online

Users browsing this forum: No registered users and 18 guests