susanBuck / e15-spring22

0 stars 0 forks source link

How to display form results in view? #33

Closed pllealfunes closed 2 years ago

pllealfunes commented 2 years ago

Hey guys,

I was trying to follow the steps under the FORMS:GET notes for my p2 form but I am unable to display the results on the same page the form was submitted which is also my home page. After I fill out the form I am taken to another page that displays the results as an array. How can I get the results to show on the same page under my form?

My Form

Screen Shot 2022-03-11 at 6 39 46 PM

After submitting results

Screen Shot 2022-03-11 at 6 39 56 PM

home.blade.php

   @extends('layouts/main')
  @section('content')
     <form method='GET' action='/search'>
         <h2>Search for a hobby under a specific category.</h2>
           <fieldset>

          <label for="categories">Choose a Category:</label>

          <select name="categories" id="categories">
              <option value="physical-hobbies">Physical</option>
              <option value="creative-hobbies">Creative</option>
              <option value="mental-hobbies">Mental</option>
              <option value="food-hobbies">Food</option>
              <option value="collecting-hobbies">Collecting</option>
              <option value="games-hobbies">Games/Puzzles</option>
          </select>

          <label for='sugesstionNumber'>
              Enter the amount of suggestions you want:
              <input type='text' name='suggestionNumber'>
          </label>
      </fieldset>

      <button type='submit' class='btn btn-primary'>Enter</button>
  </form>

  @if (!is_null($searchResults))
      @if (count($searchResults) == 0)
          <div class='results alert alert-warning'>
              No results found.
          </div>
      @else
          <div class='results alert alert-primary'>

              {{ count($searchResults) }}
              {{ Str::plural('Result', count($searchResults)) }}:

              <ul class='clean-list'>
                  @foreach ($searchResults as $result)
                      <li>{{ $result }}</li>
                  @endforeach
              </ul>
          </div>
      @endif
  @endif

@endsection

SearchController.php <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SearchController extends Controller
{
public function search(Request $request)
    {
       # Get the form nput values (default to null if no values exist)
    $category = $request->input('categories', null);
    $suggestionNumber = $request->input('suggestionNumber', null);

    # Load our json book data and convert it to an array
    $hobbyData = file_get_contents(database_path('hobbies.json'));
    $hobbies = json_decode($hobbyData, true);

    # Do search
    $searchResults = [];
   foreach ($hobbies as $hobby) {
        $filterCategory = array_filter($hobby[$category]);
        $arrayResults = array_rand($filterCategory,$suggestionNumber);
        foreach ($arrayResults as $result){
            array_push($searchResults,$filterCategory[$result]);
        }
        return $searchResults;
    }

    # Redirect back to the form with data/results stored in the session
    # Ref: https://laravel.com/docs/responses#redirecting-with-flashed-session-data
    return redirect('/')->with([
        'category' => $category,
        'suggestionNumber' => $suggestionNumber,
        'searchResults' => $searchResults
    ]);
    }
}

PageController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Arr;

  class PageController extends Controller
  {
          public function home()
              {
                  # Redirect back to the form with data/results stored in the session
                  # Ref: https://laravel.com/docs/responses#redirecting-with-flashed-session-data
                  return view('pages/home',[
                      'categories' => session('categories', null),
                      'suggestionNumber' => session('suggestionNumber', null),
                      'searchResults' => session('searchResults', null)
                  ]);
              }
          }

routes

  <?php

  use Illuminate\Support\Facades\Route;
  use App\Http\Controllers\PageController;
  use App\Http\Controllers\SearchController;

  Route::get('/', [PageController::class, 'home']);
  Route::get('/search', [SearchController::class, 'search']);
gabichuela85 commented 2 years ago

Hi @pllealfunes it looks like the issue you are running into may be in your search function of your SearchController.php file. I think once a function hits a return statement it stops running.

susanBuck commented 2 years ago

@gabichuela85 Is correct. That return $searchResults; is causing the code to stop there. Try omitting it altogether.

There's no need to return $searchResults. You simply need to populate it with data and then pass it to the view - both things you're already doing.

If that doesn't get it working as expected, let us know.

pllealfunes commented 2 years ago

It worked! Thanks @gabichuela85 and @susanBuck