2

i have this in my database.

Person | Location

student1 San Francisco

student2 San Francisco

student1 New York

student2 New york

There's a problem in my controller. here is the code:

foreach((array)$request->stud as $person){
  foreach((array)$request->studlocation as $location){
    $hm = new HouseMate;

    $hmid = rand();
    $hm->hmid=$hmid;
    $hm->rId_fk=$renterid;
    $hm->person=$person;  

    $hm->location=$location;
    $hm->save();
  }
  $hm->delete();
}

And in my view:

$(document).ready(function() {
  $("#dropdown").change(function() {
    var selVal = $(this).val();
    $("#textboxDiv").html('');
    $("#schoolloc").html('');

    if(selVal > 0) {
      for(var i = 1; i<= selVal; i++) {
        $("#textboxDiv").append('<input type="hidden" value="student'+i+'" name="stud[]" />');
        $("#schoolloc").append('<input type="text" name="studlocation[]" id="studlocation'+i+'" />');
        var input = document.getElementById('studlocation'+i+'');
        var autocomplete = new google.maps.places.Autocomplete(input);

        autocomplete.addListener('place_changed', function () {
          var place = autocomplete.getPlace();
        });
      }
    }
  });

2 Answers 2

2

Is this what you want?

Code php:

$students = (array)$request->stud;
$locations = (array)$request->studlocation;
for ($i = 0; $i < count($studens); i++){
    $hm = new HouseMate;

    $hmid = rand();
    $hm->hmid=$hmid;
    $hm->rId_fk=$renterid;
    $hm->person=$students[i];  

    $hm->location=locations[i];
    $hm->save();
    $hm->delete();
}
Sign up to request clarification or add additional context in comments.

6 Comments

Ohmygad thank you so much .... You're a life saver. I've been doing this since yesterday..
i did vote you, but it say i need 15 reputation for it to be public.
Done! hey, I have one question left if you don't mind.
@Cryptanalyst So you should open another question.
i'll still have to wait for 90 minutes to post huhuhu
|
1

the error was starting on this:

" $hm->lat=$slat[$i];"

this is my controller:

$students = (array)$request->stud;
$locations = (array)$request->studlocation;
$slat=(array)$request->slat;
$slng=(array)$request->slng;
for ($i = 0; $i < count($students); $i++){
$hm = new HouseMate;
$hmid = rand();
$hm->hmid=$hmid;
$hm->rId_fk=$renterid;
$hm->person=$students[$i];  
$hm->location=$locations[$i];
$hm->lat=$slat[$i];
$hm->lng=$slng[$i];
$hm->save();

}

and this is my view:

     $(document).ready(function() {
     $("#dropdown").change(function() {
     var selVal = $(this).val();
     $("#textboxDiv").html('');
     $("#schoolloc").html('');

    if(selVal > 0) {
        for(var i = 1; i<= selVal; i++) {
             $("#textboxDiv").append('<input type="hidden" 
    value="student'+i+'" name="stud[]" />');
             $("#schoolloc").append('<input type="text" 
    name="studlocation[]" id="studlocation'+i+'" />');
        var input = document.getElementById('studlocation'+i+'');
        var autocomplete = new google.maps.places.Autocomplete(input);
    $("#slat").html('');
    $("#slng").html('');
    autocomplete.addListener('place_changed', function () {
    var place = autocomplete.getPlace();
    var lat = place.geometry.location.lat();
    var lng = place.geometry.location.lng();

  $("#slat").append('<input type="hidden" name="slat[]" value='+lat+' />');
  $("#slng").append('<input type="hidden" name="slng[]" value='+lng + ' />');
         });
         }
       }
    });
    });

5 Comments

May be you lost the double quote at value attribute. ` $("#slat").append('<input type="hidden" name="slat[]" value="'+lat+'" />'); $("#slng").append('<input type="hidden" name="slng[]" value="'+lng + '" />'); `
Could you send me the data of request array?
what do you mean ? the view or the controller or the schema or the database?
please do it: var_dump((array)$request)
where should i put it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.