PHP - How To Pass A Variable Into An Object?
Can someone explain to me how this is done?
Similar TutorialsI 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? 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; } ?> 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? I have researched all over forums from past day. Not getting correct solution. I have 2 textboxes and a button. First box is to enter value and i will click button, i need to get the value.
Here is the code, that works without input box 1
In this code i want to modify my web address, at the end after ky= i want add my first textbox value, then click event and output in second textbox, let me know where i messed.
<script type="text/javascript"> function Assign() { <?php $html = file_get_contents("http://geoportaal.maaamet.ee/url/xgis-ky.php?ky=79401:006:0812" ); preg_match_all('(<li.*?>.*?</li>)', $html, $matches); $one=$matches[0][0]; ?> document.getElementById("OutputField").value = "<?=$one?>"; } </script> <input id="InputField" type="text" style="width:200px"/> <input type="submit" value="Assign Value" onclick="Assign()"/> <input id="OutputField" type="text" style="width:200px"/> 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 ? Hi, Im trying to pass a variable ($in_this_instance) into, and then back out of a function. The variable goes into the function no problem, and is echo'ed out fine. However the echo after the close of the function, does not give anything out. Code: [Select] $in_this_instance = 'boo'; function in_this_instance($data) { global $in_this_instance; echo $in_this_instance; $in_this_instance = 'hoo'; $rep_val = '[front banner]'; $test = strpos($data, $rep_val); if ($test === false) { return $data; } else { return $data;; } } add_filter('the_content', 'in_this_instance'); echo $in_this_instance; Could any one give me a pointer please? Many Thanks! i knw dis mst a simple question but how to pass variable to other page for eg i hav created a page where user submits username n telephone then via sms some random number goes to user mobile ...i want this random number variable in other page dis is my code for startreg.php <form action="startregprocess.php" method="post"> username:<input type="text" name="username"> telephone<input type="text" name="telephone" <input type="submit" name="submit"> </form> <?php srand ((double) microtime( )*1000000); $random_number = rand( ); echo $random_number; ?> i want tht $random_number in other page called startregprocess.php Hello! I've got an issue! I'm trying to pass a very very long variable from JS to PHP through $_GET, but it seems too long for get. Is there any encryption function that works on both JS and PHP, so I can encrypt it in JS, send it though GET and decrypt it in PHP? Thanks ! Hi guys, I have a trouble with my php snippet, when I insert the var function in the url bar something is like: http://www.mysite.com/delete.php?favorites&id=0 or http://www.mysite.com/delete.php?whateveritis&id=0 It doesn't get pass the favorites function to delete the id. It is the same things that it goes for each different function. Here's the current code: <?php Code: [Select] session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mydbuser'); define('DB_PASSWORD', 'mydbpass'); define('DB_DATABASE', 'mydbtablename'); $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)); } $favorites = clean($_GET['favorites']); $id = clean($_GET['id']); if($favorites && $id == ''){ // both are empty $errmsg_arr[] = 'favorites id are missing.'; $errflag = true; } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $insert = array(); if(isset($_GET['id'])) { $insert[] = 'id = \'' . clean($_GET['id']) .'\''; } if(isset($_GET['favorites'])) { $insert[] = 'favorites = \'' . clean($_GET['favorites']) . '\''; } if($favorites && $id) { mysql_query("DELETE FROM favorites WHERE id='$id'"); $deleted = mysql_affected_rows(); if($deleted > 0) { echo "favorites channels is deleted"; } else { echo("favorites is already deleted"); } } } ?> If you do know how to get pass the favorites function, then please say so as i need your help. Any advice would be much appreicated. Here's my search form: <form action="search" method="get" enctype="multipart/form-data"> <input type="text" name="string" maxlength="100" /><br /><br /> <input type="submit" value="Search" /> </form> When I submit it, the URL is search.php?string=Bleh What I would like is for the URL to be search/string/Bleh I'm already doing this with some other variables, like search/tag/Bleh and search/author/Bleh, instead of search.php?tag=Bleh and search.php?author=Bleh. However, those ones are passed via a link, not a form. Any ideas? You guys are great, thanks again for the help last week. Now I almost got this working but a small hiccup. here is my code: Code: [Select] <?php include("config.php"); $my_t=getdate(date("U")); $my_t1=$my_t[weekday]; $result = mysql_query("SELECT * FROM tourney where day_of_week = '$my_t1'") or die(mysql_error()); if ($result == '') echo "<br>Empty Set\n"; print_r ($my_t1); This prints correctly Code: [Select] while ($result = mysql_fetch_array($result,MYSQL_ASSOC)) { This is my error. "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /hermes/bosweb/web017/b172/ipg.dswdesignsnet/pp4c/charity1.php on line 8" Code: [Select] print "<b>Starting Time: <br></b>".$row{'start_time'}."<br><b>Tournament: </b><br>".$row{'tourney'}."<br><b>Buy-in: <br>".$row{'buy_in'}."<br><b>Starting Chips: <br>".$row{'start_chips'}."</font><p>"; This prints headers correctly, but no variables. Code: [Select] } mysql_close($dbh); ?> What did I forget to do or what did I do wrong. I'm still learning mysql and php. i have to pass a variable to so many pages i mean variable post from index.html to page1.php and it has to pass to other pages like page2.php, page3.php and so on, i tried session_start() but i am getting the below error so please tell me how it is possible to Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by and Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent I have a table that has 5 columns Quote player_id fname lname team I'm trying to get all the values from that table using this sql command Code: [Select] include_once('../database_connection.php'); $player_id = addslashes($_REQUEST['edit_player']); $sql_query = "Select * from nba_info where player_id = '$player_id'"; $result = MYSQL_QUERY($sql_query); "edit_player" above is came from a different page. They I'm fetching the data using while Code: [Select] while($row = mysql_fetch_array($result)) { $row['player_id']; $row['fname']; $row['lname']; $row['team']; $row['email']; } Then I'm trying to pass that values to a variable here Code: [Select] $id = $row['player_id']; $fname = $row['fname']; $fname = $row['lname']; $lname = $row['team']; $email = $row['email']; When I'm trying to put that variables in a textbox value, they are not showing up Here Code: [Select] <table> <form name="edit_player_form" method="post" action=""> <tr> <td>Players ID</td> <td><input type="text" name="playerid" size="20" value="<?php echo $id; ?>" /></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="fname" size="20" value="<?php echo $fname; ?>" /></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lname" size="20" value="<?php echo $lname; ?>" /></td> </tr> <tr> <td>Team</td> <td><input type="text" name="team" size="20" value="<?php echo $team; ?>" /></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" size="20" value="<?php echo $email; ?>" /></td> </tr> <tr> <td><div align="center"> <input type="submit" name="Submit" value="Edit This Player"> </div></td> </tr> </form> </table> I wonder why it ain't showing up on the textbox value? tried almost everything... Anyone? When i execute the following code, it works and retrieves the record info i am looking for for the appropriate record, 2155015898: $adgrouppass = 2155015898; $selector->adGroupIds = array($adgrouppass); However, when i try to get the adgroup variable from my url, which is where it is located, it doesn't work: $adgroup = trim($_GET['adgroup']); $adgrouppass = $adgroup; $selector->adGroupIds = array($adgrouppass); yet, in trying to fix it, if i echo the above variable, it says it is correct, which is 2155015898. Not sure if the $adgrouppass needs to be a string or variable to get it to pass it correctly. Can anyone help? Hi, I have this script, it all works fine apart from when I pass the variable on, it only passes the first word and not the whole variable The form that actions to the samples page passes the $siteName as My Test Site, if I type echo $siteName in the sameples page, it will print My Test Site, however when I use this Code: [Select] createSample&siteName=$siteName to pass the variable to the next page, it just echos 'My' instead of My Test Site. Even when I scroll over the link above it just shows My in the url. Code: [Select] case "samples": // Get submitted data and assign to variables $siteName = $_POST['siteName']; $adminEmail = $_POST['adminEmail']; $sendmailLoc = $_POST['sendmailLoc']; $imgdir = $_POST['imgdir']; $imgdirbase = $_POST['imgdirbase']; // Write variable data to text file $FileName = "data/server.txt"; $FileHandle = fopen($FileName, 'w') or die("can't open file"); $data = "<?php\n\$siteName = '$siteName';\n?>"; fwrite($FileHandle, $data); $data = "<?php\n\$adminEmail = '$adminEmail';\n?>"; fwrite($FileHandle, $data); $data = "<?php\n\$sendmailLoc = '$sendmailLoc';\n?>"; fwrite($FileHandle, $data); $data = "<?php\n\$imgdir = '$imgdir';\n?>"; fwrite($FileHandle, $data); $data = "<?php\n\$imgdirbase = '$imgdirbase';\n?>"; fwrite($FileHandle, $data); fclose($FileHandle); // Displays the create sample pages page ECHO <<<SAMPLES <table border=0 align=center bgcolor=#00CCFF> <tr> <td><span class=style1><b><center>Create Sample Pages</center></b></span></td> </tr> <tr> <td>Would you like Member Site Maker to create sample pages for: <ul> <li>index.html</li> <li>login.html</li> <li>search.html</li> <li>register.html</li> </ul> </td> </tr> <tr> <td><a href=installation.php?cmd=createSample&siteName=$siteName>Yes</a> </td> <td><a href=installation.php?cmd=mysql>No</a> </td> </tr> </table> SAMPLES; break; Can anybody tell me where I am going wrong please? Thanks I am passing the variable $searchno from form1.html using <form action="display1.php" method="post" Display1.php reads my database and displays the data. Now I want to link from the displayed page to display2.php and pass and capture the same variable $searchno. How do I do that? Hi All, I have a function that i want to pass a variable into so that i can do some SQL on it. The information that i want to pass into it is a data-id on a button that is used to trigger the function. The button that will be clicked is this <div class='modaltrigger btn btn-primary' data-id='$itemId' data-toggle='modal'>Manage</div> This button is being created by another function. I would like to know either how i pass the variable from one function to another or how i pass it from the data-id to the function in php. Thanks in advance. Hi, this works: if(file_exists("myFile.txt")){etc.} this doesn't: $myFile = "myFile.txt"; if(file_exists($myFile)){etc.} Can anybody explain this to me? I'd really like to pass the filename as a variable... Kind regards, R. my value, selectname.push(element.name); selectqty.push(element.qty); How to pass Google charts function,
$.each(data, function(index, element) { selectname.push(element.name); selectqty.push(element.qty); google.charts.load('current', { 'packages': ['corechart'] }); google.charts.setOnLoadCallback(drawChart); function drawChart() { var data1 = google.visualization.arrayToDataTable([ ['Task', 'Hours per Day'], ['Work', 11], ['Eat', 2], ['Commute', 2], ['Watch TV', 2], ['Sleep', 7] ]); // var data1 = google.visualization.arrayToDataTable([ // [element.name, element.qty], // // // ]); var options = { title: 'Top 5 Best Selling Products' }; var chart = new google.visualization.PieChart(document.getElementById('piechart')); chart.draw(data1, options); }
My website is multi-language and using mod_rewrite for clean urls. I have a $lang variable that stores the language code. For example: website.com/index.php?lang=en, which turns to website.com/en I use a $_GET["lang"] to get the language and prepare the links to the other pages, for example a link to page1.php would be page1.php?lang=$_GET["lang"]. What I want to know is if this is the right way to do it or there is a better way. |