PHP - Confusion Over An Array Within A Function Parameter
I'm fairly new to this and I have looked around but I can't find much information on it. Basically I know what an array and a function is but I'm currently working through an oop login and register sytem and I am seeing this quite a lot:
function myFunction($myVariable = array()){
$Firstvariable = $myVariable[0];
$Secondvariable = $myVariable[1];
//etc
}
This is really confusing me and I can't see any examples in any tutorial sites or books of an array and a function being used like this. Can someone just explain how something like this would work in simple terms? I have also checked the manual but I can't see an example an array being used like this. Any help would be much appreciated. Thanks.
Similar TutorialsI'm sure this is something simple that I'm just overlooking right now, but it's got me very frustrated and I just want a quick fix... I have 2 arrays, $data_old and $totals_old Let's say each looks like this (with the 123456 being different for both): Array ( [2014] => Array ( [123456] => Array ( [EMPS] => 114350 ) ) )When I merge the two, I get something like this: Array ( [2014] => Array ( [0] => Array ( [123456] => Array ( [EMPS] => 114350 ) ) [1] => Array ( [60] => Array ( [EMPS] => 1470 ) ) ) )How can I avoid the [0] and [1] and just have those be [123456] and [60], without referencing the 123456 and 60? I think i have muddled myself up here somewhere along the line I am trying to check to see if various things already exists in a database. So for example i have a collumn with names, second collumn is email address, third collumn phone number etc. So i make the usual connection to the database: $sql=("SELECT * FROM table") $result = mysql_query($sql); $row = mysql_fetch_array($result); Now to assign the various collumns is where i have come unstuck. if (in_array("?", $row)) {do something} or is it if (in_array("?", $row['name'])) {do something} ? I have it working by hard coding to a seperate php file, but its bulky and would prefer to use the database. Alright, hopefully my last question for this current project. I've build an array containing other arrays which each hold 5 values. I'm trying to loop through all the arrays in the main array but only pull out one of the values. What I have right now is: echo "<ol>"; for ($rw = 0; $rw < sizeof($serenwilde); $rw++) { echo "<li><b>The row number $rw</b>"; echo "<ul>"; for ($col = 0; $col < 5; $col++) { if (isset($serenwilde[$rw][$col])) { echo "<li>".$serenwilde[$rw][$col]."</li>"; } } echo "</ul>"; echo "</li>"; } echo "</ol>"; Which produces: Quote The row number 0 value0 value1 value2 value3 value4 How can I modify my code so I still loop through all the arrays, but only pull out say value0 from each of them? Thanks again. Code: [Select] function fetch_feedback_best() { $sql = "SELECT `like` FROM `feedback` GROUP BY `page` asc LIMIT 10"; while($row = mysql_fetch_assoc($sql)) { $results[] = $row; } return $results; } This doesn't work. My error is "Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in /blah.inc.php on line 105 ". Code: [Select] <?php $results = fetch_feedback_best(); foreach ($results as $result) { ?> echo $result['page']; } ?> Hi all
Is it possible to have a function with two parameters but and then check if one is passed before using it.
In this code here I want to remove spaces and change to lowercase.
If the $extra parameter is set I want to add that to the end of the string.
function className($nametoUse, $extra){ Hello I am experiencing some issues with a function and I cannot identify the problem. The function is not receiving any parameter. The URL values are passed correctly. I used echo to display the parameters outside the function and that worked. I also used echo to display the parameters inside the function and no parameter were displayed. So the function is not receiving parameters. Code: [Select] <?php $find = $_REQUEST['u_find']; $field = $_REQUEST['u_field']; $searching = $_REQUEST['u_search']; echo"(1a)$find, (1b)$field, (1c)$searching";//outside function function test_display($searching, $field, $find) { echo"(2a)$find, (2b)$field, (2c)$searching";//inside function } ?> Hi Chaps, I have a while loop which produces a couple of variables: do { $L_NAME.$i = 'Item 1'; $L_NUMBER.$i = '0001'; $L_QTY.$i = '1'; $L_AMT.$i = 10.50; $NVP.$i = "L_NAME.$i.= .$L_NAME.$i.&L_NUMBER.$i. = .$L_NUMBER.$i.&"; } while($i == $items); Basically what I need to do is contruct a parameter made up of all the $NVP variables. I've a feeling that I would need to add each variable to an array and then use a foreach statement, but I'm not 100% sure. I'd be interested in the correct way to go about this, any comments welcome! If I have a parameter that has a default value, how do I pass nothing such that it uses the default value? Passing NULL does not work. Code: [Select] function test($to = "test@test.com", $from) { echo $to . "<BR>" . $from; } //This will NOT show "test@test.com" but instead an empty line test( NULL, "fake@fake.com" ); HI, i'm using a class to create a database connection etc..
So I have something like this in a common.inc.php file which is include in each page
$db_numbers_args = array(); $db_numbers_args['host'] = "localhost"; $db_numbers_args['user'] = "puser"; $db_numbers_args['pass'] = "nuser"; $db_numbers_args['db'] = "nu_name"; $db_numbers = new db($db_numbers_args,TRUE); From there I can end up making query's like $res = $db_numbers->query($uqs); So that said i'm including another file that contains all functions, when the functions need to do database transactions I always need to put $db_numbers as a paramter to the function and each function I make.. How should I make this better ? how do i enable exec() in php.ini? where is php.ini? cant find it in the directories in my site. I'm just wondering if there is a function like array_key_exists that could take an array as the search parameter and check that all keys exist. It would be easy enough to code if I need to but I was hoping it would already exist. All wanted to use it for is a really quick form check to make sure that all the expected values were POSTed before I actually start filtering and validating individual fields. Thanks in advance for any recommendations I'm a newbie . I've been stuck on this error message for 2 days: I get this error message: extract() expects parameter 1 to be array, boolean given in C:\x\xampp\htdocs\user_personal.php on line 30 Code: [Select] <?php $query = 'SELECT about_me, job, hobbies, contact FROM site_user u JOIN site_user_profile p ON u.user_id = p.user_id WHERE username = "' . mysql_real_escape_string($_SESSION['username'], $db) . '"'; $result = mysql_query($query, $db) or die(mysql_error($db)); $row = mysql_fetch_array($result); extract($row); mysql_free_result($result); mysql_close($db); ?> I think joining table is hard, I just want to create a profile that would be linked to a user name. Please be my friend and help me so I can have PHP phun time. One more question. So I have a function with two parameters, $x and $y. Here it is. Code: [Select] function dydx($x,$y){ $equation = 2 * $x; return $equation; } Now here's the problem. I want $equation to be user defined. Easy enough, I use a post command and I get $equation to equal whatever the user inputs. The problem is that if I get input from the user, I'm not sure how to take that input and then have the function parameters work on it. Like if the user inputs "3*$x + 2*$y", i want to be able to let the parameters for the function dydx act on it. How could I go about doing this? Hi, Am hoping someone can please help. I've the following code: $chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); $chosen_method = explode(':', reset($chosen_methods) );
The first line returns the warning: I've tried the following: $chosen_method = ''; $chosen_method = NULL; $chosen_methods = array(); Sadly, none have worked. What am I missing? Thanks PHP Warning: file_exists() expects parameter 1 to be string, array given in /home/mysite/public_html/display.mysite.com/wp-content/themes/mytheme/form.php on line 9 PHP Warning: file_exists() expects parameter 1 to be string, array given in /home/mysite/public_html/display.mysite.com/wp-content/themes/mytheme/form.php on line 13I have a form that is having some issues and in the error log I see a bunch of these warnings. What is wrong with this?? I numbered the offending lines below (lines 9 & 13) Thanks for any help on this. function CheckExistance($VUrl) { /*9*/ if ( file_exists($VUrl) ) echo $VUrl; /*13*/ elseif ( file_exists(str_replace('www.display.mysite.com','www.display.com', $VUrl)) ) echo str_replace('www.display.mysite.com','www.mysite.com', $VUrl); else echo str_replace('www.mysite.com','www.display.mysite.com', $VUrl); } Edited by damion, 02 July 2014 - 06:01 PM. Problem: "Warning: extract() expects parameter 1 to be array, null given in C:\x\xampp\htdocs\pages\user_personal.php on line 32" - error also found on lines 33 and 38. and none of the information is displayed. Goal: I'm trying to retrieve profile information and multiple user inserted pictures and display the pictures through a while loop from one query. <?php $query = 'SELECT u.name_id, i.bio, i.exhib, p.product_code FROM user u LEFT JOIN profile i ON u.name_id = i.name_id LEFT JOIN ecomm_products p ON u.name_id = p.product_code WHERE u.name = "' . mysql_real_escape_string($_SESSION['name'], $db) . '" and p.name = "' . mysql_real_escape_string($_SESSION['name'], $db) . '"'; $result = mysql_query($query, $db) or die(mysql_error()); extract($bio); //line 32 extract($exhib); //line 33 $odd = true; while ($row = mysql_fetch_array($result)) { echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">'; $odd = !$odd; extract($rows); line 38 echo '<td><a href="' . $dir . '/' . $row['product_code'] . '.jpg">'; echo '<img src="' . $thumbdir . '/' . $row['product_code'] . '.jpg">'; echo '</a></td>'; echo '</tr>'; } echo "</table>"; echo "<p>"; ?> <ul> <li>Biography: <?php echo $bio; ?></li> <li>Exhibitions: <?php echo $exhib; ?></li> </ul> if($pid != "") { $bname = $_REQUEST['bname']; $btitle = $_REQUEST['btitle']; $btags = $_REQUEST['btags']; $bdesc = $_REQUEST['bdesc']; $btext = $_REQUEST['btext']; $bimg = $_REQUEST['bimg']; $bimgalt = $_REQUEST['bimgalt']; $data = ""; if($bname!="") { $data = $data." 'bname' => ".$bname.", "; } if($btitle!="") { $data = $data."'btitle' => ".$btitle.", "; } if($btags!="") { $data = $data."'btags' => ".$btags.", "; } if($bdesc!="") { $data = $data."'bdesc' => ".$bdesc.", "; } if($btext!="") { $data = $data."'btext' => ".$btext.", "; } if($bimg!="") { $data = $data."'bimg' => ".$bimg.", "; } if($bimgalt!="") { $data = $data."'bimgalt' => ".$bimgalt.", "; } $data = $data."'pid ' =>". $pid.", " ; $data = "[".$data."]"; $build = ""; if($bname!="") { $build = $build." page_name = :bname,"; } if($btitle!="") { $build = $build." page_title = :btitle,"; } if($btags!="") { $build = $build." page_tags = :btags,"; } if($bdesc!="") { $build = $build." page_desc = :bdesc,"; } if($btext!="") { $build = $build." page_text = :btext,"; } if($bimg!="") { $build = $build." page_img = :bimg,"; } if($bimgalt!="") { $build = $build." page_imgalt = :bimgalt,"; } $build = $build." page_id = :pid"; $sql = "UPDATE pages SET ".$build." WHERE page_id=:pid"; echo $sql."<br /><br />"; echo $data."<br /><br />"; $stmt= $pdo->prepare($sql); $stmt->execute($data); Result of echo SQL and Data
UPDATE pages SET page_name = :bname, page_title = :btitle, page_tags = :btags, page_desc = :bdesc, page_text = :btext, page_img = :bimg, page_imgalt = :bimgalt, page_id = :pid WHERE page_id=:pid Market Net is the place for you to find those goodies you saw on the markets, buy them online and support small local businesses. Watch this space for more information. Test #D hvac , 'bimg' => supportlocal.jpg, 'bimgalt' => Support Local Small Businesses, 'pid ' =>106, ] Yet I am getting said error message. What am I doing wrong? I have the following code just to insert a username and hashed password into the database but somehow I am getting this error and I couldn't find out where I am doing it wrong...can someone please give me a hand?
I tried it in two ways and both errors...
the first few lines are just connecting database which worked fine and a password.php so I can use password_hash() with my php version
$hash = password_hash('xx', PASSWORD_BCRYPT, array('cost' => 10)); $username = 'xx'; $insertQuery = $db->prepare(" INSERT INTO et_todo (username, password) VALUES (:username, :hash) "); $insertQuery->execute(array( 'username' => $username, 'password' => $hash ));also tried $hash = password_hash('xx', PASSWORD_BCRYPT, array('cost' => 10)); $insertQuery = $db->prepare(" INSERT INTO et_todo (username, password) VALUES ('xx', :hash) "); $insertQuery->execute(array( 'username' => 'xx', 'password' => $hash )); Hello,
I recently posted here about an issue I was having with my database orientated products page.
I have now run into another problem where say if, /db.php was typed or /db.php?p=IDoNotExist was typed, it returns blank.
I have in my code the desired content to be displayed, but it just doesn't seem to want to make a show.
I was also wondering if it is possible to show different content for whatever the URL is, so for no parameter, the content about the products, and a non existent one, maybe "Product not found"?
Here is my code:
<?php $db=mysql_connect ("localhost", "webwibco_charlie", "Hello123") or die ('I cannot connect to the database because: ' . mysql_error()); $mydb=mysql_select_db("webwibco_products"); include("header.php"); $status = htmlspecialchars( @$_GET ['p'] ); if ($status == "floorpuzzles") { echo "<h1>Our Floor Puzzles</h1>"; $sql="SELECT ID, Name, Tags, Description, Category FROM products WHERE Category LIKE '%" . FloorPuzzles . "%'"; $result=mysql_query($sql); while($row=mysql_fetch_array($result)){ $Name =$row['Name']; $ID =$row['ID']; $Description =$row['Description']; echo "<div class=\"box\">"; echo "<h1>$Name</h1>"; echo "<div class=\"floorbox\"><a href=\"?p=$ID\"><img src=\"images/products/catalogue/big/floorpuzzles/$ID.jpg\" class=\"small\"></a></div>"; echo "<h2>$Description</h2>"; echo "</div>"; } ?> <? }else{ if ($status == $_GET["p"]) { $sql="SELECT ID, Name, Tags, Description, Pieces, Size, Barcode, Category FROM products WHERE ID = '" . $_GET['p'] . "'"; $result=mysql_query($sql); while($row=mysql_fetch_array($result)){ $Name =$row['Name']; $ID =$row['ID']; $Description =$row['Description']; $Pieces =$row['Pieces']; $Size =$row['Size']; $Barcode =$row['Barcode']; echo "<div class=\"1\">"; echo "<h1>$Name</h1>"; echo "<div class=\"bigbox\">"; echo "<div class=\"floorbox\"><img src=\"images/products/catalogue/big/floorpuzzles/$ID.jpg\" class=\"big\"></div>"; echo "</div>"; echo "</div>"; echo "<div class=\"2\">"; echo "<p>Puzzle Pieces: $Pieces</p> <p>Puzzle Size: $Size</p> <p>Barcode: $Barcode</p>"; echo "</div>"; } }else{ ?> <? echo"<h1>Our Products</h1> <p>Our jigsaw puzzles are hand cut by skilled craftsmen and therefore each one is unique with self-correcting pieces. There is a strict quality control process at all stages by our highly experienced staff. The puzzles are durable and provide fun and excitement, enhancing learning and a child’s development.<p> <p>All of our jigsaws are made using materials from sustainable resources grown in managed forests. Where possible we support companies in the UK and source our components locally, most of our suppliers are in the East Midlands, many in Derbyshire and Nottinghamshire. We keep packaging to a minimum and take our environmental and ethical responsibilities very seriously.</p> <p>Reducing waste and recycling was a way of life for us before it became fashionable. We are constantly searching for new ideas and consult teachers when developing our jigsaws, which are often used within the national curriculum.</p> <p>As well as making our own range, we manufacture for leading suppliers to the education market. Check for \"Made in Britain\" and it is probably made by us.</p> <p>We have a wide variety of products available for viewing, from classic floor puzzles to innovative inset trays. You can take a look at all our products on this page, simply use the navigation buttons to your left.</p>"; }} include("footer.php"); ?>The final echo is what I wish to be displayed on the URL without or with an invalid parameter. Here is my site URL: http://www.webwib.co...saws/search.php (note that only the "Floor Puzzles" category has content within it). Thank you in advance for assistance. |