rick
rick

Reputation: 41

How do I echo more than one object from post in php?

Im not to familiar with php.

I am posting from a form and my php looks like this :

// Object syntax looks better and is easier to use than arrays to me
    $post = new stdClass;

    // Usually there would be much more validation and filtering, but this
    // will work for now.
    foreach ($_POST as $key => $val)
        $post->$key = trim(strip_tags($_POST[$key]));

On the form I have fields like Fist Name, Last Name , Address, Zip, City, State etc.

The information will displayed in a table on my next page (for example) with:

<?php echo $post->name; ?>

since I am using the object syntax.

If I want the cell that displays the address to combine the full address do I write it like this:

<?php echo $post->address, city, state, zip; ?>

I basically would like to show the address as an address would normally be displayed (e.g. 123 This street, City, AZ, 55555)

Ideally, it would be comma separated as an address would be written. So I am sure I am not do this correctly right now.

Upvotes: 0

Views: 232

Answers (7)

Brian Driscoll
Brian Driscoll

Reputation: 19635

Try this:

<?php echo htmlspecialchars("{$post->address}, {$post->city}, {$post->state}, {$post->zip}"); ?>

The curly braces allow you to enclose PHP object references within a string (rather than having to use concatenation).

Upvotes: 7

You could simply create variable variables like so:

foreach ($_POST as $key => $val)
{
    ${$key} = trim( strip_tags($val) );
}

echo "$street, $city, $state, $zip";

Check the full code on pastebin: http://pastebin.com/MMpSVjNv

Upvotes: 3

hakre
hakre

Reputation: 198204

If the order of the items in the post object is the order you need:

echo implode(', ', (array) $post);

If not, you need to do it outlined as in the many other answers, for example:

echo "{$post->address}, {$post->city}, {$post->state}, {$post->zip}";

An important point is missing however if you output into HTML:

echo htmlspecialchars(implode(', ', (array) $post));

resp:

echo htmlspecialchars("{$post->address}, {$post->city}, {$post->state}, {$post->zip}");

Upvotes: 3

Martin Bean
Martin Bean

Reputation: 39429

Personally, I'd have an Address object within your Post object. So it would look as follows:

class Post {
    var $title;
    var $address;
}

class Address {
    var $street_address;
    var $locality;
    var $region;
    var $postal_code;

    function __toString() {
        return implode(', ', array($this->street_address, $this->locality, $this->region, $this->postal_code));
    }
}

$address = new Address($street_address, $locality, $region, $postal_code);
$post    = new Post('title', $address);

You can then grab your address with commas like thus:

<p id="address"><?php echo $post->address; ?></p>

The address property would be an instance of your Address object, and as you echo it, it will call that object's __toString magic method, outputting the address components separated by commas.

Upvotes: 3

Alex Turpin
Alex Turpin

Reputation: 47776

You need to use $post-> everytime you want to reference a property from it. The different parameters of echo will concatenate the strings. So if you wanted to show those properties, separated by comas:

<?php echo $post->address, ',', $post->city, ',', $post->state, ',', $post->zip; ?>

Upvotes: 3

Marc B
Marc B

Reputation: 360802

Close, but not quite:

<?php echo $post->address, "<br>", $post->city, " ", $post->state, "<br>", $post->zip ?>

echo can take multiple comma-separated arguments. The alternative is to use multiple echo calls.

Upvotes: 2

0xDEADBEEF
0xDEADBEEF

Reputation: 3431

<?php echo $post->address.", ".$post->city.", ".$post->state.", ".$post->zip; ?>

the .-operator concats strings in php. you should take a look in a php-tutorial to get into the matter: http://tut.php-quake.net/en/

Upvotes: 2

Related Questions