[thelist] need guidance on arrays and loops in PHP

Ken Snyder kendsnyder at gmail.com
Fri Mar 28 08:51:43 CDT 2008


Jeremy Weiss wrote:
> ...
> Don't have much code since I'm still trying to figure out the
> direction to go. What I have is a 'favorites' feature on a real estate
> site where people can mark properties as a 'favorite'.  Then when the
> user is ready for the next step they can complete a short contact form
> that will include the MLS #s for the properties.
>
> So if all the properties (assuming there is more than one) were listed
> by the same agent, then I forward the contact information to that
> agent. If they're listed by different agents, then it goes into  a
> general queue for manual assignment.
>
> So the array only has the MLS #s. I use those to find out the lo_id
> and la_id for each. Then I have to compare each of the id's to see if
> they're the same or not.
>
> Is that any better? Clear as mud still?
>
> -jeremy
>   
I'm still not sure what lo_id or la_id refer to or why you need to 
compare them, but below is how I would approach it.  Let me know if it 
answers your question... or brings up more questions :)

- Ken Snyder


session_start();
page 1
...
$sql = "SELECT mls, ...property_info..., agent_id, agent_email FROM 
properties...";
// get a array of property records
$propertyArray = $db->select($sql);
// index each property record by the mls number
$tmp = array();
foreach ($propertyArray as $property) {
  $tmp[$property['mls']] = $property;
}
$propertyArray = $tmp;
// store the indexed array of property records to session
$_SESSION['propertyArray'] = $propertyArray;
// output your property info
foreach ($propertyArray as $property) {
  echo ...property_info...
  // use faves[] to return an array of chosen mls numbers in $_POST
  echo '<input type="checkbox" name="faves[]" value="' . $mls . '" /> 
Favorite<br />';
}
...

page 2
...
Your name <input type="text" name="contact[name]" value="" />
Your phone <input type="text" name="contact[phone]" value="" />
...

page 3
...
session_start();
// retreive our property array from session
$propertyArray = $_SESSION['propertyArray'];
// gather the leads by agent
$leadsByAgent = array();
if (is_array($_POST['faves'])) {
  // iterate through the list of mls numbers marked as favorite
  foreach ($_POST['faves'] as $mls) {
    // check to make sure we know that mls number
    if (isset($propertyArray[$mls])) {
      // get the property record tied to that mls number
      $faveProperty = $propertyArray[$mls];
      // get the agent's id
      $agentId = $faveProperty['agent_id']];
      // initialize an array for this agent if this is the first fave 
for this agent
      if (!isset($leadsByAgent[$agentId])) {
        $leadsByAgent[$agentId] = array();
      }
      // store the leads by agent
      $leadsByAgent[$agentId][] = $faveProperty;
    }
  }
}
// email each agent the list of properties and the contact info of the user
foreach ($leadsByAgent as $agentId => $propertyArray) {
  emailAgentTheLead($agentId, $propertyArray, $_POST['contact']);
}




More information about the thelist mailing list