PHP - Dynamically Calling Method Via Url
So i have an url routing class set up. I wan to call method via url so it would look something like this: http://www.mysite.com/index.php/Class/Method/Parameter. I was able to instantiate class like this:
Code: [Select] $controller = new $this->UrlPath[0]; But i can't call method the same way Code: [Select] $controller->$this->UrlPath[1](); Is there a way to call method with an array? Similar Tutorials
My script has 3 classes (that are relevant to this discussion): DB, User and Validate. They are all in independent files and loaded automatically, when required, by an autoloader.
The error messages I am getting a Any pointers as to what I am doing wrong, or what I should be doing, would be most welcome. i have a constructor Quote public function __construct(){ // check if any action related to this page was generated if(isset($_GET['action'])){ $this->action = ucwords($_GET['action']) . '()'; self::{$this->maction}; } //code to generate the list of all the systems users $user = new userDAO; $this->results = $user->fetchAll(); } if action is present in the querystring, eg. delete, it would be stored into $this->action as Delete(). Similarly, if the action was edit, it would be stored as Edit(). My intent is to call the method named Delete() or Edit() depending upon which action was generated. I have defined the methods in the same class. Once i assign the current action to the $this->action, i want to call the method without explicitly specifying the method name.... here i have tried self::$this->action.... i even tried $this->action only but its not working? i think the string stored in the self::$this->action is not being interpolated? or....sth. i dont know. pls help out guys Hi, I've a two classes A,B. A is a parent class and B is a child class. B is extends from A. I made an object of class A. Now i want to access function of class B from this object instance. e.g Code: [Select] class A { function myfunctiona() { ....... ....... } } class B extends A { function chlidfunction() { return "hyne"; } } $sani = new A(); //here i want to access echo $sani->b->childfunction(); please guide how can called child class function from parent object instance? Thanks I looked for and found a similar thread, but the resulting bunny trail was specific to that OP's situation. I'll jump right to it. I've seen this done on another site, and I want to implement it the same way on another site I'm doing. Any typical page of the site will have includes files, quite a few of them nested like those little russian dolls. For instance, the about us page calls includes/header.php... <?php include("includes/header.php") ?> ...and the header.php file calls the meta information... <?php include("includes/site_titles.php") ?> <?php include("includes/site_keywords.php") ?> <?php include("includes/site_descriptions.php") ?> ...and those files have arrays... <?php $SITE_TITLES = array(); $SITE_TITLES['about_us'] = $SITE_TITLE . " About Us"; $SITE_TITLES['page2'] = $SITE_TITLE . " page2"; $SITE_TITLES['page3'] = $SITE_TITLE . " page3"; ?> ...and going back to our header.php, in the appropriate place, we echo the title and meta... <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title><?=$SITE_TITLES[$page]?></title> <meta name="description" content="<?php echo $SITE_DESCRIPTIONS[$page]?>" /> <meta name="keywords" content="<?php echo $SITE_KEYWORDS[$page]?>" /> ...which, when uploaded, *should* display in "view source" the correct title tag, keywords, and description. However, when I do this, I get empty tags. Am I supposed to define the site variable ($SITE)? Or am I doing something else wrong? I have the original source where I first saw this in Dreamweaver, but am unable to find a file where $SITE and all it's little buddies are defined to correspond with a file name ('about' = ("about.php"). Any help or advice is greatly appreciated. Myotch <?php $headers[0] = 'Content-Type: text/namevalue'; $headers[1] = 'X-PAYPAL-SECURITY-USERID: blah'; $headers[2] = 'X-PAYPAL-SECURITY-PASSWORD: blah'; $headers[3] = 'X-PAYPAL-SECURITY-SIGNATU blah'; $headers[4] = 'X-PAYPAL-APPLICATION-ID: blah'; $headers[5] = 'X-PAYPAL-REQUEST-DATA-FORMAT: NV'; $headers[6] = 'X-PAYPAL-RESPONSE-DATA-FORMAT: NV'; $endpoint = 'https://api-3t.paypal.com/nvp'; $method = 'BMGetButtonDetails(hostedbuttonid:AUKAUSD1)'; $options = array('http' => array('header' => 'Content-type: application/x-www-form-urlencoded', 'method' => 'POST', 'content' => http_build_query($method))); $response = file_get_contents($endpoint, $method, $options, $headers); header('Location: gwpvlist.mv?done=aukretrieved&pvi=AUK&actioni=list&identityi=AUKMUSD1&sesx=54467585000E3C510000766200000000&ans='.$response);?>
It seems to go somewhere but there is nothing in $response.
By the way, I am an infant at php usage. (and an imbecile otherwise!)
Ok. I know you can pass the object of a class as an argument. Example: class A { function test() { echo "This is TEST from class A"; } } class B { function __construct( $obj ) { $this->a = $obj; } function test() { $this->a->test(); } } Then you could do: $a = new A(); $b = new B($a); Ok so that's one way i know of. I also thought that you could make a method static, and do this: (assuming class A's test is 'static') class B { function test() { A::test(); } } But that is not working. I'd like to know all possible ways of accomplishing this. Any hints are appreciated. thanks Hi Can you call Class A's methods or properties from Class B's methods? Thanks. Hello, I am having some issues with a project I am working on. I am calling a php file on another server using Code: [Select] <script type="text/javascript" src="http://www.website.com/test/file.php"></script> I have the correct type header in my php file and the php outputs JS. example Code: [Select] echo "document.write(\"some stuff\");"; that kind of thing happens alot I also have it outputting a few JS functions, example Code: [Select] echo 'function dostuff(){ stuff }'; the problem I am having is when I add the JS call to the page that calls the php it does nothing I am using document.ready I am not really sure where to go from here, I know php well enough to do anything I need but I do not know JS at all I can not post the actual source code, I know that would be much easier to troubleshoot this but this is something I just can not do. Also this needs to be done this way, I need to call to php files on another server so they can do some stuff and then use JS to change some things on another page on another server. This is a content locker, a lot like CPALead and things like that. What am I missing? Any help is greatly appreciated I define a Javascript in the <head> section of a .php file and call it from the <body> section with the following: echo "<script language='javascript'>function();</script>". The function displays a confirm box with the normal Cancel and OK buttons just fine, but I want to capture the function return of true or false for which button was selected. So far I haven't found a way to get return a value from the function. Any ideas??? Hi, I'm currently in the process of making my first website. The problem is that I'm trying to change the language the website will be in by pressing a button that will call the setlocale() function. But I can't seem to get it to work. From what I read on the internet you can't use php in javascript because it's serverside. But I can't really think of an alternative way of doing this. Any sugestions? This is my code btw for what it's worth. Code: [Select] <form action="SetLangEN()" > <input type="image" src="../images/ENflag.jpg" alt= "EN" width="20px" height="15px" onclick="SetLangEN()" /> </form> </form> </div> <script type="text/javascript"> function SetLangEN() { <?php setlocale(LC_ALL, "en_US"); ?> } </script> This is probably a quick one. I am pretty new to OOP, in fact somewhat a novice. I am working a lot more with objects lately as my previous "flat" php experience doesn't allow me to create clean and expandable applications. Of course though, questions will always pop up (that's what you guys are for?) One concept I am having trouble understanding, is the method to call a class. What is the difference between?: Code: [Select] <?php $cat = new class(); ?> and just simply: Code: [Select] <?php new class(); ?> Both appear to have the same output from my small amount of practice, but the first way seems strange to me. Being that I am very new to OO PHP, the first way looks like it is just defining a var. But in reality it is calling the class and running it? This seems very backwards to me and I am having a hell of a time understanding it. I am aware that the first way seems to be the "proper" way, but just can't fathom it. Could someone explain this to me a little further? thanks much, How can i call a specific row when ever a like. example : Code: [Select] $record = mysql_query("select * from table_name"); ($getValue= mysql_fetch_row($record) //i'm not sure what should i use. should i use mysql_fetch_array, mysql_ fetch_ assoc, mysql_ fetch_ object, mysql_ field_ name and etc. print $getValue[$column_name][0]; 0 = value 1st row in the database/table_name 1 = value 2nd row in the database/table_name 2 = value 3rd row in the database/table_name ...... .... ...... ..... etc. is there a code i can do except from using sql_fetch_array then loop and put in into a multidimensional array? I have a PHP config file called config.php. inside the file is simular to this: Quote <?php class JConfig { var $offline = '0'; var $editor = 'tinymce'; var $list_limit = '20'; } ?> From another file, i have included config.php, but how do I call $editor to get "tinymce"? Thanks I am trying to call a javascript function from a php script. This is the approach I'm using. I have an alert in the function, just to confirm the function executes. Code: [Select] echo "<script type='text/javascript' src='http://localhost/Project/JScript/File.js'>"; echo "<script type='text/javascript'>functionName();</script>"; Is this the correct approach? Hi, I got a little problem with calling a PHP function from a href. I got my site all set up, and am calling the function like this: Code: [Select] <?php echo '<a href="'.changeRecentPosts().'" class="image">' ?> This does indeed work, but it calls the function every time I refresh my page.. Which isn't the effect I was trying to achieve. I tried to change it to : Code: [Select] <?php echo '<a href="#" onClick="'.changeRecentPosts().'" class="image">' ?> But that didn't work either. It also seems it only calls it when I refresh my page, but never when I click it (I guess because PHP is handeled before HTML or something similar?). If you can think of any way (even in AJAX, or whatever other language...) feel free to, I think I can figure it all out. Thank you! i have a database which contains a table named girls with three columns ...Serial(int) name(text) hits(int) <?php error_reporting(E_ALL ^ E_NOTICE); $con = mysql_connect("localhost","gaurav",""); mysql_selectdb("website",$con); $index1 = rand(1,$count); $sql = mysql_query("SELECT * FROM girls WHERE Serial=$index1"); $row = mysql_fetch_array($sql); $name1 = $row['name']; $hits1 = $row['hits']; echo <<<ECO <html> <script language="javascript"> increment(){ // some php code to increase the hits in database where name = $name1 } </script> <input type="button" value="increment" onclick="increment();" name="button"> </html> ECO; ?> sorry for the code to be really haphazard..i jst started with php.....now i dont know wat code to put to increment the hits in increment function since it would also use varialble $name1 and $index1..... How can I make a link that posts to a script? I using just a link but since it doesn't post I just get a URL with a variable and I can't do anything with it. If you need anything else then just ask I am using simple machines forum, and by placing a link to ssi.php in the top of my php page Code: [Select] <?php require_once('/forum/SSI.php');?>I am able to use some ssi functions. My problem is calling another function from within a function. My first function is this: Code: [Select] <?php $allowed_groups = array(1,5); $can_see = FALSE; foreach ($allowed_groups as $allowed) if (in_array($allowed, $user_info['groups'])) { $can_see = TRUE; break; } if ($can_see) { echo ' <div> this is text that i want certain groups to see </div>'; } else { //message or section to show if they are not allowed. echo 'this is text that i dont want certain groups to see'; } ?> This is my second function: Code: [Select] <?php ssi_welcome(); ?> I want to put the second function inside the first function, like so: Code: [Select] if ($can_see) { echo ' <div> <?php ssi_welcome(); ?> </div>'; this just gives me the "<?php ssi_welcome(); ?>" displayed on the actual site, it is not calling the separate function. I have tried different approaches, e.g. $function ssi_welcome; but all it does is display this as text as well.. Please can some one point me in the right direction. Thank you Hi, I was looking around the web for a image resizer script and I came across this one. http://www.scriptol.com/scripts/thumbnail-maker.php The weird thing is, it says to use this syntax to get it to work php resizer.php -j myimage.png Well.. I don't seem to understand how that makes it work, Normally I would run a php script by looking for it's main function and running it like: mainfunction(); Maybe I'm interpreting it wrong, but how is this script suppose to run? I have this piece of code: Code: [Select] function newmessage() { box = new LightFace({ title: 'New message', width: 600, height: 400, content: "<form method='post' action='sendmessage.php'>To: <br><input type='text' name='to'><br><br>Subject: <input type='text' size='94' name='subject'><br><br><textarea name='message' rows='13' cols='63'></textarea><br><br><input type='submit' value='Send' name='send'><input name='from' type='hidden' value='<? echo $username; ?>'></form>", buttons: [ { title: 'Close', event: function() { this.close(); } } ] }); box.open(); } As well as this piece of code: Code: [Select] $userid = mysql_real_escape_string($_SESSION['userid']); //WALLNOT $db2 = "SELECT status FROM comments WHERE touser='$username' AND status='0'"; $db2connect = $database->query($db2); $status2 = "SELECT status FROM friends2 WHERE user2='$username' and status='0'"; $test = $database->query($status2); $statfromdb = "SELECT stat FROM wallposts WHERE person='$username' AND stat='0'"; $status = $database->query($statfromdb); $GetStatus = "SELECT byuser, status, dtime FROM commentstatus WHERE status='$Naw'and person='$username' AND byuser!='$username'"; $ConnectGetStatus = $database->query($GetStatus); $Getmsg = "SELECT status FROM messages WHERE status='0' AND touser='$username'"; $ConnectGetmsg = $database->query($Getmsg); $row = mysql_fetch_array($test); $row2 = mysql_fetch_array($ConnectGetmsg); $getrow = mysql_fetch_array($status); $getrow2 = mysql_fetch_array($db2connect); $StatusComment = mysql_fetch_array($ConnectGetStatus); //Select profile picture $GetProfilePicture = "SELECT profilepicture FROM users WHERE username='$username'"; $ConnectProfilePicture = $database->query($GetProfilePicture); $ProfilePicture = mysql_fetch_array($ConnectProfilePicture); $MyProfilePicture = $ProfilePicture['profilepicture']; That I use in 19 different pages, is there anyway I can put them all into one page, and just them through a function? It takes so much time when loading if I have it on each page? And if you have any other tips how to optimize my loading speed please write them. Thank you in advance! *Edit: Bad grammar. |