开发者

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

开发者 https://www.devze.com 2023-04-11 04:10 出处:网络
开发者_开发百科Im not to familiar with php. I am posting from a form and my php looks like this :
开发者_开发百科

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.


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).


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; ?>


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.


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}");


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


<?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/


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号