28, May 2012

unkown number of arguments - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: unkown number of arguments
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: unkown number of arguments  (Read 1877 times)
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« on: Jun 12, 2006, 01:42:50 pm »

Hello,

I have to write a function where the number arguments is dynamic. Is the use of an array the only way to handle this?

example:
Code:
<?php
$string 
"<a href=\"/{FOLDER}/script.php?lang={LANG}\">some text</a>";
$language "nl";
$dyn_val = array("folder"=>"some_folder""lang"=>$language); // this array can hold more or less elements

function test_overload($string$dyn_val) {
foreach ($dyn_val as $key => $val) {
$string str_replace("{".strtoupper($key)."}"$val$string);
}
return $string;
}
echo 
test_overload($string$dyn_val);
?>


I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #1 on: Jun 12, 2006, 05:51:02 pm »

This $dyn_val array is created by a query?

The fastest way to do this, is to make it without the use of str_replace. Something like

Code:
<?php
foreach ( $dyn_val as $v )
     echo 
"<a href=\"/".$v['folder']."/script.php?lang=".$v['lang']."\">some text</a>";

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #2 on: Jun 12, 2006, 08:50:20 pm »

the array can hold a x number of arguments, also the string is different every time

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #3 on: Jun 12, 2006, 08:57:59 pm »

If you need a faster way to achieve that the only thing you can do is get rid of the str_replace.

If you could tell me exactly what you want to do, maybe I can help you more.

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #4 on: Jun 12, 2006, 10:07:34 pm »

I have actually all this messages in my classes:
Code:
<?php
$msg[33] = "Hello,\r\n\r\nthe new e-mail address must be validated, click the following link:\r\n".$host.$this->login_page."?id=".$this->id."&validate=".md5($this->user_pw)."&language=".$this->language."\r\n\r\nkind regards\r\n".$this->admin_name;
$msg[34] = "There is no e-mail address for validation.";
$msg[35] = "Hello,\r\n\r\nEnter your new password next, please click the following link to enter the form:\r\n".$host.$this->password_page."?id=".$this->id."&activate=".$this->user_pw."&language=".$this->language."\r\n\r\nkind regards\r\n".$this->admin_name;
$msg[36] = "Your request is processed and is pending for validation by the admin. \r\nYou will get an e-mail if it's done.";
$msg[37] = "Hello ".$this->user_full_name.",\r\n\r\nThe account is active and it's possible to login now.\r\n\r\nClick on this link to access the login page:\r\n".$host.$this->login_page."\r\n\r\nkind regards\r\n".$this->admin_name;
$msg[38] = "The confirmation password does not match the password. Please try again.";
$msg[39] = "A new user...";
$msg[40] = "There was a new user registration on ".date("Y-m-d").":\r\n\r\nClick here to enter the admin page:\r\n\r\n".$host.$this->admin_page."?login_id=".$this->id;
$msg[41] = "Validate your e-mail address..."// subject in e-mail
I want to store them in a database for easier access and translations.
You seen a lot of them are very different. This function will act as kind of parser. The {VALUE} stored  in the database is easy to recognize by not programmers during translations

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #5 on: Jun 13, 2006, 09:03:24 am »

I see. I have made a different solution for that.

I keep the database for easier access and traslations, and each time there is a change on the database, a script creates the php file to include.

This way is still very fast, without loosing the flexibillity of the database.

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #6 on: Jun 13, 2006, 09:43:24 am »

yes I understand, but do you place all variables into the database field?

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #7 on: Jun 13, 2006, 09:47:54 am »

I use this form :

id -> message id
lang_id -> language id
message

And the file looks like :

$msg[$langid][$id] = $message

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #8 on: Jun 13, 2006, 09:58:40 am »

I use this form :

id -> message id
lang_id -> language id
message

And the file looks like :

$msg[$langid][$id] = $message

I think about the same, but I need to find a solution that non prgrammers (translations agencies) can handle this kind of text without breaking the code:
Code:
There was a new user registration on ".date("Y-m-d").":\r\n\r\nClick here to enter the admin page:\r\n\r\n".$host.$this->admin_page."?login_id=".$this->id;

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #9 on: Jun 13, 2006, 10:06:00 am »

That would be more difficult.

I guess you will have to create a script for translators that will strip those special characters/functions and show something like
Code:
, so you will do those str_replace in the input, or split messages that have functions.

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #10 on: Jun 13, 2006, 10:11:16 am »

thats the point where I cam back to my snippet, the question was more about to give arguments to this function Cheesy
I read something about function overload, but this is not supported in PHP(4) right?

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #11 on: Jun 13, 2006, 10:16:06 am »

function overload? You mean class overload?

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #12 on: Jun 13, 2006, 10:23:25 am »


I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #13 on: Jun 13, 2006, 10:31:23 am »

I think you can. Check this :

Code:
<?php
function foo()
{
   
$numargs func_num_args();
   echo 
"Number of arguments: $numargs<br />\n";
   if (
$numargs >= 2) {
       echo 
"Second argument is: " func_get_arg(1) . "<br />\n";
   }
   
$arg_list func_get_args();
   for (
$i 0$i $numargs$i++) {
       echo 
"Argument $i is: " $arg_list[$i] . "<br />\n";
   }
}

foo(123);
?>


http://gr.php.net/manual/en/function.func-get-args.php

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #14 on: Jun 13, 2006, 10:40:08 am »

... that wil say I can add so much as arguments to a function like I want? like:

Code:
<?php
function someFunction($var) {
  
// my code
}

and call this function like:
Code:
<?php
someFunction
("value for var""some other value"12true45.89"etc.");

EDIT: I think I repeated you answer Smiley

Didn't used functions like this before...

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #15 on: Jun 13, 2006, 10:41:34 am »

Yeah, but I am not sure if it works with simple functions, or only with class methods.

Make a test to be sure.

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #16 on: Jun 13, 2006, 10:43:04 am »

Yeah, but I am not sure if it works with simple functions, or only with class methods.

Make a test to be sure.
I will... thanks

I let you know if this works outside the class scope (By the way I will use this kind of function inside a class)

Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=2844
Tags : php databases snippets Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: unkown number of arguments
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 01:06:48 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.