23, November 2008

unkown number of arguments - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ 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

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


It's time to use PHP5!


« on: Jun 12, 2006, 02: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);
?>



Last blog : Just a better Internet portal provided by Google
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8250
42487 credits
Members referred : 3



« Reply #1 on: Jun 12, 2006, 06: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 Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6441
39470 credits
Members referred : 374


It's time to use PHP5!


« Reply #2 on: Jun 12, 2006, 09:50:20 PM »

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


Last blog : Just a better Internet portal provided by Google
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8250
42487 credits
Members referred : 3



« Reply #3 on: Jun 12, 2006, 09: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 Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6441
39470 credits
Members referred : 374


It's time to use PHP5!


« Reply #4 on: Jun 12, 2006, 11: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


Last blog : Just a better Internet portal provided by Google
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8250
42487 credits
Members referred : 3



« Reply #5 on: Jun 13, 2006, 10: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 Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6441
39470 credits
Members referred : 374


It's time to use PHP5!


« Reply #6 on: Jun 13, 2006, 10:43:24 AM »

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


Last blog : Just a better Internet portal provided by Google
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8250
42487 credits
Members referred : 3



« Reply #7 on: Jun 13, 2006, 10: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 Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6441
39470 credits
Members referred : 374


It's time to use PHP5!


« Reply #8 on: Jun 13, 2006, 10: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;


Last blog : Just a better Internet portal provided by Google
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8250
42487 credits
Members referred : 3



« Reply #9 on: Jun 13, 2006, 11: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 Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6441
39470 credits
Members referred : 374


It's time to use PHP5!


« Reply #10 on: Jun 13, 2006, 11: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?


Last blog : Just a better Internet portal provided by Google
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8250
42487 credits
Members referred : 3



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

function overload? You mean class overload?

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

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6441
39470 credits
Members referred : 374


It's time to use PHP5!


« Reply #12 on: Jun 13, 2006, 11:23:25 AM »



Last blog : Just a better Internet portal provided by Google
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8250
42487 credits
Members referred : 3



« Reply #13 on: Jun 13, 2006, 11: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 Visit through proxy

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

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6441
39470 credits
Members referred : 374


It's time to use PHP5!


« Reply #14 on: Jun 13, 2006, 11: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...


Last blog : Just a better Internet portal provided by Google
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8250
42487 credits
Members referred : 3



« Reply #15 on: Jun 13, 2006, 11: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 Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6441
39470 credits
Members referred : 374


It's time to use PHP5!


« Reply #16 on: Jun 13, 2006, 11: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)


Last blog : Just a better Internet portal provided by Google
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....

Topic sponsors:
Get a permanent link here for $1.99!


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?
Nov 23, 2008, 01:32:03 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!


Forum Statistics
Total Posts: 37.756
Total Topics: 7.652
Total Members: 4.400
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: GregH

37 Guests, 6 Users online :

11 users online today:



Readers

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