Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
Sounds like you are trying to solve the wrong problem. Why are you allowing the same user to "like" the same status multiple times?
-
Also, when defining strings with variables, I prefer to use the double-quotes (as you have done) and include the variables {inside} the quoted string. Always going in and out of the quotes and concatenating content makes it harder to see these errors. Also, how you are defining the limit value doesn't look correct. It looks like you are putting the numerical values in quotes and also trying to do a subtraction. These should not be in quotes. You could probably do the subtraction, but would probably want to put it in parenthesis. But, I would do the match before creating the query. $limitStart = $_POST['QUARTER'] - 13; $query="SELECT WeekNo.WNo, WeekNo.WCom, Countries.Country, ctryvisits.CVisits FROM ctryvisits LEFT JOIN Countries ON ctryvisits.country = Countries.CID LEFT JOIN WeekNo ON ctryvisits.WNo=WeekNo.WNo WHERE Countries.Country = '{$_POST['Country']}' ORDER BY ctryvisits.WNo LIMIT {$limitStart}, {$_POST['QUARTER']}"; By the way, this is wide open to SQL Injection. Definitely look into prepared statements.
-
Well, you have problems with that current code, but that aside $lineNo = 0; foreach ($_SESSION["products"] as $cart_itm) { $product_code = $cart_itm["code"]; $results = $mysqli->query("SELECT * FROM products WHERE product_code='$product_code' LIMIT 1"); $obj = $results->fetch_object(); $lineNo++; echo "{$lineNo} {$obj->product_name}<br>\n"; Or, you could put the output into an ordered list
-
This is important. Do you have a table with all the possible values of "cilindro"? If not, and there is any value where the quantity for all products is 0 - there would be no records in the result set.
-
Your images are broken. I assume you have some type of "property" to use as the column headers that specifies some type of description for the records and you want to show the quantity in stock for each item by that property. Something like this? Name | Blue | Green | Red | Yellow --------------------------------------- Item 1 | 5 | 6 | 6 | 17 Item 2 | 2 | 6 | 0 | 9 Item 3 | 12 | 13 | 7 | 4 Item 4 | 4 | 6 | 0 | 3 Item 5 | 0 | 6 | 3 | 23 (with a possible "total" column at the end). If that is correct,then I assume it is possible for some records to have a 0 quantity for some columns. Are the columns "fixed" or would you be retrieving them from the database? Your current query is grouping by the item_id. To do the above we would also need to group by the column header value. What field would represent the column headers?
-
update and insert a record depending on row existence
Psycho replied to mythri's topic in PHP Coding Help
I'm not going to try any decipher your code and provide a solution. But, from your explanation, what you want is to use a single INSERT query with the ON DUPLICATE KEY UPDATE clause. This allows you to execute one query which will either insert a record if no unique constraints exist or update an existing record if there are unique constraints. You stated So,the page_id field in the table should be set as a unique field in the DB schema. You can then run a query such as this INSERT INTO access_level (page_id, department, position, active) VALUES (5, 3, 4, 1) ON DUPLICATE KEY UPDATE department=VALUES(department), position=VALUES(position), active=VALUES(active) The part after the "ON DUPLICATE KEY UPDATE" may be confusing. Let me explain: department=VALUES(department) This basically says set the value of the field department to the value that was defined for department in the VALUES clause. This way, if you were building a query, you don't have to insert the same variable data multiple times. -
What are you using to edit your files? Are you copy/pasting code it? The problem is your quotes! When coding in PHP you can either use the strait single quotes or the strait double quotes to define strings. These are the strait quotes: ' and " You cannot use the angled quotes when defining strings. These are types of angled quotes: ”, “ and ‘ In the code you posted, the string you are trying to define on that line is using angled quotes.
-
I goofed. That code should have been $var = trim($_POST['foo']); if($foo == '') { //Invalid input }
-
I would trim() the value first - then do an empty() validation. But, that can be problematic too because empty() will return true for values such as "0", "0.0". If the character "0" is a valid input, then using empty() would flag the value as invalid. If "0" or "0.0" is not considered valid, then you could use it. But, if you have a field that can contain any number of arbitrary characters (at least one) then you could simply do this $var = $_POST['foo']; if($foo == '') { //Invalid input } But, I typically only have a simple non-empty check for things such as a name field. Many fields (email, age, dates, etc.) will have much more validation anyway.
-
@hansford: Why check if $_POST['add'] is empty() then trim() it? A value of any non-printable character, e.g. a space, would pass the empty() check but would then be empty when added to the array. Also, I'm not sure the condition to check if array_push failed is appropriate. Per the manual array_push() returns So, if nothing was added and the array was not empty to begin with it would pass that condition even if nothing was added. In this specific case that might work since we are always starting with an empty array, but it would seem to be bad practice.
-
Duplicate every row of table AND also change one field?
Psycho replied to galvin's topic in MySQL Help
I have to ask the obvious question: why do you need to do this? If you're duplicating everything except the weeknumber it seems there is a flaw in how you are using the data currently. There is a very simple solution, but I suspect that this is a band aid for a different problem. So, I'd like to know I am not facilitating bad practices before allowing you to continue to go down the wrong path. -
populating data from DB in the 2nd listbox depending on 1st listbox
Psycho replied to jessentha's topic in PHP Coding Help
Well, I would say you *may* need to use AJAX. You really have three options: 1. Create a process for the user to select an option in the first select list and do a submission to have the page release with the first selection and the second select list populated with the relevant values. This isn't a great user experience, but it is easy and a good place to start since you need all the same code/logic as a base. 2. Create a JavaScript only solution. If the total amount of data for the primary and secondary options is not humongous, you can extract all the options from the database when the page loads and write the data into JavaScript variables. Then you can control the select list using just JavaScript. The benefit of this is that the functionality will not be impacted by any momentary connectivity delays. But, if the amount of data is huge, it could slow the time for the page to load. 3. Use AJAX. You would use the same JavaScript logic to manage the select list, but instead of getting the data from local JavaScript variables, make a request to the server to get the list of values for the 2nd select list when a change is made, For options 2 & 3 I would highly suggest using JQuery. There are plenty of 3rd party plug-ins for JQuery to do what you want. Here is one example: http://www.appelsiini.net/projects/chained/demo.html -
So, you want us to just write code to build all new functionality for you? This is not simply a matter to changing a few lines of code in that script. The functionality you are asking would require all new code. It is not "hard", but it is not trivial either.
-
Texan78, 1) Just because values of 0.95 are not valid today, does not mean they would not be valid in the future. Why write code with obvious gaps when you can write it one time that will not break in the future if the input values were to be more precise as some later point. It is simply bad practice. 2) Yes, your code "works", but it is working by accident. I tried giving you a very detailed explanation of why that is so. Just as above, why write code that is flawed. If a future release of PHP was, for example, to change the comparison between the evaluated value and the case value to be strictly compared the code you have would fall apart completely. It is not working because the conditions in your case statements are being evaluated to TRUE. It is working because those conditions are evaluated to TRUE and then they are being compared to $magnitude from the switch constructor. In fact, with what you have, you could just as easily put any trivial value into the switch statement other than a value not interpreted as false (such as FALSE or 0). This would work exactly the same for the values you tested for $magnitude $magScale = ''; switch('ABCDEFGHIJKLMNOPQRSTUVWXYZ') { case $magnitude >=0 && $magnitude <=0.9: $magScale = 'rgb(195, 218, 236)'; break; case $magnitude >=1 && $magnitude <=1.9: $magScale = 'rgb(210, 238, 197)'; break; case $magnitude >=2 && $magnitude <=2.9: $magScale = 'rgb(244, 240, 202)'; break; case $magnitude >=3 && $magnitude <=3.9: $magScale = 'rgb(244, 223, 202)'; break; case $magnitude >=4 && $magnitude <=4.9: $magScale = 'rgb(240, 199, 205)'; break; case $magnitude >=5 && $magnitude <=20: $magScale = 'rgb(212, 195, 236)'; break; } Does that make sense or seem right to you?
-
Well, I will admit that the code example I posted was off, but if you actually read what I posted you would see that your case statements do, in fact, have gaps. And, your numerous tests are not valid if all you ever do is pass values that you know will pass. As, I asked before, what would be the result for a value such as 0.95? switch(TRUE) { case $magnitude >=0 && $magnitude <=0.9: $magScale = 'rgb(195, 218, 236)'; break; case $magnitude >=1 && $magnitude <=1.9: $magScale = 'rgb(210, 238, 197)'; break; . . . The answer is there wouldn't be a result because it wouldn't match any of the conditions. My reasoning in the prior post was 100% correct. I just didn't modify the code correctly. It should be like this { case $magnitude >=0 && $magnitude <1: $magScale = 'rgb(195, 218, 236)'; break; case $magnitude >=1 && $magnitude <2: $magScale = 'rgb(210, 238, 197)'; break; case $magnitude >=2 && $magnitude <3: $magScale = 'rgb(244, 240, 202)'; break; case $magnitude >=3 && $magnitude <4: $magScale = 'rgb(244, 223, 202)'; break; case $magnitude >=4 && $magnitude <5: $magScale = 'rgb(240, 199, 205)'; break; case $magnitude >=5 && $magnitude <=20: $magScale = 'rgb(212, 195, 236)'; break; Note that there are no equal signs in the right conditions. That means there are zero gaps between the statements. A value could never intentionally or accidentally fall between them. As for your understanding of a switch, there is still a significant principle that is being overlooked. Here is what is in the manual Let me provide a detailed explanation of why your current logic "works" but why it works by accident. $magnitude = 1.5; //Test value $magScale = ''; switch($magnitude) { case $magnitude >=0 && $magnitude <=0.9: $magScale = 'rgb(195, 218, 236)'; break; case $magnitude >=1 && $magnitude <=1.9: $magScale = 'rgb(210, 238, 197)'; break; case $magnitude >=2 && $magnitude <=2.9: $magScale = 'rgb(244, 240, 202)'; break; case $magnitude >=3 && $magnitude <=3.9: $magScale = 'rgb(244, 223, 202)'; break; case $magnitude >=4 && $magnitude <=4.9: $magScale = 'rgb(240, 199, 205)'; break; case $magnitude >=5 && $magnitude <=20: $magScale = 'rgb(212, 195, 236)'; break; } OK, let's walk through this step-by-step. 1. We set the variable $magnitude to 1.5 for testing purposes 2. $magScale is set to a default of an empty string 3. Here is where the real work start. The switch statement evaluates the contents in the parenthesis. This can really be anything, but is usually just a single value as you have it here. In this case, the value is 1.5. 4. The switch statement then compares the value evaluated previously (1.5) and COMPARES it (loosely) to the case statement. So, PHP checks to see if 1.5 is compares (is equal) to what is in the case statement $magnitude >=0 && $magnitude <=0.9 Let's run through that comparison step-by-step 1.5 == ($magnitude >=0 && $magnitude <=0.9) Replace value for $magnitude 1.5 == (1.5 >=0 && 1.5 <=0.9) Convert the inner comparisons to Booleans 1.5 == (TRUE && FALSE) Apply the AND condition 1.5 == (FALSE) Hmm, 1.5 Does not compare (loosely) to FALSE. The first switch fails. 5. Now here is where it get really interesting and illustrates why this works, for the wrong reason. Let's run through the second case the same way 1.5 == ($magnitude >=1 && $magnitude <=1.9) Replace value for $magnitude 1.5 == (1.5 >=1 && 1.5 <=1.9) Convert the inner comparisons to Booleans 1.5 == (TRUE && TRUE) Apply the AND condition 1.5 == (TRUE) This condition passws. But, NOT directly because the condition resulted in true. The reason is that the condition evaluated to true and when compared with any NON FALSE value would result in a TRUE condition because of how PHP does loose comparisons by default. Take a look at table 2 on this page. In fact, if you tested with a $magnitude of 0, the code would fail because you would end up with this comparison for the first case statement. 0 == TRUE Note that the manual states that the switch is comparing the value in the parens to the value in the case statements. It doesn't state that it is checking if the values in the case statements evaluate to TRUE - which is what you were thinking was happening.
-
@Ch0cu3r The function login() in that code returns FALSE on errors. But, on success it returns $page which is defined from a return value from the DB. @MightBeforeGod, You do not need to include the page URL for the users in the database. You should just have a generic page (e.g. showUserPage.php). Then you can simply define the page adding the user's ID as a parameter to the URL: showUserPage.php?id=$userID
-
That sounds like a MS Office replacement application. What you need is a Web based interface that can read the source code of the Word docs, convert it to content that you can represent in a web interface, allow the user to make edits to the content (using features such as bold, color, font (whatever you need), and finally convert that web content back into the format for a word document. Take a look at these search results: https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=php%20microsoft%20word%20library
-
Yeah, I had thought about using count. Seems like an awkward method since I assume the number of Types would be variable. But, is the sub-query required in that statement. Would this work SELECT s.shoe_id, s.shoe_name FROM shoe s JOIN shoe_type st ON s.shoe_id = st.shoe_id AND st.type_id IN (1,3) GROUP BY shoe_id WHERE COUNT(s.shoe_id) = 2
-
How are we supposed to continue to help when you are not providing any information. I pointed out what would cause a syntax error and you state that it was a Copy/Paste error. I don't see how you can get a copy/paste error in the middle of the content unless you are copy/pasting only sections of the code. If so, it is impossible to help. A syntax error is typically on a line before the line on which the error is reported. The reason is that the PHP parser is not a human, it will continue reading/processing code until it come to something that it cannot make sense of. For example, if you forget the closing quote mark when defining a string, the PHP parser will assume all the content following it is part of that string. $foo = "This is a string echo $foo; $bar = "this is another string"; In that code, the syntax error would be reported on line 3, even though the real error is on line 1. The reason is that since the string was not terminated the PHP parser will continue to read the following code as part of that string. It is not until line three that it finds a "closing" quote mark. Then, after that quote it would attempt to parse the remaining content as PHP code this is another string"; That would cause the syntax error on line 3
-
To add to CroNiX's respons: I did not read the entire thing, but I do see that the script sets the following session value when authentication passes $_SESSION['user_id'] = $user_id; So, in addition to starting the session on any pages that require the user to be authenticated, you would check to see if that session key isset() to determine if they are logged in. Plus, if you want to limit access to that page for only that user, you would want to check the value of the session key to see if it is the same one as the page being requested.
-
Cliff, If you paid someone to code this, you really need to go back to them. For us to try an debug such an issue when you are not familiar with the code will be problematic. If they used any reasonable process for coding the site, the issue could be in any number of files - not necessarily in the one you think it is. Plus, the file that you think the problem is in is too big to post? So, you expect us, with no knowledge of the code, to review the entire file to try and detect a bug? The problem could very well be that the application is not properly configured to send email (or maybe where it is hosted doesn't have an email server available). Email configuration can be complicated and very time consuming. I suspect you are going to need someone that can have access to all the source files in order to walk through the code and debug. In other words, you would have to pay someone for the significant time they would need to invest.
-
Filter items that have common properties in JS
Psycho replied to silvercover's topic in Javascript Help
How are you saving/associating the properties to the objects? If this is data that is stored in the Database, then AJAX may be a better solution rather than putting logic on the client side. But, you haven't provided any details (i.e. code) to what you have now, so I can't provide any possible solutions. -
Microsoft word documents are not plain "text". The files contain extensive markup text, similar to HTML, to specify properties such as font, color, position, etc. etc. etc. Not only that, but several years back MS made a significant change in the format of that markup. Previously (.doc), it used a MS proprietary format and now (.docx) it uses an XML based format. So, it is not a trivial task to create an on-line editor. Trying to code an editor yourself will be a huge amount of effort and would require that you limit what a user can do. I suspect there are some APIs/Classes available that can do most, if not all, of what you are doing. I would start by doing some Googling for that. But, I suspect that either you would find some free tools that are limited, or you will find more comprehensive tools that cost money.
-
@Barand, Maybe it's because I haven't finished my coffee this morning. I know the schema is valid. But, going back to the OPs initial statement I don't know how the query would be efficiently created to return shows that match multiple "types". If we JOIN the shoe_types table on the shoe table there would be an individual record for each shoe/type combination. But, since we are looking for a shoe that matches two types, would we need to JOIN on the shoe_type table twice? It would seem unwieldy the more parameters that are trying to be matched. I'm guessing there is a strait forward method that I am not connecting the dots on.
-
You want to loop over the results of the query, you don't want to run queries in loops as you were previously doing. So, you will want to iterate through the results of the query and create an array in the format you need, then convert to JSON. Based on your first post, this may be what you need: $sql = "SELECT m.NAME_OF_FIELD_THAT_HOLDS_THE_FILENAME FROM musics m JOIN playlists p ON m.playlist_id = p.id WHERE p.user_id = :id"; $query = $con->prepare($sql); $query->execute(array("id" => $userID)); $results = $query->fetchAll(PDO::FETCH_ASSOC); //Create multidimensional array of songs with keys $music = array(); foreach($results as $songTitle) { $music[] = array('name' => $songTitle); } //Create playlist array $playlist = array( 'Playlists' => array( 'Name' => 'etc', 'musics' => $music ) ); //convert to JSON $output = json_encode($playlist);