Skip to content Skip to sidebar Skip to footer

Create Divs Associated To Objects

I asked a similar question earlier but I haven't been able to find anything to help me. I have now updated my code to something working but I am now completely stuck. This is my ph

Solution 1:

Not sure what you mean by divs associated with that person but for first problem this should work.

Lets say you have an array of objects named $objs

foreach($objsas$obj)
{
   echo"<div>".$obj->name."</div>";
}

Solution 2:

I'm not sure this is what you're looking for, but this should work:

// create an array of persons, I'm assuming that SE or SD is// unique for each object$persons = (
   new person("Allan", 1, 1);
   new person("Bobbo", 2, 2);
   new person("Cicci", 3, 3)
);

The objects could be sorted by name using usort():

usort($persons, function($a, $b) {
   return$a->name > $b->name;
});


// loop through each object and output a div// notice that the id will be unique and based on// the `SE` member of the object, you can then reference// a specific div from javascript $('#person-1') etcforeach ($personsas$person) {
   print'<div class="person" id="person-' . $person->SE . '">' . $person->name . '</div>';
}

Using jQuery you could then get an object based on the unique id:

$(document).ready(function() {
  $('.person').click(function() {
    $.ajax('object.php', {
      type: 'post',
      data: 'id=' + $(this).attr('id').substr(7),
      dataType: 'json',
      success:function(obj) {
         alert('You clicked on ' + obj.name + ' ' + obj.SE + ' ' + obj.SD);
      }
    });
  });
});

You would search in you $persons array to find the object with a corresponding SE (or whatever you deside to use as the unique identifer) value:

// object.phpif (isset($_POST['id'])) {
   $id = intval($_POST['id']);
   foreach ($personsas$person) {
     if ($person->SE == $id) {
       // return json objectprint json_encode($person);
       break;
     }
   }
}

Post a Comment for "Create Divs Associated To Objects"