chriscloyd Posted January 25, 2007 Share Posted January 25, 2007 would this be how to start a class if im trying to get an id from a urlsay if my url is index.php?project=3 <=== that 3 is the idhow do i echo the class to get the $p_name[code]<?phpclass 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] Quote Link to comment https://forums.phpfreaks.com/topic/35643-php-class/ Share on other sites More sharing options...
trq Posted January 25, 2007 Share Posted January 25, 2007 [code]<?phpclass 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. Quote Link to comment https://forums.phpfreaks.com/topic/35643-php-class/#findComment-168814 Share on other sites More sharing options...
chriscloyd Posted January 25, 2007 Author Share Posted January 25, 2007 ya but how do u do the things like $project = new getprojecti have looked for tutorials but cant find any Quote Link to comment https://forums.phpfreaks.com/topic/35643-php-class/#findComment-168815 Share on other sites More sharing options...
trq Posted January 25, 2007 Share Posted January 25, 2007 [code]<?php $project = new getproject(); echo $project->p_name();?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/35643-php-class/#findComment-168816 Share on other sites More sharing options...
chriscloyd Posted January 25, 2007 Author Share Posted January 25, 2007 so if i do it the way u show i can call it up like this[CODE]<?php $project = new getproject(); echo $project->p_name();?>[/CODE] Quote Link to comment https://forums.phpfreaks.com/topic/35643-php-class/#findComment-168817 Share on other sites More sharing options...
chriscloyd Posted January 25, 2007 Author Share Posted January 25, 2007 i get this errorParse error: parse error, unexpected T_VARIABLE in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\includes\porfolio_getfunctions.php on line 3[CODE]<?phpclass 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] Quote Link to comment https://forums.phpfreaks.com/topic/35643-php-class/#findComment-168825 Share on other sites More sharing options...
trq Posted January 25, 2007 Share Posted January 25, 2007 What version of php are you using? Quote Link to comment https://forums.phpfreaks.com/topic/35643-php-class/#findComment-168829 Share on other sites More sharing options...
trq Posted January 25, 2007 Share Posted January 25, 2007 By the way.... you'll never get your query to return $row['name'] if you are selecting title. Quote Link to comment https://forums.phpfreaks.com/topic/35643-php-class/#findComment-168831 Share on other sites More sharing options...
chriscloyd Posted January 25, 2007 Author Share Posted January 25, 2007 im using the php 5 Quote Link to comment https://forums.phpfreaks.com/topic/35643-php-class/#findComment-168834 Share on other sites More sharing options...
trq Posted January 25, 2007 Share Posted January 25, 2007 I don't think you can declare and assign a property in one go in php5. Use....[code]<?phpclass 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] Quote Link to comment https://forums.phpfreaks.com/topic/35643-php-class/#findComment-168836 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.