Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
You're asking this question in a forum titled "PHP Freaks"? Not exactly a good place to get non-biased responses. But, that question really is difficult to answer. There may be more jobs looking for skill in one language vs. another. But, all that matters is the job that you are looking for. Also, I must assume you are doing this learning "on your own". If you were going for a degree you will be exposed to many languages. Having said that, if you are going to do self learning then pick any one that you are most interested in. Because that is the one you will be more motivated to learn. Because, if you are going to be self taught the only way someone is going to hire you is based upon your portfolio of what you have accomplished.
-
That's because the DB "typically" returns records in the order they were created. But, there is absolutely no guarantee that this will be the case. Just because it works today doesn't mean it will work that way tomorrow.
-
Not really. If you have an auto-increment field you can order by that field - but it is not guaranteed to be in order based upon when the records were created. It 'probably' is, but that's a poor way to order records by the date created. You'll probably want to create a date field for this. The good thing is you can create the field so it is auto-populated with the date/time of when the record is created and you don't need to put anything in your INSERT queries. But, you will need to set the value for the records that were already created.
-
If you are not going to take suggestions - then why ask for help? You are not checking for errors on the INSERT query which I showed how to do.
-
How is that anything like your initial request?
-
EDIT: If this is your table data id | tag1 | tag2 | tag3 ------------------------------- 1 | apple | pear | banana 2 | pear | orange | apple 3 | orange | kiwi | banana 4 | kiwi | apple | orange 5 | apple | apple | tomato 6 | apple | apple | apple 5 | tomato | apple | apple Then you can get the sum of how many times a tag is used in each record using the query I provided previously SELECT *, ((tag1 = 'apple') + (tag2 = 'apple') + (tag3 = 'apple')) as total FROM table_name ORDER BY total DESC The results would be id | tag1 | tag2 | tag3 | total 6 | apple | apple | apple | 3 5 | apple | apple | tomato | 2 7 | tomato | apple | apple | 2 1 | apple | pear | banana | 1 2 | pear | orange | apple | 1 4 | kiwi | apple | orange | 1 3 | orange | kiwi | banana | 0 What you posted makes no sense. You stated that the tag fields contained "text" values - you can't "add" text values. You stated previously that you wanted the count of those text values that matched a search term. I already provided that. I'm guessing you didn't even try the code I provided because you didn't understand it.
-
As vinny42 stated, this is best handled by normalizing the data such that the tags are stored in a related table with a foreign key reference to the parent record. This makes do any type of operations on the Tag values much easier. But, it can be done with three tag columns in the current records. It's just that the query will be more complicated and it will not scale - if you ever added/removed tag fields from that table the query would have to be updated. Something such as SELECT *, ((tag1 = 'word') + (tag2 = 'word') + (tag3 = 'word')) as total FROM table_name ORDER BY total DESC YOu don't say if the word will be an exact match or a partial match. If you need partial matches then change the tag1 = 'word' to tag1 LIKE '%word%'
-
Hmm, are you saying that the text "track_length="4,307 km" track_record="1:19.880" record_holder="Mr.X" car="AUDI"" is a value that is stored in a single field called "custom_fields"? You should really parse the data and store the values individually. With what you have, it would be difficult to do any searching, sorting, analyzing of that data. But, with what you have, this might work (of course you'll need to format the output into how you want it - this will just extract it into a usable array): while($row = mysql_fetch_assoc($result)) { $customFieldsSrc = explode('" ', $row['custom_fields']); $customData = array(); foreach($customFieldsSrc as $fieldString) { list($name, $value) = explode('="', $fieldString); $customData[$name] = $value; } echo "<pre>" . print_r($customData, 1) . "</pre>"; }
-
You have several things going on there between the form, the PHP and the JavaScript. When trying to debug code you need to remove "variables" (the things that can be different not the variables such as $foo) to see where the actual problem is. It appears you have one set of input fields and are using JavaScript to duplicate the fields. So, I must assume the JavaScript is creating the fields else you would be stating that is the problem. I'm betting that the problem is that the JS is not creating the fields properly and the submitted data is not what you think it is. So, start with doing a print_r($_POST) to verify exactly what is being sent in the post data. If the passed data looks correct then the error is probably in the processing code. If it doesn't then the problem is probably in the JS code that creates the dynamic fields.
-
Well, yeah, the query will return what you ask it return. If you ask for the wrong thing then, obviously, you will get the wrong results. All of the "reasons" for this being a bad idea are either invalid and/or are reasons why the data should have normalized your data to begin with. If the database get's a new column your select query will never now about it unless *you* modify it If the data was normalized then the query would not have to change when new fuel types are added. It would just JOIN out to the dependent table and do a SUM() using GROUP BY. This may be of trivial concern to you as a single developer but if this table is in a big system you don't want to have your users decide what the "total" is, or indeed keep track of whether the cancluation for the total has changed. Again, a normalized database with an appropriate query would solve this. Even without a normalized database, if this was to be used for a "big" system with multiple developers then it should use a Data Abstraction Layer - the developers should not be writing their own queries. They should instead use a library of predefined methods for database activities. Then when a change in the DB occurs only those methods would need to be changed and not any of the business logic code. And yet you can't sort on a dynamically calculated total if your table has a decent number of records in it. Considering the OP is apparently planning to output the data based upon dates I don't see how that is relevant.
-
First off, what you are wanting to do is a poor implementation. There is no good reason to create a field that automatically holds the sum of the other fields when you can just as easily write your query to get that data for you. SELECT Gas, Gasoline, Diesel, (Gas + Gasoline + Diesel) as total FROM table_name Yes, you can create a trigger to sum the values and populate another field, but it opens up the possibility of getting the sum and the values out of sync if you don't cover all your bases (all the different INSERT & UPDATE scenarios). Dynamically generating the sum is the best way to go for something like this as it guarantees to get you the right value each and every time. Databases are made to do these types of calculations and do so very efficiently. But, if you really want to go with the other approach, then just create a trigger. Create a new field called 'total' using the same field type that you used for the other fields. Then run this query in PHPMyAdmin to create a trigger. CREATE TRIGGER ins_sum BEFORE INSERT ON TABLE_NAME FOR EACH ROW SET NEW.Total = NEW.Gas + NEW.Gasoline + NEW.Diesel Now, whenever a new record is added, the value for "Total" will be auto-populated with the sum of the other three fields.
-
Yes it is possible - just not how you are thinking to do it.
-
I was also going to add that if you normalized the $fruitvegi array to be lowercase and forced the input to be lowercase you could just use in_array() to check for a match in the sub arrays rather than doing a foreach loop over all the elements.
-
"watermelon" does not equal "Watermelon". You want to do a case insensitive comparison. Use strcasecmp().
-
The problem is that you are using the assignment operator (a single equal sign) and not the comparison operator (the double equal sign). So your conditions are trying to assign the value to the variable 'mytrip'. Since that operation can be performed the result of the condition is True. Edit: for this you should probably be using a switch anyway as it makes it much easier to edit/maintain. function val() { var count = parseInt(document.getElementById("over_16").value); var mytrip = document.getElementById('privated').text; var cost; switch(mytrip) { case 'Lake District': cost = 55; break; case 'Private Trip': cost = 120; break; default: echo 'Invalid trip selection'; return; } var sum = count * cost; document.getElementById("sum").value = sum; }
-
Which is basically what I already stated You can do whatever you want. The more complicated you make it - the more complicated it will be to code. It all depends on your needs and the cost/benefit of any solution.
- 3 replies
-
- pagination
- carousel
-
(and 1 more)
Tagged with:
-
You can either download all the data and have the functionality completely run client-side or you can progressively load more results as the user clicks the next button. There are advantages and disadvantages for both methods. Here are some: Download all data: Advantages: Will run very fast with little to no delay (assuming that the total data is not excessively large) In my opinion, this is typically easier to implement Disadvantages: Will not update if any changes to the data takes place after page load Will not scale with large data sets Progressively load data (i.e. AJAX) Advantages: Will scale for scenarios with a lot of data. If there is a ton of data this may be the only viable option. Can adjust dynamically if data changes after initial page load Disadvantages: There can be a slight lag in displaying more results. This can be reduced by "thinking ahead" - for example, load the next page beforehand. When page loads get page 1 and page 2. When user navigates to page 2, immediately show page 2 data and then get page 3 data. This can be a little more difficult to implement
- 3 replies
-
- pagination
- carousel
-
(and 1 more)
Tagged with:
-
delete(multiple) work,but edit(multiple) not work
Psycho replied to bangkit's topic in PHP Coding Help
You are making this harder than it needs to be. Never run queries in loops unless you are sure there is no other way. instead of this if(isset($_POST['hapus'])){ $ip=$_POST['ip']; foreach($ip as $ip1){ $query="delete from dokter where k_dokter='$ip1'"; $dd=mysql_query($query); } } Do something like this; if(isset($_POST['hapus'])){ $ipList = "'" . implode("', '" $_POST['ip']) . "'"; $query = "DELETE FROM dokter WHERE k_dokter IN ($ipList)"; $dd = mysql_query($query); } Note; There's a lot more you should be doing, but I've not the time to get into all of it right now -
I'm pretty sure the error is occurring on the SELECT query which is malformed $duperaw = mysql_query("SELECT * FROM bx_sites_main", $link['link']); Also, I don't see anywhere where you select the DB to use. Try this if (!mysql_connect($db['host'], $db['user'], $db['passwd'])) { die('Could not connect: ' . mysql_error()); } if(!mysql_select_db('NAME_OF_DATABASE') // <==== Enter the DB name here { die('Could select DB: ' . mysql_error()); } $sql = "SELECT url FROM bx_sites_main WHERE url = '{$link['link']}'" $result = mysql_query($sql); if(!$result) { die('Error running Select Query: "{$query}"<br>Error: ' . mysql_error()); } elseif (mysql_num_rows($result) > 0) { echo "Value already exists DB"; } else { $sql = "INSERT INTO bx_sites_main (url) VALUES ('{$link['link']}')"; $result = mysql_query($sql); if(!$result) { die('Error running INSERT Query: "{$query}"<br>Error: ' . mysql_error()); } else { if (mysql_query($sql)) echo "Data Inserted"; } } mysql_close($linksav);
-
Possible, yes. Practical, probably not. When in Facebook, it will occasionally "guess" who is in a photo because people have already tagged that person in a great many other photos. Your app would not be able to tap into the Facebook database without their direct approval - which they would not be willing to do without a great deal of money. But, you state " . . . would have to create a database of Faceshots of the individual that is uploaded by them that would only be used for locating their social media" So, your idea is to have people upload pictures to this new app - only for the purpose of locating their social media pages? If I have pictures of someone chances are I'm already friends with them on Facebook. You'll have to provide more value to users to upload pictures to your app. I'm just not seeing the value here.
-
MYSQL HELP .. PLEASE ANYONE HELP ME WITH THIS
Psycho replied to Luther_Caloring's topic in MySQL Help
You would best be done in the processing code after you run the query. $query = "SELECT * FROM tbl_payment ORDER BY `Payment No.`"; $result = mysql_query($query) or die (mysql_error()); $total = 0; while($row = mysql_fetch_assoc($result)) { $total += $row['Amount']; } -
So, as already requested, give us some details. Show us some example data from the two tables and what you are trying to achieve.
-
Based upon what you have stated, my understanding is that you want the data from both of those two queries to be returned from a single query. If so, then the answer is to use UNION. So, what you are asking makes no sense because you are basically stating you want the results of a UNION query, but you don't want to use a UNION query. Sort of like asking how to stop a car, but without using the brakes. Now, if you are asking what I think you are asking - then it is a stupid question. So, I think mac_gyver is making an assumption that you really want something else. Specifically, something in which you need the data from those two queries based upon some relationship. I guess he is giving you some slack that you may not know how to properly ask the real question so he is trying to get more information to really understand your problem. But, instead of being grateful to people that are taking their own time to try and help you and responding as such, you respond in, let's just say, a less than helpful way.
-
Assuming you don't want to deal with the trouble of learning to use a database there are better solutions. The simplest one is to store the text for the announcements in a simple text file on the web server. When someone goes to the announcements page it will read the text from that file to display it. Then, when your mom wants to edit that page, give her a page which loads the text file into a form and allows here to edit it and submit. When submitted it will update the text file. No need for her to write in notepad, save it, and then upload it. And, you can easily expand that solution to use a "rick text" type editor such as this forum uses for writing your posts that allows you to add properties such as bold, italic, underline etc. There are plenty of free options available and you can easily implement them to store the content in a text file as above.
-
I can't believe I missed that. There are requirements that some values - based upon the field type - must be enclosed in quotes. Right now, all the values are simply separated by commas - so think about what would happen if a value was Lastname, Firstname? how would the database know that that is one value and not two? So, quotes are required around all "text" type fields., but not around numeric type fields for example. I don't know the requirements for every single field type, but there is no reason you can't put quotes around values for all field types. So, it is a good practice to always use them. Also, since values can also have quotes in them you need to make sure you are escaping the values first (mysql_real_escape_string - at least until you move to mysqli_ or PDO). $sql = "INSERT INTO contact_reg (loginip, group_name, place, managerName, managerMobile, managerEmail, category, AstName, AstMobile, AstEmail) VALUES ('$loginip', '$group', '$place', '$managerName', '$managerMobile', '$managerEmail', '$category', '$AstName', '$AstMobile', '$AstEmail')"; I don't know where you are defining those values, but if you are not escaping the values (as noted above) you could get a failure if any of the values have single quotes in them.