Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Danny971's avatar

is there are better way to do this which follows DRY

here is my code

  <form action="{{url('requestFormData')}}" id="step-form-horizontal" class="step-form-horizontal" method="POST" >
                                    
                               

                                    <div>
                                        <h4>Client Info</h4>
                                        @csrf
                                        <section >
                                           
                                            
                                            <div class="row">

                                                <div class="col-lg-2 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label">First Name*</label>
                                                        <input type="text" name="firstName" class="form-control " placeholder="FirstName" required >
                                                    </div>
                                                  </div>
                                                  
                                                 <div class="col-lg-2 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label">Last Name*</label>
                                                        <input type="text" name="lastName" class="form-control" placeholder="LastName" required>
                                                    </div>
                                                 </div>


                            
                                              
                            
                                               <!-- drop dowwn with agency -->
                                               <div class="form-group col-md-4" >
                                                                    <label>Select agency* </label>
                                                                    <select class="form-control" id="inputState" name="agency" required>
                                                                        <option selected="" >Choose...</option>
                                                                        @foreach ($agencyModel as $agency)
                                                                       <option value="{{ $agency['agencyname']}}">{{ $agency['agencyname']}}</option>
                                                                        @endforeach
                                                                        
                                                                    </select>
                                                </div>

                                                 
                                                <div class="col-lg-2 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label">Advocate First Name*</label>
                                                        <input type="text" name="advocatefirstName" class="form-control" placeholder="FirstName" required>
                                                    </div>
                                                  </div>
                                               
                                                 <div class="col-lg-2 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label">Advocate Last Name*</label>
                                                        <input type="text" name="advocatelastName" class="form-control" placeholder="LastName" required>
                                                    </div>
                                                 </div>
                                               
                                               </div> <!--  check here end of 1st row -->
                                                 
                                                   <div class="row"> <!-- start row 2 -->
                                                       
                                                   <div class="col-lg-4 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label">Phone Number*</label>
                                                        <input type="text" name="phone_number" class="form-control" placeholder="Phone Number" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" title="Please enter a valid phone number in the format XXX-XXX-XXXX" required>
                                                    </div>
                                                </div>


                                                <div class="col-lg-4 mb-4">
                                                    <div class="form-group form-material" >
                                                        <label class="text-label">Received Date*</label>
                                                        <input type="text" name="receivedDate" class="form-control" id = "mdate" placeholder="Enter the date case was received" required>
                                                    </div>
                                                </div>


                                                <div class="col-lg-4 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label">Move In Date*</label>
                                                        <input type="text" name="moveInDate" class="form-control" placeholder="Enter the date the client moves in " required>
                                                    </div>
                                                </div>
                                                

   </div> <!--  check here end of 2nd row -->
                                                
                                                   <div class="row"> <!-- 3rd row -->
                                                    <!-- drop dowwn with gender -->
                                                    <div class="form-group col-md-3">
                                                    <label>Select Gender*</label>
                                                    <select class="form-control" id="inputState" name="Gender" required>
                                                    <option selected disabled>Choose...</option>
                                                    <option value="Male">Male</option>
                                                    <option value="Female">Female</option>
                                                    </select>
                                                    </div>

                                                    
                                                 

                                                     <!-- drop dowwn with maritial status -->
                                                     <div class="form-group col-md-3">
                                                    <label>Select Maritial Status*</label>
                                                    <select class="form-control" id="inputState" name="MaritalStatus" required>
                                                    <option selected disabled>Choose...</option>
                                                    <option value="Married">Married</option>
                                                    <option value="Divorced">Divorced</option>
                                                    <option value="Separated">Separated</option>
                                                    <option value="Widowed">Widowed</option>
                                                    <option value="Single">Single</option>
                                                    <option value="Never Married">Never Married</option>
                                                    </select>
                                                    </div>

                                                             

                                                             
                                                   
                                                <div class="form-group col-md-3">
                                                    <div class="form-group">
                                                        <label class="text-label">Date of Birth*</label>
                                                        <input type="text" name="dob" class="form-control" placeholder="DateOfBirth" required>
                                                    </div>
                                                </div>


                                                              <div class="form-group col-md-3">
                                                                  <div class="form-group">
                                                                  <label class="text-label">Email*</label>
                                                                <input type="email" placeholder="[email protected]" name="email" class="form-control" pattern="[^@\s]+@[^@\s]+\.[^@\s]+" title="Please enter a valid email address" required>
                                                                </div>
                                                                </div>

                                                   </div> <!-- end of 3rd row -->

                                                   <div class ="row"> <!-- start of address row -->

                                                       <div class="col-lg-4 mb-4">
                                                         <div class="form-group">
                                                        <label class="text-label"> Address 1*</label>
                                                        <div class="input-group">
                                                            <input type="text" class="form-control"  name="address" id="inputGroupPrepend2" aria-describedby="inputGroupPrepend2" placeholder="address 1" required>
                                                        </div>
                                                     </div>   
                                                    </div>

                                                    <div class="col-lg-4 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label"> Address 2*</label>
                                                        <div class="input-group">
                                                            <input type="text" class="form-control"  name="address2" id="inputGroupPrepend2" aria-describedby="inputGroupPrepend2" placeholder="address 2" required>
                                                        </div>
                                                     </div>   
                                                </div>
                                                 
                                                <div class="col-lg-4 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label"> City* </label>
                                                        <div class="input-group">
                                                            <input type="text" class="form-control"  name="city" id="inputGroupPrepend2" aria-describedby="inputGroupPrepend2" placeholder="City" required>
                                                        </div>
                                                     </div>   
                                                </div>

                                                <div class="col-lg-4 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label"> State*</label>
                                                        <div class="input-group">
                                                            <input type="text" class="form-control"  name="state" id="inputGroupPrepend2" aria-describedby="inputGroupPrepend2" placeholder="State" required>
                                                        </div>
                                                     </div>   
                                                </div>

                                                <div class="col-lg-4 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label"> Zip Code*</label>
                                                        <div class="input-group">
                                                            <input type="text" class="form-control"  name="zip" id="inputGroupPrepend2" aria-describedby="inputGroupPrepend2" placeholder="zipCode" required>
                                                        </div>
                                                     </div>   
                                                </div>
                                                       
                                                              <div class="col-lg-2 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label">Number of Adults*</label>
                                                        <input type="number" name="numberofAdults" id = "numberofAdults" min ="0"  max ="10" class="form-control" onchange="addAdultFields()" required>
                                                    </div>
                                                </div>

                                                <div class="col-lg-2 mb-4">
                                                    <div class="form-group">
                                                        <label class="text-label">Number of Children*</label>
                                                        <input type="number" name="numberofChildren" id = "numberofChildren" min ="0" max ="10" class="form-control" onchange="addChildrenFields()" required >
                                                    </div>
                                                </div>
                                                
                                              </div> <!-- end of address row -->

                                    
                                                <div class="row">

                                         

                                                </div>

                                    
                                               
                                        </section>
                                        
                                        
                                        <h4>Family Deographics</h4>
                                        
                                        <section >
                                           
                                        
                        <div class="card">
                            <div class="card-header">
                                <h4 class="card-title">Family Deographics </h4>
                            </div>
                            <div class="card-body">
   
                                                
                                                
                                         <div class = "familycontainer">

                                         <div id="adultFormcontainer" ></div>
                                        <div id="children-form-container" ></div>

                                        </div>

                            </div>
                        </div>
                                        </section>
                                        
                                        
                                        <h4>Order Section</h4>
                                        <section>
                                           
                                            
                         
                        <div class="card">
                            <div class="card-header">
                                <h4 class="card-title">Order Section </h4>
                            </div>
                            <div class="card-body modal-dialog-scrollable" >
                                <div class="table-responsive frm-container" >
                                    
                                    <table class="table table-bordered verticle-middle table-responsive-sm "  >
                                        <thead>
                                            <tr>
                                                <th scope="col">LIVING ROOM</th>
                                                <th scope="col">kITCHEN</th>
                                                <th scope="col">BEDROOM</th>
                                                <th scope="col">MISC</th>
                                             
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr>
                                                <td>
                                                 <table class ="table table-bordered verticle-middle table-responsive-sm">
                                                <thead>
                                                 <th scope="col">NEED</th>
                                                 <th scope="col">ITEM</th>
                                                 

                                                </thead>

                                                <tbody>
                                                @foreach ($livingrooms as $livingroom)
                                                <tr>
                                                <td><input type="number" name="livingroomitems[{{$livingroom['id']}}]" id = "livingroomitems" min ="0"   class="form-control"></td>
                                                <td> {{$livingroom['livingroom_item']}} </td> 
                                                </tr>
                                                @endforeach
                                                </tbody>

                                              </table>



                                                </td>



                                                <td>
                                                 <table class ="table table-bordered verticle-middle table-responsive-sm">
                                                 <thead>
                                                 <th scope="col">NEED</th>
                                                <th scope="col">ITEM</th>
                                                </thead>

                                                <tbody>
                                                @foreach ($kitchens as $kitchen)
                                                <tr>
                                                
                                                <td> <input type="number" name="kichenitems" id = "kitchenitems" min ="0"  class="form-control"> </td>
                                                <td> {{$kitchen['kitchen_item']}} </td> 
                                                </tr>
                                                @endforeach
                                               
                                                </tbody>

                                                </table>

                                                </td>
                                               




                                                <td>
                                                   <table class ="table table-bordered verticle-middle table-responsive-sm">
                                                   <thead> 
                                                   <th scope="col">NEED</th>
                                                <th scope="col">ITEM</th>
                                               

                                                </thead>
                                                <tbody>
                                                @foreach ($bedrooms as $bedroom)
                                                <tr>
                                                
                                                <td> <input type="number" name="bedroomitems" id = "bedroomitems" min ="0"  max = "5" class="form-control"> </td>
                                                <td> {{$bedroom['bedroom_item']}} </td> 
                                                </tr>
                                                @endforeach
                                                </tbody>
                                                </table>
                                                </td>

                                                <td>
                                                <table class ="table table-bordered verticle-middle table-responsive-sm"> 
                                                <thead>
                                                <th scope="col">NEED</th>
                                                <th scope="col">ITEM</th>
                                                

                                                </thead>
                                                <tbody>
                                               
                                                @foreach ($miscellaneous as $miscellaneou)
                                                <tr>
                                                <td> <input type="number" name="miscsitems" id = "miscsitems" min ="0"  max = "5" class="form-control"> </td>
                                                <td> {{ $miscellaneou->misc_item }} </td> 
                                               
                                                </tr>
                                                @endforeach
                                                
                                                </tbody>
                                                </table>
                                                </td>
                                                
                                            </tr>
                                           
                                        
                                        </tbody>
                                    </table>





                                    <table class="table table-bordered verticle-middle table-responsive-sm">
                                        <thead>
                                            <tr>
                                                <th scope="col">BABY</th>
                                                <th scope="col">SMALL APPLIANCE</th>
                                                <th scope="col">BEDDING</th>
                                                <th scope="col">BATH</th>
                                             
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr>
                                                <td>
                                                 <table class ="table table-bordered verticle-middle table-responsive-sm">
                                                <thead>
                                                <th scope="col">NEED</th>
                                                <th scope="col">ITEM</th>
                                                </thead>

                                                <tbody>
                                                @foreach ($babies as $baby)
                                                <tr>
                                                <td> <input type="number" name="babyitems" id = "babyitems" min ="0"   class="form-control"> </td>
                                                <td> {{ $baby->baby_item }} </td> 
                                                </tr>
                                                @endforeach
                                                </tbody>

                                              </table>



                                                </td>



                                                <td>
                                                 <table class ="table table-bordered verticle-middle table-responsive-sm">
                                                 <thead>
                                                 <th scope="col">NEED</th>
                                                <th scope="col">ITEM</th>
                                                </thead>

                                                <tbody>
                                                @foreach ($smallAppliances as $SA)
                                                <tr>
                                                <td> <input type="number" name="SAitems" id = "SAitems" min ="0"  class="form-control"> </td>
                                                <td> {{ $SA->smallAppliances_item }} </td> 
                                                
                                                </tr>
                                                @endforeach
                                                </tbody>

                                                </table>

                                                </td>
                                               




                                                <td>
                                                   <table class ="table table-bordered verticle-middle table-responsive-sm">
                                                   <thead> 
                                                   <th scope="col">NEED</th>
                                                <th scope="col">ITEM</th>
                                                

                                                </thead>
                                                <tbody>
                                                @foreach ($beddings as $bed)
                                                <tr>
                                                <td> <input type="number" name="beddingitems[{{$bed->id}}]" id = "beddingitems_{{$bed -> id }}" min ="0"   class="form-control"> </td>
                                                <td> {{ $bed->bedding_item }} </td> 
                                                
                                                </tr>
                                                @endforeach
                                                </tbody>
                                                </table>
                                                </td>

                                                <td>
                                                <table class ="table table-bordered verticle-middle table-responsive-sm"> 
                                                <thead>
                                                <th scope="col">NEED</th>
                                                <th scope="col">ITEM</th>
                                               

                                                </thead>
                                                <tbody>
                                                @foreach ($baths as $bathe)
                                                <tr>
                                                <td> <input type="number"  name="bathitems[{{ $bathe->id }}]" id="bathitems_{{ $bathe->id }}" min ="0"  class="form-control" > </td>
                                                <td> {{ $bathe->bath_item }} </td> 
                                                
                                                </tr>
                                                @endforeach
                                                </tbody>
                                                </table>
                                                </td>
                                                
                                            </tr>
                                           
                                        
                                        </tbody>
                                    </table>
                                    
                                </div>
                            </div>
                         </div>
                   
                                            
                                
                         
 
                       </section>
                           
                       <!-- Back order incase there is something requested and not on the main order form

                                    

                       <h4> BackOrder </h4>

                       <section>
                       
                            <div class="card-header">
                                 <h4 class="card-title">BackOrder </h4>
                            </div>
                                            
                                        <div class="card-body">
                                            
                                        </div>


                       </section>
                       
                       -->
                                        
                                        <h4> Preview& Agreement </h4>
                                        <section>
                                            
                                            <div class="col-lg-12 ">
                                            <div class="card-header">
                                             <h4 class="card-title"> Preview & Agreement </h4>
                                             </div>
                                            
                                        <div class="card-body">

                                            
                              

                                         </div>

                                            </div>
                                        </section>
                                    </div>
                                </form>

and here is the php. How can I optimize my php code

 // handling the third section of the multi step form for request order
    
      
       $bathQuantities = $_POST['bathitems'];
       $itemNames =[];

       // retrive all items from the database

       $items = DB::table('bath') -> get();

       // the following code creates an array to store the item names

       foreach ($items as $item) {

        $itemNames[$item->id] = $item -> bath_item;
       }

       $outputs = []; // this array will hold the output for each item


      

 // Insert the selected items into the requestorder table
 $bathrequestOrder =[
  'family_id' => $familyId, 
  'category' => 'BATH', // set the requested item type
  'categoryTotal' => array_sum($bathQuantities), // set the total amount needed
  'daterequested' =>  now()  // set the current date and time
 ];


       foreach($bathQuantities as $id => $quantity) {
        
        if($quantity >0){

            // using optional object operator to avoid errors

            $item = DB::table('bath') ->where('id',$id)->first();
            
            if($item){

                $itemName = $item->bath_item;
                DB::table('requestorder')->insert([
                    'family_id' => $familyId,
                    'category' => 'BATH',
                    'categoryTotal' => array_sum($bathQuantities),
                  'requestedItem' => $itemName,
                  'amountNeeded' => $quantity,
                  'daterequested' =>  now() 
                ]);




             $output = $item ->bath_item. ':'.$quantity;
             $outputs[] = $output; // append the output to the array
            }
        }
  

       }

       $outputString = implode(', ', $outputs); // Join the outputs into a single string
//dd($outputString);
       
       // condition if any item from living room category was selected 
       
       $livingroomQuantities = $_POST['livingroomitems'];
       $livingroomItemsNames =[];
       
       //retrive all items from table for livingroom category
       $livingroomItems = DB::table('livingroom') -> get();
       
       // create an array to store the items
       
       foreach($livingroomItems as $livingroomItem) {
           $livingroomItemsNames[$livingroomItem -> id] = $livingroomItem -> livingroom_item;
       }
       
       $livingroomOutputs =[]; // this array will hold the output for each item
       
       $livingroomrequestOrder = [
          'family_id' => $familyId, 
  'category' => 'LIVINGROOM', // set the requested item type
  'categoryTotal' => array_sum($livingroomQuantities), // set the total amount needed
  'daterequested' =>  now()  // set the current date and time 
       ];
       
       
       foreach($livingroomQuantities as $id => $livingRoomQuantity) {
           if ($livingRoomQuantity > 0) {
               
               $livingroomItem = DB::table('livingroom') -> where('id',$id)->first();
               
               if( $livingroomItem) {
                   
                    $livingroomItemsName = $livingroomItem -> livingroom_item;
                    DB::table('requestorder') -> insert([
                  'family_id' => $familyId,
                  'category' => 'LIVINGROOM',
                  'categoryTotal' => array_sum($livingroomQuantities),
                  
                  'requestedItem' => $livingroomItemsName,
                  'amountNeeded' => $livingRoomQuantity,
                  'daterequested' =>  now() 
                    ]);
                    
                 
                    $livingroomOutput = $livingroomItem -> livingroom_item. ':' .$livingRoomQuantity;
                    $livingroomOutputs[] =$livingroomOutput; //append the output to the array 
               }
           }
       }
       
       $livingroomOutputString = implode( $livingroomOutputs);
       
       
       // condition if any item from bedding category was selected 
       
       $beddingQuantities = $_POST['beddingitems'];
       dd($beddingQuantities);
       $beddingItemsNames =[];
       
       //retrive all items from table for livingroom category
       $beddingItems = DB::table('bedding') -> get();
       
       // create an array to store the items
       
       foreach($beddingItems as $beddingItem) {
           $beddingItemsNames[$beddingItem -> id] = $beddingItem -> bedding_item;
       }
       
       $beddingOutputs =[]; // this array will hold the output for each item
       
       $beddingrequestOrder = [
          'family_id' => $familyId, 
  'category' => 'BEDDING', // set the requested item type
  'categoryTotal' => array_sum($beddingQuantities), // set the total amount needed
  'daterequested' =>  now()  // set the current date and time 
       ];
       
       
       foreach($beddingQuantities as $id => $beddingQuantity) {
           if ($beddingQuantity > 0) {
               
               $beddingItem = DB::table('bedding') -> where('id',$id)->first();
               
               if( $beddingItem) {
                   
                    $beddingItemsName = $beddingItem -> bedding_item;
                    DB::table('requestorder') -> insert([
                  'family_id' => $familyId,
                  'category' => 'BEDDING',
                  'categoryTotal' => array_sum($beddingQuantities),
                  
                  'requestedItem' => $beddingItemsName,
                  'amountNeeded' => $beddingQuantity,
                  'daterequested' =>  now() 
                    ]);
                    
                 
                    $beddingOutput = $beddingItem -> bedding_item. ':' .$beddingQuantity;
                    $beddingOutputs[] =$beddingOutput; //append the output to the array 
               }
           }
       }
       
       $beddingOutputString = implode( $beddingOutputs);
      
       // condition if any item from bedroom category was selected 
       
       $bedroomQuantities = $_POST['bedroomitems'];
       $bedroomItemsNames =[];
       
       //retrive all items from table for livingroom category
       $bedroomItems = DB::table('bedroom') -> get();
       
       // create an array to store the items
       
       foreach($bedroomItems as $bedroomItem) {
           $bedroomItemsNames[$bedroomItem -> id] = $bedroomItem -> bedroom_item;
       }
       
       $bedroomOutputs =[]; // this array will hold the output for each item
       
       $bedroomrequestOrder = [
          'family_id' => $familyId, 
  'category' => 'BEDROOM', // set the requested item type
  'categoryTotal' => array_sum($bedroomQuantities), // set the total amount needed
  'daterequested' =>  now()  // set the current date and time 
       ];
       
       
       foreach($bedroomQuantities as $id => $bedroomQuantity) {
           if ($bedroomQuantity > 0) {
               
               $bedroomItem = DB::table('bedroom') -> where('id',$id)->first();
               
               if( $bedroomItem) {
                   
                    $bedroomItemsName = $bedroomItem -> bedroom_item;
                    DB::table('requestorder') -> insert([
                  'family_id' => $familyId,
                  'category' => 'BEDROOM',
                  'categoryTotal' => array_sum($bedroomQuantities),
                  
                  'requestedItem' => $bedroomItemsName,
                  'amountNeeded' => $bedroomQuantity,
                  'daterequested' =>  now() 
                    ]);
                    
                 
                    $bedroomOutput = $bedroomItem -> bedroom_item. ':' .$bedroomQuantity;
                    $bedroomOutputs[] =$bedroomOutput; //append the output to the array 
               }
           }
       }
       
       $bedroomOutputString = implode( $bedroomOutputs);
       
      
   
       
       $kitchenQuantities = $_POST['kichenitems'];
       $kitchenItemsNames =[];
       
       //retrive all items from table for livingroom category
       $kitchenItems = DB::table('kitchen') -> get();
       
       // create an array to store the items
       
       foreach($kitchenItems as $kitchenItem) {
           $kitchenItemsNames[$kitchenItem -> id] = $kitchenItem -> kitchen_item;
       }
       
       $kitchenOutputs =[]; // this array will hold the output for each item
       
       $kitchenrequestOrder = [
          'family_id' => $familyId, 
  'category' => 'KITCHEN', // set the requested item type
  'categoryTotal' => array_sum($kitchenQuantities), // set the total amount needed
  'daterequested' =>  now()  // set the current date and time 
       ];
       
       
       foreach($kitchenQuantities as $id => $kitchenQuantity) {
           if ($kitchenQuantity > 0) {
               
               $kitchenItem = DB::table('kitchen') -> where('id',$id)->first();
               
               if( $kitchenItem) {
                   
                    $kitchenItemsName = $kitchenItem -> kitchen_item;
                    DB::table('requestorder') -> insert([
                  'family_id' => $familyId,
                  'category' => 'KITCHEN',
                  'categoryTotal' => array_sum($kitchenQuantities),
                  
                  'requestedItem' => $kitchenItemsName,
                  'amountNeeded' => $kitchenQuantity,
                  'daterequested' =>  now() 
                    ]);
                    
                 
                    $kitchenOutput = $kitchenItem -> kitchen_item. ':' .$kitchenQuantity;
                    $kitchenOutputs[] =$kitchenOutput; //append the output to the array 
               }
           }
       }
       
       $kitchenOutputString = implode( $kitchenOutputs);
       ///////////////////////////////////////////////////////////////////////////
       
  // condition if any item from MISC category was selected 
       
       $miscellaneousQuantities = $_POST['miscsitems'];
       $miscellaneousItemsNames =[];
       
       
       $miscellaneousItems = DB::table('miscellaneous') -> get();
       
       // create an array to store the items
       
       foreach($miscellaneousItems as $miscellaneousItem) {
           $miscellaneousItemsNames[$miscellaneousItem -> id] = $miscellaneousItem -> misc_item;
       }
       
       $miscellaneousOutputs =[]; // this array will hold the output for each item
       
       $miscrequestOrder = [
          'family_id' => $familyId, 
  'category' => 'MISC', // set the requested item type
  'categoryTotal' => array_sum($miscellaneousQuantities), // set the total amount needed
  'daterequested' =>  now()  // set the current date and time 
       ];
       
       
       foreach($miscellaneousQuantities as $id => $miscellaneousQuantity) {
           if ($miscellaneousQuantity > 0) {
               
               $miscellaneousItem = DB::table('miscellaneous') -> where('id',$id)->first();
               
               if( $miscellaneousItem) {
                   
                    $miscellaneousItemsName = $miscellaneousItem -> misc_item;
                    DB::table('requestorder') -> insert([
                  'family_id' => $familyId,
                  'category' => 'MISC',
                  'categoryTotal' => array_sum($miscellaneousQuantities),
                  
                  'requestedItem' => $miscellaneousItemsName,
                  'amountNeeded' => $miscellaneousQuantity,
                  'daterequested' =>  now() 
                    ]);
                    
                 
                    $miscellaneousOutput = $miscellaneousItem -> misc_item. ':' .$miscellaneousQuantity;
                    $miscellaneousOutputs[] =$miscellaneousOutput; //append the output to the array 
               }
           }
       }
       
       $miscellaneousOutputString = implode( $miscellaneousOutputs);
       ///////////////////////////////////////////////////////////////////////////
       
  // condition if any item from BABY category was selected 
       
       $babyQuantities = $_POST['babyitems'];
       $babyItemsNames =[];
       
       //retrive all items from table for baby category
       $babyItems = DB::table('baby') -> get();
       
       // create an array to store the items
       
       foreach($babyItems as $babyItem) {
           $babyItemsNames[$babyItem -> id] = $babyItem -> baby_item;
       }
       
       $babyOutputs =[]; // this array will hold the output for each item
       
       $babyrequestOrder = [
          'family_id' => $familyId, 
  'category' => 'BABY', // set the requested item type
  'categoryTotal' => array_sum($babyQuantities), // set the total amount needed
  'daterequested' =>  now()  // set the current date and time 
       ];
       
       
       foreach($babyQuantities as $id => $babyQuantity) {
           if ($babyQuantity > 0) {
               
               $babyItem = DB::table('baby') -> where('id',$id)->first();
               
               if( $babyItem) {
                   
                    $babyItemsName = $babyItem -> baby_item;
                    DB::table('requestorder') -> insert([
                  'family_id' => $familyId,
                  'category' => 'BABY',
                  'categoryTotal' => array_sum($babyQuantities),
                  
                  'requestedItem' => $babyItemsName,
                  'amountNeeded' => $babyQuantity,
                  'daterequested' =>  now() 
                    ]);
                    
                 
                    $babyOutput = $babyItem -> baby_item. ':' .$babyQuantity;
                    $babyOutputs[] =$babyOutput; //append the output to the array 
               }
           }
       }
       
       $miscellaneousOutputString = implode( $miscellaneousOutputs);
       ///////////////////////////////////////////////////////////////////////////
       
        
       
       $smallappliancesQuantities = $_POST['SAitems'];
       $smallappliancesItemsNames =[];
       
       
       $smallappliancesItems = DB::table('smallappliances') -> get();
       
       // create an array to store the items
       
       foreach($smallappliancesItems as $smallappliancesItem) {
           $smallappliancesItemsNames[$smallappliancesItem -> id] = $smallappliancesItem -> smallAppliances_item;
       }
       
       $smallappliancesOutputs =[]; // this array will hold the output for each item
       
       $SArequestOrder = [
          'family_id' => $familyId, 
  'category' => 'SMALL APPLIANCES', // set the requested item type
  'categoryTotal' => array_sum($smallappliancesQuantities), // set the total amount needed
  'daterequested' =>  now()  // set the current date and time 
       ];
       
       
       foreach($smallappliancesQuantities as $id => $smallappliancesQuantity) {
           if ($smallappliancesQuantity > 0) {
               
               $smallappliancesItem = DB::table('smallappliances') -> where('id',$id)->first();
               
               if( $smallappliancesItem) {
                   
                    $smallappliancesItemsName = $smallappliancesItem -> smallAppliances_item;
                    DB::table('requestorder') -> insert([
                  'family_id' => $familyId,
                  'category' => 'SMALL APPLIANCES',
                  'categoryTotal' => array_sum($smallappliancesQuantities),
                  
                  'requestedItem' => $smallappliancesItemsName,
                  'amountNeeded' => $smallappliancesQuantity,
                  'daterequested' =>  now() 
                    ]);
                    
                 
                    $smallappliancesOutput = $smallappliancesItem -> smallAppliances_item. ':' .$smallappliancesQuantity;
                    $smallappliancesOutputs[] =$smallappliancesOutput; //append the output to the array 
               }
           }
       }
       
       $smallappliancesOutputString = implode( $smallappliancesOutputs); 
       ///////////////////////////////////////////////////////////////////////////
0 likes
4 replies
jlrdw's avatar

This wasn't answered in your other post?

1 like
Danny971's avatar

@jlrdw I also tried this

// Function to handle processing for a specific category function processCategory($familyId, $categoryName, $categoryQuantities, $categoryTable) { $categoryItems = DB::table($categoryTable)->get(); $categoryItemsNames = [];

foreach ($categoryItems as $categoryItem) {
    $columnKey = strtolower($categoryName) . '_item';
    $categoryItemsNames[$categoryItem->id] = $categoryItem->$columnKey;
}

$outputs = [];
$requestOrder = [
    'family_id' => $familyId,
    'category' => strtoupper($categoryName), 
    'categoryTotal' => array_sum($categoryQuantities),
    'daterequested' => now(),
];

foreach ($categoryQuantities as $id => $quantity) {
    if ($quantity > 0) {
        $item = DB::table($categoryTable)->where('id', $id)->first();

        if ($item) {
            $columnKey = strtolower($categoryName) . '_item';
            $itemName = $item->$columnKey;
            DB::table('requestorder')->insert([
                'family_id' => $familyId,
                'category' => strtoupper($categoryName),
                'categoryTotal' => array_sum($categoryQuantities),
                'requestedItem' => $itemName,
                'amountNeeded' => $quantity,
                'daterequested' => now(),
            ]);

            $output = $item->$categoryName . ':' . $quantity;
            $outputs[] = $output;
        }
    }
}

return implode(', ', $outputs);

}

$bathOutputString = processCategory($familyId, 'bath', $_POST['bathitems'], 'bath'); $livingroomOutputString = processCategory($familyId, 'livingroom', $_POST['livingroomitems'], 'livingroom'); $beddingOutputString = processCategory($familyId, 'bedding', $_POST['beddingitems'], 'bedding');

but no luck

Danny971's avatar

I'm trying to figure out a way to optimize the php code

Please or to participate in this conversation.