Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
Your statements above are confusing and have no meaning. You are explaining what you need based upon someone who is working on the project (i.e. it makes sense to you but not to anyone else). Try taking a step back and explaining it to someone as if they are not working on this project. The only thing I can come up with from what you have stated above is that you are probably making it a LOT harder than it needs to be. For example, you have a variable called 'options' which apparently refers to the number of options ('option1', 'option2') that are passed on the query string. That's not necessary. You can determine the options programmatically. You should instead label the options differently. If the numerical index of the options is not important, then just label all the options as 'option[]'. If the numerical index is important than name them as 'option[1]', 'option[2]'. You can then determine the number of options using count($_GET['option'])
-
How is this a PHP Coding Question? The "algorithm?". Well, that would depend on what "red" and what "green" you want to use. It would be presumptuous for me to assume that you want to use the red "FF0000" or the green "00FF00". That red is probably a safe bet, but the "full" green is a pukey lime green. A better choice might be something closer to a hunter green such as "008000". Assuming those are the two colors you will use. The red parameter goes though the full 256 values and the green goes through 128 values. 10,000 / 256 = 39 10,000 / 128 = 78 So, starting with 00FF00"" for 0 units, for every 39 units you would subtract 1 from the red component of the color code and for every 78 units you would add 1 to the green component. I'll let you figure out the programming.
-
Then that value is not getting passed to the page. What is the full URL that is displayed in the browser when you access this page?
-
My array had ~ 650 elements. It means it has to calculate count($array) 650 times, instead of once and then saving it to the variable. More importantly, in this specific implementation, the bigger problem was that count() of the array would change on each iteration which would cause the loop to exit prematurely. To make it simpler. Let's say we had an array of 10 elements (0-9) and the loop will remove every odd element starting at 1 for($i = 1; $i < count($array); $i = $i+2) { unset($array[$i]); } On the first iteration $i=1 and the count=10, so the item at index 1 is unset On the second iteration $i=3 and the count=9, so the item at index 3 is unset On the third iteration $i=5 and the count=8, so the item at index 5 is unset On the fourth iteration $i=7 and the count=7, the for loop is exited since $i is not < the count of the array. Both the indexes at 7 and 9 should have been removed, but they would not be since count() is being calculated on each iteration. But, as already stated it is poor standard to put a calculation in the parameters of the for loop that are calculated on each iteration - if they really shouldn't be calculated. There are instances where doing so would make sense. This just isn't one of them.
-
You should not use "count($array)" inside the for() loop: 1) it would need to be executed on each loop and is not efficient. 2) It will cause the function not to work properly! On every iteration the count() is changing and would cause the loop to exit prematurely. That is also why running separate times fo the 8's and the 9's would also be problematic. function filter_keys(&$array, $n, $offset) { $arrayCount = count($array); for($i = $offset; $i < $arrayCount; $i+=$n) { unset($array[$i]); unset($array[$i + 1]); //For a more generic function, remove this and then call the function twice. } }
-
It would be possible, but I doubt it would be 'quite easy'. Which is probably why people want compensation for taking to build something that is not easy. Just saying
-
SELECT p.Week, t1.teamname, t2.teamname FROM pairings AS p JOIN teams AS t1 ON p.Pairing1 = t1.teamid JOIN teams AS t2 ON p.Pairing2 = t2.teamid ORDER BY p.Week
-
Complicated database and PHP ordering and combining
Psycho replied to rockinaway's topic in PHP Coding Help
So, do this. Create one table called "updates". That table would have columns for user id, an update date, update type and update id. You can use that table as an intermediary between the user and the various "updates" in the four other tables! Then those four tables can store the data that is unique to each update type. The query becomes much easier then. Something like: SELECT * FROM users JOIN relationships on users.id = relationships.user_id JOIN updates ON relationships.user_id = updates.user_id LEFT JOIN audio_updates ON updates.update_type = 'a' AND updates.update_id = audio_updates.id LEFT JOIN video_updates ON updates.update_type = 'v' AND updates.update_id = video_updates.id LEFT JOIN text_updates ON updates.update_type = 't' AND updates.update_id = text_updates.id LEFT JOIN other_updates ON updates.update_type = 'o' AND updates.update_id = other_updates.id ORDER BY updates.update_date -
Complicated database and PHP ordering and combining
Psycho replied to rockinaway's topic in PHP Coding Help
Hmm... I would probably have a better idea if I understood what is being stored in the tables for the updates. Do you really need separate tables for them? It would be much easier if you had one table for the "updates" and used a column in that table to indicate the "type" of update. However, it can be done with the separate tables, it is just going to be more complicated. It will be hard to provide an explanation without some context to the actual tables and their structure, but I'll try. It seems you want the data from the "updates" tables to be ordered by their timestamp. But, if all the data is coming from separate tables if you just do JOINS the timestamps will be in separate columns and you can't order all the associated records as you want. So, you need to pull data from the Users/Relationship table a you normally would and then pull data from the "updates" tables as a single result associated with the first - to do this you would use UNION. Here is a mock query that could possibly work. SELECT users.*, FROM users JOIN relationships ON users.id = relationships.user_id LEFT JOIN (SELECT user_id, activityName, activityDate FROM activities UNION SELECT user_id, userupdateName, updateDate FROM updates UNION SELECT juser_id, obsName, jobsDate FROM jobs UNION SELECT user_id, othersName, othersDate FROM others ) as events ON events.user_id = relationships.user_id OR events.user_id = relationships.friend_id But, to be honest I doubt that actually query would work. I would need the tables and to do some testing to figure out the right structure. -
Complicated database and PHP ordering and combining
Psycho replied to rockinaway's topic in PHP Coding Help
I'm not really following your explanation. Can you give some sample data from the tables and how you want it returned/displayed? -
echo "<table border='1'>\n"; foreach($fansubarray as $name) { echo "<tr>\n"; echo " <td>{$name['FansubID']}</td>\n"; echo " <td>{$name['Designation']}</td>\n"; echo " <td>{$name['Website'}</td>\n"; echo " <td>{$name['tracker'}</td>\n"; echo " <td>{$name['irc']}</td>\n"; echo "</tr>\n"; } echo "</table>\n";
-
If you are new, then you should be reading the manual or some tutorial. Anyway, you can have an if() statement by itself if(1==1) { echo "foo"; } echo "bar"; //Output: foobar You can have an if() statement with ONE optional else statement if(1==2) { echo "foo"; } else { echo "bar"; } //Output: bar You can have an if() statemetn with multiple elseif() conditions and ONE optional else statement if(1==2) { echo "foo"; } elseif(1==1) { echo "bar"; } else { echo "nada"; } //Output: bar You cannot have an if() statement with multiple else statements if(1==2) { echo "foo"; } else { echo "bar"; } else { echo "nada"; } //Output: ???? will create error Here is your previous code with some modifications and comments highlighting the problems if ($row = mysql_fetch_assoc($query)) { while ($row = mysql_fetch_assoc($query)) { $dbusername = $row['username']; $dbpassword = $row['password']; } if ($username==$dbusername&&$password==$dbpassword) { echo "du er logget ind $username" } else //This else corresponds to the previous if() statement - OK echo "Adgangskoden er Forkert"; //This curly brace and the following else do not correspond to //any opening brace or if() statement } else die("Denne burger findes ikke"); } else //This else corresponds to the if() at the verytop of this code - OK die("Angiv venligst brugernavn og adgangskode"); You did a pretty good job of structuring your code by indenting it, but didn't quite get it right. Had you been more careful with the formatting the errors shoudl have been obvious. You also appear to have some closing curly braces (}) that don't have a corresponding opening brace. I see you don't enclose the line of code following the else statements in curly braces - and technically you don't have to if it is only a single line of code. But, IMHO, it is much easier to read the code when you always do the same thing. So, I always enclose my code-blocks after an if/elseif/else conditions within curly braces. Also, that while() loop makes no sense. You are simply redefining the variables in the loop and not doing anything with them - in the loop. So, you are left with the values from the last record only. I have to assume you expect only ONE record in the query results. If so, test for one record and don't use a while() loop Lastly, I think your logic is completely wrong. You don't show the query, but I assume the query is looking for records matching the username and possibly the password. So, why are you checking the returned values from the query to the username and password? The query will do that for you. You only need to check if a record was returned. The problem with checking the DB values against the user entered value is that the username will need to be an EXACT match (i.e. "myname" != "MyName"). Your DB should likely be set to do case insensitive matches. Without seeing some of the previous code I can't be sure, but here is how your logic could look. $username = trim($_POST['username']); $password = trim($_POST['password']); if(empty($username) || empty($password)) { echo "Error: Username and password are required"; } else { //Escape and prepare input values for query $username = mysql_real_escape_string($username); //Run password through your hashing method (never store in plain text) $password = md5($password); $query = "SELECT username FROM users WHERE username='{$username}' AND password='{$password}'"; $result = mysql_query($query) or die (mysql_error()); if(!mysql_num_rows($result)) { echo "Username and/or password are incorrect. } else { echo "Login passed!"; } }
-
MasterAce14 already identified the problem with your DELETE problem. You are creating a loop and defining $delete_id, but in the query you are using $grade_id. But, there is another issue as well. Here is your current code: if (isset($_POST['remove'])) { foreach($_POST['delete'] as $delete_id) { $query = "DELETE FROM grades WHERE grade_id = $grade_id"; mysqli_query($dbc, $query) or die ('can\'t delete user'); } } 1) First off, we know you are referencing the wrong variable. 2) Why do you do an isset check on 'remove' then use the post value for 'delete'? What if 'delete' doesn't exist or is not an array? You only need to check 'delete'. 3) You should never run queries in loops. They are a huge performance hog. You can delete ALL the records with a single query by using IN. 4) You are performing no validation of the id's being passes Here is what I would do if (isset($_POST['delete']) && is_array($_POST['delete'])) { //Convert values to ints to prevent injection and convert to comma separated string $delete_ids = implode(',' array_map($_POST['delete'], 'intval')); //Create ONE query to delete all selected records $query = "DELETE FROM grades WHERE grade_id IN ($delete_ids)"; mysqli_query($dbc, $query) or die ('can\'t delete user'); }
-
Use CODE tags when posting code. You have three else statemetns in succession, but I don't see three if statement to match those up with.
-
And the problem is? What is it supposed to do that it isn't? Errors you are getting? What? EDIT: It does look like your If/Else conditions are out of whack. Could you provide comments in English for the echo/die statements so we can understand what the expectations are for those branches
-
To quote someone else from today "I hate to turn your posts back at you, but . . ." His example shows the scores sorted in reverse order. If the scores are the values then the function to use would arsort() which will sort the values in reverse order and maintain index association. however this might be an example array.. we are not sure if the array will be in reverse order everytime.. I like choosing the order manually here, however if the array is always in reverse order.. then yes arsort() would be a good option.. The OP gave an example of how the records were to be sorted. The only thing that is sorted in that example is the score - in reverse order. I am not going to make wild conclusions based on information that the OP did not provide. Even, ManiacDan had suggested using a reverse sort, he just goofed and provided a function that would sort by the name instead of the score, which is clearly not what the OP is looking for.
-
Now you removed the ending double quote for the onclick/action handler. You need to enclose the onclick/action value within double quotes. Then the value inside that in a javascript window.location with it's own value that should be in single quotes (not you can swap which ones use single vs double quotes. echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onClick=\"window.location='[url=http://www.trucksterusa.com/post_reply.php?cid=]http://www.trucksterusa.com/post_reply.php?cid=[/url]{$cid}&tid={$tid}'\" /><hr />";
-
I forgot a closing single quote right before the closing double quote. Double check the actual html created. It should look something like this: <input type='submit' value='Add Reply' onClick="window.location='http://www.yahoo.com'" />
-
I believe you need a full url for windows.location. It is changing the URL in the browser and it would have no reference for the domain to use (unlike a normal link). Also it would be a good idea to enclose the value of window.location in quotes Try this (modifying the root url as needed) echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onClick=\"window.location='http://www.mysite.com/post_reply.php?cid={$cid}&tid={$tid}\" /><hr />";
-
To quote someone else from today "I hate to turn your posts back at you, but . . ." His example shows the scores sorted in reverse order. If the scores are the values then the function to use would arsort() which will sort the values in reverse order and maintain index association.
-
Well, if you don't use a case, you are stuck with running multiple queries in a loop - which is very bad for performance and scalability. Personally, I try to refrain from creating pages where you can update multiple records concurrently and instead give a list of records with a button/link for each record to go to a page to edit that individual record.
-
memory_get_usage() may not tell the whole story since it only shows memory usage by PHP , not the database. But, it may be useful nonetheless. Just out of curiosity, what is the purpose of the WHERE condition. Primary key IDs typically start at 1, do you actually have IDs that are less than 1? In addition to the suggestions by DavidAM, are you sure you aren't running the query (or any other queries) in a loop?
-
Here is a revise of your previous code implementing the changes needed. Also, I separated the "logic" from the "presentation". It helps to put all your processing logic in the head of your document and then put the output (i.e. presentation) in the bottom of the script (or even another file). <?php $functionsList = ''; if(!empty($occupations)) { foreach($occupations as $occupation) { $id = $occupation['companyid']; $name = $occupation['companyname']; $function = $occupation['function']; $functionsList .= "<div class='formElement'>\n"; $functionsList .= " <div class='formFieldLabel'><label for='occupation_{$id}'>{$name}</label></div>\n"; $functionsList .= " <div class='formField'>\n"; $functionsList .= " <input id='occupation_{$id}' type='text' name='occupation[{$id}]' value='{$function}' />\n"; $functionsList .= " </div>\n"; $functionsList .= "</div>\n"; } } ?> <h3 id="job_function">Job Functions</h3> <?php echo $functionsList; ?> Then your processing logic could look something like this: <?php if(isset($_POST['occupation'])) { //Create array to hold the update values $updateValuesAry = array(); //Loop through the post data foreach($_POST['occupation'] as $id => $function) { //Parse input data $id = intval($id); $function = mysql_real_escape_string(trim($function)); //Check if record is valid if($id==0 || empty($function)) { //Update record is invalid - need to insert error handling code. You can: // 1. Skip the record entirely // 2. Don't process any records and provide error message // 3. Only process valid records (and provide error message) // 4. ??? } else { //Add update clause to array $updateValuesAry[] = "WHEN '{$id}' THEN '{$function}'" } } //Create one update query for all records $updateValuesStr = implode("\n", $updateValuesAry); $query = "UPDATE `table_name` SET `function` = CASE `companyid` {$updateValuesStr} ELSE `function` END"; $result = mysql_query($query); } ?> Note: none of this is tested, but the logic is valid
-
Since you state you already have records in the database, you need to provide the database primary key (i.e. the record id) as part of the data being sent via POST otherwise you have no way to determine which filed should update which record. Do not use seaparte fields (one for id and another for the input). You can incorporate both into a single input field as follows: echo "<input id='occupation_{$i}' type='text' name='occupation[{$occupation['companyid']}]' value='{$occupation['function']}' />"; If you want to allow the user to update multiple records on one page, then you have no choice but to run an update query for all the records because you have no way of knowing which ones the user updated. You can run all the updated via a single query using the MySQL CASE statement.
-
OK, your graphical example changes some of my earlier assumptions. As I stated, the code I provided was only a proof of concept. I stated what the exact logic is, so it would only be a matter of modifying that logic to incorporate the process above. I've already provided a proof of concept with a good deal of logic from scratch. So, I'm not going to rewrite it based on this new information. This forum is for people who want help with code they have written - so I have already dome more than what should be expected. But, I will be happy to provide some guidance on how you can proceed. If you want me to build the logic for you, contact me via PM and we can work something out. So here is how I would modify the previous code: 1) The input data will need to include a radius for each station. 2) In the processing of the drill positions you would change the logic to first test if the drill position is within the radius of the station. 3a) If no: skip that station and test the next (start over with next comparison) 3b) If yes, check if the drill position was assigned to a prev station 4a) If no, assign the drill position to that station (start over with next comparison) 4b) If yes, check the priorities of the two stations. 5a) If prev station had a higher priority (start over with next comparison) 5b) If this station has a higher priority, assign the drill position to current station (start over with next comparison) 5c) If both stations have the same priority, assign drill position based upon current count of drill positions for the two