Jump to content
Larry Ullman's Book Forums

Antonio Conte

Members
  • Posts

    1084
  • Joined

  • Last visited

  • Days Won

    126

Everything posted by Antonio Conte

  1. I'm thinking more about the generation of the code. This is what I got by using json_encode(). I also think this structure is easier to change as I can add new keys very easily. (In the PHP array, and it will just be converted for me) Yes. Should be X-series. Must have mixed up with BMW. Cars is not my speciality eighter.
  2. Worked perfectly when i created JSON objects from the PHP array. I had to remove the property "make_name" from the object, but I guess never used it anyway. Here is what I ended up with: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>JS select element generator</title> </head> <body> <form action="" method="get"> <select name="brands" id="brands"> <option value="0">Velg bilmerke</option> <option value="1">Alfa-Romeo</option> <option value="2">Aston Martin</option> <option value="3">Audi</option> </select> <select name="models" id="models"> <option value="0">Pick a car brand</option> </select> <button type="submit">Search</button> </form> <script src="models.js"></script> <script> document.getElementById('brands').onchange = function () { var html = '<option value="0">All models</option>'; for (var key in models[this.value]) { if (models.hasOwnProperty(this.value)) { html += '<option value="' + key + '">' + models[this.value][key] + '</option>'; } } document.getElementById('models').innerHTML = html; }; </script> </body> </html> The JS file holding JSON (models.js): var models = {"1":{"1":"Alfa-Romeo Giulietta","2":"Alfa-Romeo MiTo"},"2":{"3":"Aston Martin DB9","4":"Aston Martin V8 Vantage"},"3":{"5":"Audi A1","6":"Audi A3","7":"Audi A4","8":"Audi A5","9":"Audi A6","10":"Audi A7","11":"Audi A8","12":"Audi Q3","13":"Audi Q5","14":"Audi Q7","15":"Audi R8","16":"Audi RS4","17":"Audi RS5","18":"Audi S3","19":"Audi S4","20":"Audi S5","21":"Audi S6","22":"Audi S8","23":"Audi TT","225":"Audi Z-Serie"}}
  3. Yes, perfect. I think I'll go for the separate file option. The only thing I need additionally, is keys for the models, as they are connected to rows in the models table. I also want more info here, like number of models per brand, etc. I should be able to fix that my selves now that I know how. I have learned a lot about JS lately. Got most of my solutions to work so far. Still need a little help making whole table rows clickable, improve the speed on google maps, changing my file upload script a little, etc. I'll edit my first post soon to explain the problems. Thanks, Jon.
  4. Interesting thread. I agree with Jon. It would be possible to store the window object as a property inside the navigator object (for unknown reasons), but as the navigator object doesn't hold the window object, I'll agree with that the parent object must be the window object. That is also what I've always read.
  5. I didn't before you simplified it, but your last version was easy to understand. Eistein's quote about explanations is so true. I'm really amazed by your Javascript knowledge. You actually use map every time you use an associative array in PHP. I'm guessing you know this, but if you find data structures interesting, you should watch this Youtube video from PHP UK Conference. I've studied data structures and find them very interesting, but I generally think PHP developers would gain a lot by learning more about them. PHP developers take them for granted as arrays can do so much. Not really a "must see" by any stretch of the imagination, but I would recommend it for anyone interesting. Sorry about the off topic, btw. Guess this thread has more theoretical value than anything else, so maybe it's not that bad.
  6. Sorcery! While I kind of understand the code, I could never have written something like that myself.
  7. The function/method is a property of the object because the method is really only an object assigned to a property. When you call the property, the method is invoked. Everything, even functions, are objects in Javascript. For clarity, you should call them methods, though they technically are objects assigned to properties. Because the act like methods, they should be called that though.
  8. This is happening because the error handler is declared several times. (Hence the error) This happens when you include/require a file several times or use the same function name several times. It looks like you should remove the require call at line 75.
  9. Thanks, Jon I think I'll go for creating a multidimensional array. I think I'll write to a .js-file every time a change is done to the car database. Because we'll only deal with new cars this should cut the amount of queries needed to search by a good portion. It's not really a problem, but the site will feel a lot quicker for sure. I'm mostly thinking about this to reduce the amount of data for mobiles. Regarding the events, I've found out that I can do something along these lines: // Will perform an Ajax-search for models var getBrandModels = function(brand_id) { $.post( 'ajax/get_models_by_brand_id/'+brand_id, function(data) { $("#model").html(data); } ); } // Perform model search when change-events are caught $("#brand").change( function(){getBrandModels($(this).val())} ); // Duplicate for other events. Haven't really looked that much at other events yet, but I've seen .click(). It seems to be handling to special cases I've had problems with, but will perform a new post request for every click event. With a static JS array instead of requests, though, this seems like a viable solution. Will update the first post with new questions. I'm considering this one improved and solved. A quick one, though. How should i save datasets in JS? Should I link to a javascript file, include the array in script-tags or what would you recommend?
  10. Good. The only problem with that method is that I don't really know how to work with JSON data in Javascript. The HTML method worked fine, so I stuck to that initially. I'm guessing that getting the data as JSON would be pretty simple, as I'm only generating HTML in the form of <option="<?=$id;?>"><?=$value;?></option>. This data is then echoed and then appended to the #models select box. Generating the HTML client side is something I would like to do. The reason for using the .get-method is that I found the .ajax() or .post() methods harder to understand. I got the .get()-method to work quickly, so I stuck to that. I wouldn't mind a switch, or even use regular Javascript if that doesn't complicate things a lot. Regarding "the problem", the Ajax script always receives correct values. If not, a standard value is used, so the problem must be related to the events not being sent (as a result, no a.get()-request is done). .change() won't fire an event if there is no change in values. I'm looking for something to bullet-proof the event handling. The problem is hard to replicate effectively, but sometimes, the select box just refuses to populate. A refresh solves that problem. As an example, let's say Alfa Romeo is used, a search is done, then the user's used backspace to go back. Then, a new click on Alfa Romeo won't load the models as the value has not changed. Links can be sent on PM for those able to help. As the project is still in development, I don't want to post links in a public forum.
  11. Hey, everyone. Currently working on a project that requires a lot of Javascript. This includes Ajax requests, some event listeners, Google maps integration, a Wysiwyg-editor and probable some more. I'm currently working with JQuery as I'm not much of a Javascript developer. I will group the code by usage, show an example and tell you if I experience weird behavior. 1. Drop-down search functionality (SOLVED) 2. Multiple file uploads as attachments Intro: The system is based around offers. These offers should allow attachments and possibly a main image. (Think like Wordpress' main article image) Problems/questions/etc: 1) I want the uploading to be done with Ajax. I found a good script that handles this. The problem is that the attachments must be tied to the ID of an offer. This ID, however, does not exists before an offer is published or saved as a draft. How can I tie these two together? 2) As users must be logged in, I create a folder for the user if one does not already exists. This folder is named by the User-ID. This is done because I remember reading about a problem occuring when too many files are lookated in the same folder. Is this neccasary or good, or would you recommend something else? Table structures and data flow: (Only the key structure) Offers ( offer_id, status, user_id* ..... ) Offers_attachments ( offer_id, path, extention.... ) Users ( user_id, ...info... ) Upload script used: (Not that it really matters) valums file-uploader Help me pick a solution: 1.) A two-step process. The users fill out vital information first, click next, (and the article is saved) then add attachments and save with a status. 2.) An empty offer is inserted to the DB when a user clicks "Add new offer". We now have an offer ID to tie attachments to. A daily cron job deletes offers/attachments that were never "Saved" by clicking "Save the offer" by the user. 3.) Attachments are tied to the user instead. The users can then add uploaded attachments found in the database related to their user.
  12. Strings can be handled like arrays. Do a for loop over the string, and check for white space for each char. Upon white space, change the string.
  13. This is called algorithm design, and can be done theoretical. In programming, this is called big O-analysis. I don't know how joins are doing in that regard, though. My instinct, if you have 500 000 rows, would be to create another denormalized table holding all the data from your join. (That's a pretty simple query) and then perform operations on that data instead. You are right about speed in that regard. Lets say joining data is in a best case scenario a linear algorithm. That means, if 50 000 rows would take 1 second to calculate, 500 000 rows would take 10 seconds. Not really workable in a live site. Relational databases are proving to great in that regard though. Try performing calculations on large data sets to see how it's going.
  14. Read my post again. You need to add a column called 'views' to you post table. You then need separate queries for the front page and the single post page.
  15. That won't really work. What you do here is to count how many times a title occurs in the Database. That won't let you count number of view. What you need to do is update the table record each time a post is queried. This query should only be used when a SINGLE POST is queried, hence, it's actually viewed. This query is run when you have a link like this: - http://domain.com/?post_id=10 // Get value from GET array $id = int_val($_GET['id']; // This will be post_id 10 if you use the link above // Set queries $q = "SELECT * FROM posts WHERE post_id = {$id} LIMIT 1"; $q .= "UPDATE posts set views=(views+1) WHERE id = {$id}"; // Notice .= here. This means it will be added to first string // run both queries $r = @mysqli_multi_query($dbc, $q); When THAT is done, you can tweek your first query a little bit; $q = "SELECT * FROM posts ORDER BY views DESC LIMIT 5"; That will give you 5 posts sorted by number of views
  16. Quick search. Both a class and some code to look at how to use it: http://www.kidmoses.com/blog-article.php?bid=56
  17. You should learn about normalization and how to structure your data. This is the most important thing in a relational database. It's some theory you need to learn here before using it practically. You should then learn how to join the data again in queries. The JOIN statement is the most important here. These two things are the largest building blocks on how to learn MySQL. When you master that, you can do pretty much everything with a DB. You can find a lot of information/good articles about this online.
  18. Glad you solved it. Sorry about that, but I offered you two different solutions. One for PHP, and one for MySQL. Use only one of them. I would also look at MySQL(i)_set_charset.
  19. Look in the footer. At the very bottom, you find a link called "Mark Community Read". Press it, and click "Mark all as read". Easy as pie.
  20. It's kind of the name of the game, unfortunately. PHP is mostly used for web development, and therefor it kind of requires knowledge on HTML and CSS. The main goal is of course to create a dynamic website, and a website needs HTML and CSS. Luckily for you, learning HTML and CSS is relativly easy compared to learning to code. If I where you, I'd focus on getting the coding part right, then start playing with html later. I would just download a page template you like. That way, you can focus on PHP and still get a good enough visual impression. I guess Rob was a little hard on you because it did not seem like you were trying that hard. If you are really stuck, you will get help with "dumb questions" here anyway. It's a good community. Good luck.
  21. The thing is, no-one will code a working solution for you. We are providing you some steps on the way to do what you want, thought. A tip. Strip out all HTML from the code her. Then start playing with getting the divs right. Search on an article on float in CSS if you get problems with the styling. Don't try to get everything done at once. Make it work step by step. Writing on mobile now. Formatting my computer at the moment. I'll provide some code later if no-one else does it.
  22. PHP: echo <p> Content: ' . substring($row['content '], 0, 30). '</p> '; MySQL: SELECT SUBSTRING(content, 31) AS content
  23. You should look at font-squirrel for your font problems. You should also use good fall-back fonts. http://www.fontsquirrel.com/fontface/generator Looking at your CSS, you can do a lot to prevent bugs. Declarations like this one: #topNav > ul Should instead be written: #topNav ul Etc. Hope that will help a little bit.
  24. This is not really tabular data. You should consider switching to divs instead. Float the divs with a width of something like 30% width, and you should be good to go.
×
×
  • Create New...