Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
The tutorials are still there - just hard to find: http://www.phpfreaks.com/tutorial/basic-pagination This site has been pretty much converted to a forum only existence.
-
For pagination you will want to run two queries. One to get the records for the current page and a second to get the total number of records across every page. The only one-query solutions I can think of would be less efficient: 1) query all the records and then filter the records in PHP (pretty stupid method) or do some type of sub-query that is joined to the results. But, again, that would be less efficient.
-
Are you really looking at the errors? I'll go back to the first code you posted. You received these two errors: This is line 68 while ( $row = mysql_fetch_array($result)) { And you say: The error was about the variable $result not being defined - that caused both errors. You assigned the DB results to a different variable and used the wrong one in that line.
-
Forgive me for being skeptical, but I've had similar situations before and there is always a logic explanation. Can you try the two lines I showed above EXACTLY as I put them - one immediately before the if() statement and one immediately after? In fact, I would suggest echoing the values of the variables used in the if() condition as well. Give this a try and post the results: echo "watchPost before if() statement: {$watchPost}<br>\n"; echo "watchTime before if() statement: {$watchTime}<br>\n"; echo "arrKey before if() statement: {$arrKey}<br>\n"; echo "thread[arrKey]['puretime'] before if() statement: {$thread[$arrKey]['puretime']}<br>\n"; if( $watchTime < $thread[$arrKey]["puretime"] ) { echo "watchPost after if() statement: {$watchPost}<br>\n"; echo "watchTime after if() statement: {$watchTime}<br>\n"; echo "arrKey after if() statement: {$arrKey}<br>\n"; echo "thread[arrKey]['puretime'] after if() statement: {$thread[$arrKey]['puretime']}<br>\n";
-
This topic has been moved to PHP Freelancing. http://forums.phpfreaks.com/index.php?topic=361804.0
-
How did you confirm that $watchPost is changing value after the if statement? Did you try something like this echo "watchPost before if() statement: {$watchPost}<br>\n"; if( $watchTime < $thread[$arrKey]["puretime"] ) { echo "watchPost after if() statement: {$watchPost}<br>\n"; I don't see anywhere in that code that $watchPost is actually used in output, so I'm curious how you verified that hypothesis. I don't see anything in the code that would cause that so there is probably some other explanation. It would be a good idea to echo all/most of your variables to the page at key points in the logic to verify they contain what you think they contain.
-
It's really difficult to follow the flow of your code because it keeps going in and out between HTML and PGP plus there is no structure to the PHP code - i.e. indenting of the code to show the logical structure. Personally, I would suggest separating the logic from the presentation as it makes for much cleaner code and these things are much easier to debug. (I'd also note I see the use of GLOBAL a lot which also points to poor practices) But, I would bet that somewhere in that code you have queries being run in loops. Running many queries is always a bad idea, Most likely you can combine some queries using an appropriate JOIN to get the same results with just a single query. But, YOU can easily find where the problems are by commenting out sections until the problem disappears. Then identify the section that is causing the problem. THEN you can post just that section to make it easier for us to help.
-
Yeah, right? The array WAS begin passed to the function and that function tried declaring a global instance of the passed array. Makes no sense whatsoever.
-
Well, as to the original problem: it is due to variable scope. The variable $numsarray is declared outside the function and then inside the function it attempts to reference that variable - which doesn't exist inside the function. Thus the error: The array passed to the function is named as the parameter $a and that is how it should be referenced inside the function.
-
And to build upon the array example, it is a simple operation to create a query to update all the records in mass. Just use IN within the query's WHERE clause and implode the IDs. Of course you should run the array through a process to force them to be ints to prevent SQL injection. //Force array values to integers and remove 0 values $submittedIDs = array_filter(array_map('intval', $_POST['check'])); //Convert to comma separated string $IDlistStr = implode(', ', $submittedIDs); //Create UPDATE array $query = "UPDATE table_name SET fieldname = 'somevalue" WHERE id IN ($IDlistStr)";
-
EDIT: Barand beat me to it, but I'll submit this nonetheless since I included some add'l info. Also, you *could* have done the subtraction in the query. The problem was that you were creating the value as a string by putting quotes around the mathematical expression: UPDATE users SET bonusp = 'bonusp-$bonus' WHERE id='$user->id' Therefore the value inside the quote marks would be something like '5-2Normally you can put a number inside quotes for input into a numeric field, but it's not really proper. It is only because the MySQL engine can interpret something like '5' as a number. But, if you do any mathematical expression in quotes the MySQL engine cannot resolve that string value into a numeric value. This is no different than how PHP would have handled that: $var1 = '5'; $var2 = '5 - 2'; //This will not be evaluated $var3 = 5 - 2; //This will be evaluated echo $var1; // 5 echo $var2; // 5 - 2 echo $var3 // 3 So, you could have just changed the query to [color=#0000BB]mysql_query[/color][color=#007700]([/color][color=#DD0000]"UPDATE users SET bonusp = bonusp-[/color][color=#0000BB]$bonus[/color][color=#DD0000] WHERE id='[/color][color=#0000BB]$user[/color][color=#007700]->[/color][color=#0000BB]id[/color][color=#DD0000]'"[/color][color=#007700])[/color]
-
Retrieving posts and threads then sorting by date
Psycho replied to MySQL_Narb's topic in MySQL Help
Barand only provided a 'sample' query based upon what would be considered a common table structure. We can't help if you aren't going to provide the information for us to do so. A proper database query is going to be different based upon the actual table structures involved. If you are not able to modify the sample query for your particular need then provide examples of the queries you are currently using. -
I'd agree. The other issue I see with this problem is that it instills the assumption that in these types of situations you need to store the results in an array in order to do these types of operations. In this case you could just as easily create the output for the result of each die within the loop and add to a total variable. Then it's a simple operation to generate the average at the end. Far too often I see people loop through database results and dump them into an array only to later loop through the array to actually use the results.
-
How to check first 2 letter of string is capital or not
Psycho replied to shahzad429's topic in PHP Coding Help
Totally forgot about ctype_upper() - good call. -
How to check first 2 letter of string is capital or not
Psycho replied to shahzad429's topic in PHP Coding Help
And why not just use a comparison using strtoupper()? function checkFirstTwo($string) { $firstTwo = substr($string, 0, 2); return ($firstTwo == strtoupper($firstTwo)); } -
Well, I could see where one would expect 3.9 to produce 4 full stars. Otherwise, it would be impossible for an item to have 5 full starts (assuming 5 is the max) if there is even one rating less than 5. I would round the value to the nearest 1/2 value. Then assuming the "stars" are images you need to output the full, half and stars. You could use multiple images with all five stars together and simply name them using the numeric value: star_0, start_0.5, star_1.0, etc. Or you can do the logic in a loop, i.e. for $i=0 to 5 and do a comparison on each iteration. But, I think single images for all five stars is the easiest.
-
Well, I wasn't certain, so I tested it. You can do a comparison of database timestamps fields using +/- seconds, but you cannot do that for fields set as date types. I didn't check the datetime field type. You should test that the comparison is working as you think it should.
-
The query I posted to get the username AND the total points is where you need to go back to. If something isn't working right then let's figure that out. Don't go and create a "flat" query to get the data from the betting table then ask how you get the username. I already provided that. I don't have your database and am not going to take the time to ensure all the field names in the query are correct. Ok, first off if you have a variable for "bonus" the value should NOT be 'yes'. When you use text as values for those sorts of things you have to add logic to interpret then. Normally you should use the logical TRUE/FALSE values, but in this case I would suggest using 1 or 0 - which can also be interpreted as true/false. But, that way you can also use the "bonus" value directly in your UPDATE query with no elaborate coding to handle the logic: UPDATE betting SET win_amount = 35 * ($bonus +1) WHERE fight_id='$fight_id' AND fighter_id = '$winner' AND round = '$round' AND method = '$method' So, if $bonus = 0 (i.e. bonus NOT enabled) then the set value will be SET win_amount = 35 * (0 +1) 35 * (0 +1) = 35 * (1) = 35 If $bonus = 1 (i.e. bonus IS enabled) then the set value will be SET win_amount = 35 * (1 +1) 35 * (1 +1) = 35 * (2) = 70
-
OK, if you are simply awarding a flat amount of 23, 25, 10, etc. then the query to update the betting table would not have a multiplier. Here are the queries to update the betting table with the win amounts. UPDATE betting SET win_amount = 35 WHERE fight_id='$fight_id' AND fighter_id = '$winner' AND round = '$round' AND method = '$method' UPDATE betting SET win_amount = 25 WHERE fight_id='$fight_id' AND fighter_id = '$winner' AND round = '$round' AND method <> '$method' UPDATE betting SET win_amount = 25 WHERE fight_id='$fight_id' AND fighter_id = '$winner' AND round <> '$round' AND method = '$method' UPDATE betting SET win_amount = 20 WHERE fight_id='$fight_id' AND fighter_id <> '$winner' AND round = '$round' AND method = '$method' UPDATE betting SET win_amount = 15 WHERE fight_id='$fight_id' AND fighter_id = '$winner' AND round <>'$round' AND method <> '$method' UPDATE betting SET win_amount = 10 WHERE fight_id='$fight_id' AND fighter_id <> '$winner' AND round = '$round' AND method <> '$method' UPDATE betting SET win_amount = 10 WHERE fight_id='$fight_id' AND fighter_id <> '$winner' AND round <> '$round' AND method = '$method' That may not be the most efficient, but 7 queries as opposed to an unlimited amount of looping queries is definitely better. The to get the user point totals you would just run something like this SELECT u.name, SUM(b.win_amount) as total_points FROM users AS u LEFT JOIN betting AS b ON b.user_id = u.id ORDER BY total_points
-
I'm going to venture a guess that the site was built with some code that assumes the site is running at the root of the website even if you do set the path in the config using $config['base_url'] = 'http://organiclife.com/public/CLFarms/'; Ideally, that shouldn't be the case. But,if you are going to run the site from the root as I would suspect, there is another solution. Depending upon your host you can set up different web roots for different domains or even sub-domains. If so, you can set up a sub-domain and point it to a different folder. For example set up the sub-domain dev.organiclife.com and point it to a specific folder and dump your files there. I would suggest a completely separate folder rather than a sub-folder of the main site to keep everything separate.
-
You are making this too difficult. I wouldn't even have a "results" table and instead would simply add fields to the "fights" table for winner id, round, etc. I was going to suggest that you only determine a user's amount dynamically, but I see that you are awarding different amounts based upon different criteria. If you were to only calculate the total dynamically and were to change the awarded amounts at a later time it would throw everything off. But, you still have two choices. You can update the user's "points" directly as you are trying to do, but then you lose the history of which bet provided what amounts. I would suggest the following: You need one table to track add/removes of user points that take place outside of bets. I would assume there must be some manner to do this otherwise how do new users start and what do they do with their points? Second, use the current bets table to track all transactions with regard to bets. You could have one column for the bet amount and another column for the win amount (The default is 0). Then, when a match is completed update the win amount as needed. Lastly, you just need an appropriate query to get a user's total points. This format allows you to easily pull a history of every addition/subtraction to a users points as needed. OK, to update the "betting" table at the end of a match you could run ONE query to update all the appropriate records. You can put an IF statement in the UPDATE clause for all the different amounts. but, that gets difficult to read. I would create a few different queries for each award amount. Here is an example of one of the queries to update the betting table with win amounts after a fight. You would need an appropriate query for each award amount. UPDATE betting SET win_amount = bet_amount * 35 WHERE fight_id='$fight_id' AND fighter_id = '$winner' AND round = '$round' AND method = '$method' Then to get the total points for a user you need to pull data from the betting table as well as the "inventory" table where points are added/subtracted outside of betting activities. How this is done would be based upon how much data is needed. You could do this through a UNION to merge data from the two tables into a combined results set.
-
OK, if you look at my signature I do state Typically I am providing "logic" to solve a particular problem. In these instances I expect the recipient to put forth some effort to fix any minor display/output type issues. A quick look at the function to create the links shows that an ending </a> tag was omitted. Aside from that I also see what is probably another bug if different products are returned. But, to test it would require me to set up a database and test data - which is more work than I am willing to invest. But, I will provide some mock code for you to figure out what you want. The main thing to figure out is EXACTLY how you want the output displayed for multiple companies. If you can write out the HTML exactly as you would expect it to be dislayed when there are two companies associated with a product I can provide some code.
-
@seventheyejosh: The code you posted is checking every value/column of the sub-arrays and returning true/false for each sub-array. The OP stated he wanted to check
-
I'm curious. If you have a defined list of image_types then would you have records in your image table that do not have one of those defined types? Or, are the types in the array a subset of all the types (that would make sense)? Also, having a hard-coded array of types will make any changes much harder to implement and it will have a greater potential for bugs since a single typo in one type can go unnoticed unless you did testing for each and every type. A better solution, IMHO, is to have a separate table for the types and then use the id for those types in your current table. Assuming the list above is only a subset of all the types that you want to use for that particular query, the types table might need a field to identify that subset. Here's an example id | type | admin 1 Managing Director 1 2 Manager 1 3 Director 1 4 The Principal 1 5 Teacher 0 6 Custodian 0 Then using the same type of logic Barand provided you can simplify things and, more importantly, you can add/delete types without any changes needed to the core code SELECT .... FROM mytablename WHERE type IN (SELECT id FROM types WHERE admin=1)