unidox Posted May 29, 2008 Share Posted May 29, 2008 I am just starting to use classes because it will help me stop repition of code, but I was wondering I have $_SESSION['user'] to show the user name of the person on the page. I need this to be accessed by each function in the class, I tried using var $username = $_SESSION['user']; but I guess var needs to be an int. How would I go about doing this? Link to comment https://forums.phpfreaks.com/topic/107866-solved-class/ Share on other sites More sharing options...
FlyingIsFun1217 Posted May 29, 2008 Share Posted May 29, 2008 I don't know if it's anything like C++, but if so, just make it a public member. FlyingIsFun1217 Link to comment https://forums.phpfreaks.com/topic/107866-solved-class/#findComment-552919 Share on other sites More sharing options...
next Posted May 29, 2008 Share Posted May 29, 2008 Yeah, it doesn't have to be int, declare it as public type. Link to comment https://forums.phpfreaks.com/topic/107866-solved-class/#findComment-552937 Share on other sites More sharing options...
unidox Posted May 29, 2008 Author Share Posted May 29, 2008 How do that, and I have another question... Here is my code: function delete($confirm, $s, $access, $id, $link, $table, $table_id, $log, $user, $name) { $q = mysql_query("SELECT * FROM `pcp_users` WHERE `username` = '$user'") or die (mysql_error()); $r = mysql_fetch_array($q); $lvl = $r['level']; // Find the users level. $date = date("F jS, g:i a"); $group = $r['group']; $user_name = $r['username']; if ($lvl == 1) { $q2 = mysql_query("SELECT * FROM `pcp_groups` WHERE `level` = '2'"); $r2 = mysql_fetch_array($q2); $pages = $r2['pages']; $array = split(":", $pages); } else { $q2 = mysql_query("SELECT * FROM `pcp_groups` WHERE `name` = '$group'"); $r2 = mysql_fetch_array($q2); $pages = $r2['pages']; $array = split(":", $pages); } if (($s == 'delete_user') && ($confirm == 'true') && (in_array($access, $array))) { if (!is_numeric($id)) { header("Location: index.php?p=error&h=" . $link . "&e=" . $link . "_1" . ""); exit(); } $q = mysql_query("SELECT * FROM `pcp_$table` WHERE '$table_id' = '$id'") or die (mysql_error()); $r = mysql_fetch_array($q); return mysql_num_rows($q) . "<br />" . $r['level']; } } But when I do $class = new functions(); echo $class->delete(true, "delete_user", "users", "69", "users", "users", "user_id", "User", "unidox", "username"); It doesnt output anything. Whats wrong? BTW the class the function is in is called functions. Link to comment https://forums.phpfreaks.com/topic/107866-solved-class/#findComment-552943 Share on other sites More sharing options...
DarkWater Posted May 29, 2008 Share Posted May 29, 2008 public (int) $user = $_SESSION['username']; Link to comment https://forums.phpfreaks.com/topic/107866-solved-class/#findComment-552945 Share on other sites More sharing options...
unidox Posted May 29, 2008 Author Share Posted May 29, 2008 I got the error: Parse error: syntax error, unexpected T_INT_CAST, expecting T_VARIABLE in /home/purecp/public_html/class.php on line 49 Line 49 is public (int) $user = $_SESSION['username']; Link to comment https://forums.phpfreaks.com/topic/107866-solved-class/#findComment-552954 Share on other sites More sharing options...
DarkWater Posted May 29, 2008 Share Posted May 29, 2008 Woops, forgot you can't use (int) with visibility declarations. Just use public $user = whatever. Link to comment https://forums.phpfreaks.com/topic/107866-solved-class/#findComment-552969 Share on other sites More sharing options...
unidox Posted May 29, 2008 Author Share Posted May 29, 2008 This is my class: <?php class functions { // Vars var $user; var $lvl; var $group; function mysql_conn() { $db_user = "user"; // Username $db_pass = "****"; // Password $db_database = "db"; // Database Name $db_host = "localhost"; // Server Hostname $db_connect = mysql_connect ($db_host, $db_user, $db_pass); $db_select = mysql_select_db ($db_database); } function delete($id, $link, $table, $table_id, $log, $name) { if (!is_numeric($id)) { header("Location: index.php?p=error&h=" . $link . "&e=" . $link . "_1" . ""); exit(); } $q = mysql_query("SELECT * FROM `pcp_$table` WHERE '$table_id' = '$id'") or die (mysql_error()); $r = mysql_fetch_array($q); return mysql_num_rows($q) . "<br />" . $r['level']; } } $class = new functions(); $class->mysql_conn(); echo $class->delete("69", "users", "users", "user_id", "User", "username"); ?> All it does is return 0 and a blank, but there are 16 users in the db. Whats wrong? Link to comment https://forums.phpfreaks.com/topic/107866-solved-class/#findComment-552985 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.