Jump to content
Larry Ullman's Book Forums

Terry

Members
  • Posts

    87
  • Joined

  • Last visited

  • Days Won

    4

Posts posted by Terry

  1. Hi,

     

    I am trying to come up with a way to validate multiple forms with ONE validation file or function.

     

    Problem is each form would have different fields, some maybe same, and could be empty.

     

    I was thinking of doing a check of isset and not empty before allowing validation of each field but what happens if a particular field is empty but is required?

     

    I am using an altered form_functions.inc.php file, it is essentially same as in book but added a few field types that weren't in there, checkbox, radio fields etc...

     

    I would like to simplify the validations into a function or altered validation.php file that gets included on each form page but I am having difficulty thinking through the logic on this. I just want fields that exist on the form to be validated and those that don't exist get by passed and NO error gets returned if the validation code thinks it should exist and is just empty when in fact that field does not exist on the form at all.

     

    I am trying to prevent creating seperate validation pages for each form, just do it all in one place that gets called and handles all validation no matter what type of form or fields exist.. eg: registration, login, questionaire, poll, profile pages or what have you.

     

    Maybe there is an obvious way here I am just not seeing it right now.

     

    Thanks for any help.

  2. Hi,

     

    Yeah, I checked out that link and apparently there is an issue with 5.3+ with that command. I didn't totally understand what it meant about the need to pass variables, sort of extra work seems to me and duplicates setting up static variables seems to me anyway. Though I am not sure 100% what they meant in their note about 5.3.

     

    Passing and array or delimited string to parse within a function would be way to go here I guess.

     

    Thanks for your response.

  3. This may not be the right place to post this, forgive me if this should be posted in another location or this has been answered already somewhere else. Please direct me to examples if you know any, thank you.

     

    I would like to create a function that accepts a non-static number of variables.

     

     

    Normal Function:

     

    function myniftyfunction($var1, $var2) {

     

    -- do something here --

     

    }

     

     

    That is a function that accepts two variables. What if I want to send variables that vary in number? Maybe 1 in one instance and 5 in another? The thing is at this point I do not have a maximum number I may send. This is a function I am creating for wide usage for many different scripts and applications.

     

     

    I can set a static number by doing something like this:

     

    function myniftyfunction($var1 = 0, $var2 = 0, $var3 = 0, $var4 = 0) {....

     

    But whatever number of variables I set there, what happens when I have an application that needs more? Yes, I can go in and add there but my question is, is there a way to create this so that I do not have to? Make it stretchy so to speak?

     

    I don't know if there is a way to do this but I am curious if Larry or anyone else here has any ideas on how to achieve this.

     

     

    Thanks.

  4. Some do this with js but it could also be done with an image split up into sections using a photo editor software and using a table to arrange the split sections on page, then use hyper links to pass the version to the next page.

     

    Then you could do something like set variables everywhere content and prices would change and then pull from the database as you already are thinking. Instead of multiple IF statements to filter content I would setup a variable to pass in a database query. You would need to add a field in the database table for whatever version of content IF you were going to filter content otherwise you already have the prices setup as you explained above.

     

    A little more work but would give flexibility to the site and you would not need multiple physical pages for each version.

     

    Anyway, some more thoughts I came up with.

     

    Thanks.

    • Upvote 1
  5. From the last link in my post at the bottom of that post the person gives instruction on how to show the status:

     

    In phpMyAdmin select the database and import the file.

     

    To see stored procedure created or not enter the below command

     

    SHOW PROCEDURE STATUS;

     

    Then apparently if you do this it will show the actual result

     

    To view the stored procedure,

     

    SHOW CREATE PROCEDURE NAME_OF_PROCEDURE;

     

     

    Those are quotes directly from the post in that other article, I have not tried them.

    Let us know if those commands work for you.

     

     

     

     

    Terry: The links you gave seem to be about creating the procedures, which I have done successfully. Calling them from within phpMyAdmin is the problem I now have (as per my last post).

     

    thanks for your response,

    Chop

  6. Has anyone here used a debugger for PHP (such as Zend or XDebug)?

     

    I started using it a bit, and I'm only able to debug the page I select for debugging. Is it possible to debug a website while I'm actually using it? For example, I login to the site, and then visit a page where I perform an action that queries MySQL, and then visit another page to do something else, etc. Is it possible for me to debug all those pages as I browse through them?

     

    You will need a IDE such as netbeans an eclipse based IDE (free) or phpStorm from jetbrains (commercial).

    I personally use phpStorm and have Xdebug setup it connects to Xdebug via firefox in my case and steps through the code line by line, you can also set watches on variables and it makes debugging complex PHP rather simple. This is NOT possible with a text editor like PsPad etc...

     

    That is the only way I know of is to use a 3rd party IDE specifically for PHP and the walk through the code line by line as it executes. I like seeing the variables populate and see where my code breaks at which line. Makes it very easy to debug.

     

    I chose a commercial software but there are free software too, I just tested out as many options as I could and the paid software just worked better for me. Though there are other options besides the two I listed above.

    • Upvote 1
  7. Oh, okay. Sorry if I sounded rather harsh in my post. I was just thinking why write about something that's already been written about well by so many people. Anyway, your examples are appreciated. Thank you, and sorry if I sounded like a prick.

     

     

    No worries. I didn't see it talked about here in THIS forum. Though I have not looked too deep into the other sections of the site. I am sure it is redundant info but when I get into posting mode I just like to share. Looking over this forum it seems I am sort of taking over with gabillions of posts so will probably take a break from posting for awhile.

     

    Thanks.

  8. Apparently it is possible, I found this post not too long ago at MySQL's forum.

    http://forums.mysql.com/read.php?98,415004,415066#msg-415066

     

    Also phpMyAdmin has been updated on May 20th, 2011. The current version is 3.4.1

     

    I have not used prepared statements so I don't know how well or what limitations there are or what new features the recent versions bring to that capability.

     

    Edit:

     

    It seems in the last post in that thread someone figured out how to use delimiters within phpMyAdmin to make stored procedures work and call them.

     

    http://forums.mysql.com/read.php?98,415004,415210#msg-415210

    • Upvote 1
  9. I feel like two relatively unrestricted database calls will be less of a load on your server than one call that has a ton of limiting factors built in (although I'm not sure about that).

     

    I don't know either, I never tested it to see. I would assume that indexes would save on processing power as well. Also caching results, either through MySQL or outputting static files that are updated once content is added or changed.

     

     

    maybe you could always return the 20 most recent records, as you'll be guaranteed to have 5 within those 20 that match your filters.

     

    I can't know this. What if there are 40 posts for level 1 before one post is added at a higher level or 60 posts before one post at level 3 for instance. I don't know where they will be in the database, it is not a preset thing.

     

     

    if you could use a database call to determine where the 5th record that meets your conditions is, you could simply retrieve all the records to that point

     

    That would be what the SQL would do for the 5 top results box, but would count upto 5 then stop. Essentially grab the first five that match the criteria. I have never written SQL that starts at the last number before, Say there are 15 matches in the database you would say start at number 5 and work back to number 1. I am not sure that is how it works, I think what happens is the database gets all the records upto a given number that match then sorts by order and ascending or descending depending if you sorted the output or not.

     

     

     

    On a side note, I recently read an article about Facebook's technique for loading pages so quickly, and it involves using a custom object called BigPipe to make multiple asynchronous Ajax calls to a database, so that as each call is returned, it can load a small part of the page without having to wait for the rest of the database calls to finish.

     

    I have heard of this tactic before. I do not know how to do it. I also do not know much about ajax, I did purchase Larry's book on building a website with ajax and I also found a couple ajax books I had here but I have not gone through them yet.

     

    I think that my idea of checking if any matching rows are within the current page and then search for additional records as needed through a second SQL query is the only option unless I just go straight to the second call from the beginning. I don't know that much about prepared statements so I don't know if those would work here or not.

     

    I think I may end up creating a cache via static files that are created from database output and then just update those pages as content changes. Some pages it is impossible to do this, such as a search page but if most of the pages are static it won't really matter I guess.

    I will look more into that later.

     

    Thank you for your responses.

  10. A quicker way is to just do a find replace,

     

    FIND -> $' or $'.

     

    REPLACE -> E' OR if you searched for $'. replace with E'.

     

    I don't know how to make the symbol so I just used E.

     

    Make sure you do this manually and look at each place it wants to do a replace on because it may not be what you want replaced. Don't do a automatic find replace all.

     

    DO NOT just do a search for $ or it will find ALL instances of variables everywhere in your code, and if there is no ' next to the dollar sign you need replaced the find wont find it. Verify in your web browser all the signs are replaced.

    • Upvote 1
  11. I assume you are talking about the second site example.

     

    Page 214 ch8, in the #6 section "Print Each Item:" if you look in the example code for the line with "Price:" in it you will see the dollar sign, replace it with the EU sign.

     

    I am sure there are places in other sections of code that shows the prices, just look there and find the dollar sign and replace with the sign you need it to be.

     

    If you are going to offer both US and UK prices I would setup a variable and place it there instead and just replace the contents of the variable with the correct sign as needed.

    • Upvote 1
  12. I read in another post someone asking about pagination.

     

    This is an example from a Thread of mine here.

     

     

    This is ONE possible method to do pagination, it uses MySQL. I am only going to show the SQL as it is the main component that makes it work. From the understanding of how it works you can easily create the PHP required to make it work for any pagination purpose. I do NOT know if this is the method Larry uses but it is one that I use in my own projects.

     

     

    Here is the SQL:

     

    SELECT * FROM table ORDER BY record_id LIMIT $pageNum, $totalRows

     

     

    I simplified the SQL from my other thread to better explain how this works.

     

    This is a normal SELECT statement.

     

    I am using the record_id to order the display of the rows returned from the database, you can further choose ASC or DESC depending if you want most recent first or oldest first.

     

    I tend to use the ID of the table rows to filter most recent records instead of date because in general the highest record id is the most recent record. This is not 100% correct in all situations but I find in my own projects it is good rule of thumb but I double check via the programming logic before saying yes that is indeed correct because it might not be.

     

    What makes the pagination work is the last part of the SELECT statement.

     

    LIMIT $pageNum, $totalRows

     

    I am not going to explain the purpose of LIMIT or how it works, you can go to php.net and look it up yourself or read about it in one of Larry's other books.

     

    $pageNum is the current page you happen to be on, the one you want to retrieve. It starts at 0 and goes up. Page 5 would be $pageNum == 4;

    This is what is used to move from page to page, it is NOT saying I want X pages displayed from the records.

     

    $totalRows is what determines how many pages there will be.

    At each call to the database get X records, depending on how many records there are in the database in total and how many you get out at one time will determine how many pages there are.

     

    Example Only:

    -----------------

    50 total records/rows in the database.

     

    $totalRows=5;

     

    This would make 10 pages assuming only 50 records in database.

     

    $totalRows=15;

     

    Now there are 3 pages.. This is a dynamic thing, you do not need to do the math for it, MySQL does it all for you.

     

    You do however have to create the PHP to properly manipulate the returned result from the database. At each database call the number of rows are returned from the particular page you tell MySQL to retrieve.

     

    It IS very possible to tell MySQL to return records that do not exist from pages that do not exist OR records that do not exist that did exist on a page but don't any more. You will get weird results in your pages when calling for data and/or pages that don't actually exist.

    The returned mysql result will be NULL or blank but depending on your code either nothing will show up or weird things will happen because you did not account for no results returned.

     

    That is the very basics of how this particular method of pagination works. There are others but this one I find works well.

     

    Another method I came across is to retrieve ALL records at once and then in code or javascript filter the results and make however many pages you want, but you need to think through the math and how many records per page etc... Also if you have a lot of records and a lot of visitors you can end up crashing your hosting server and your site could come to a crawl and not be speedy loading with this method.

     

    There are other ways to do pagination but the one I show above works good for me, others prefer other ways though, it comes to personal preference I guess.

     

    Hope this was useful.

     

    Thanks.

    • Upvote 1
  13. You have a bunch of data in a database that you want to paginate, but instead of having to make a new database call everytime you go to another page, you want to know if it's possible to make one database call at the beginning to get all the data, and then still achieve pagination. Is that correct?

     

    No.

     

     

    On right side of page there is list of blog posts, split up by X number of pages in pagination.

     

    This SQL call handles that.

     

    SELECT * FROM posts WHERE posts.status='publish' AND posts.level='".$access_level."' ORDER BY id LIMIT $pageNum, $totalRows

     

    On left side in a smaller sidebar is a box that contains the last 5 most recent posts filtered by special filters and it is possible that not all of the 5 most recent posts will be contained within one page. Depending on how many pages, in my example above there are 10 pages with 5 posts on each page, so there could be 1 matching post on page 2, another on page 4 etc...

     

     

    2 separate calls, required. Two separate display filters for same content contained in database.

     

    I am using the call above, that the original purpose is for pagination to also check if there are any matching records for the top 5 box.

     

    Problem is, it only works for the page in the blog post listing you happen to be on.

     

    I am trying to limit database calls and merge calls as much as possible.

     

    In this case, what I am thinking that will work is to use the first call to check if there are any matching records for the recent posts box and if there is not a total of 5 records found on that particular page to then make a separate call specifically for this 5 posts box, which is separate call than the one above, that SQL call is not listed here due to not being written yet.

     

    With the call I show above, there is no way to get a TOTAL of all records in the database, it only shows in this case the last 5 records in a descending order, last to first.

     

    The 5 recent posts box is NOT just a recent posts listing, or there would not be an issue. It is a list of recent posts of certain access levels, and 2 other filters. It is not just the last 5 records in the database.

     

    My questions are:

     

    1: How can I get TOTAL records in the database from the call I show above?

    Antonio, showed a method, I will try it out when I start working again.

     

     

    2: Is it possible to reduce these database calls into ONE call?

     

    I am not sure about this. I am checking the pagination call to see if there are any matching top 5 records and this could save on the second call needed OR reduce the amount of records required to filter in the second call. The second call is not written yet and would be a separate SQL to the one listed above.

     

     

    3: How can this process be further optimized?

     

    This is a brain teaser problem. Can this process be optimized further?

     

     

    You do NOT want to retrieve all records and rows into an Array and then just filter through the array. You will end up crashing the server if there is a lot of traffic and/or a lot of records. The useable memory will get used up fairly quickly.

     

    Lets say you had 1200 records and you are doing 30 filters with the data on page one. You have 2500 unique visitors a day each one retrieving all 1200 records to do 30 filters on page one... At the very least your site will crawl to a halt and your site would disappear from the search engines rankings, due to not loading. Getting all records and performing calculations, filters etc.. is a very bad idea unless you have a very small amount of records at all times.

     

    I tried to detail the exact process in my first post, step by step what it is doing. I hope this post clarifies further.

     

    Thanks.

  14. This does not fit exactly with this book but I am not sure where to post this. I have a situation where I am trying to reduce database calls and am trying to come up with a possible solution.

     

    This is an example from a CMS I am putting together which I am updating based in part from the site one example.

     

     

    I came up with an idea of how to go about it but wanted input to see if others had better way.

     

     

    Query:

     

    SELECT * FROM posts WHERE posts.status='publish' AND posts.level='".$access_level."' ORDER BY id LIMIT $pageNum, $totalRows

     

     

    This is a pagination call to the database to retrieve X rows per page, so each query only has X posts returned.

    A new call is queried on each page and then X more posts are retrieved based on WHERE filters and then I have

    further filters in code.

     

    I have a top 5 recent posts box, based on filters via IF statements, that display post titles only.

     

    It is possible to limit posts retrieved per page to any number.

    Lets say there are 50 total posts in the database and only 5 posts shown per page.

    This makes 10 pages and 10 separate calls to display all records, 5 at a time.

     

    I want to limit MySQL calls and wondered if there is a way to do the following in one call.

     

     

    Problem:

    --------------

     

    50 total records.

     

    5 records per page.

     

    10 total pages.

     

    Each call results in only 5 records being pulled from database.

     

    Top 5 recent posts displayed by title, filtered by access_level ordered by record id in descending order.

     

    It is possible that less than 5 records matching filters for top 5 recent posts, will not be return in ONE call.

     

    Example:

    -----------

    Possible 2 on page 1, another 1 on page 4 and remaining two posts on pages 7 and 9,

    making it total of 5 top recent posts filtered by access_level and several other factors not mentioned.

     

    To reduce MySQL calls I use the result from the initial page query and see if any matching records are inside.

    If so then it adds the title of the post to the Recent Posts box on the page.

     

    I am using a WHERE clause for this, resetting the seek of the mysqli result array to the 0 element and setting two counters.

    One counter to count the total matching rows so it knows to stop when it reaches 5 matching rows

    and a second counter to check total rows and compare to current loop iteration and break out of the WHILE loop

    or else a never ending loop would happen if there are indeed less than 5 matching rows found.

     

     

    Question:

    -------------

     

    Is there a way to get TOTAL records in the database from the single query above using MYSQLI functions or something?

    Right now I only get the total records returned by the current page, which in this example is 5 records NOT 50.

     

    Right now if there are no matching records or less than 5 in the first return result then I need to make a second call

    to the database to bring out all records. It would be simpler to setup a dedicated query just for this and let mysql

    handle this for me but I wanted to see if there was a way to do it in one call not two seperate ones.

     

    The best solution I can come up with is to check the current returned result from the first query for the page above,

    then if there are not a total of 5 matching results THEN do a second call via a dedicated query for this Top 5 box.

    I would need to compare and filter out results that matched, if any, from the first query or else I would possibly

    only have 5 total results but with 2 or more duplicates.

     

    This is sort of a complex problem but minor compared to scope of project.

     

    I need to know if it is possible to get total records in database from a LIMITed based query

    like the one above.

     

    The more I look at this the last solution I came up with might be the only way, is there better way?

    I am trying to limit as much database querying as possible.

     

     

    Thanks.

     

     

     

    I am using mysqli and currently my testing environment is set to php version 5.3.6 though I alternate between 5.2.17, 5.3.4 and 5.3.6 via a custom software I created that changes the versions for me on the fly.

  15. Look at page 166 Figure 7.3 for diagram of the Product Tables.

     

    Sizes are located in a separate table and linked to the specific_coffee table.

     

    On page 195 section 5 at bottom of page and then continue to page 196 Larry explains what is happening with sizes and how they relate to the coffee products.

     

    As to specific how's of linking the tables, that goes beyond the scope of this book and assumes you know this information already.

     

    Why, in the specific_coffees table, does each coffee only have one size_id?

     

    Walk into any coffee shop, star bucks or what have you and you will see that different sizes are at different prices, which makes them a separate product.

    On page 196 he says as much but also adds the equation limited supplies of products.

     

    Where he is calling this information and putting the sizes together is described starting at the bottom of page 196 under the section Looking at the Stored Procedure Queries. You will need to look through the rest of the book yourself to find specific code using these within the site two example.

     

     

     

    Thanks for your input Terry. I totally understand that the features of the store might be limited in an example store. I just wanted to clarify if that's actually what's happening here, because I'm trying to learn the SQL as we go.

     

    Elsewhere in the forum Larry has said that each coffee was available in 5 sizes. The one size_id for each specific coffee seemed to contradict that.

    • Upvote 1
  16. To understand this in more detail I would recommend going through the first website example again and focus on the PDF upload part of the site.

     

    I learn much better by example myself and that part of the book if you go through it until you understand it, it really goes through this really really well. Even though it is a PDF document the same exact thing works for ANY file format.

  17. How would you go about converting currencies on the fly? You would need to access a service somewhere that has the current value of each currency and then either it converts the currency or you have to do it in code. Do you have any information about this, Larry?

     

    Thanks.

     

     

    How you design your database depends upon the issue I suggested earlier, which perhaps you didn't follow. I understand what you're saying about the user interface, but you'll need to make a decision about how you store prices: using both currencies or using one base currency and converting on the fly.

  18. This was actually answered in the book and in several threads in the forum.

     

    My understanding of indexes in MySQL is that anytime you are going to retrieve information from a table column such as using a WHERE clause in your SQL statements you should index that column. It saves time is the reason. Open up one of the many programming books available, especially the ones in the 800 to 900 page range, if there was no index or table of contents and you needed to know a specific thing, how would you find it? Read through every page and every word until you come to the part you need? What if it is on page 759? are you going to start at page 1 to begin your search?

     

    Indexing allows MySQL or any database software to find things easier. It will still be found if you don't index, computers can go through information quicker than humans, but it may take longer than it would if there was a bookmark to the location.

     

    Primary indexes are for table columns that lead what information is in the table, such as a record_id. Primary indexes columns need to hold unique content such as record id's where there is no duplicates. I have one table in a project database of mine that has no unique columns so I can't use a Primary index but since I use each column at some point in a WHERE clause each column in this particular table is set as an index.

     

    I am not expert in SQL, I understand enough about joins that they bring two tables or seperate databases together and allow you to query the data, that's it though. I use a 3rd party tool to actually create complex SQL for me because I don't know how to do it myself or it would just take too long. I believe there is a QRY builder built into PhpMyAdmin, I haven't really used it much though.

     

     

     

     

    I`m looking at the tables that are being created on page 169 of teh book.

     

    In each table, Larry creates a Primary key(id) and also usually some other keys. For example in the carts table

     

    Key 'product_type' ('product_type`, `product_id),
    Key 'user_session_id' ('user_session_id')

     

    I understand the syntax of these keys (thanks to another thread on the forum) but I don`t quite understand why these keys are created, and, conversely, why other keys were not created.

     

    What wouldn`t you be able to do if these keys weren`t created?

     

    Are these keys necessary for subsequent SQL statements, like a join?

     

    I`m asking this so that, when I hopefully/eventually create my own MySQL databases, I will know what situations will require creation of extra keys.

     

    Thanks if you can help

    • Upvote 1
  19. I can't speak for larry but my experience with tutorials like the two sites in Larry's book is that they are just basic examples to show enough to get you up to speed on how to create sites like the two examples. There is a lot of things left out, on purpose.

     

    If you want to have more than one size for your coffee store for each coffee go ahead and add more sizes, with the information taught it is very easy to add and expand these two site examples to do a lot more. Though that is entirely up to you. You will never find tutorials that are all inclusive, it goes out of bounds of the instructional nature of the teaching. It is left up to the student to expand and take the basic site beyond what it is.

     

    There are a lot of things that Larry does that I personally do not code that way. I went through the book up to the point I needed information and did it exactly how Larry has it in the book and in the downloaded examples. Once I understood what was going on I was able to alter the code to the way I needed it to work and merged in with my own projects. A lot of things was not in the book or here on the forum, I had to go else where to find examples and further instruction but in the end I was able to do what I needed.

     

    Those are my thoughts, but for the specific reasoning and thinking behind why Larry did things they way he did, Larry will need to reply specifically.

    • Upvote 1
  20. ..The SSL is working on the admin folder files, without giving a warning. The https is green.

     

    This confuses me.

     

    In the admin folder you must have the shared url hard coded somewhere because your domain name url doesn't have an SSL.

     

    If you change the main htaccess file as mentioned above it all should be working under the shared url including the admin folder.

  21. Yes. This behavior is interesting, and quite unlike Java, VB, C based languages, etc...

     

    Like I said I have not seen that done in php before but if you got it all to work how you need it to then great.

     

    I do not know if there would be a time savings enclosing a function within a function you happen to call it from, sort of a private function, but if there is I can't see it being very much versus being included from a separate file, the call to the function itself is no different.

     

    Glad you got it all working.

×
×
  • Create New...