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
https://forums.phpfreaks.com/topic/69505-solved-display-array-values-in-a-class/
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>";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.