-
Posts
16,734 -
Joined
-
Last visited
-
Days Won
9
Everything posted by PFMaBiSmAd
-
Copying PHP variable array to a new variable array?
PFMaBiSmAd replied to laZuRe's topic in PHP Coding Help
Yes, for your specific example, it means that the battle function can only do one thing without being rewritten (and retested), because the input values it operates on are hard-coded into it (it might as well not have function name (){} syntax around the code in it.) However, if your battle function was written to be general purpose so that it accepts all the input data it needs, for the two parties that are to battle, as call time parameters, it would be reusable, as is, for your current case, were a real player is battling the computer, if you have a real player battling another real player, or you want the computer to battle itself. You would just supply the correct two sets (arrays) of data when you call the battle function - <?php function battle($player,$opponent,$battleAction){ //... code uses the $player array, $opponent array, and the $battleAction call time parameters for all the data it needs } // for your current case of a real player VS computer - $result = battle($player,$enemy[$enemyID],$_GET['battleAction']); // for the case of a real player VS a real player - $result = battle($player1,$player2,$_GET['battleAction']); // for the case of the computer VS itself - $result = battle($enemy[$enemyID1],$enemy[$enemyID2],$someRandomAction); Also for this specific example it would force the data structures to be consistent and contain all the relevant data, which will tend to simplify and clarify the logic. Your current code has a $username, which might or might not be the same as $player['name'], and those might or might not be the same as $_SESSION['username']. Short-answer: As far as your main code is concerned, functions should be general purpose black boxes, that optionally accept input data as call time parameters, perform some useful operation, and returns the result back to the main code. Once you write and test a function, you should be able to use it in any application that needs the operation that it performs, without needing to rewrite and retest it every time you use it with a different source of input data. For the Alternative syntax vs traditional syntax suggestion. I can tell by the left-hand side of your editor's screen that it take no special notice of the Alternate syntax. If you use the traditional {} syntax in php aware editors, the editor keeps track of the matching opening/closing {} and you can put the cursor on one and have the matching one highlighted and you can expand/hide blocks of code between matching {} to get it out of view if it is not currently relevant or if you just want to see the program logic without the code. -
Copying PHP variable array to a new variable array?
PFMaBiSmAd replied to laZuRe's topic in PHP Coding Help
Based on what I have been able to determine from the code you have posted, the sa_enemystats table can have multiple rows for each player's username, one for each enemy that a user has battled? If so, your queries that select and update the sa_enemystats table must have WHERE username = '$username' AND enemy_id = $enemyID so that they operate on the correct row. Also, please DON'T use the global keyword in your functions to bring values into the function. You should pass values into functions as call time parameters, the same way you are passing in the $username value into the battle function. And, unless the programming editor you are using supports matching up elements using the alternative syntax for control structures (using if(): else: endif;) I recommend that you use the traditional syntax (using if(){}else{}) so that your editor can help you match up elements belonging to the same block. -
Copying PHP variable array to a new variable array?
PFMaBiSmAd replied to laZuRe's topic in PHP Coding Help
I have a basic question about your overall game play. For each turn (i.e. one click on someone else's username link) the current visitor (in $_SESSION['username']) only battles that ONE username that he clicked on? If so, why would you have an array of enemies that you use the $enemyID to access one of in the first place? For any turn, you only need the current visitor's data (I'm not sure if he is the player or the enemy) and the data for the username that the current visitor clicked on. Part of the reason I need to ask such a basic question is because we only see the code you post and the posted code does not (clearly) indicate what the values you are passing into the function mean and it has much excess/unclear data and code (without any comments), it is difficult for us to know what the overall code and data is doing or means. Here's at least one unneeded bit in the code you posted - you don't need to make $db_enemy a 2D array because it only exists inside that function where you are retrieving the data at and it only contains the data for the username that the query retrieved. I also suspect that you have probably already retrieved that username's data somewhere else in the overall code and should either use the already retrieved values or you should only retrieve the username's data in the battle function code and not elsewhere. -
Copying PHP variable array to a new variable array?
PFMaBiSmAd replied to laZuRe's topic in PHP Coding Help
If you are expecting only one row from the query (which is what your first code implies), there's no need to use any loop. If $enemy doesn't exist and you want it to be an array that holds all the associative values that your query selected, just use - $sql = "SELECT * FROM sa_enemystats WHERE username='$username'"; $res = mysql_query($sql); $enemy = mysql_fetch_assoc($res); -
Sanitizing, how's the best way of doing it?
PFMaBiSmAd replied to Matt Ridge's topic in PHP Coding Help
No, you are still skimming over and then not thinking about what you actually see in front of you. $var will only be equal to 3, if you do something that assigns the value three to it. Setting $var = 1; followed by a line that sets $var=2;, will only result in $var being equal to 2. In the example that mjdamato posted, the only 3 result was due to a mathematical expression involving $var +2, where the starting value in $var was 1, i.e. 1 + 2 = 3. Numbers in variables and equations in a programming language are NO different than that you learned in Algebra in school. -
Apparantly theres something wrong with my variable. I disagree
PFMaBiSmAd replied to FinalFrontier's topic in PHP Coding Help
You cannot use a minus sign - in a variable. $margin-leftdelivery is a mathematical expression and is trying to evaluate the variable $margin - (minus) the constant leftdelivery. -
Two == signs is a comparison operator. One = sign is an assignment operator. Your first code was assigning $idNum to $row['idNum'] in the if(){} statement, and then testing if the value that was assigned evaluated to a true or false.
-
Apache is a web server. In fact, I believe, it is the current most popular web server in use around the world. The URLs you have been entering in your browser - http://localhost/somefile.php cause HTTP requests to be made to the Apache web server you have installed on your computer. I have a follow up question about your actual problem. How did you obtain and install php, because if you used the .msi installer package, you must use the Windows control panel add/remove menu to add php extensions.
-
I would look at the web server's error log to see if there are any relevant error messages, such as 'cannot load dll', permission errors...
-
Based on the information in your post, you need to check that the php.ini that you are changing is the one that php is using. Make a .php script file with the following in it, and browse to this file - <?php phpinfo(); ?> The value for - Loaded Configuration File is the php.ini that php is using. Also, since you are using Windows, have you changed the Windows setting that 'hides extensions for known file types' as this could be hiding the actual extension of the php.ini that you have been editing (it may not be just php.ini).
-
If the point of this specific code is to get something like global settings (maybe someones multipliers in a RPG...), your query should just retrieve the columns you want and you would be done. In programming, the best method of accomplishing something generally takes knowing what you are actually doing, which is why just showing a few lines of your code out of context rarely results in the best solution. I'm not trying to drag this out to give you a hard time. If what you are trying to create is a web application that will some day need to perform its best to handle a large number of visitors at the same time, having line after line of code that does something to data, only to be followed by more code that undoes what was just done, is something you want to nip in the bud as soon as possible in your coding.
-
Nop, that's still too much unnecessary code. What's wrong with the array: $row (or just use your final name when you fetch it, something like - $settings = mysql_fetch_assoc($result) At this point it, without seeing your query statement and what you are trying to get as an end result, cannot specifically help you more than to mention the concept and that every time we see someone with a series of numbered variables that their code is overly complex for what is being accomplished.
-
If these are columns in a database table, when you fetch them they are ALREADY in an array. Why have lines of code that assign them to a series of scaler php variables, then have more code to put them back into an array?
-
Database, table, and column names may only contain alphanumeric characters and the under_score character. Using any other characters in the name, having an all numeric name, or a name that is a reserved mysql keyword requires special handling. The use of back-ticks `` around the name identifies it as a database, table, or column name that requires special handling. We generally suggest using a different name that doesn't require special handling so that you don't need to use any extra syntax to make them work.
-
Displaying and testing your web page locally ARE two steps of the development process. Those aren't mutually exclusive things. Rather than listen to things someone else states, think about what you are doing and the steps needed to accomplish the task.
-
How are your original $field1, $field2, $field3,.. variables getting set? Having code that sets them, then having more code to put them into an array is doubling the amount of code, processing time, and memory needed. It's often easily possible to directly get them into an array when they are created.
-
Here's a slightly different slant on the problem. Your form processing code should only access the form data if the form has been submitted. You should have an if(isset($_POST['user_name_submit'])){... all the form processing code goes here...} statement around all the form processing code so that the form processing code will only be executed when the form has been submitted. The first step of your user_name field validation logic, that is testing if $_POST['user_name'] is empty or not, should also determine if you execute any further code that uses the user_name value. If the user_name field is empty, you should setup an error message to be output telling the user that the required form field was empty. You would only execute the preg_match logic if the user_name form field was not empty.
-
Almost there!! Problems with search engine and pagination page...
PFMaBiSmAd replied to rodhow's topic in PHP Coding Help
If you post your current code, someone can probably help with what it is or is not doing. -
You would use usort - <?php $individuals = explode("~", $string); function cmp($a,$b){ // compare first three elements of each string x|y|z|... $a = explode('|',$a); // convert the first three elements into a number that can be compared by magnitude $aa = 100* $a[0] + 10 * $a[1] + $a[2]; $b = explode('|',$b); // convert the first three elements into a number that can be compared by magnitude $bb = 100* $b[0] + 10 * $b[1] + $b[2]; if ($aa == $bb) { return 0; } return ($aa < $bb) ? -1 : 1; } usort($individuals, "cmp");
-
I believe the form submission on our website has been hacked....
PFMaBiSmAd replied to JenCollette's topic in PHP Coding Help
Right after the foreach(){} loop code that you replaced, there is now a missing } that corresponds to the else { that is just above that foreach(){} loop code. -
Use nl2br when you output the data.
-
Almost there!! Problems with search engine and pagination page...
PFMaBiSmAd replied to rodhow's topic in PHP Coding Help
I'm not sure is this is the only reason for a blank page (or do you mean just empty data/no data), but you must build the links with the $_GET['keywords'] value on the end of the URL so that each page knows what to search for. If you replace your code, starting at the $range = 3 statement, with the following, it should work - <?php $range = 3; $links = ''; // build links in a string (output it later in your actual content on the page) // if not on page 1, don't show back links if ($currentpage > 1) { // show << link to go back to page 1 $_GET['currentpage'] = 1; // set/replace the pagination GET parameter (all other GET parameters unchanged) $links .= " <a href='?" . http_build_query($_GET, '', '&') . "'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page $_GET['currentpage'] = $prevpage; // set/replace the pagination GET parameter (all other GET parameters unchanged) $links .= " <a href='?" . http_build_query($_GET, '', '&') . "'><</a> "; } // end if // loop to show links to range of pages around current page for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { // if it's a valid page number... if (($x > 0) && ($x <= $totalpages)) { // if we're on current page... if ($x == $currentpage) { // 'highlight' it but don't make a link $links .= " [<b>$x</b>] "; // if not current page... } else { // make it a link $_GET['currentpage'] = $x; // set/replace the pagination GET parameter (all other GET parameters unchanged) $links .= " <a href='?" . http_build_query($_GET, '', '&') . "'>$x</a> "; } // end else } // end if } // end for // if not on last page, show forward and last page links if ($currentpage != $totalpages) { // get next page $nextpage = $currentpage + 1; // echo forward link for next page $_GET['currentpage'] = $nextpage; // set/replace the pagination GET parameter (all other GET parameters unchanged) $links .= " <a href='?" . http_build_query($_GET, '', '&') . "'>></a> "; // echo forward link for lastpage $_GET['currentpage'] = $totalpages; // set/replace the pagination GET parameter (all other GET parameters unchanged) $links .= " <a href='?" . http_build_query($_GET, '', '&') . "'>>></a> "; } // end if echo $links; // echo the links wherever you want in the content on your page The above code only modifies/sets the $_GET['currentpage'] value when building the links, but leaves any other $_GET value as is. It builds the navigation links in a variable so that you can output them anywhere on your page, for instance if you wanted to put links above and below the resultant display of the data. It also corrects the use of < and > characters in the navigation in favor of html entities that won't break the html on the page. -
Add the following three lines of code immediately after your first opening <?php tag on both pages, try again, and report any errors - ini_set("display_startup_errors", "1"); ini_set("display_errors", "1"); error_reporting(-1);
-
This might be a good time to mention that you need to have php's error_reporting set to E_ALL (or even better a -1) and display_errors set to ON in your master php.ini so that php will help you by reporting and displaying all the errors it detects. You will save a TON of time.
-
This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=348558.0