Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
LOL, I didn't even read the query. Shame on me.
-
Just a side comment. It will help you greatly if you write the code that creates the query in a readable format. The way you have it now makes it extremely difficult to 1) see the whole query and 2) confirm that the all the quote marks are correct since it keeps going in and out of the quoted text. Here is how I would write that in my code: $update = "UPDATE users SET (login_email, login_password, confirm, first_name, last_name, address_one, address_two, town_city, county_option, post_code, phone_number) VALUES ('{$email}', '{$pass}', '{$confirm}', '{$fname}', '{$lname}', '{$addressone}', '{$addresstwo}', '{$towncity}', '{$countyoption}', '{$postcode}', '{$phone}') WHERE id = '{$id}'";
-
Why does my unCheckRadios function recall other functions?
Psycho replied to Frank P's topic in Javascript Help
Correction, the reason it is submitting is because that is the ONLY button in the form. I've seen this before. When there is only one button in a form it may act as a submit button even if not defined as one. I don't know if this is a browser specific issue or in the standards. You also have some inefficiencies in the code function unCheckRadios(name,itemId,buttonId) { var radios = document.getElementsByName(name); for (var n = 0; n < radios.length; n++) { radios[n].checked = false; document.getElementById(itemId).style.backgroundColor = ''; document.getElementById(buttonId).style.display = 'none'; } return false; // to prevent premature submitting of the form } 1. Do not use 'radios.length' in the for() statement. It causes the code to recalculate that value on each iteration. It's better to define a variable with that value before the loop and then use that in the for() declaration. 2. Put the lines to change the background color and button style before or after the loop. There's no reason to run it multiple times. -
Why does my unCheckRadios function recall other functions?
Psycho replied to Frank P's topic in Javascript Help
I don't know. I don't see anything in the code that would case a submission - yet it is being submitted nonetheless. I was able to verify that and work around the issue by adding the following parameter to the form tag: onsubmit="return false;" -
Why does my unCheckRadios function recall other functions?
Psycho replied to Frank P's topic in Javascript Help
The form is getting submitted. -
Well, you definitely need to slow down. I see some things in that code that are making things more difficult than they should be. For example this: $_ids = $_POST['ids']; $total = count($_ids); if($total){ for($i=0; $i<$total; $i++){ $_tids .= $_ids[$i] . ","; } Should be replaced with this //Set $_ids to all numeric, non-empty values in the POST array $_ids = array_filter(array_map('intval', $_POST['ids'])); //Check if there were any values if(count($_ids)){ //Create comma separated string of the values $_tids = implode(',', $_POST['ids']);
-
dalecosp is correct on the first point. Your first query is selecting ALL records in order to get a count. That means all the data for those records have to be queried and loaded into memory. A simple COUNT() query will give you the count without all that overhead. Also, the "NOT LIKE" part of the query could create some performance issues as well. It's a pretty simple comparison, but if you still have issues after making the above change you could try using a SUBSTR() comparison AND SUBSTRING(kataloski_broj, 1) <> '1'" Second, you are running other queries in the loop that output the query results to display. Never run queries in loops! You should create ONE query that returns all the data you need. Looking at the main query, you have an if/else that either includes the 'kalkulacija_zamjene_staro' table or not just to check if a particular value is 0 or not. But later you do another query on that table anyway. So, the main query should include that table regardless and the only change should be the check for a 0 in that particular field. In fact, the logic to determine total records doesn't take this into account and would get incorrect results. Not to mention the comparison is an assignment (i.e. uses a single equal sign instead of two) so it would ALWAYS run the first query. Also, this won't improve performance, but will make your app easier to create and maintain. You have a lot of lines where you are formatting a number in a specific way $zarada_diler_p = number_format(round(($row["zarada_diler_post"]),2),2,",","."); Instead of writing the same complicated code on every line, create a function to format the values. That way you are ensured to get consistent results. Otherwise, a simple typo on one line could cause problems that are hard to debug and fix function myNumberFormat($number) { $returnValue = number_format(round(($number),2),2,",","."); return $returnValue; } //Then call it in your code $neto_VPC = myNumberFormat($row["neto_VPC"]); $neto_MPC = myNumberFormat($row["neto_MPC"]); $trosak_firme = myNumberFormat($row["trosak_firme"]); // Etc. . . Think about how much easier it would be to change the format if needed. So, after looking at your code, I think you need three things. 1) A condition to determine if you want to exclude those records where 'ne_koristi_se' = 0 2) A query to determine the total count of records that takes into account item #1 above 3) A single query to get ALL the data for the selected page. I think the code below is correct based upon looking through what you have. But, the logic to output the results from the single query will need to be modified. It contains three sections of code and I left some things out in between (such as calculating the LIMIT). //Determine if the results should exclude 'ne_koristi_se' = 0 records $checked = ($_SESSION["checked"] = "checked") "AND kzs.ne_koristi_se = '0'" : ''; //Create and run query to detemine total number of records $query = "SELECY COUNT(*) FROM kalkulacija_stavke AS ks LEFT JOIN kalkulacija_zamjene_staro AS kzs ON ks.kataloski_broj = kzs.kataloski_broj_stari WHERE ks.id_kalkulacija = '$id_kalkulacije' AND ks.kataloski_broj NOT LIKE '1%' $checked"; $result = mysql_query($query) or die(mysql_error()); $brojcanik = mysql_result($result, 0); //Create and run ONE query to get all data for the page $upit = "SELECT ks.*, kzs.ne_koristi_se, kzs.kataloski_broj_novi, krk.naziv_artikla FROM kalkulacija_stavke AS ks LEFT JOIN kalkulacija_zamjene_staro AS kzs ON ks.kataloski_broj = kzs.kataloski_broj_stari LEFT JOIN kalkulacija_import_kategorija AS kik ON kik.kat_br = kzs.kataloski_broj_stari WHERE ks.id_kalkulacija = '$id_kalkulacije' AND ks.kataloski_broj NOT LIKE '1%' $checked ORDER BY kataloski_broj ASC $limit"; $result = mysql_query($query) or die(mysql_error());
-
Select data where ID not exist in table 2 (Beginner)
Psycho replied to webbhelp's topic in MySQL Help
A normal, or INNER, JOIN will only return results where the records from the two tables being JOINed actually have a reference. So, the records from the Variables table that do not have any records in the Translations table will not be returned because there is nothing for them to JOIN on. A LEFT or RIGHT JOIN will return results even when there are no matches for the JOIN. The best example I can come up with would be an "authors" table and a "books" table. Assume that there are some author records for which there are no records in the books table and that there are some book records with a NULL references to authors. Normal/Inner JOIN SELECT * FROM authors JOIN books ON authors.id = books.authorID This will return the results of every author and their associated books. It will not return any authors that do not have books or any books that have no associated author LEFT JOIN SELECT * FROM authors LEFT JOIN books ON authors.id = books.authorID This will return the results of every author and their associated books. Because it is a LEFT JOIN it will include ALL records from the LEFT table (i.e. authors) even if there is no matching record in the books table. That record will have NULL for all the values from that second table. It will not include any records from the books table that do not have a reference to an author RIGHT JOIN SELECT * FROM authors RIGHTJOIN books ON authors.id = books.authorID This will return the results of every author and their associated books. Because it is a RIGHT JOIN it will include ALL records from the RIGHT table (i.e. books) even if there is no reference to an author id. That record will have NULL for all the values from the first/left table. It will not include records from the author table that do not have any associated books. -
Select data where ID not exist in table 2 (Beginner)
Psycho replied to webbhelp's topic in MySQL Help
SELECT * FROM Variables WHERE id NOT IN (SELECT variable FROM Translations) You can also do this by JOINing the tables as you did above - you just took the wrong approach. You have to do a LEFT JOIN - else the records without a match to JOIN on will not be included. Then filter the records where there is a NULL value SELECT Variables.* FROM Variables LEFT JOIN Translations ON Variables.id = Translations.variable WHERE Translations.variable IS NULL But, I believe the first example is a better solution. If you've never had to use JOINs then you're probably using your tables completely wrong. -
Not quite. The ORDER BY will ensure you are getting the records in order so the newest is first or the oldest is first (based on whether you sort in ascending or descending). But, to get the first record you want to use LIMIT SELECT HIREDATE FROM EMP ORDER BY HIREDATE ASC <-- or "DESC" LIMIT 1
-
To add to AbraCadaver's response, once you properly normalize the tables you can get ALL the information you need for your output in a single query. So, let's say you have the following tables: persons (the table to describe the individual people) workshops (the table to describe the individual workshops) person_workshop (the table to associate people to one or more workshops) You could then run the following query to show all people and the workshops they are signed up for SELECT * FROM persons LEFT JOIN person_workshop ON persons.person_id = person_workshop.persons_id LEFT JOIN workshops ON person_workshop.workshop_id = workshops.workshop_id The LEFT joins will ensure you get a record for every person - even if they don't have any workshops registered. If you don't need those, then remove the "LEFT" from the joins. Also, I just used '*' in the select portion of the query,but you should list out the fields you need.
-
MIN() is another GROUP BY function. This is not a problem to be resolved with grouping. See Barand's response above. He decided to throw you a bone.
-
You want a single record. GROUP BY is used to "group" records to get things such as the SUM, AVG, etc across many records. There are two specific functions you will want to use to get the records in a particular order and to select only the one you want for each query.
-
And what have you tried so far and/or what questions do you have? this forum is for people to get help with code they have written, not for people to do your homework for you. Unless your instructor is a moron I suspect he has recently gone over several different SQL functions recently. Go over those and give it a try.
-
Slightly off topic. Instead of a bunch of OR statements in your condition, I'd suggest creating an array of allowed types and using in_array() $allowedTypes = array('image/jpeg', 'image/jpg', 'image/pjpeg', 'image/x-png', 'text/css', 'image/png'); if(in_array($_FILES["file"]["type"], $allowedTypes)) { //File is allowed } else { //File is not allowed }
- 14 replies
-
- file upload
- php
-
(and 2 more)
Tagged with:
-
@mentalis: I don't think that is what the instructions are asking for. you're describing the solution for data to flow across a row then create a new row after n records. What I am reading is that each row is a complete record with each column representing a different piece of data for that record. @zatox123, what I think you need to do is create what I refer to as a "header" row where each item in that row is a column header that describes the type of data in that column. For this you would use TH tags (e.g. "table header") instead of TD tags ("table data"). Additionally, the header should be created before the data - so it would be before the loop. Give it a try and get back to us with any problems/questions. EDIT: looks like Ch0c3r went ahead and gave you the solution. I was wanting you to try and figure it out yourself.
-
I'm not sure what you trying to accomplish. But, I would venture to say you are doing it in a way that is more difficult than it needs to be. Let me state what I *think* you are doing and how I think it should be accomplished. I assume you are receiving a form of POST data and in that post data there are multiple field sets for various menu options. For each of those menu options you are wanting to create a concatenated list of the POST values into a variable. What you have above should work and is actually preferable (making them array values) instead of making them individual variables - except you are putting the values back into the $row variable which is getting overwritten on each iteration of the loop! Plus, you are not passing the value to the function, you are passing a string.You would want to put the values into a new array. $output = array(); while($row = mysql_fetch_array($result)) { $output[course_menuname] = getAnswers($row['course_menuname']); } But, you can probably do what you need without a DB query at all. Are there any POST fields that are arrays which you do NOT want to be processed in this way? If not, you could simply iterate over the POST values $output = array(); foreach($_POST as $menu => $values) { if(is_array($values)) { $output[$menu] = getAnswers($values); } } Or, if you may have other fields that are in arrays - just put all the menu fields in a parent sub-array: E.g. $_POST['menu']['lunch_main'] Lastly, what are you doing with these concatenated values. I hope you are not storing these in the database. It will make it difficult, if not impossible, to perform queries to search/calculate based upon certain options.
-
And to follow up with my previous response, you should use array_filter() to remove empty values before you concatenate them with the semi-colon. function getAnswers($input) { return implode(';', array_filter($_POST[$input])); }
-
You may have the same issue with preg_replace: http://us1.php.net/manual/en/function.preg-replace.php You may have to resort to doing a loop to iterate over each character.
-
Reread your reply with the emphasis that I've added. FYI: I'm not trying to be difficult, but this is obviously schoolwork, so I want to help guide you to seeing the problem for yourself. You'll learn a lot more that way.
-
Read this bit of code again. // Don't bother doing anything if it's an empty array if (!empty($input)) { return; } Also, in this code, why are you adding unnecessary quotes - i.e. concatenating an empty string? foreach($_POST[''.$input.''] as $results) { Should use $_POST[$input] But, there's no need to even use a foreach() loop. Just implode() the array using "; " as the glue. EDIT: I would give you a one-line solution to solve all those problems, but I suspect this is homework. So, I'll give you this advise. The only PHP functions you need to use are array_filter() and implode().
-
I've already told you - move the files outside the web accesible folders. Then no one can access the files directly through a URL and the only way to get the files is through the download script. What part of my response on Reply #8 was not clear? 1. Move the files to a folder that is not accessible through the web. You should have a root folder for your website. The folder for the files should be on the same level or above that folder. If this is on a shared host and you created your site in the root of the folder your host provided, then you need to put your site in a subfolder and configure the domain to point to that subfolder. Then the root folder your host provided is no longer web accessible. 2. Change the readfile() function to read the files in the new folder location. NO REWRITE/HTACCESS CONFIGURATIONS ARE NEEDED.
-
Well, that's not really your code is it? Pretty sure that last line would cause a syntax error. It's typically a not a good idea to pass the file name as a parameter since you will have to deal with spaces and other disallowed characters. But, using that same code, you can simply put the files in a folder that is not publicly accessible as I stated before. Not knowing what folder that script is located in I can't say for sure what the relative path would be. But, for arguments sake, let's say the folder 'download_files' that is up two levels from where the script download.php is located. In that case just change the last line to this readfile('../../{$file_name}.pdf");
-
You never answer my question.
-
looping through array, compare dates if duplicate ID
Psycho replied to jenkins's topic in PHP Coding Help
That would only work if you are 100% certain that the last entry for any duplicate dates contains the value you want. Since that wasn't stated as a known prerequisite I coded it to always ensure the last date would be the one set for each id.