PHP - Functions And Variables
I have a database that holds a list of companies. the database includes and ID, and name of each company. Now when an articles gets submitted to my site it can then be related to company. We a relate a company to an article its related through its ID. What I want to do is create a function that would let me easly convert my company ID into company name within having to write out the queries over and over again in my code. Only problem is I can't get it to work. I have two files, index.php and functions.php.
functions.php Code: [Select] function cname($pubid) { global $db; $company= $db->query("SELECT * FROM my_database WHERE id='$pubid'") or die(mysql_error()); while($company= $db->fetch_array($company)) { $cname = $company['name']; } return $cname; } and within index.php I put Code: [Select] echo cname($companyID); Now, when I first looked at index.php I was getting an error. I know it had something to do with me using my variables within the function and after some searching I ended up getting rid of the error by adding "global $db;" to my code. Please note that I include functions.php at the top of index.php. What am I doing wrong and why wont it work? HELP PLEASE! also note that my i've rechecked and all the database info is correct and names spelt right. Just dont know what else to do. Similar TutorialsStill working on my project, and i have been learning a lot here! Thank you so much. But as you may have guessed, i still have problems. User registration is working, and new users are put in the database with a rank of 0. This means they can't do anything. (can there be trouble with this. I mean, a rank of "0"?) An admin needs to give access to these accounts, but that is where it becomes difficult. The following code is showing the new accounts to the admin. Code: [Select] <?php include("navbar.php"); if ($admin<2) //normal guy or not { die ("Du har ikke rettigheder til at se denne side!"); } else if(isset($_POST['submit'])) { //code to make the user able to use stuff } { $connect = mysql_connect("localhost","root",""); mysql_select_db("eksamen - phoenix"); $query = mysql_query("SELECT * FROM users WHERE rank='0'"); ?> <form action='admin.php' method='POST'> <table> <?php while($row = mysql_fetch_assoc($query)) { echo " <tr> <td> ".$row['username']." </td> <td> ".$row['email']." </td> <td> ".$row['real_name']." </td> <td> <input type=\"checkbox\" name=".$row['username']." value=\"Godkend\"> </td> </tr> ";} ?> <tr> <td> <input type='submit' name='submit' value='Register'> </td> </tr> </table> </form> <?php } ?> As you may see, there is a lot of turning php on and off. I made it work this way, but i guess there is nothing wrong with it. The problem is that the username is not stored, so i can connect to the database and change the "rank" value. Changing that value should be easy, but storing the username is as easy as i thought. Any ideas? I have this function: Code: [Select] function echo_exist($pre1,$ee,$post1) { if ($ee!='') { return $pre1; return $ee; return $post1; } else { //do nothing } } But when I try to echo it Code: [Select] echo_exist("(","$row['payment_type']",")"); I get syntax error, unexpected T_ENCAPSED_AND_WHITESPACE If $row['payment_type'] exists, I want it to be displayed in parentheses like: (AUTO) I suspect it has to do with my ' characters for the associative array, but I am new with functions so perhaps it is the multiple arguments that are wrong? I have a little problem. I have made a script(function) to connect to the database. this is the script: connect(php) Code: [Select] <?php $server = "localhost"; $username = "username"; $password = "password"; function connectdatabase($type,$database) { if($type == "mysql") { $mysql = mysql_connect($server, $username, $password); mysql_select_db($database, $mysql); } else if($type == "mysqli") { $mysqli = new mysqli($server, $username, $password, $database); } else if($type == "mssql") { $mssql = mssql_connect($server, $username, $password); mssql_select_db($database, $mssql); } function query($query) { if($type == "mysql") { mysql_query($query); } else if($type == "mysqli") { $mysqli->query($query); } else if($type == "mssql") { mssql_query($query); } } } ?> and aanmelden(php) Code: [Select] <?php include("config.php"); connectdatabase("mysql", "[ledensysteem]"); //example if(!empty($_POST)) { if(!preg_match('/^[A-Za-z1-9-]{'.$Minimale_Gebruikersnaam_Karakters.',}$/', $_POST['gebruikersnaam'])) { if(!isset($error)){ $error=1;}else{$error=$error+1;} echo "Je gebruikersnaam moet minimaal {$Minimale_Gebruikersnaam_Karakters} tekens bevaten en mag geen komma of andere onbedoelde tekens zijn<br>Toegestaan is <br>A t/m Z <br>a t/m z <br>1 t/m 9 <br>en -"; } else { echo "geldige gebruikersnaam(goedgekeurd)"; } if(preg_match('/^[A-Za-z1-9-]{'.$Minimale_Wachtwoord_Karakters.',}$/', $_POST['wachtwoord']) && preg_match('/^[A-Za-z1-9-]{'.$Minimale_Wachtwoord_Karakters.',}$/', $_POST['herhaalwachtwoord'])) { if($_POST['wachtwoord'] != $_POST['herhaalwachtwoord']) { if(!isset($error)){ $error=1;}else{$error=$error+1;} echo "niet hetzelfde wachtwoord"; } /*else { echo "hetzelfde wachtwoord (goedgekeurd)"; }*/ } else { if(!isset($error)){ $error=1;}else{$error=$error+1;} echo "wachtwoord moet minimaal {$Minimale_Wachtwoord_Karakters} tekens bevatten!"; } if(!preg_match("/^[A-Za-z1-9_.-]{1,}@[A-Za-z1-9-]{1,}\.[A-Za-z1-9]{2,3}$/", $_POST['email'])) { if(!isset($error)){ $error=1;}else{$error=$error+1;} echo "onjuiste email"; } else { echo "goedgekeurd!"; } if(!isset($error)) // this problem is fixed yesterday on phpfreaks.com forum! { echo "goedgedaan geen errors!"; query("SELECT username FROM phpfreaks WHERE password = private"); // example } } else { ?> <HTML> <HEAD> <TITLE>New Document</TITLE> </HEAD> <BODY> <form method="post"> <input type="text" name="gebruikersnaam" value="gebruikersnaam" maxlength="20" size="20"> <input type="password" name="wachtwoord" value="wachtwoord" maxlength="20" size="20"> <input type="password" name="herhaalwachtwoord" value="herhaalwachtwoord" maxlength="20" size="20"> <input type="text" name="email" value="voorbeeld@domeinnaam.extensie" maxlength="50" size="20"> <input type="submit" name="login" value="inloggen"> </form> </BODY> </HTML> <?php } ?> the problem is, is that i want to pass a variable between functions like $type (the database type) between connectdatabase(); and query(); in the 'aanmelden.php' file is an example of how i use the function (on line 3 and 45 the ones with //example comment) thanks for reading. please help. ps. config.php contains include("connect.php"); I think I need to use $this, but I need to be able to add the two values returned from both of my functions. How would I do that? Code: [Select] public function get_users_edge($uid) { $users_primary->get_users_primary_edge($uid); $users_dynamic->get_users_dynamic_edge($uid); echo $users_primary + $users_dynamic; } Learning something new here so if anyone can tell me why I this wont return a value? page1.php Code: [Select] <?php require "page2.php"; getuserid(); echo $userID; ?> page2.php Code: [Select] <?php function getuserid() { $user =& JFactory::getUser(); $userID = $user->id; global $userID; } ?> I've written the following code: Code: [Select] $mode = 'development'; // 'Development' or 'Public' // Writes a buffer to a file /** * ob_file_callback() * Writes the generated CSS to a file. * @param mixed $buffer * @return void */ function ob_file_callback($buffer) { // stylesheet-public.css is the output generated file $ob_file = fopen('stylesheet-' . $mode . '.css','w'); fwrite($ob_file, $buffer); } $buffer = 'test'; ob_start('ob_file_callback'); $mode is always empty when I run this code. Is it an issue because ob_file_callback is an overridden function? I have some questions about the scope of navigation : if i define a constant like define('constant'), php manuel says it's global, does this mean that i can use of different files also? like, i defined in define.php and used in anotherfile.php, or shoud the latter file be included then? what about file a.php includes file b.php which in turn includes file c.php. Can functions of a use variables of file c? ultimately: what about variables that are declared global inside a function but this variables is not initialized in that script, like in wordpress: function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) { global $wp_locale; // more code } $wp_locale can be initialized in another script file? thanks in advance Hi, In all my classes, I need to declare globals for everything else, from site variables even to objects (other classes). I obviously don't want to as globals are horrible and I need to have functions clean without globals. I have included a list of all the global vars in the construct and it still doesn't work. Example: start.php <?php $vars = array(some values); $some = more; $variables = foo; $helper = new Helper(); $home = new Home(); This is a little bit simplistic, but for some reason, the variables just don't want to go in the main class without a global. Functions will not run if they require any of these variables. I have a script I am putting together that simulate a cricket game. The only issue is, that there are a huge number of functions because there doesn't seem to be any other way to do this properly. As well as this, there a while() loop and all this seems to be leading to the page reaching a max 30 second timeout when generating the result. My code is attached below, it is quite messy at the moment because i've just be working on it, but I was wondering if anyone has any solutions of how I can speed this up or change to prevent a timeout: <?php // Error reporting error_reporting(E_ALL); // Connect DB mysql_connect("wickettowicket.adminfuel.com", "rockinaway", "preetha6488") or die(mysql_error()); // Select DB mysql_select_db("wickettowicket") or die(mysql_error()); // MySQL queries to find batsmen and bowlers $array_batsmen = mysql_query('SELECT id, name, ability, strength FROM wtw_players WHERE team_id = 1 ORDER BY id ASC'); $array_bowlers = mysql_query('SELECT id, name, ability, strength FROM wtw_players WHERE team_id = 2'); // Start table for data $data = '<table width="600px">'; // Create blank scorecard while ($array_bat = mysql_fetch_array($array_batsmen)) { $data .= '<tr><td>'.$array_bat['name'].'</td><td></td><td></td><td>0</td></tr>'; } // Set up arrays for players $current_batsman = $current_bowler = array(); // Reset query mysql_data_seek($array_batsmen,0); $in_one = $in_two = $it = ''; function currentBatsman($id, $name, $ability, $strength, $out, $in, $runs) { global $current_batsman; $current_batsman = array ( 'id' => $id, 'name' => $name, 'ability' => $ability, 'strength' => $strength, 'out' => $out, 'in' => $in, 'runs' => $runs ); echo 'set current'; } // Set up arrays of batsmen while ($array = mysql_fetch_array($array_batsmen)) { if ($it < 3 && $in_one == '') { currentBatsman($array['id'], $array['name'], $array['ability'], $array['strength'], 0, 1, 0); $batsmen[$array['id']] = array ( 'id' => $array['id'], 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'], 'out' => 0, 'in' => 1, 'runs' => 0 ); $in_one = $array['id']; $current = $array['id']; $it++; } else if ($it < 3 && $in_two == '') { $batsmen[$array['id']] = array ( 'id' => $array['id'], 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'], 'out' => 0, 'in' => 1, 'runs' => 0 ); $in_two = $array['id']; $it++; } else { $batsmen[$array['id']] = array ( 'id' => $array['id'], 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'], 'out' => 0, 'in' => 0, 'runs' => 0 ); } } // Bowler Array while ($array = mysql_fetch_array($array_bowlers)) { $bowlers[] = array ( 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'] ); } // Reset both queries mysql_data_seek($array_bowlers,0); mysql_data_seek($array_batsmen,0); function changeBatsman($just_out) { global $array_batsmen, $batsmen; //Update array $batsmen[$just_out] = array ( 'in' => 1, 'out' => 1 ); while ($array = mysql_fetch_array($array_batsmen)) { if ($just_out != $array['id'] && $batsmen[$array['id']]['out'] != 0) currentBatsman($array['id'], $array['name'], $array['ability'], $array['strength'], 0, 1, 0); } // Reset query mysql_data_seek($array_batsmen,0); echo 'change batsman'; } function swapBatsman($other_batsman) { global $array_batsmen, $batsman; while ($array = mysql_fetch_array($array_batsmen)) { if ($other_batsman != $array['id'] && $batsman[$array['id']]['out'] != 0 && $batsman[$array['id']]['in'] == 1) currentBatsman($array['id'], $array['name'], $array['ability'], $array['strength'], 0, 1, 0); } // Reset query mysql_data_seek($array_batsmen,0); echo 'swap batsman'; } $runs = $outs = $balls = $overs = 0; $played = array(); function selectBowler() { global $bowlers, $current_bowler; // Select random bowler $choose_bowler = array_rand($bowlers, 1); $current_bowler = array ( 'name' => $bowlers[$choose_bowler]['name'], 'ability' => $bowlers[$choose_bowler]['ability'], 'strength' => $bowlers[$choose_bowler]['strength'] ); } /* function selectBatsman(); { global $array_batsmen; while ($array_batsmen[]['out'] != 1) { }*/ function bowl() { global $batsmen, $bowlers, $current_bowler, $current_batsman, $data, $balls, $outs, $runs; if ($current_batsman['out'] == 0) { echo 'bowling'; // Set the initial number $number = rand(0, 190); // Ability of batsman if ($current_batsman['ability'] > 90) $number += 30; else if ($current_batsman['ability'] > 70) $number += 15; else if ($current_batsman['ability'] > 50) $number += 2; else $number = $number; // Strength of batsman if ($current_batsman['strength'] > 90) $number += 15; else if ($current_batsman['strength'] > 70) $number += 10; else if ($current_batsman['strength'] > 50) $number += 5; else $number = $number; // Depending on overs if ($balls > 270) $number += 30; else if ($balls > 120) $number -= 10; // Ability if ($current_bowler['ability'] > 90) $number -= 30; else if ($current_bowler['ability'] > 70) $number -= 15; else if ($current_bowler['ability'] > 50) $number -= 2; else $number = $number; // If batsman has made a huge total of runs, we need to knock some numbers off - more likely to get out if ($current_batsman['runs'] > 200) $number -= 70; else if ($current_batsman['runs'] > 100) $number -= 30; // Finally sort out runs if ($number > 190) $run = 6; else if ($number > 170) $run = 4; else if ($number > 160) $run = 3; else if ($number > 100) $run = 2; else if ($number > 50) $run = 1; else if ($number > 10) $run = 0; else if ($balls > 120 && $number > 0) $run = 0; else $run = -1; // Increase number of balls $balls += 1; // Are they out? if ($run == -1) { $current_batsman['out'] = 1; $played[] = $current_batsman['id']; $find = '<tr><td>'.$current_batsman['name'].'</td><td></td><td></td><td>0</td></tr>'; $replace = '<tr><td>'.$current_batsman['name'].'</td><td></td><td>'.$current_bowler['name'].'</td><td>'.$current_batsman['runs'].'</td></tr>'; $data = str_replace($find, $replace, $data); changeBatsman($current_batsman['id']); echo 'out'; } else { $current_batsman['runs'] += $run; $runs += $run; if ($run == 1 || $run == 3) { swapBatsman($current_batsman['id']); echo 'time to swap'; } echo $run; } // Count outs if ($current_batsman['out'] == 1) $outs += 1; } } function game() { global $main, $batsmen, $bowlers, $data, $batted, $balls, $outs, $current_batsman; // Check if possible while ($balls <= 295 && $outs < 10) { selectBowler(); // Actually bowl now bowl(); } } game(); echo $data; I teaching myself php, but I am coming from java and other compiled languages, so the process has been a little bumpy. I am trying to do something like this: Code: [Select] class my_class { function one () { $two = two (); $three = three (); $five = $two + $three; return $five; } function two () { $two = 2; return $two; } function three () { $three = 3; return $three; } } Unfortunately, I keep getting an error message saying that my call to two () is an undefined function. I am gathering from this that the scope of one () is not aware of the existence of two (). Is there a way to get around this so I can call two () and three () from one ()? does anyone know how to decode this XML variable value into string values? I also need to know the oposite way: creating variable values into xml. I've tried several code examples but they did filter the requested data. Code: [Select] $xml='<?xml version="1.0" encoding="utf-8"?> <elements> <text identifier="ed9cdd4c-ae8b-4ecb-bca7-e12a5153bc02"> <value/> </text> <textarea identifier="a77f06fc-1561-453c-a429-8dd05cdc29f5"> <value><![CDATA[<p style="text-align: justify;">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>]]></value> </textarea> <textarea identifier="1a85a7a6-2aba-4480-925b-6b97d311ee6c"> <value><![CDATA[<p style="text-align: justify;">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>]]></value> </textarea> <image identifier="ffcc1c50-8dbd-4115-b463-b43bdcd44a57"> <file><![CDATA[images/stories/red/cars/autobedrijf.png]]></file> <title/> <link/> <target/> <rel/> <lightbox_image/> <width><![CDATA[250]]></width> <height><![CDATA[187]]></height> </image> <text identifier="4339a108-f907-4661-9aab-d6f3f00e736e"> <value><![CDATA[Kramer 5]]></value> </text> <text identifier="ea0666d7-51e3-4e52-8617-25e3ad61f8b8"> <value><![CDATA[6000 RS]]></value> </text> <text identifier="90a18889-884b-4d53-a302-4e6e4595efa0"> <value><![CDATA[Eindhoven]]></value> </text> <text identifier="410d72e0-29b3-4a92-b7d7-f01e828b1586"> <value><![CDATA[APK Pick up and return]]></value> </text> <text identifier="45b86f23-e656-4a81-bb8f-84e5ea76f71f"> <value><![CDATA[15% korting op grote beurt]]></value> </text> <text identifier="3dbbe1c6-15d6-4375-9f2f-f0e7287e29f3"> <value><![CDATA[Gratis opslag zomerbanden]]></value> </text> <text identifier="2e878db0-605d-4d58-9806-8e75bced67a4"> <value><![CDATA[Gratis abonnement of grote beurt]]></value> </text> <text identifier="94e3e08f-e008-487b-9cbd-25d108a9705e"> <value/> </text> <text identifier="73e74b73-f509-4de7-91cf-e919d14bdb0b"> <value/> </text> <text identifier="b870164b-fe78-45b0-b840-8ebceb9b9cb6"> <value><![CDATA[040 123 45 67]]></value> </text> <text identifier="8a91aab2-7862-4a04-bd28-07f1ff4acce5"> <value/> </text> <email identifier="3f15b5e4-0dea-4114-a870-1106b85248de"> <value/> <text/> <subject/> <body/> </email> <link identifier="0b3d983e-b2fa-4728-afa0-a0b640fa34dc"> <value/> <text/> <target/> <custom_title/> <rel/> </link> <relateditems identifier="7056f1d2-5253-40b6-8efd-d289b10a8c69"/> <rating identifier="cf6dd846-5774-47aa-8ca7-c1623c06e130"> <votes><![CDATA[1]]></votes> <value><![CDATA[1.0000]]></value> </rating> <googlemaps identifier="160bd40a-3e0e-48de-b6cd-56cdcc9db892"> <location><![CDATA[50.895711,5.955427]]></location> </googlemaps> </elements>'; Hi. I have some code which needs to return a single variable from the function and stored as a variable within this page, so it can be echoed later on in the page. I couldn't get it to return as a variable, so i used "return compact();" to return the variable and "extract (myFunction());" to extract it to variables. However, when I turned on php's display errors and error reporting function, I got an error message saying "Warning: extract() [function.extract]: First argument should be an array in /my/web/site/index.php on line 6" (which is where my extract function is). This works fine with passing more than one variables through, is there another way pass one variable from a function to be stored as a variable on the page which called the function? Here is the function: Code: [Select] <?php //This is a list of numeric error codes against their text. this will return the error code as the variable $issue function checkLoginIssue() { //If there is an error code if (isset($_GET["issue"])) { //cycle through the list until the code is reached, return the text and break the switch switch ($_GET["issue"]) { case "1": $issue = '<p class="warning">Please log in to view this page</p>'; break; case "2": $issue = '<p class="warning">Wrong Username or Password</p>'; break; case "3": $issue = '<p class="warning">No user found with those details</p>'; break; } //return the variable in an array with a single value return compact('issue'); } } ?> And here is the code which calls the function: Code: [Select] <?php extract(checkLoginIssue()); ?> Can someone tell me by looking at the following code why function pcust() does not work, I am new to programming? Code: [Select] $firstname = "chris"; $surname = "reynolds"; $address1 = "12 birch end"; $town = "Wallington"; $county = "surrey"; $postcode = "wh20 3bg"; $telephone = "01372 854785"; $cust_details = fix_cust($firstname, $surname, $address1,$town, $county, $postcode, $telephone ); function fix_cust($n1, $n2, $n3, $n4, $n5, $n6, $n7) { $n1 = ucfirst(strtolower($n1)); $n2 = ucfirst(strtolower($n2)); $n3 = ucwords(strtolower($n3)); $n4 = ucfirst(strtolower($n4)); $n5 = ucfirst(strtolower($n5)); $n6 = ucfirst(strtoupper($n6)); $n7 = ucfirst(strtolower($n7)); return array($n1, $n2, $n3, $n4, $n5, $n6, $n7); } echo $cust_details[0] . " " . $cust_details[1]; echo "<br />"; echo $cust_details[2]; echo "<br />"; echo $cust_details[3]; echo "<br />"; echo $cust_details[4]; echo "<br />"; echo $cust_details[5]; echo "<br />"; echo $cust_details[6]; echo "<br />"; function pcust(){ $length = count($cust_details ); for ($i = 0; $i < $length; $i++) { echo $cust_details[$i]; echo "<br />"; } } pcust(); Hi. I am trying to write a function but they are new to me and confusing me. On multiple pages I am requesting users details. I thought instead of writing out the query every time I want to do this I would include a functions page with the query in it. The only variable I carry from the user is the user name as a session when they log in. The user name is the email address of the person signed in. so here is my first function. As im sure you can see it doesn't work but I don't understand why or how to call it properly. Can someone please explain.... function GetUser($user = "") { $qFindUser = "SELECT * FROM members WHERE email = '$user'"; $rFindUser = mysql_query($qFindUser) or die (mysql_error()); $UserInfo = mysql_fetch_array($rFindUser); } calling the function to return all details of a member as an array. <?php $Name = $_SESSION['MM_Username']; $UserInfo = GetUser($Name); echo $UserInfo['forename']; ?> i am wondering what the '@' is used for when used in front of a function like Function(@$VAR); What is the benefit of the @? I have a function to get some data from the database. However it always comes back with: Code: [Select] Notice: Undefined variable: db in C:\wamp\www\ASF A Simple Forum\functions\functions.php on line 233 Fatal error: Call to a member function query() on a non-object in C:\wamp\www\ASF A Simple Forum\functions\functions.php on line 233 here is the code: require_once(ASF_ROOT."includes/init.php"); // this is where the db connection is function get_db_info($type, $select, $table, $where="", $order="DESC", $limit="") { if (empty($type) || empty($select) || empty($table)) { try { throw new emptyArgs(); } catch (emptyArgs $invalid) { echo "<div style=\"border:1px solid #f00; padding:5px; width:350px; line-height:10px;\">"; echo "<b>$invalid</b>"; echo "<p>get_info() needs atleast 3 arguments (Type, Select and Table) - none supplied</p>"; echo "</div>"; } } else { if ($type == "num") { $query = $db->query(" SELECT $select FROM ".TBL_PREFIX."$table $where $order $limit ") or die($db->error()); $result = $db->num_rows(MYSQLI_NUM); return $result; } else { $query = $db->query(" SELECT $select FROM ".TBL_PREFIX."$table $where $order $limit "); $result = $query->fetch_array(MYSQL_ASSOC); return $result; } } } I have a function for mysqli_real_escape_string above this function and it works with no errors. Have i done something wrong? as you can see i am including the file which holds the connection but it doesnt seem to use it. every other file that uses the connection works fine. Hey guys. Can anyone tell me what this means? function foo { } It is basic php but I don't get it. Should it be called for execution, or does it work automatically, or both? The php manual at w3schools and php.net are too tough for me. That's why I posted here. Thanks! Hi, I've got 3 different upload forms in one form. I'm making a function so it just one piece of code. I'm having a bit of a problem changing some of the code so it works for all 3. Here is the function function loadImages ($action, $position) { $link = mysql_connect("","",""); if (!$link) { die('Error: could not connect: ' . mysql_error()); } $db_select = mysql_select_db("",$link); if (!$db_select) { die ("Error: database selection failed: " . mysql_error()); } $page = ($_POST["pageName"]); if ((($_FILES["bfile"]["type"] == "image/gif") || ($_FILES["bfile"]["type"] == "image/jpeg") || ($_FILES["bfile"]["type"] == "image/pjpeg")) && ($_FILES["bfile"]["size"] < 9000000)) { if ($_FILES["bfile"]["error"] > 0) { echo "Return Code: " . $_FILES["bfile"]["error"] . "<br />"; } else { $success = '<p style="font-weight: bold; color: #C00">Upload successful<br />'; // move uploaded temporary file to final destination move_uploaded_file($_FILES["bfile"]["tmp_name"], "uploads/" . $_FILES["bfile"]["name"]); // update database with new filename $filename = $_FILES["bfile"]["name"]; $sql = "UPDATE images SET filename= '$filename' where page = '$page' and position = '$position'"; if (!mysql_query($sql,$link)) { die('Error: ' . mysql_error()); } } } else { $fail = '<p style="font-weight: bold; color: #C00">Invalid file'; } mysql_close($link); } Now the $action is the bit that will determine whether it's updateTop updateMiddle or updateBottom. $position finds out if it's top middle or bottom if (isset($_POST['updateTop'])) { loadImages ($_POST['updateTop'], "top"); } Here is a sample of the html, it's in a switch case "Products": echo "<p>Image Top</p> <input type=\"file\" name=\"file\" id=\"file\" /> <input type=\"submit\" name=\"updateTop\" value=\"Update $page Top\" /> <p>Image Middle</p> <input type=\"file\" name=\"mfile\" id=\"mfile\" /> <input type=\"submit\" name=\"updateMiddle\" value=\"Update $page Middle\" /> <p>Image Bottom</p> <input type=\"file\" name=\"bfile\" id=\"bfile\" /> <input type=\"submit\" name=\"updateBottom\" value=\"Update $page Bottom\" /> "; break; What I need to do is get $_FILES["bfile" into a variable to see whether it's "bfile" "mfile" or "file" but not sure how to write it. Thanks for the help. Hi, I could use a little guidance here. 1) is a function with values to be sent to 2). 1) <!-- use ajax - OnCalc function to send data to your database. --> function OnCalc(value1,op,value2,total) { 2) php file using above function values <?php header( "refresh:5;url='http://localhost/hom...lcprint.php'"); echo 'You\'ll be redirected in about 5 secs. If not, click <a href="http://localhost/hom...hp">here</a>.'; echo '<script type="text/javascript">' , 'OnCalc();' , '</script>'; No error but code not working? OK, so if I've got this in my one of my class files: public function userr($user) { $thisquery = mysql_query("SELECT * FROM users WHERE username='". $user ."'"); if(mysql_num_rows($thisquery) == 1) { return true; } return false; } Could I use something like this: if($class->userr($user)) { // Something } If not, how would I go about doing something like it? And is there any way I could use a mysql_fetch_assoc in a function? |