Jump to content

mikesta707

Staff Alumni
  • Posts

    2,965
  • Joined

  • Last visited

Everything posted by mikesta707

  1. Well all your problems stem from $_POST['weight'] being an empty string. I don't see any real problems in your code that would make $_POST['weight'] unset. Try doing print_r($_POST); at the top of the page and see what it outputs. Other than that, your form and code all seem fine
  2. what he means is that you are trying to set the html value attribute of an input tag to a PHP array. This is impossible. What happens is PHP tries to convert your array into a string (as you are using it like a string in that context). WHen you convert an array into a string, it always becomes the string 'Array' regardless of the size or contents of the array. Lets take a simpler example $array = array("a", "bunch", "of", "values"); echo $array; /**** Output is: Array ****/ What you want to do is provide the column you want as the key. So for example, if the column you want from $row is called 'column', you need to do echo "<td><input type='checkbox' name='chkbox[]' value='".$row['column']."'/></td>"; also, each $value in each iteration of foreach($_POST['chkbox'] as $value) { echo $value; } is NOT an array. It is a single value.
  3. what you posted isn't the object itself, and doesn't really give much more context than the original snippet you posted. However, given the context, it seems what I posted is a pretty safe bet for what he is trying to accomplish translated to a procedural style.
  4. Well I notice that you use a while loop in with those queries producing the errors. When you are pulling a single record from your database (IE querying based on a unique row, like ID, or username, etc.) you dont need to do use a while loop. Using one results in the second run of the while loop producing the warning you get. Ill give you an example $result = mysql_query("SELECT something FROM somewhere WHERE id=some id");//query that will grab 1 row always while ($row = mysql_fetch_array($result)){ //do some stuff } Now lets examine what happens with this while loop. Remember, that a while loop will always try to execute what is in its conditional statement, so what happens is first iteration: we set $row to the first result in the $result's result set. This also happens to be the only row. because of this, $result is now boolean false, second iteration: remember that the result set only had 1 row so $result is a boolean. EVen though its boolean, we still call the fetch_array function using $result as a parameter. Obviously when you do mysql_fetch_array(false) it returns a value of false this produces an error as fetch_array expects a mysql result resource, and you have passed a boolean however, $row is set to false because mysql_fetch_array returned false, so the while loop exits without a 2nd iteration of the code block fixing this is easy though. Just get rid of the while loop. So if we take our example above, to fix it we would do $result = mysql_query("SELECT something FROM somewhere WHERE id=some id");//query that will grab 1 row always $row = mysql_fetch_array($result); //do stuff with row //continue with rest of code EDIT: ahh seems PF caught your original error. Can't believe I missed that. As for your latest question, without seeing your friends object, its kind of difficult to guess what he means to use. However, it seems that he is setting some sort of user data, so you can use 'user_data' as the key. In your friends example, $loggedinUserDataArray would be the value at that key. so $_SESSION['user_data'] = $loggedinUserDataArray;
  5. try setting your forms action from post to get.
  6. hmm still don't see anything wrong with that code. Illegal offset errors generally mean you are using a non string/integer type for the array keys (IE doing something like: $object = new GenericObject(); $array = array($object => 'value'); In that example, i tried using an object as an array key. This will cause the warning you are having. However, I don't see anywhere in your code where you use anything but a string as a key. Please note, that what is occurring is a warning, and not an error. So your script isn't breaking. I don't want to tell you to post all your code, but to be completely honest, I can't see anything wrong with what you posted, so I don't really know what to tell you.
  7. instead of using add/remove class you could just use hide/show
  8. I don't see anything wrong with what you posted. Try posting the code that is around this line.
  9. dont know if thats possible. Im unaware of any CSS rules that would allow you to basically change how an image is rendered. The link a provided kind of spoofs this using absolute positioning of a span to position it over an image. This is what an overlay is.
  10. Please don't repost your questions. Especially don't repost them in the wrong board. This is the PHP help board. You probably want to post this in the css help forum. Doing a quick google search may also help you. I turned up with this: http://stackoverflow.com/questions/403478/css-how-to-overlay-images you aren't trying to overlay a small image over a larger one, but the concept is pretty much the same. Just don't use the small image.
  11. What do you mean by "I want to this babu through a text".. I have no idea what you are asking for help with here. Do you mean you want to pass babu through a form? if so read this tutorial on forms: http://www.tizag.com/htmlT/forms.php and how they work with php: http://www.tizag.com/phpT/examples/formex.php Set the method of the form to post, and then you can access the post data through the $_POST super global. if this is not what you want you need to clarify your question.
  12. just use file() This takes a file and returns an array of each different line, which is exactly what you want if im not mistaken EDIT: Sorry misread your post. Yeah you are using the wrong newline character (\n not /n) as webstyles pointed out
  13. There are many scripts and tutorials on the internet for handling multiple file uploads on the front end/client side. This is usually handled via javascript or some other sort of client side language unless you simply want to add a static amount of upload boxes. A quick google search will aid you in this part of it. As far as the PHP, read here: http://php.net/manual/en/features.file-upload.multiple.php that details how you handle multiple file uploads on the php side.
  14. that code will never write the IP since all you do is set $a to an array containing the IP details on one person. The count for this array will always be 1, and thus the condition will always be false. That being said, I'm not sure what exactly you are trying to accomplish. Do you want to basically store someone's IP when they get to a "login" page once, but not store it again if their IP is already stored? If so you will need to first get the contents of the text file, make sure that the IP is not in there, and then write to it if not. However, i'm not entirely sure why you aren't using a database. Besides the fact that this is a terrible way to log someone in, it would be alot simpler with a database. but something along the lines of $ip = ...;//however you get the IP. probably from the $_SERVER super global $lines = file("path/to/file.txt"); $found = false;//flag to decide if we have found the IP or not foreach($lines as $line){ if ($line == $ip){ $found = true; break; } } if (!$found){//if ip isnt in file $handle = fopen($filename, 'a'); fwrite($handle, $ip."\n"); } But remember, as I said, I strongly recommend using databases as its much easier and will cause less strain on your system.
  15. Ahh yes thats true. If that is the case you are going to have to have a special condition where you keep checking the next index if you are going to go with explode. However, that being the case, you may want to go with a regular expression
  16. What you are looking for is the SUM mysql function. For example $res = mysql_query("SELECT SUM(kills) as num_kills FROM playerinfo WHERE kills > 0"); $row = mysql_fetch_array($res); echo "There are : ". $row['num_kills']." total kills"; EDIT: bah sniped, but ill post anyways, as I have an example
  17. well assuming you didn't write it yourself, I wouldn't mess around with it. Perhaps if you posted what methods it has, or at least the function it uses to get the data I would be able to give a more definitive answer
  18. I see. So you need to basically parse the output table and insert each row into its own row in the table. THere is no easy way to go about this. If you have access to the grabber function, you can alter that. Otherwise, you will have to use some combination of explode, and simple substring matching, or more complicated regular expression matching on the string you catch with output buffering see: explode:http://php.net/manual/en/function.explode.php substr: http://php.net/manual/en/function.substr.php strpos: http://php.net/manual/en/function.strpos.php for regular expression see preg_match: http://php.net/manual/en/function.preg-match.php for a general tutorial on what regular expressions are see: http://www.regular-expressions.info/tutorial.html Hope this helps
  19. well, assuming you won't ever get a malformed response, you know that the number of entries will be the number of entries in the exploded array divided by 4. So for example, you could explode by the space character " ". and then go through exploded array, 4 at a time. for example $str = "however you get the string"; $pieces = explode(" ", $str); //go through it 4 at a time for ($i = 0; $i < sizeOf($pieces); $i += 4){ $piece1 = $pieces[$i]; $piece2 = $pieces[$i+1]; $piece3 = $pieces[$i+2]; $piece4 = $pieces[$i+3]; }
  20. There are some conceptual issues going on with this code. Firstly, You don't seem to understand how parameter passing works, and return values from functions work. For example here, where you error is originating from $db_selected = mysql_select_db($database, $this->connect_DB($this->link = $link)); Lets examine this. Firstly, you use a variable $link. However, we are in the method: select_DB, where you have not defined $link yet. Yeah sure, its a parameter in the connect_DB method, but the scope of that variable is local to the connect_DB method. As such, you cannot access it in select_DB. When you use undefined variables, PHP gives them a value of undefined or null. So essentially what you are doing because of this part ($this->link = $link) is setting your classes data member to a value of null. Now, after this null assignment, we go into the connect_DB method. This method is below public function connect_DB($link) { $link = mysql_connect(host, username, password); if (!$link) { die('Could not connect: ' . mysql_error()); } } Now remember how we assigned null to the argument earlier. Now the $link variable in this function is null. However, this doesn't really matter because we reassign it anyways. However, we are only reassigning the local $link variable, and our datamember, $this->link remains null. I'm assuming where you write host, username, password in the posted code, in the actual code is the actual username password and host (whether as hardcoded strings, or variables or whatever). I'm guessing this line works, as you didn't post anything about not being able to connect. However, note that you don't return anything from this function. It just ends. However, what you don't consider is what happens when it ends. Remember, this function is called in the select_DB function so we return back. Recall this is here $db_selected = mysql_select_db($database, $this->connect_DB($this->link = $link)); and you have the call to connect_DB as the second parameter to mysql_select_db. Recall that your function doesn't return anything, so what happens is this call essentially has the value of null. What you have, and the following are equivalent the way things are currently set up. $db_selected = mysql_select_db($database, NULL)); This is why you are getting the second error. Now, as far as the OOP goes, you seem to misunderstand the reason we have OOP. It isn't really about simply making classes, you have to set up your class correctly for it to be more useful than simply coding procedurally. Otherwise you basically have procedural code with the word class strewn about. Probably the most important and used concept of OOP is encapsulation. The concept is pretty simple. It basically means that your object is like a discrete entity in your code, with specific ways to input and output data to the rest of the program, but otherwise the data in the object is only available to your object, and data outside is only available outside. However, in your object, all your methods and datamembers are public. That means anyone using your class could set the variables to arbitrary meaningless values ("like setting your database member to the string 'hello') which could potentially break your object. This may seem like an example of something that would be obvious to you to not do, and so may never occur. But the ramifications of doing something similar in a more complex system are sometimes drastic, and may be difficult to spot. What you should do with an object, is keep data that the user of the class doesn't need to mess with as private, but other things, like methods to invoke certain functionality in the class should be public. I will make some alterations in your class above that makes some use of encapsulation, and hopefully fixes the current problems. class connect_to_DB { //the user of this class has no need to change these two variables directly //so why let them? We will use metators and accessors to interact with this data private $databaseName; private $link; function __construct($database) { //now before we even select a database, we need to connect to mysql first //now, our $link datamember will store our connection resource $this->link = mysql_connect(host, username, password); //it may be better to abstract that login data into its own class //bt thats a topic for later //instead of the old way, we set our member to the passed in argument $this->databaseName = $database; //remember, we just set that, and data members are available EVERYWHERE in the class //so there is no need to pass an argument as we already have it $this->select_DB(); } //this is a function that is used to set up the class, and is called automatically by the constructor //we don't need to let the user access this function directly private function select_DB() { //now we can just use our link datamember, as we defined it before. $db_selected = mysql_select_db($this->databaseName, $this->link); if (!$db_selected) { die ('Can\'t use '.$this->databaseName.' : ' . mysql_error()); } } //now, we need a way to change the database we are connected to! //lets make a mutator to do so public function change_DB($db_new){ //we set our databaseName datamember to the new one. $this->databaseName = $db_new; //now that its set, we can just call select_DB again. $this->select_DB(); } //Now what if we want to get the value for the $link or the $databaseName members //the user may have some use with their values. lets make some accessors public function get_DBName() { return $this->databaseName; } public function get_Connection() { return $this->link; } //and finally a simple query wrapper function to give it some extra functionality public function query($sql){ return mysql_query($sql); //we could add some fancy error handling and stuff //but thats a topic for later } } //now how can I use this class? Well lets instantiate an object $dbConn = new connect_to_DB('my_database'); //we are connected, we can do a query $dbConn->query("SELECT something FROM somewhere WHERE some_column='some value'"); //maybe we should change the database $dbConn->change_DB('different_database'); //do a different query on this new databse $dbConn->query("SELECT something_else FROM somewhere_else WHERE some_other_column='some other value'");
  21. Oh sorry, didn't noticed I misstyped there. yes thats correct
  22. oh well since you seem to be using a string, you need to surround your $userid variable with quotes. This is assuming ur userid column is of type varchar $query_RsgetApID = "SELECT * FROM users LEFT JOIN bookings_admin_users ON bookings_admin_users.username = users.username WHERE users.userid='.$userid.' LIMIT 0 , 30 "; unless thats some sort of hex typed column or something, but the default type for hex numbers in mysql is a string if I remember correctly
  23. what is this? This really doesn't have anything to do with OP's question, and in fact has a syntax error in it. Even if you did have correct syntax, all this would do is display that $row is a string, and print the contents of the string? Anyways, for OP, printing out a select box with the info you want is fairly easy. I'll give an example $sql = "SELECT something FROM somewhere WHERE somecolumn='somevalue'"; $res = mysql_query($sql); echo "<select name='someName' ... >";//print out begining select tag once while($row = mysql_fetch_array($res)){ echo "<option value='".$row['column_you_want']."' >".$row['column_you_want']."</option>";//print each option for every row } echo "</select>";//print out closing select tag Hope this helps
  24. You need to use session_start in order to access the $_SESSION super global. session_start(): http://php.net/manual/en/function.session-start.php
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.