Jump to content

[SOLVED] Display array values in a class


mbowling

Recommended Posts

Hi, I'm trying to diplay values from an HTML mutiple select list in a class. I'm trying to pass the array of selected values in the same manner as the strings in the other sets and gets in the code below. But I don't seem to be getting an array when I try to use getArrayCities. How do I pass the array and display the array values by using getArrayCities? Thank you.

 

index.php


<?php  
include("includes/header_footer.inc");
include("includes/central_oregon_addresses_form.inc");

$co_addresses = new central_oregon_addresses();
$form_submitted = (string)null;

if (isset($_POST['check_form_submission']))
{		
$co_addresses->setFirstName((string)$_POST['first_name']);
$co_addresses->setLastName((string)$_POST['last_name']);
$co_addresses->setAddress((string)$_POST['address']);
$co_addresses->setZipCode((string)$_POST['zip_code']);
$co_addresses->setCitiesArray((string)$_POST['cities_array']);
$form_submitted = (string)$_POST['check_form_submission'];
}

if ($form_submitted == "form_submitted")
{
$valid_form = $co_addresses->validate_form();	
if ($valid_form)
{			
	$co_addresses->display_data();
}
else
{	
	$co_addresses->display_form();	
}
}
else
{
$co_addresses->display_form();	
}
?>

 

central_oregon_addresses_form.inc


class central_oregon_addresses 
{
private $first_name;

public function getFirstName()
{
	return trim($this->first_name);
}

public function getCitiesArray()
{
	return trim($this->cities_array);
}

public function setCitiesArray($cities_array)
{
	$this->$cities_array = $cities_array;
}
Bunch of sets and gets...

 

 

The following error is returned when running the for loop in the validate_form function:

Warning: Invalid argument supplied for foreach()

 


foreach($this->getCitiesArray() as $name)
	    {
	    	if ($count_cities < count($this->getCitiesArray()))
	    	{
	    		$selected_cities = $selected_cities . "$name,\n";
	    	}
	    	else
	    	{
	    		$selected_cities = $selected_cities . "$name\n";
	    	}
	    	$count_cities = $count_cities + 1; 
	    }	

Link to comment
Share on other sites

I've removed trim from the array, and I presume the calling code is within the class itself and that's why your using this... However this works...

class central_oregon_addresses 
{
private $first_name;

public function getFirstName()
{
	return trim($this->first_name);
}

public function getCitiesArray()
{
	//return trim($this->cities_array);
	return $this->cities_array;
}

public function setCitiesArray($cities_array)
{
	$this->cities_array = $cities_array;
}

}

$co = new central_oregon_addresses();
$a = array('a', 'b', 'c');
$co->setCitiesArray($a);

foreach($co->getCitiesArray() as $name)
{
echo $name."<br>";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.