PHP - When Passing Parameters, Do I Pass Only What I Need Or Do I Pass Entire Parent Object?
I work with a large codebase and I have this situation come up fairly often:
the legacy class has large objects such as:
$design->motor->dimensions->a; $design->motor->dimensions->bd; $design->specifications->weight; $design->data->height; //etcWhen I create a new class, that class sometimes operates on the specific data, so say: class MyClass { public function compute($weight, $height, $a) { //stuff } } //and I call this for example as such: (new MyClass())->compute($design->specifications->weight, $design->data->height, $design->motor->dimensions->a);CONS: Potential issue: if design specs change and I need to change things parameters in "compute" function, I need to "re-key" the variables I pass to the function, and adjust things accordinly in MyClass as wel. PROS: only the exact data is being passed, and this data can come from anywhere it is viable, so in effect the function is fairly well separated, I think. Alternative option for me is to pass the entire design object, i.e class MyClass { public function compute(&$design) //can also be done via DI through a setter or constructor. { print $design->specifications->weight; print ($design->data->height + $design->motor->dimensions->a); //stuff } } (new MyClass())->compute($design);PROS: In this case when things change internally in which variables are needed and how things are computed, I only need to change the code in compute function of the class itself. CONS: MyClass now needs to be aware of how $design object is constructed. When it comes to this parameter passing, and Dependency Injection like this in general, does Computer Science advocate a preference on this issue? Do I pass the entire object, or do I pass only the bits and pieces I need? Similar TutorialsI tried many ways but unable to pass sessions to virtual subdomains on my site.. I edited ihi.ini file and included Code: [Select] session.cookie_domain = .mydomain.com I like to add that session is active on my main domain for www.mydomain.com only.. So how do I pass sessions for WWW to other subdomains ? Thanks n Advance I'm trying to get a simple table to display and am not sure how to make it happen. Table will have just 2 columns. A TIME and an EVENT. What I want to have happen is for the first row to show the TIME and next to that, the EVENT. If a TIME has more than one event going I want the next row to show an empty first cell and then the second event below the first. If I set up the loop the only way I know how (so far) it would also output the TIME value again and again as many times as there were events for that same time. For example what I don't want is: 9:15 Sunday School 9:15 Nursery Available What I do want is: 9:15 Sunday School Nursery Available Thanks. Can someone explain to me how this is done? I have some function or method. Is there a better design patter to implement this?
function myFunction($a=null,$b=null,$c=null,$d=null,$e=null,$f=null,$g=null,$h=null) { //Do a bunch of stuff }Maybe the second function? function myNewFunction($data=array()) { $data=array_merge(array('a'=>null,'b'=>null,'c'=>null,'d'=>null,'e'=>null,'f'=>null,'g'=>null,'h'=>null),$array); //Do a bunch of stuff }Please provide decision making factors why you would use one approach over the other. hello how do i pass a foreach loop through "Passing by Reference" say i have a foreach loop in a function like this: Code: [Select] function findText(&$output){ $word = Text::find_all(); foreach ($word as $words){ $output = $words->text; } } then one the page i put Code: [Select] findText($output); echo $output; that will just give me the last word in the database. so how do i get it to echo out the array on the page ? thanks I am calling CURL and trying to do a POST request with parameters: Code: [Select] $curl = curl_init(); curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Accept: application/json", "Content-Type: application/json")); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 15); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($curl, CURLOPT_USERAGENT, "curl 7.23.1 (x86_64-unknown-linux-gnu)"); curl_setopt($curl, CURLOPT_USERPWD, "username:password"); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_URL, "https://www.mydomain.com/route"); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, "key1=blah1&key2=blah2"); return curl_exec(curl); The problem, inside the request at http://www.mydomain.com/route I am not seeing any POST parameters passed. I.E. Code: [Select] print_r($_POST); Code: [Select] Array ( ) Should have key1=blah1 and key2=blah2. Any ideas? When i pass entire url in a get variable as http://www.example.com/index.php?url=http://www.yahoo.com/ I am getting the following error Forbidden You don't have permission to access /index.php on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. But there is no problem when i use the following http://www.example.com/index.php?url=www.yahoo.com/ http://www.example.com/index.php?url=yahoo.com/ http://www.example.com/index.php?url=yahoo&value=1 I am getting "Forbidden-error" only when i include "http://"(even if it is urlencoded) in get variables. Can any one help me... Code: [Select] $col = 0; if ($num_products_count > 0) { while (!$specials_index->EOF) { $list_box_contents[$row][$col] = array('params' =>'<a href="#">' .zen_image(..). '</a>'), $col ++; } } now i want to pass some values<img src="1.jpg">,<img src="1.jpg">,<img src="2.jpg">,<img src="3.jpg">,<img src="4.jpg">,<img src="5.jpg"> to zen_image(..). how should i do? namely, if there are five loops.i want to change zen_image(..) output result to (<img src="1.jpg">,<img src="1.jpg">,<img src="2.jpg">,<img src="3.jpg">,<img src="4.jpg">,<img src="5.jpg">) Hello again, Though this question is somewhat related to my prior note, I thought it was probably different enough to justify a separate topic... So, I have a link at site1.com/dir/admin/?param1=value¶m2=value, etc, which I want to redirect to site2.com with all the rest being the same. Thanks to your help, it's working with two specified parameters However, there is a relatively short list of potential parameters (maybe 5?) but not all of them are passed in all calls to the link. In other words, sometimes the link is called with param1 and param2, sometimes with 1 and 3, sometimes with 1,2, and 4, etc. What is the best way to do the redirect which would ensure that all the parameters which are sent to the initial link are then included in the redirect? Is there a way to search the incoming link and grab all parameters in it? Or do I put together the list of possible parameters and then grab the values with $_GET for all of them and then pass all of them even if the values are null (not sure whether the app will accept that or not...) I hope I'm stating the question clearly enough! Thanks in advance for your help! I just signed up for an affiliate network (commission junction) to help me drive traffic to my donation generation website for Charities. My Software uses CAKE PHP on an Apache Server with MySql. I had to put a pixel on the order confirmation page passing the Transaction ID and The Purchase Amount from the database. The Transactions are located in the transaction table but the code that was written is passing EVERY Transaction in the database every time something is purchased. The Columns for the transaction table are ID CREATED MODIFIED USERID CLASS TRANSACTION_TYPE_ID and AMOUNT I have 32 types of Transactions in my Transaction_Type Table, the only transaction_types that are sales generated by the affiliate network are transaction_type 2. The code I have now which isn't working correctly is. <?php // Make a MySQL Connection mysql_connect("localhost", "frankgth", "andrew2011!") or die(mysql_error()); mysql_select_db("frankgth_groupon5") or die(mysql_error()); // Get all the data from the "example" table $result = mysql_query("SELECT * FROM `transactions` ") or die(mysql_error()); // keeps getting the next row until there are no more to get - while($row = mysql_fetch_array( $result )) { ?> <img src="https://www.emjcd.com/u?CID=1521593&OID=<?php echo $row['id'];?>&TYPE=349217&ITEM1=PURCHASE&AMT1=<?php echo $row['amount'];?>&QTY1=1&CURRENCY=USD&METHOD=IMG" height="1" width="20"> <?php } ?> How Do I only show this pixel if the action is Transaction_Type_ID is equal to number 2,with the Amount and Transaction ID. Thanks in advaced. This site helps charity so i really want to get it working! I'd like to pass a value from a URL to an iframe. This would look as follows: URL: http://www.mysite.com?www.mysite2.com The value (www.mysite2.com) will be an actual web address which should then be inserted into an iframe on www.mysite.com via php. Since I know little to no php, I don't know how the URL has to be structured and what php coding I need to use to include the web address in the iframe. Any suggestions are highly appreciated hello how do i pass a value from 1 function to another i want to use this part Code: [Select] //send the list order to the navigation or list page $PHL = PhLists::by_listNo($PHGlno); foreach ($PHL as $PHLs){ $PHLlno = $PHLs->phList_No; $PHLorb = $PHLs->orderBy; $PHLord = $PHLs->orderDir; } from this very long function. Code: [Select] function include_placeHolders($position, $phNo, $pageID){ $langBS = basicSettings::find_by_id(1); $langID = $langBS->language_id; //FIND PLACEHOLDER BY ID $PH = Placeholders::find_by_pageID($pageID); foreach ($PH as $PHs){ $PHpos = $PHs->position; $PHnum = $PHs->phNumber; $PHgNo = $PHs->phGroup_No; $PHgFx = $PHs->PHGfx_id; $PHgThm = $PHs->PHGtheme_id; if($phNo == $PHnum && $position == $PHpos){ //FIND ELEMENT THEMES echo '<style type="text/css" media="screen">'; include(PAGES.DS."phGroups".DS."theme{$PHgThm}".DS."css".DS."styles.css"); echo '</style>'; echo'<div id="PHgroup">'; //FIND GROUPS BY phGroupNo $PHGodr = PhGroups::find_order($PHgNo); foreach ($PHGodr as $PHGodrs){ $PHGob = $PHGodrs->orderBy; $PHGod = $PHGodrs->orderDir; } //FIND PHGROUPS BY BY GROUPno & ORDER $PHG = PhGroups::find_by_order($PHgNo, $PHGob, $PHGod); foreach ($PHG as $PHGs){ $PHGid = $PHGs->id; $PHGname = $PHGs->name; $PHGgno = $PHGs->phGroup_No; $PHGlno = $PHGs->list_No; $PHGce = $PHGs->contElements_id; $PHGpl = $PHGs->pageLink_id; $extLink = $PHGs->extLink_id; $PHfx = $PHGs->PHfx_id; $PHthm = $PHGs->PHtheme_id; //send the list order to the navigation or list page $PHL = PhLists::by_listNo($PHGlno); foreach ($PHL as $PHLs){ $PHLlno = $PHLs->phList_No; $PHLorb = $PHLs->orderBy; $PHLord = $PHLs->orderDir; } //FIND EXTERNAL LINK $PHGeL = ExtLinks::find_link($extLink); foreach($PHGeL as $PHGeLs){ $PHGurl = $PHGeLs->url; $PHGtarget = $PHGeLs->target; } //FIND ELEMENTS $CE = ContElements::find_by_phg($PHGce); foreach ($CE as $CEs){ $CEeName = $CEs->name; $CEetype = $CEs->elementTypes_id; $CEefx = $CEs->elFx_id; $CEethm = $CEs->elTheme_id; //FIND ELEMENT TYPE NAME $CEt = ElementsTypes::find_Etype($CEetype); foreach ($CEt as $CEts){ $CEtName = $CEts->name; //FIND PLACEHOLDER THEMES echo '<style type="text/css" media="screen">'; include(PAGES.DS."placeHolders".DS."theme{$PHthm}".DS."css".DS."styles.css"); echo '</style>'; //FIND ELEMENT THEMES echo '<style type="text/css" media="screen">'; include(ELEMENTS.DS."{$CEtName}".DS."themes".DS."theme{$CEethm}".DS."css".DS."styles.css"); echo '</style>'; //FIND INTERNAL OR EXTERNAL LINK $pageLNK = (empty($PHGpl)) ? '' : '<a href="index.php?pageID='.$PHGpl.'">'; $extLNK = (empty($extLink)) ? '' : '<a href="'.$PHGurl.'" target="'.$PHGtarget.'">'; $ENDpageLNK = (empty($PHGpl)) ? '' : '</a>'; $ENDextLNK = (empty($extLink)) ? '' : '</a>'; echo $pageLNK; echo $extLNK; echo'<div id="ph"> <div id="PHelement">'; include(ELEMENTS.DS."{$CEtName}".DS."{$CEeName}.php"); echo'</div></div>'; echo $ENDpageLNK; echo $ENDextLNK; } } } echo'</div>'; } } } i want the next long function to use these values Quote $PHLlno = $PHLs->phList_No; $PHLorb = $PHLs->orderBy; $PHLord = $PHLs->orderDir; this is where i want to use them in the next function Quote $nH = PhLists::navOrder($PHLlno, $PHLorb, $PHLord); Code: [Select] function nav(&$navLink){ $nH = PhLists::navOrder($PHLlno, $PHLorb, $PHLord); foreach ($nH as $nHs){ $nav_typ = $nHs->type; $nav_lvl = $nHs->levels; $nav_pLnk = $nHs->pageLink_id; $nav_eLnk = $nHs->extLink_id; $nav_tType = $nHs->titleType; $nav_ctBid = $nHs->custTxtBridge_id; //INTERNAL LINKS if($nav_typ == "Internal"){ //USE SET TITLE if($nav_tType == "SetTitle"){ $navPage = Pages::findNav($nav_pLnk); foreach ($navPage as $navPages){ $NPbid = $navPages->PCbridge_id; } $STBhead = PCbridge::find_setTitle($NPbid, $langID); foreach ($STBhead as $STBheads){ $text = $STBheads->pageContent_id; } $navText = PageContent::find_text($text); foreach ($navText as $navTexts){ $navLink = $navTexts->title; } //USE CUSTOM TITLE }else if($nav_tType == "Custom"){ $CTBhead = PCbridge::find_customTitle($nav_ctBid, $langID); foreach ($CTBhead as $CTBheads){ $text = $CTBheads->pageContent_id; } $navText = PageContent::find_text($text); foreach ($navText as $navTexts){ $navLink = $navTexts->title; } } } //EXTERNAL LINKS if($nav_typ == "External"){ //USE SET TITLE if($nav_tType == "SetTitle"){ $navExt = ExtLinks::findNav($nav_eLnk); foreach ($navExt as $navExts){ $navLink = $navExts->name; $PHGurl = $navExts->url; $PHGtarget = $navExts->target; } //USE CUSTOM TITLE }else if($nav_tType == "Custom"){ $navExt = ExtLinks::findNav($nav_eLnk); foreach ($navExt as $navExts){ $PHGurl = $navExts->url; $PHGtarget = $navExts->target; } $CTBhead = PCbridge::find_customTitle($nav_ctBid, $langID); foreach ($CTBhead as $CTBheads){ $text = $CTBheads->pageContent_id; } $navText = PageContent::find_text($text); foreach ($navText as $navTexts){ $navLink = $navTexts->title; } } } //NO LINK if($nav_typ == "Non"){ //USE CUSTOM TITLE if($nav_tType == "Custom"){ $CTBhead = PCbridge::find_customTitle($nav_ctBid, $langID); foreach ($CTBhead as $CTBheads){ $text = $CTBheads->pageContent_id; } $navText = PageContent::find_text($text); foreach ($navText as $navTexts){ $navLink = $navTexts->title; } } } } } thanks so I need some help passing these variable from this page to final.php. how do I pass these arrays? I know if it were singled....not arrayed, I could use hidden fields in a form and echo them out....but these are multiples....not singled. The form way is prefered.....but it doesn't have to be. I just need these passed to the page where I am going to process them. I am not good at working with arrays. Thanks in advance Code: [Select] <?php include_once("connect.php"); session_start(); foreach($_POST["product"] AS $key => $val) { $product = $val; $month = $_POST['month'][$key]; $day = $_POST['day'][$key]; $year = $_POST['year'][$key]; $date = $_POST['date'][$key]; $price = $_POST['price'][$key]; $qty = $_POST['qty'][$key]; $id = $_POST['id'][$key]; $total = $_POST['total'][$key]; $academy = $_POST['academy'][$key]; $priceunit = $price * $qty; } ?> Hello there,
I have 2 questions:
1) Look he http://www.hellastra...ooking/taxi.php
I have a form and i want to pass values from URL to fields.
So, for example to pass a value to Prefered driver ID field you write this:
http://www.hellastra...?drivercode-123 and the value 123 is passed to that field. I did this with this code:
<?php $sDriverCode = $_REQUEST['drivercode']; ?> <input style="width:100px;" name="drivercode" id="drivercode" value="<?=$sDriverCode?>" data-mini="true" />At the same form i have some radio-type fields. Its the one you see at Type of Vehicle (Standard, Premium etc). When i try to do the same, i cannot get this to work. What i am trying to do is this: Hi guys, I need your help, I have got a problem with the if statement. When I don't insert the pass function in the url like this: Code: [Select] http://www.mysite.com/myscript.php?image=myimagelocation&strings=mystrings&user=test I will get this on my php page: Code: [Select] PASSWORD are missing Code: [Select] <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mydbusername'); define('DB_PASSWORD', 'mydbpass'); define('DB_DATABASE', 'mydbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $image = clean($_GET['image']); $strings = clean($_GET['strings']); $username = clean($_GET['user']); $pass = clean($_GET['pass']); if($username == '' && $pass) { $errmsg_arr[] = 'username are missing'; $errflag = true; }elseif($username && $pass =='') { $errmsg_arr[] = 'PASSWORD are missing'; $errflag = true; } if($username == '' && $pass == '') { $errmsg_arr[] = 'username or password missing'; $errflag = true; } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $insert = array(); if(isset($_GET['image'])) { $insert[] = 'image = \'' . clean($_GET['image']) . '\''; } if(isset($_GET['strings'])) { $insert[] = 'strings = \'' . clean($_GET['strings']) . '\''; } if(isset($_GET['user'])) { $insert[] = 'user = \'' . clean($_GET['user']) .'\''; } if(isset($_GET['pass'])) { $insert[] = 'pass = \'' . clean($_GET['pass']) . '\''; } if (count($insert)>0) { $names = implode(',',$insert); $required_fields = array('image', 'strings', 'user'); if($image && $strings && $username) { echo "working 1"; } elseif($username && $pass) { echo "working 2"; } } } ?> Do anyone know how to fix this? how can i do this ? i have a number of links , when i click one a pop up is displayed, i need to run a php script depending upon which link is clicked.
Or can i run a javascript function once a query string is passed in the url
Please advice.
Edited by chauhanRohit, 12 July 2014 - 04:10 AM. Hello
I'm using a form to collect user data (is a Cognito form) it is displayed on vacation rental properties pages, so it collects user´s email, in date, out date, etc. in the cognito form I have one text hidden field called REF, I wonder how to do in order to use the same form and be able to identify where the info comes from. I thougt that if I could put the page url into the REF variable, the problem would be solved, but I'm not sure if that is possible and how to do it. It is? If not: is it possible to handle that ? i hav this url n whenever i click on this url i get som mssg send on my mobile i hav a form whose code is Code: [Select] <form action="formprocess.php method="post"> telephone<input type="text name="telephone> <input type="submit" name="submit"> </form> i want to send telephone as parameter to this url so whenever user enters his telephone he gets d mssg after clicking on submit.....but i hav no idea how to do tht...help me somone plssssssss http://www.abc.in/smsapi/sendsms.php?sendto=9987234123&msg=your Trying to pass VARS $URL1 and $type to header location.Heres the code. Code: [Select] $URL1 = $_POST['url']; $type = $_POST['type_request']; session_start(); $string = strtoupper($_SESSION['string']); $userstring = strtoupper($_POST['userstring']); session_destroy(); if (($string == $userstring) && (strlen($string) > 4)) { header("Location: $success"); exit(); The VARS come from a FORM and then go to this script for checkind data. I need to have them continue to the next page. Hi guys, I have got a problem with the if variable statements. When I insert the text of the image location, the name of the strings and when I did not insert the username, all I get this: Code: [Select] image, strings or username are missing Username or password are missing. It should not display with the "Username or password are missing.", only the "image, strings or username are missing". The area of the code i am working on: if($image == '' && $strings == '' && $username == '') { $errmsg_arr[] = 'image, strings or username are missing'; $errflag = true; } elseif($username == '' && $password == ''){ $errmsg_arr[] = 'Username or password are missing.'; $errflag = true; } <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'myusername'); define('DB_PASSWORD', 'mypassword'); define('DB_DATABASE', 'mydbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $image = clean($_GET['image']); $strings = clean($_GET['strings']); $username = clean($_GET['user']); $pass = clean($_GET['pass']); $delete = clean($_GET['delete']); if($image == '' && $strings == '' && $username == '') { $errmsg_arr[] = 'image, strings or username are missing'; $errflag = true; } elseif($username == '' && $password == ''){ $errmsg_arr[] = 'Username or password are missing.'; $errflag = true; } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $insert = array(); if(isset($_GET['image'])) { $insert[] = 'image = \'' . clean($_GET['image']) . '\''; } if(isset($_GET['strings'])) { $insert[] = 'strings = \'' . clean($_GET['strings']) . '\''; } if(isset($_GET['user'])) { $insert[] = 'username = \'' . clean($_GET['user']) .'\''; } if(isset($_GET['pass'])) { $insert[] = 'pass = \'' . clean($_GET['pass']) . '\''; } if(isset($_GET['delete'])) { $insert[] = 'delete = \'' . clean($_GET['delete']) . '\''; } if (count($insert)>0) { $names = implode(',',$insert); if(isset($image) && ($strings) && ($username)) { echo "test"; } elseif($username && $delete == 'all') { if ($delete != NULL) { mysql_query("DELETE FROM user_list WHERE username='$username'"); $deleted = mysql_affected_rows(); if($deleted > 0) { echo("The data are now deleted"); } else { echo("The user's data is empty"); } }else{ echo("failed"); } mysql_close($link); } } } ?> Do anyone know how i can get pass on those methods if I enter the images, the name of the strings and the username or the or the username with the password? |