Jump to content

mikesta707

Staff Alumni
  • Posts

    2,965
  • Joined

  • Last visited

Everything posted by mikesta707

  1. no, it simply sends another request to the page, not a redirect, but if you don't stop execution with die() or exit(), the rest of the page will be executed (at least until the header request is sent, and the page is reloaded adding a die() or exit() after the header should fix it
  2. Change the limit in the query from 50 to 1?
  3. when you are using the value of one element in the array in a string (like in your example, which uses an element of the $_GET array as part of the string it passes to some function) , you can either use concatenation (something you may have seen, which looks like this:) echo "my name is " . $array['key'] . " and I greet you!"; the example you gave is the other way, which is to use curly braces "{}" to put the array element directly in the string. echo "helly my name is {$array['key']}"; its similar to using a regular variable straight in a string (delimited by double quotes) echo "hello my name is $name"; the reason you use curly braces is because just putting the array element in a string like so echo "hello my name is $array['key']"; is ambiguous. It can't decide whether to say the value of the array $array at the index 'key', or to say the value of a string $array with the literal string "['key']" next to it.
  4. why dont you just do something like $lookup_name = "john"; $sql = "select * from table where username='$lookup_name'"; $query = mysql_query($sql); if (mysql_num_rows($query) > 0){ echo "username exists"; } that database class you are using seems to be complicating things
  5. are you expecting the alert to happen when the if statement is true? if so maybe you should echo it so it actually gets written to the page an executed, rather than just storing it into a variable
  6. seems to me that the username of the current user is stored in the get variable $_GET['username']. try that. you have a very strange way of handling log ins by the way. perhaps you should look into sessions
  7. how does your login script work? You mentioned not using cookies. does that mean you use sessions? I would need this information to give a concrete answer
  8. are you sure that a username named "zelig" exists in the table? You would have a blank screen if $data was empty. try changing your function to function fish($username){ $data = ""; $dbh=dbconnect() or die ('Fish error: ' . mysql_error()."<br>"); mysql_select_db("katarra_game"); $result = mysql_query("SELECT energy FROM users WHERE username = '" . mysql_real_escape_string($username) . "'") OR die(mysql_error()); if (mysql_num_rows($result) > 0){ $data = stripslashes(mysql_result($result,0,"energy")); } else { echo "No rows returned"; return 0; } mysql_close($dbh); $data = str_replace("&#38;#39;","'",$data); return $data;} for debugging purposes. If the message "No rows returned" pops up, then your query isn't returning any results
  9. try echoing $energy and see what data it has
  10. when you echo an array in PHP, all it does is echo the literal string 'Array' judging by the fact that its actually saying array, the code is probably working pretty well. if you want to print the array in a human readable format, use print_r like ginger robot suggested
  11. im assuming you still have the if ($user->energy < 0){.... code after what you posted? Get rid of that. first off, $user is never defined (as evident by the first error you get) and because of that, you can't use it like an object. Im assuming the $data variable being returned by fish() is how much energy whoever username is passed in has. Well in that case changing the if to if (fish('zelig') <= 0){ //not enough energy } alternatively, instead of callign the fish() function directly in the if statement, you can store the value that it returns in a function if you will need it later on, IE $energy = fish('zelig'); if ($energy <= 0){ //wdo whatever } //later on in the script do something else with $energy if you want
  12. i see you are using print_r to generate the string as I expected here: $str = print_r($r, true) . PHP_EOL; Try using serialize (check out the link from my post above) to generate the string version of the arrays to store. Then you could simply go through the file line by line, using unserialize to regenerate the PHP data. Alternatively, you could use json_decode/encode but serialize would probably be the better choice as what your doing probably has nothing to do with json
  13. that happens because of this line print $data; I think you have an issue with variable scope, as data isn't defined in the scope you are trying to use it in. When you define a variable in a function, it isn't usable anywhere outside of the function. more info: http://php.net/manual/en/language.variables.scope.php Since you return data from the function, you want to call the function, and "catch" the returned $data variable. something like I posted above (the echo fish(..) thing) would allow you to echo the contents of $data that was returned, but you probably want to store it into a variable like so $data = fish('some username'); remember though, the $data I just initialized in the line above is completely different from the $data that is in your fish function. Also, in order for the query to be run, you NEED to call the function. When you define a function, it won't ever be run unless you call it. I think this is part of your problem also Edit: just saw your new post. How do you check if someone is logged in? Is it session based? If so, then you could use a session variable to hold the character name, and use that session variable when running the query
  14. I think what he posted is the text file. OP do you have to store the information like that in the text file? if so parsing the file and turning the contents into seperate arrays will be somewhat difficult. not impossible, but time consuming. Perhaps the serialize() function would be better for storing the contents of whatever arrays into the file, and then you could use unserialize() to get the information from the text file back into a php array.
  15. I don't see where you even call that function in the code you posted. if you never call the function, the query never runs. Also, where is $user defined? are you sure $user is an object? Are you getting any errors from that page? try adding error_reporting(E_ALL); ini_set('display_errors', 1); to the top of the page and see if you have any errors being spit back at you. edit: just saw updated code. perhaps you mean to do function fish($username){ $data = ""; $dbh=dbconnect() or die ('Fish error: ' . mysql_error()."<br>"); mysql_select_db("katarra_game"); if (mysql_num_rows($result) > 0) $data = stripslashes(mysql_result($result,0,"energy")); mysql_close($dbh); $data = str_replace("&#39;","'",$data); return $data;} print fish('some username'); where some username is, you input whatever username you are trying to query for.
  16. Thats called pagination, and there are many wonderful tutorials on the internet for that sort of thing. You do have the right idea using $_GET with a pid. Here is a link to a tutorial on phpfreaks http://www.phpfreaks.com/tutorial/basic-pagination
  17. read the error message. the variable $session isn't an object (and you seem to be using it as if it were) can you post the rest of the code on text.php
  18. Can you give us an example line that will be read? your regex looks a little off
  19. Are you getting an error message?
  20. echo the errors inside the body tag? <body> if (isset($_POST['submit']) && !empty($errors)){ echo $errors; } as for the stars, there are multiple ways you could do it. a simple way would be to create a star variable for each item in the form, and at the beginning of the script, set them to an empty string and echo them next to each individual input, IE $star_name = ""; .... etc ... html and stuff <?php echo $star_name ?><input name="name" type="text" /> when you find an error, you can just set the individual star variables to a star or whatever. You can use CSS to make it red if you want to limit a field to just numeric characters, you can use the function is_numeric() to test. example if (is_numeric($string)){ echo "its a string of numbers"; } else { echo "There are some letters or non numeric characters in that string"; }
  21. you could have the timer on every page, and every time they go to a new page, restart the timer.
  22. The following worked for me. $first_time="18:12"; $second_time="07:12"; echo "First time :", $first_time ,"<br />","Second time:",$second_time, "<br />"; $first_time=strtotime($first_time); $second_time=strtotime($second_time); echo "First time stamp:", $first_time ,"<br />","Second time stamp:",$second_time, "<br />"; $res_time=$first_time-$second_time; $r = $res_time / 3600;//num hours $min = $res_time%3600; echo "$r hours and $min minutes"; you could change to echo to be echo "$r:$min"; if you want to make sure they are padded with zeros (IE 07 insead of 7,) you can do $r = str_pad($r, 2, "0", STR_PAD_LEFT); $min = str_pad($min, 2, "0", STR_PAD_LEFT); to pad them
  23. that looks ok. One thing is you don't need to use the keyword "var" anymore to define a class variable. but you may want to alter the scope of some of those variables if you wish to take advantage of encapsulation
  24. if you are showing this data on a webpage (ie an HTML page) you want to change all the newlines to line breaks. the function nl2br() would work. example echo nl2br($textWithNLs);
  25. include and echo don't work that way. All include does is more or less take the code from the file you include, and plop it wherever the include line was in your program. If you want to echo stuff from an include file, then echo it directly inside the include file
×
×
  • 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.