PHP - Multi Level Methods?
In javascript you can do multiple methods on the same line like:
if(document.getElementById('myElement').className.match(/^[0-9]/)){/*Do something*/} in that we have getElementById() and match() on the same line, and it works the same as if you were to split them on multiple lines. Is it possible to do that with php? For example: $obj = new MyObject(); $obj->add(2, 3)->to_string(); Similar TutorialsHi all I have a menu function which basically produces a menu which looks like Code: [Select] Products Apple iMac iPod iPhone Microsoft Windows Office and the code I use is; THE FUNCTION function menu($parentID, $mymenu) { $html = ""; if (isset($mymenu['parentID'][$parentID])) { $html .= " <ul>\n"; foreach ($mymenu['parentID'][$parentID] as $menu_id) { if(!isset($mymenu['parentID'][$menu_id])) { $html .= "<li>\n <a href='/".$mymenu['menu_item'][$menu_id]['url']."'>".$mymenu['menu_item'][$menu_id]['value']."</a>\n</li>"; } if(isset($mymenu['parentID'][$menu_id])) { $html .= "<li>\n <a href='/".$mymenu['menu_item'][$menu_id]['url']."'>".$mymenu['menu_item'][$menu_id]['value']."</a>"; $html .= menu($menu_id, $mymenu); $html .= "</li>"; } } $html .= "</ul>"; } return $html; } CREATE MENU CODE $result = mysql_query("SELECT id, value, url, parentID FROM menu WHERE active = 1 AND deleted = 1 ORDER BY position ASC"); $mymenu = array('menu_item' => array(),'parentID' => array()); while ($menu_item = mysql_fetch_assoc($result)) { $mymenu['menu_item'][$menu_item['id']] = $menu_item; $mymenu['parentID'][$menu_item['parentID']][] = $menu_item['id']; } echo menu(0, $mymenu); The problem I have is the URLS, at the moment the menu URLS are outputted as Code: [Select] Products - http://localhost/Products Apple - http://localhost/Apple iMac - http://localhost/iMac iPod - http://localhost/iPod But I need to alter my function so that URLS are outputted as Code: [Select] Products - http://localhost/Products Apple - http://localhost/Products/Apple iMac - http://localhost/Products/Apple/iMac iPod - http://localhost/Products/Apple/iPod Is this at all possible? Thanks very much everyone John What am I missing here? The array: protected $form_bonus = array( "Attacker" => array( "Ashwin" => array( "normal" => 1.15, "rps" => 1.38), "Cordelon" => array( "normal" => 1.15, "rps" => 1.38), "Mersan" => array( "normal" => 1.15, "rps" => 1.38), "Phlanixian" => array( "normal" => 1.195, "rps" => 1.494), "Slythe" => array( "normal" => 1.15, "rps" => 1.38) ), "Defender" => array( "Ashwin" => array( "normal" => 1.15, "rps" => 1.38), "Cordelon" => array( "normal" => 1.15, "rps" => 1.38), "Mersan" => array( "normal" => 1.15, "rps" => 1.38), "Phlanixian" => array( "normal" => 1.15, "rps" => 1.38), "Slythe" => array( "normal" => 1.15, "rps" => 1.38) ) ); accessing it: $bonus *= form_bonus[$this->role][$this->race]['rps']; error: PHP Parse error: syntax error, unexpected '[' I want to create an array from a file with this structure TITLE1, HEADER1, key11, key12, key13, key14 TITLE2, HEADER2, key21, key22, key23, key24 . . . How I can get a foreach with these elements: $title="TITLE" $header="HEADER" $key="(one random key)" If needed, I can change the file structure too. I want to have a multi level dropdown selection tool come out of an array (not database; almost all of available codes on the internet are based on mysql databse). I have an array as each element has this format: "Country | City | ID". Using explode, I want to make a dropdown to choose the country, then choosing the cities of the chosen country. Finally, in a simple search box make action="result.php" to lead to result.php?q=$id Thanks for your kind attention db table username------>id->username->cash->points->referrer
db table referral_levels------>id->level->earnings->signupBonusCash->signupBonusPoints->status
username referrer -------- -------- admin kelly88 admin // UPDATE USERNAME ADMIN WITH referral level 1 POINTS/CASH // jacob kelly88 // UPDATE USERNAME ADMIN WITH referral level 2 POINTS/CASH AND USERNAME kelly88 WITH referral level 1 POINTS/CASH // david16 jacob // UPDATE USERNAME ADMIN WITH referral level 3 POINTS/CASH AND USERNAME kelly88 WITH referral level 2 POINTS/CASH AND USERNAME jacob WITH referral level 1 POINTS/CASH //Is this possible. If yes - HOW? Current test registration code with referral level 1 <?php if(!empty($_GET['ref'])){ $referrerUsername = filter_input(INPUT_GET, 'ref', FILTER_SANITIZE_STRING); if(usernameExist($referrerUsername, $db) === TRUE){ $_SESSION['ref'] = $referrerUsername; } } // define variables with the value for each field // the value from POST,GET if this exist, or an empty value $errors = array(); $username = isset($_POST['username']) ? filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING) : ''; $referrer = !empty($_SESSION['ref']) ? $_SESSION['ref'] : (isset($_POST['referrer']) ? filter_input(INPUT_POST, 'referrer', FILTER_SANITIZE_STRING) : ''); if(!empty($_POST['submit'])){ if(empty($username)){ $errors[] = $lang['error']['a_019']; } else if(validUsernameLenght($username) === FALSE){ $errors[] = $lang['error']['a_020']; } else if(validUsernameChars($username) === FALSE){ $errors[] = $lang['error']['a_021']; } else if(usernameExist($username, $db) === TRUE){ $errors[] = $lang['error']['a_022']; } if(!empty($referrer)){ if(usernameExist($referrer, $db) === FALSE){ $errors[] = $lang['error']['a_037']; } else if($username == $referrer){ $errors[] = $lang['error']['a_038']; } } } if(!empty($_POST['submit']) and empty($errors)){ /* $queryOne = 'INSERT INTO users(username, referrer) VALUES (:username, :referrer)'; $insertOne = $db->prepare($queryOne); $insertOne->bindParam(':username', $username, PDO::PARAM_STR); $insertOne->bindParam(':referrer', $referrer, PDO::PARAM_STR); $successOne = $insertOne->execute(); */ if($referrer){ $query = 'SELECT signupBonusCash AS sbc, signupBonusPoints AS sbp FROM referral_levels WHERE level = 1 AND status = "enabled"'; $select = $db->query($query); $row = $select->fetch(PDO::FETCH_ASSOC); $queryTwo = 'UPDATE users SET points = points + :points, cash = cash + :cash WHERE username = :referrer'; $selectTwo = $db->prepare($queryTwo); $selectTwo->bindParam(':cash', $row['sbc'], PDO::PARAM_STR); $selectTwo->bindParam(':points', $row['sbp'], PDO::PARAM_STR); $selectTwo->bindParam(':referrer', $referrer, PDO::PARAM_STR); $selectTwo->execute(); } } if(!empty($errors)){ foreach($errors as $error){ print $error.'<br>'; } } print ' <form method="POST"> <table style="width:100%"> <tr> <td style="width:30%;font-weight:bold">Username</td> <td style="width:70%"><input type="text" name="username" maxlength="255" style="width:200px" value="'.cleanOutput($username).'"></td> </tr>'; if(!empty($_SESSION['ref'])){ print ' <tr> <td style="font-weight:bold">'.$lang['global']['a_047'].'</td> <td><input type="text" name="referrer" readonly="readonly" maxlength="255" style="width:200px" value="'.cleanOutput($referrer).'"></td> </tr>'; }else{ print ' <tr> <td style="font-weight:bold">'.$lang['global']['a_047'].'</td> <td><input type="text" name="referrer" maxlength="255" style="width:200px" value="'.cleanOutput($referrer).'"></td> </tr>'; } print ' <tr> <td colspan="2" style="text-align:center"><input type="submit" name="submit" value="Submit"></td> </tr> </table> </form>'; ?> $form_bonus = array( "Attacker" => array( "Ashwin" => array( "normal" => 1.15, "rps" => 1.38), "Cordelon" => array( "normal" => 1.15, "rps" => 1.38), "Mersan" => array( "normal" => 1.15, "rps" => 1.38), "Phlanixian" => array( "normal" => 1.195, "rps" => 1.494), "Slythe" => array( "normal" => 1.15, "rps" => 1.38) ), "Defender" => array( "Ashwin" => array( "normal" => 1.15, "rps" => 1.38), "Cordelon" => array( "normal" => 1.15, "rps" => 1.38), "Mersan" => array( "normal" => 1.15, "rps" => 1.38), "Phlanixian" => array( "normal" => 1.15, "rps" => 1.38), "Slythe" => array( "normal" => 1.15, "rps" => 1.38) ) ); echo "<pre>".print_r($form_bonus)."</pre>"; output: 1 what am i missing here? Hi guys, I'm still not having any luck with coming up with an idea of how to make my multi-level drop down menu work with items from a database. I don't necessarily need code, but an idea of how to make it work, so any ideas are welcome. The table: Column Descriptions: id - The unique id label - The text to display link - The link to be used relation - I cannot remember why I wanted that... just disregard it parent - The parent item's id (0 for no parent) sort - A sort number for the item (ascending) active - 0 for false, 1 for true (not implemented yet, will add a WHERE `active` = 1 into the sql later) depth - How many parent items there are Now, here's what it has to do (the class): Grab items from the database (sorted by depth, parent, and sort) Place the children in a child array of the parents (or however it can be sorted) Implode the array and turn it into an html menu with the below structure Menu HTML Structure Code: [Select] <div id="menu"> <ul> <li><h2>Level 1 A</h2> <ul> <li><a href="?r=level2/a">Level 2 A</a> <ul> <li><a href="?r=level3/a">Level 3 A</a></li> <li><a href="?r=level3/b">Level 3 B</a></li> </ul> </li> </ul> </li> <li><a href="?r=level1/b"><h2>Level 1 B</h2></a></li> </ul> </div> And here is the code I have so far, but I can't figure out how to get the children into the parent array, or at least sort it so that I can achieve the above HTML structure. Thank you in advance for your help, and as I said above, I don't necessarily need code, but an idea of how to make it work, so any ideas are welcome. Mmm i'm not sure if i'm approaching this correctly. Anyways, I wanted to create a multi-level thread, Where the child thread would be after the parent thread. Something like this: Hello World - Hello World - Hi! - What's up? - Hi Bob! Goodbye World - ....This guy got issues - Dude! You should watch Despicable Me! "It's so fuzzy i'm going to dieee!" Anyways, so my table looks something along the line of this: | post id | parent_id | title | body | date | 1 | NULL | Hello | Hi, i'm K | date 2 | NULL | Thread One | Body One | date 3 | 1 | Hello World | Body One | date 4 | 2 | Thread post 4 | Body One | date 5 | 1 | Thread post 5 | Body One | date 6 | 1 | Thread post 6 | Body One | date 7 | 3 | Hi What's up | Body One | date And here's the PHP code, although it's not very neat I want to clean it up and make it neater. I can only go two level deep so far. Any suggestion or recommendations? Using while & for: // Select * Threads $q = "SELECT * FROM test_db ORDER BY post_id ASC"; $r = @mysql_query($q); while ($pid_row = @mysql_fetch_object($r)) { // Array $pid_arr[] = $pid_row->post_id; // Assign Indexes to Post ID $ppid_arr[] = $pid_row->parent_id; // Assign Indexes to ParentID $md_arr = array($pid_arr, $ppid_arr); // Create multi-dimensional array matrix $parent_id = $pid_row->parent_id; } for ($i = 0; $i < count($pid_arr); $i++) { for ($a = 0; $a < count($ppid_arr); $a++) { $pid = $pid_arr[$i]; // pid = post id $ppid = $ppid_arr[$i]; // ppid = parent id //echo "<div>~~ PID $pid ~~ PPID $ppid ~~</div>"; if ($ppid === NULL) { $ppid_is_null = " & parent_id is NULL"; } else { $ppid_is_null = NULL; } // Select Top level Thread $z = "SELECT * FROM test_db WHERE post_id = '$pid'"; $y = @mysql_query($z); while ($t_row = @mysql_fetch_object($y)) { $title = $t_row->title; // title of child thread $level = $t_row->level; // level of child thread...is this useful? } // Display top level thread lvl 1 if ($ppid === NULL) { // If the parent_id is NULL, display top level thread echo '<div>PID: ' . $pid . ' || PPID: ' . $ppid . " || Title: " . $title . "</div><br>\n"; $post = $pid; // PostID = 1 $parent = $ppid; // Parent ID ex: [1] // Get child id, where parent id = [1] // trying to get the child thread here...but it's not working $child_q = "SELECT * FROM test_db WHERE parent_id = '$post'"; $child_r = @mysql_query($child_q); while($c_row = @mysql_fetch_object($child_r)) { $c_arr[] = $c_row->post_id; $c_ppid_arr[] = $c_row->parent_id; } for ($n = 0; $n < count($c_arr); $n++) { if ($c_ppid_arr[$n] === $post) { echo "<div>$c_arr[$n]</div>"; } } break; //echo "<div>PID: $pid</div>"; } break; } } Using While: <?php require_once('db_connection'); echo '<hr><hr><br>'; // I just realized that selecting * where parent id is null might limit the amount of rows retrieved. // Query to select threads $n = NULL; $q = "SELECT * FROM test_db WHERE (test_db.parent_id is NULL) Order By date ASC"; $r = @mysql_query($q); while ($threads = @mysql_fetch_object($r)){ $pid = $threads->post_id; $sid = $threads->subject_id; $parent_id = $threads->parent_id; $title = $threads->title; $body = $threads->body; echo '<div>PostID: ' . $pid . ' || Parent: ' . $parent_id . ' || SubjectID: ' . $sid . ' || Title: ' . $title . ' || Body: ' . $body . '</div>'; $q2 = "SELECT * FROM test_db WHERE parent_id = '$pid' Order by date DESC"; $r2 = @mysql_query($q2); while ($level2 = @mysql_fetch_object($r2)) { $level2_pid = $level2->post_id; $level = 1.5 *($level2->level); // lvl 2: 3em echo '<div style="text-indent:' . $level . 'em">' . 'PostID: ' . $level2->post_id . ' || ParentID: ' . $level2->parent_id . '</div>'; $q3 = "SELECT * FROM test_db WHERE parent_id = '$level2_pid'"; $r3 = @mysql_query($r3); while ($level3 = @mysql_fetch_object($r3)) { $level3 = 1.5 + $level; echo '<div style="text-indent:' . $level3 . 'em">' . 'PostID: ' . $level3->post_id . ' || ParentID: ' . $level3->parent_id . '</div>'; } } } ?> I'm not a comp. sci major or anything, so I'm learning this as I go. Any help would be greatly appreciated. It seems like the while & for is more practical, but i'm not quiet sure. What am I doing wrong? - K I just joined this forum today ^_^ Hope to be good friends with you guys and gals Well the title may seem a bit confusing, but heres an example: Code: [Select] <?php class User{ public $uid; public $username; protected $password; protected $email; public $usergroup; public $profile; public function __construct($id){ // constructor code inside } public function getemail(){ return $this->email; } public function getusergroup(){ return $this->usergroup; } public function getprofile(){ $this->profile = new UserProfile($this->uid); } } class UserProfile(){ protected $avatar; protected $bio; protected $gender; protected $favcolor; public function __construct($id){ // constructor code inside } public function formatavatar(){ // avatar formatting code inside } public function formatusername(){ // format username? } } ?> As you can see, the User class(an outer class) has a property called Profile, which can be instantiated as a UserProfile object(an inner class). The two objects have distinct functionalities, but there are times when the UserProfile object needs to access property and methods from the user object. I know its easy for outer class to access methods from inner class by using the single arrow access operator twice, but how about the other way around? Lets say from the above example the userprofile can format the username displayed to the screen by adding a sun to the left of the username if the usergroup is admin, a moon if the usergroup is mod, and nothing if its just a member. The usergroup property is stored in the outer class, and can be accessed with this $user->getusergroup() method only. I know I can always do the hard way by passing a user object to the method's argument, but is there an easier way for the inner class UserProfile to access properties/methods for outerclass User? If so, how can I achieve that? hey guys im wdondering if there's a magic method which works like
__GET()but allows you to put a parameter in...thank you <?php class test { public function run() { $this->object->('string'); // __get with parameter? } public function __get($value) { return $this->{$value} } } ?> what are magic methods? when do we need to use magic methods? what are the advantages of using magic methods? thanks in advanced. I don't know if this belongs here; sorry if it doesn't. I'm using a method=get on a form as I need to pass information through a URL once the form has been submitted. But I also need to INSERT some data from that form to multiple tables. - How can I stop the GET from passing my other variables into the URL? - How do I make the form submit to itself? I need to use the 'index.php?p=checkout' as this matches with a switch statement, which matches to the checkout page. The function PHP_SELF will just refresh to the index.php page, which will not have any of this checkout's page validation. Here's my code: Code: [Select] <h2>Please enter your details</h2> <h3>All fields required</h3> <div id="checkout"> <?php if (isset($_GET['checkout'])){ require_once ('./includes/connectvars.php'); $title = $_GET['title']; $fname = $_GET['fname']; $sname = $_GET['sname']; $ctype = $_GET['ctype']; $cnumber = md5($_GET['cnumber']); $syear = $_GET['smonth'] . $_GET['syear']; $fyear = $_GET['fmonth'] . $_GET['fyear']; $service = $_GET['cardAuth']; $amount = $_REQUEST[$total]; $msg = rand(1000,9999); $api = 'd41d8cd98f00b204e9800998ecf8427e'; $b_house = $_GET['b_house']; $b_postcode = $_GET['b_postcode']; $b_city = $_GET['b_city']; $b_country = $_GET['b_country']; $d_house = $_GET['d_house']; $d_postcode = $_GET['d_postcode']; $d_city = $_GET['d_city']; $d_country = $_GET['d_country']; $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $query = "INSERT INTO customer (cust_id, first_name, last_name) VALUES ('', '$fname', '$sname') "; $query = "INSERT INTO bill (cust_id) VALUES ('') "; $query = "INSERT INTO deliver (cust_id) VALUES ('') "; mysqli_query($dbc, $query); } //.'service='.$service.'msg_id='.$msg.'num_md5='.$cnumber.'amount='.$amount.'currency=GBP'.'api_key='.$api. ?> <form method="get" action="index.php?p=checkout"> <table> <tr> <td><input type="hidden" name="cardAuth" /></td> </tr> <tr> <td> Title: </td> <td> <select name="title" value="<?php if (!empty($title)) echo $title; ?>" > <option></option> <option>Mr</option> <option>Sir</option> <option>Ms</option> <option>Miss</option> <option>Mrs</option> </select> </td> </tr> <tr> <td> First Name: </td> <td> <input type="text" name="fname" value="<?php if (!empty($fname)) echo $fname; ?>"/> </td> </tr> <tr> <td> Surname: </td> <td> <input type="text" name="sname" value="<?php if (!empty($sname)) echo $sname; ?>"/> </td> </tr> <tr> <td> </td> </tr> <tr> <td> Card Type: </td> <td> <select name="ctype" value="<?php if (!empty($ctype)) echo $ctype; ?>"> <option>mastercard</option> <option>visa</option> <option>amex</option> <option>solo</option> <option>maestro</option> <option>jcb</option> <option>diners</option> </select> </td> </tr> <tr> <td> Card Number: </td> <td> <input type="text" name="cnumber" value="<?php if (!empty($cnumber)) echo $cnumber; ?>"/> </td> </tr> <tr> <td> Valid From: </td> <td> <select name="smonth" value="<?php if (!empty($smonth)) echo $smonth; ?>"> <option>01</option> <option>02</option> <option>03</option> <option>04</option> <option>05</option> <option>06</option> <option>07</option> <option>08</option> <option>09</option> <option>10</option> <option>11</option> <option>12</option> </select> <select name="syear" value="<?php if (!empty($syear)) echo $syear; ?>"> <option>07</option> <option>08</option> <option>09</option> <option>10</option> <option>11</option> </select> </td> </tr> <tr> <td> Expires End: </td> <td> <select name="fmonth" value="<?php if (!empty($fmonth)) echo $fmonth; ?>"> <option>01</option> <option>02</option> <option>03</option> <option>04</option> <option>05</option> <option>06</option> <option>07</option> <option>08</option> <option>09</option> <option>10</option> <option>11</option> <option>12</option> </select> <select name="fyear" value="<?php if (!empty($fyear)) echo $fyear; ?>"> <option>10</option> <option>11</option> <option>12</option> <option>13</option> <option>14</option> </select> </td> </tr> <tr> <td><h4>Billing Address</h4></td> </tr> <tr> <td> House Name/ Number: </td> <td> <input type="text" name="b_house" /> </td> </tr> <tr> <td> Postcode </td> <td> <input type="text" name="b_postcode" /> </td> </tr> <tr> <td> City: </td> <td> <input type="text" name="b_city" /> </td> </tr> <tr> <td> Country: </td> <td> <input type="text" name="b_country" /> </td> </tr> <tr> <td><h4>Delivery Address</h4></td> </tr> <tr> <td> House Name/ Number: </td> <td> <input type="text" name="d_house" /> </td> </tr> <tr> <td> Postcode </td> <td> <input type="text" name="d_postcode" /> </td> </tr> <tr> <td> City: </td> <td> <input type="text" name="d_city" /> </td> </tr> <tr> <td> Country: </td> <td> <input type="text" name="d_country" /> </td> </tr> <tr> <td> </td> </tr> <tr> <td> </td> <td> <input type="submit" name="checkout" value="Checkout"/> </td> </tr> </table> </form> </div> I have a class that has the __get and __set magic methods defined. The idea is to use those to communicate with the __get and __set methods in a database table object which is a protected attribute of this class. Then I can quickly build out models without having to bother with method definitions aside from those that need modifications made to the data before saving. The problem is, they don't seem to be doing anything. I get an undefined function error the first time I call getVarx(). I don't actually have any of the attributes I'm trying to get and set defined in the object, but I thought that was the point of these magic methods. Am I missing something here? Thanks, Brandon I have two classes were one extends the other but they both have magic methods __get __call and __set but when trying to call a magic method it conflicts one with the other as you can imagaine...is there a way of getting around this ie. name spaces
or do I simply have to rewrite my classes
thank you
Hi, I am trying to get my head around classes, interfaces and methods for different animals
this is what I have so far: <?php interface animal { function walk(); function fly(); function swim(); } class monkey implements animal{ const interface1 = "I am from test class"; function noise() { echo "ooh ooh ah ah!"; } function walk() { echo "monkey is walking"; } function fly() { echo "monkey is Flying"; } function swim() { echo "monkey is Swimming"; } public function display() { echo monkey::interface1; echo PHP_EOL; } } class bear implements animal{ const interface1 = "I am from test class"; function noise() { echo "Grrr!"; } function walk() { echo "Bear is walking"; } function fly() { echo "Bear is Flying"; } function swim() { echo "Bear is Swimming"; } public function display() { echo bear::interface1; echo PHP_EOL; } } $Obj = new monkey(); $Obj->display(); $Obj1 = new bear(); $Obj1->display(); ?> I'm a bit stumped as animal interface not calling all functions?! please help I just have a quick question, what happens if I don't assign a visibility element to methods? In other words, if I just have: [php] class Article_model { public $data; function index() { return $data; } [php] ...instead of declaring public, private, static, and so on before the function. Will php just consider it public? Is there any way to produce a custom magic method? Let's say when I try to get an attribute that doesn't have any value, a method gets called that gives that attribute a value. So I want a method to be triggered when I try to get a value from an attribute that doesn't have any value. Hi PhpFreaks, I have a method that I need to be initialised dynamically and I am not sure if the code below will work I have been able to workout if the method exists but I have not found a way to it get to return the array. Has anyone go any ideas? The error I get is: Catchable fatal error: Object of class Forms could not be converted to string Code: [Select] public function setMethodForms($methodName){ echo $methodName." and does it exist ". (int)method_exists($this->formsObj, $methodName); //test to see if the method exists if ((int)method_exists($this->formsObj, $methodName) == 1){ //if it does execute //print_r ($this->formsObj->projects()); //test to see if object works $return call_user_func($this->formsObj."->".$methodName."()"); } return $return; } So, I'm starting a new project and thought I would try a new approach at loading files and classes to process output. I think my logic is correct, but for some reason I'm getting a blank page. Basically instead of having seperate pages to output different functions and such, I store everything in one page. It's the same deal as the switch case approach, but it uses a foreach loop to cycle through the actions, classes, and functions which are stored in an array. Here's my index.php file: Code: [Select] <?php require('config.php'); include('languages/english.php'); include('classes/Template.php'); $template = new Template; $viewPages = array( 'category' => array('classfile' => 'Category.php', 'classname' => 'category', 'functions' => array('create', 'delete', 'modify', 'merge')), 'questions' => array('classfile' => 'Question.php', 'classname' => 'Question', 'functions' => array('create', 'delete', 'modify', 'votegood', 'votebad')) ); $currentPage = $_REQUEST['action']; foreach($viewPages as $action => $settings){ if($currentPage == $action){ require(INCLUDE_ROOT.'/classes/'.$settings['classfile']); $class = new $settings['classname']; $function = $_REQUEST['do']; $class->$function(); $template->loadTemplate($settings['classname']->viewFile, $vars = array()); if($template->message == FALSE){ die($template->message); } } } ?> Here's the Template class file: Code: [Select] <?php class Template { var $file; var $vars; var $message; function loadTemplate($file, $vars){ if(empty($file) || empty($vars)){ $this->message = LANG_ERR_7; } else if($file = file_get_contents(INCLUDE_ROOT.'/'.$file)){ $this->message = LANG_ERR_7; } else { foreach($vars as $key => $val) $file = str_replace('{'.$key.'}', $val, $file); } $this->message = FALSE; return $file; } } ?> I tried visiting the category action(index.php?action=category;do=create). This is the classfile for that Code: [Select] <?php class category { var $id; var $title; var $description; var $uri; var $message = array(); var $vars = array(); var $viewFile = ''; function create(){ if($_POST['submit']){ //Review the user input and make ure everything is ok $messages = array(); if($this->title == FALSE){ $messages['title'] = LANG_ERR_1; } else if(strlen($this->title) > 30 || strlen($this->title < 5)){ $messages['title'] = LANG_ERR_2; } else if($this->description == FALSE){ $messages['description'] = LANG_ERR_3; } else if(strlen($this->description) > 400 || strlen($this->description) < 10){ $messages['description'] = LANG_ERR_4; } else { $messages = FALSE; } if($messages != FALSE){ $this->messages = $messages; $this->viewFile = INCLUDE_ROOT.'/template/index_body.tpl'; } else { $this->title = htmlentities($this->title); $this->title = stripslashes($this->title); $this->title = htmlspecialchars($this->title); $this->description = htmlentities($this->description); $this->description = htmlspecialchars($this->description); $this->description = nl2br($this->description); $query = " INSERT INTO categories (c_title, c_desc) VALUES('".$this->title."', '".$this->desscription."')"; if(mysql_query($query)){ $this->message['succes'] = LANG_ERR_5; } else { $this->message['fail'] = LANG_ERR_6; } $this->viewFile = INCLUDE_ROOT.'/template/message_body.tpl'; } } else { $this->viewFile = INCLUDE_ROOT.'/template/create_category.tpl'; } } } ?> Now if you look at the Category class, the template file it sets is the "category_create.tpl" file, which I haven't created yet. I'm expecting there to be an error associated with the template class and the file_get_contents function that calls the file, but like I said, I'm just getting a blank page. I've never tried this approach before, so I have no idea what's causing this. Any help would be greatly appreciated. Hi, I'm new to php and am attempting to build a site where the navigation and content is built with query functions. My current tables in my database are Categories, Products, and Variation. So a person chooses a category -> then a product -> then a product page shows variations for that product. The problem I am currently having is after when a person selects a section the previous section selected is reset. So lets a person selects a category the product list is shown, but when a person selects a product, the category is no longer selected in the navigation. Or if someone selects product and sees all the variations, if he selects a variation the product ID is reset. I've tried to comb through the internet for some type of solution. I came across concatenating URL encode, but am not sure if this is the correct solution. I tried concatenating one of my urls with Code: [Select] "<a href=\"product_detail.php?cat=" . urlencode($sel_cat["id"]) . '&' . "product=" . urlencode($sel_product['id']) . '&' . "var=" . urlencode($var["id"]) . "\" its a bit of a stab in the dark. My $_GET function is Code: [Select] function find_selected_product() { global $sel_cat; global $sel_product; global $sel_var; if (isset($_GET['cat'])) { $sel_cat = get_cat_by_id($_GET['cat']); } elseif (isset($_GET['product'])) { $sel_cat = NULL; $sel_product = get_product_by_id($_GET['product']); } elseif (isset($_GET['product'])) { $sel_product = get_product_by_id($_GET['product']); $sel_var = get_default_var($sel_product['id']); } elseif (isset($_GET['var'])) { $sel_product = NULL; //test $sel_var = get_variation_by_id($_GET['var']); } else { $sel_cat = NULL; $sel_product = NULL; $sel_var = NULL; } } My navigation code is Code: [Select] function public_navigation($sel_cat, $public = true) { $output = "<td class=\"c1\"><div class=\"sidenav\"><ul>"; $cat_set = get_all_cats($public); while ($cat = mysql_fetch_array($cat_set)) { $output .= "<li>"; if ($cat["id"] == $sel_cat['id']) { $output .= "<a class=\"current\""; } $output .= "<a href=\"index.php?cat=" . urlencode($cat["id"]) . "\">{$cat["cat_name"]}</a></li>"; } $output .= "</ul>"; return $output; } My product is divided into gender and is written like Code: [Select] function cat_content_ladies($sel_cat, $sel_product, $female = true) { $output = "<ul class=\"regions\">"; $cat_set = get_all_cats($female); while ($cat = mysql_fetch_array($cat_set)) { if ($cat["id"] == $sel_cat['id']) { $product_set = get_products_for_female($cat["id"], $female); $output .= "<tr>"; while ($product = mysql_fetch_array($product_set)) { if ($cat["id"] == $sel_cat['id']) { $output .= "<tr>"; $output .= "<div class=\"pre\"><a href=\"product_detail.php?product=" . urlencode($product["id"]) . "\"></li>"; $output .= "<img src=\"{$product["img_src"]}\" height=\"121\" width=\"121\">"; $output .= "<div>{$product["prod_name"]}</a></div>"; } } $output .= "</tr>"; } } return $output; } My variation table is set up in thumb nails and is written in this function Code: [Select] function show_thumb($sel_product, $sel_var, $public = true) { $output = "<ul class=\"subjects\">"; $product_set = get_all_products($public); while ($product = mysql_fetch_array($product_set)) { $output .= "<li"; if ($product["id"] == $sel_product['id']) { $output .= " class=\"selected\""; } //$output .= "><a href=\"product_detail.php?product=" . urlencode($product["id"]) . //"\">{$product["id"]}</a></li>"; if ($product["id"] == $sel_product['id']) { $var_set = get_variations_for_product($product["id"], $public); $output .= "<ul class=\"pages\">"; while ($var = mysql_fetch_array($var_set)) { $output .= "<li"; if ($var["id"] == $sel_var['id']) { $output .= " class=\"selected\""; } $output .= "><a href=\"product_detail.php?var=" . urlencode($var["id"]) . "\">{$var["id"]}</a></li>"; } $output .= "</ul>"; } } $output .= "</ul>"; return $output; } any light on this subject would be greatly appreciated. |