Jump to content

php class


chriscloyd

Recommended Posts

would this be how to start a class if im trying to get an id from a url
say if my url is index.php?project=3  <=== that 3 is the id
how do i echo the class to get the $p_name

[code]
<?php
class getproject {
    var $id = $_GET['project'];
    function p_name {
          $get_name = mysql_query("SELECT * FROM projects WHERE id = '$id'") or die(mysql_error());
          $p = mysql_fetch_assoc($get_name);
          $p_name = $p['name'];
    }
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/35643-php-class/
Share on other sites

[code]
<?php
class getproject {
  var $id = $_GET['project'];
  function p_name() {
    if ($result = mysql_query("SELECT `name` FROM projects WHERE id = '{$this->id}'")) {
      if (mysql_num_rows($result) > 0) {
        $row = mysql_fetch_assoc($result);
        return $row['name'];
      }
    }
  }
}
?>
[/code]

Looks to me like you might be trying to run before you can walk.
Link to comment
https://forums.phpfreaks.com/topic/35643-php-class/#findComment-168814
Share on other sites

i get this error

Parse error: parse error, unexpected T_VARIABLE in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\includes\porfolio_getfunctions.php on line 3


[CODE]
<?php
class getproject {
var $id = $_GET['project'];
//project name
function name() {
if ($result = mysql_query("SELECT `title` FROM projects WHERE id = '{$this->id}'")) {
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);
return $row['name'];
}
}
}
}
?>
[/CODE]
Link to comment
https://forums.phpfreaks.com/topic/35643-php-class/#findComment-168825
Share on other sites

I don't think you can declare and assign a property in one go in php5. Use....

[code]
<?php
class getproject {

        private $id;

        function __construct() {
                $this->id = $_GET['project'];
        }

function name() {
if ($result = mysql_query("SELECT `title` FROM projects WHERE id = '{$this->id}'")) {
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);
return $row['name'];
}
}
}
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/35643-php-class/#findComment-168836
Share on other sites

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.