PHP class not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

PHP class not working

Post by sulen »

I am just trying to write a simple class to display maps to a location ......... but when i submit to this page it just comes up blank and does not display what needs to be shown. The code is provided below. Kindly let me know what I am wrong. Thanks

Code: Select all

<?php
 class map {
var $addr = $_POST[address];
var $csz = $_POST[csz];
var $country = $_POST[country];
var $link = "http://maps.yahoo.com/maps_result?addr=$addr&csz=$csz&country=$country&new=1&name=&qty=";
  
function displayLink() {
?>
<HTML>
<HEAD>
<title>Link to the Map</title>
</HEAD><BODY>
<p><a href='<?=$link?>' class='five'>Link to the Map</a>
</BODY></HTML>
<?
}
}

$map = new map;
$map->displayLink( );
?>
Any help will be appreciated.

JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Use [ php ] tags when posting PHP code. ;) Nice try.

Your problem is that you're trying to pass dynamic values into the "default" value of a variable. Unfortunantely, default variables must be clearly defined, so that's a nono. Plus, the variables don't exist!

Try this, using the new class Map (since classes are traditionally named with uppercase). Untested, of course.

Code: Select all

<?php
class Map {
    var $addr;
    var $csz;
    var $country;
    var $base_link = "http://maps.yahoo.com/maps_result";
    var $link;
    
    function Map() {
        $this->addr = $_POST['address'];
        $this->csz = $_POST['csz'];
        $this->country = $_POST['country'];
        $this->link = $this->base_link . 
          $this->parseURL(
            array('addr'=>$this->addr,
                  'csz'=>$this->csz,
                  'country'=>$this->country,
                  'new'=>'1')
          );
    }
    
    function parseURL($array) {
        $output = '?';
        $first = true;
        foreach($array as $key => $value) {
            if (!$first) {
                $output .= '&';
            } else {
                $first = false;
            }
            $output .= $key;
            $output .= '=';
            $output .= urlencode($value);
        }
        return $output;
    }
    
    function displayLink() {
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Link to the Map</title>
</head><body>
<p><a href="<?php echo $this->link; ?>" class="five">Link to the Map</a></p>
</body></html>
<?php
    }
}
 
$map = new Map;
$map->displayLink();
?>
Update - Oops, forgot to return $output. Fixed.
Update 2 - Hmmm... it just occured to me: using Objects probably is overkill for this particular task. You should either stay procedural, or maybe make the class more abstract.
Update 3 - <? to <?php. Shorttags is evil.
Update 4 - Actually tested it. Boy, loads of errors. Moved variable assignment inside constructor, changed parseURL() to $this->parseURL(), and all that other good stuff. Seems to work now.
Update 5 - $this->$addr to $this->addr. Big difference.
Last edited by Ambush Commander on Sat Jun 25, 2005 2:19 pm, edited 3 times in total.
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

I posted the new code as you have given me .......... but it is still not displaying anything...........the page comes back as blank. Let me know what I am doing wrong............... Thanks
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Okay, I updated the code because I actually tested it. You should turn on error reporting.

Oh wait, no, it doesn't work. I'm going to have to update the snippet again. Wait a sec. Should work in about five seconds.

Update - Okay, now it should totally work. Just remember that it's POST, not GET.
Post Reply