Jump to content
Larry Ullman's Book Forums

Paul

Members
  • Posts

    147
  • Joined

  • Last visited

  • Days Won

    1

Everything posted by Paul

  1. Hi all, Please see the following code: if (isset($_POST['curtain_add_to_basket'])) { $quote_no = $_POST['basket']; //request the inclusion of the mysql connect file require (MYSQL); $q = "SELECT * FROM saved_curtains WHERE quote_no. = '$quote_no' "; $r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n MySQL Error: " . mysqli_error($dbc)); if (mysqli_num_rows($r) == 1) { echo' It worked '; exit(); } } I get the following error: An error has occurred in script 'C:\xampp\htdocs\Dis Soft Furnishings\web\account.php' on line 40: Query: SELECT * FROM saved_curtains WHERE quote_no. = '11' MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '11'' at line 3 Date/Time: 6-15-2012 16:02:21 . print_r(Array, 1) . The quote number should be 11, so it's picking up the correct figure from $_POST. I've tried putting $quote_no in quotes and not in quotes. Same error. Any ideas would be great. Cheers Paul
  2. Hi all, I've had a problem which I've now solved but I feel that my solution is not a good one and would like to try and understand a better way of doing it. A user gets a quote and the information is stored in a session. In order to save that quote to the database I require the user to register. This information also went into a session. If the user logged in first and then got a quote then everything was fine. If the user got a quote first and then either registered or logged in then the process of logging in overwrote the session data stored for their quote. Somehow I was structuring my sessions incorrectly. I have tweeked the structure of my users session array and now it worked, which is great. But I'm being reactive rather than understanding how sessions should be structured. So in my example above what is the correct way to store mulitple information in a single session. If it were arrays you would just give the arrays different names: $users = array(user_id => 1, first_name => Paul....etc); $curtain_quote = array(drop => 100, width => 200....etc); But $_SESSION is a fixed name (isn't it). How do you assign multiply information to this single array? Can $_SESSION be treated as the name of a multidimensional array. Not sure if the syntax is strictly correct but something like: $_SESSION = array ('users' => $users = array(user_id => 1, first_name => Paul....etc), 'curtain_quote' =$curtain_quote = array(drop => 100, width => 200....etc), etc etc; Would this be correct? Cheers Paul
  3. Sonal, Thanks for your help, it worked. Apologies, though, I really thought I'd tried that...must have done everything else but. Cheers Paul
  4. The above HTML has not shown correctly, it should be: <p>Track or Pole? Track<input name="track_pole" type="radio" value="track" checked="checked" />Pole<input name="track_pole" type="radio" value="pole" /></p> Sorry. Paul
  5. Hiya, I have created the following field in a form. Track or Pole? TrackPole As you can see the values are strings. I have then assigned these values to a variable in a session. I am now trying to insert this value into a database and have received the following error: An error has occurred in script 'C:\xampp\htdocs\Dis Soft Furnishings\web\confirm_save.php' on line 56: Query: INSERT INTO saved_curtains (user_id, fabric_drop, track_pole, track_pole_width, heading_tape, lining, fullness, interlined, fabric_width, final_curtain_quote, saved_date) VALUES (6, 100, track, 100, three, regular, 2, no, 139, 56.20, NOW() ) MySQL Error: Unknown column 'track' in 'field list' Date/Time: 6-5-2012 13:54:52 . print_r(Array, 1) . I then changed the values of 'track' and 'pole' to '1' and '2' and the error changed to exactly the same except: MySQL Error: Unknown column 'three' in 'field list' This proves to me that the problem lies in the fact that it doesn't like the string as opposed to a number and that the error just moved 'along' to the next string. I understand that in SQL strings need to be quoted but how do I quote: {$curtain_list['track_pole']}, which is the extract from the INSERT command? I have tried changing the quotes in the original HTML form and also various ways on the INSERT command but doesn't seem to work. Cheers Paul
  6. Blimey, HartleySan, you read the entire EU Cookie Law, that was brave! Were you having trouble sleeping? Thanks for the quick response and my impressions were similiar with regard to the EU law and to tracking cookies. Cheers Paul
  7. Hi all, I've seen a bit in the news recently about Microsofts announcement that IE10 will have tracking cookies turned off by default. Apparently Chrome and Firefox are both doing the same. I have also read that recent EU legislation appears to demand that a website declare that it uses cookies, although there does appear to be a bit of confusion about this. Would I be right in thinking that the type of cookies used for php work and sessions are not tracking cookies and that tracking cookies are a certain type (flavour!) of cookie that are a potential privacy issue? What makes we wonder otherwise is that 2 websites I've visited recently, the BBC news and a UK building society both have big banners accross the top declaring that their websites use cookies. As someone who is currently working on a website that will use sessions if the user turns off cookies (or they are turned off by default) the whole website would grind to a halt, would it not? Sessions need a cookie don't they? Does anyone have any thoughts on this? Cheers Paul PS Larry. Would this be a good subject for your excellant monthly 'What is Larry thinking?'?
  8. Hi all, I have the following code that DW is telling me is incorrect syntax. $q = "INSERT INTO saved_curtains (user_id, fabric_drop, track_pole, track_pole_width, heading_tape, lining, fullness, interlined, fabric_width, final_curtain_quote, saved_date) VALUES ('$_SESSION['user_id']', '$curtain_list['fabric_drop']', '$curtain_list['track_pole']', '$curtain_list['track_pole_width']', '$curtain_list['heading_tape']', '$curtain_list['lining']', '$curtain_list['fullness']', '$curtain_list['interlined']', '$curtain_list['fabric_width']', '$curtain_list['final_curtain_quote']', NOW() ) "; I suspect it's something to do with all the quotes. I have tried changing the single quotes into double quotes every way I could and nothing seems to fix it. Any ideas? Cheers Paul
  9. Antonio, Thanks for your answer. It won't surprise you to know that it's not so much the function I've a mental block with, I think I can see how it works now, it's where it gets it's initial value from that I still don't get. I think I have a real problem 'seeing' how arrays are structured. Can I go through the process step by step, please tell me where I'm wrong: 1. Line 56. The database query. It returns the task_id, parent_id and task in an array. Key 0 = the first line in the database, key 1 = the second line etc. This cannot be accesssed until mysqli_fetch_array is run. 2. Line 62. The while loop. It loops through the database array one line at a time assigning task_id, parent_id and task to variables of the same name. That's what the 'list' code does. Without the 'list code it would assign the values as, well, values in an array. Not sure I've explained that clearly but by using variables they can be used in the line below. 3. Line 65. For each loop that it does this for it assigns $task_id, $parent_id and $task to an array called $tasks. Now this is the line I don't get......... Actually in working through this I think I might get it. Could the same thing be achieved a much more long winded way such as $parent_id = array($task_id => $task); $tasks = array($parent_id) . This line is creating the array from scratch isn't it, but using variables from lione 62? 4. The bit I still don't get is the $parent parameter. I think I don't understand what that parameter does. Is $parent an arbituary word that is only used within the function? Could I call it $mickey_mouse and providing I used Mickey later in the function (i.e. in the foreach loop) then it will work. The actually information that the function uses is assigned when the function is called, i.e. $tasks[0]. Am I warm? Thanks once again for your help and patience. Paul
  10. Hi, I'm working on script 1.3. It works OK but I can't see how. 1. The user defined function: function make_list($parent) requires the parameter $parent. I can't see where it gets $parent from. 2. The only other mention of $parent is within a foreach loop: foreach ($parent as $task_id => $todo) {etc.... My understanding of foreach loops is that $parent should be an array but I cannot see in the script where an array called $parent is created. The only array I can see being created is $tasks. Any clarrification would be great. Cheers Paul
  11. Hi all, I have been studying the above subject at the very beginning of Larry's book. At first I just didn't get it and couldn't understand how it would work. I've found the attached question and answer that was clear enough even for me to understand. If you're interested in knowing what actually happens with the sort then take a look. http://stackoverflow.com/questions/1091740/phps-usort-callback-function-parameters I know that this isn't strictly a question but I hope this helps anyone who's interested. Cheers Paul
  12. Gents, Sorry for the delay in responding, had a couple of days off. Paul Swanson, that was great. I changed the code as you suggested and added the $key bit that HartleySan suggested and it worked. Well, actually it worked too well! It added the entire curtain_basket array togethor, the drop numbers, the width etc. which made it a very expensive quote! I played with it a bit more to try and isolate just the totals but couldn't get it to work. I then tried Antonio's suggestion and bingo! I added a touch of number formatting and it works a treat. By way of a little but more information, as I mentioned above I used the unset function to allow users to remove individual array elements (quotes) from the quote array. Antonio's suggestion came up with an error because using the unset function doesn't reset the keys, so the incremental loop failed. So I used the array_values function to reset the keys and Antonio's answer worked a treat. You have no idea how much I have learned from this epic and really appreciate all the help from everyone. Cheers Paul
  13. Guys, Antonio, wow. That post must have taken you ages. I read it and then stepped away from the computer, made myself a coffee and had a think about the points you were making. I have no idea how objects work. I think that Larrys PHP5 advanced book starts on those which I have just began. I think I need to divide my time up between trying to make this project work and learning more. A little bit more background may help here. A curtain quote is 99% labour. There are nearly no products, except for fabric that the user supplies. The pole refers to the width of the window, which the user supplies (usually already attached to their wall!). Most of the information collected from the form, and therefore the contents of the quote arrays are in order to supply the parameters for making them. There is no fixed stock to choose. It is very different from say, ordering a car, where you choose additions to the main car that can be stored in a database. The closest analogy I can think of is of supplying a website. Primarily the supply of a website is only labour cost, there is no stock. However there are loads of parameters that affect that quote. HartleySan. I tried as you suggested: $total = 0; foreach ($SESSION['curtain_basket'] as $key => $value) { $total += array_sum($SESSION['curtain_basket']); } echo '<p>Total: ' . $total . '</p><br />'; There was the same error message. Is there a way of accessing each final quote array element, assigning them a different variable, like $final_quote0, $final_quote1, $final_quote2 and then adding them togethor. It would mean that the array_sum function would not be needed? Just trying to think outside the box a bit. The irony here is that this is the last bit of the basket page. I've just finished the process whereby a user can remove individual quotes from their basket and it worked fine. What I'm thinking as a last resort is that I could not supply a total on the basket page. The next step is that the user either goes somewhere else, the basket is not saved and the session is removed or they 'checkout' in which case they will be asked to register and then the quotes can go onto a Db and I can total them up from there. Not perfect but needs must....... This is good stuff. Cheers Paul
  14. Hiya, A 3D array...blimey! The reason I have $_SESSION['curtain_basket'] is that once I get this lot to work I need to do the same thing for Roman Blinds, i.e. $_SESSION['roman_blinds'] . So I could have a user that has multiple curtain and Roman Blind quotes in their basket. I tried your suggestion as follows: $total = 0; foreach ($SESSION['curtain_basket'] as $key => $value) { $total += array_sum($SESSION[$key]); } echo '<p>Total: ' . $total . '</p><br />' ; I got the following errors: An error has occurred in script 'C:\xampp\htdocs\Dis Soft Furnishings\web\basket.php' on line 80: Undefined variable: SESSION Date/Time: 4-25-2012 13:08:11 . print_r(Array, 1) . An error has occurred in script 'C:\xampp\htdocs\Dis Soft Furnishings\web\basket.php' on line 80: Invalid argument supplied for foreach() Date/Time: 4-25-2012 13:08:11 . print_r(Array, 1) . Total: 0 From your code above would it not be trying to add up 0, 1, 2 etc depending on the number of quotes in the basket. Just trying to understand how it would know to add up 'final_curtain_quote' within each quote, which is only one of many elements. Is there a way of using foreach loops nested within foreach loops to reach 'inside' and then use the '+=' bit to add them togethor? The idea is there, the syntax is beyond me. Something like: foreach ($SESSION['curtain_basket'] as $value) { foreach ($value as $sum) { $total += array_sum($sum['final_curtain_quote']); } } echo '<p>Total: ' . $total . '</p><br />'; Cheers Paul
  15. Gents, To summarise. I have a MD array that is a session. The top level is a basket which is an indexed array of quotes. Each quote is an assoc. array. The user can add quotes to their basket. It would have been far esaier to have used a DB but I don't know who the user is until they checkout. One of the keys (is that the right word?) of each quote array is a price. It is this price that I'm trying to total on the basket page by accessing that part of each quote array. I did actaully try Antonio's approach using a foreach function on $_SESSION['curtain_basket']['final_curtain_quote'] and then the '+=' but I think I was getting my syntax wrong because it didn't work, as per below. I came to the conclusion that there must be an easier way to do this so looked at the PHP Manual and tried array_sum. I looked at it again this morning and am still not clear on what can and can't be 'summed' with an array_sum function. For instance HartleySan is using array_sum($_SESSION[$key]). Anyway I tried the following: $total = 0; foreach ($_SESSION['curtain_basket']['final_curtain_quote'] as $value) { $total += $value; } echo '<p>Total: ' . $total . '</p><br />'; Now I think that the above is failing because $_SESSION['curtain_basket'] does not have a key called 'final_curtain_quote', its keys are 0,1,2 etc. If I wanted to access the 'final_curtain_quote' information I would need to access: $_SESSION['curtain_basket'][0]['final_curtain_quote'] $_SESSION['curtain_basket'][1]['final_curtain_quote'] $_SESSION['curtain_basket'][2]['final_curtain_quote'], etc. Is that right? But how to do that in a foreach loop? If I could do that then I can use '+=' as per both your suggestions. Cheers Paul PS. I do very much appreciate all the help I receive in this forum, especially you guys and Larry and if we weren't spread around the world I'd buy you a beer (or 2)! If it's any conciliation for the seemingly endless stream of questions I do spend quite some time trying to work it out myself before resorting to this forum. I also don't just want to know the answer of how to do something but try to understand why it's working. Anyway, if I don't come accross as appreciative as I should, I'm sorry.
  16. Hi all, I have a multidimensional array in which one of the elements is a number. I want to be able to add these numbers togethor to get a grand total for this element from all the arrays within the multidimensional one. I've tried the following line of code: echo' <p>Total:' . array_sum($_SESSION['curtain_basket']['final_curtain_quote']) . '</p><br />'; I get the following error: An error has occurred in script 'C:\xampp\htdocs\Dis Soft Furnishings\web\basket.php' on line 55: Undefined index: final_curtain_quote Date/Time: 4-24-2012 16:00:04 . print_r(Array, 1) . An error has occurred in script 'C:\xampp\htdocs\Dis Soft Furnishings\web\basket.php' on line 55: array_sum() expects parameter 1 to be array, null given Date/Time: 4-24-2012 16:00:04 . print_r(Array, 1) . I ran a print_r function for $_SESSION['curtain_basket'] so I know that element is there. Could it be that it's become a string somewhere along the line as opposed to a number? Does array_sum only add the entire contents of an array? Cheers Paul
  17. HatleySan, Fantastic. I added the reference to each echo as above and it worked perfectly. You guessed my intention which is that each new array added to the basket is a seperate quote involving all those 9 parameters. You most certainly didn't come accross as terse or rude.....I struggle with arrays and the foreach function. I'm glad it works but I'm still not at all sure why it works! If I could break it down: After the first iteration of the foreach does $list contain all 9 fields which it then proceeds to list out as per the echo lines or just 'fabric_drop'? Cheers Paul
  18. HartleySan, Thanks for the response but unfortunately it's not simple enough for me. I'm not sure where you're going. Sorry! Paul
  19. Hi all, Further to the above post I continued on and populated the rest of the fields (track_pole etc.) and using print_r could see that they were added to the $_SESSION['curtain_basket'] array OK. I added 3 different quotes and it shows up as keys 0,1 and 2 as expected. So that was great. Thanks for your help and I think I now understand multidimensional arrays a bit better. Next I wanted to display all the quotes on a basket page. So I tried the following code: foreach ($_SESSION['curtain_basket'] as $key => $list) { echo $key; echo $list['fabric_drop']; echo $list['track_pole']; echo $list['track_pole_width']; echo $list['heading_tape']; echo $list['lining']; echo $list['fullness']; echo $list['interlined']; echo $list['fabric_width']; echo $list['final_curtain_quote']; } It came up the following message: Notice: Undefined variable: _SESSION in C:\xampp\htdocs\Dis Soft Furnishings\web\basket.php on line 3 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Dis Soft Furnishings\web\basket.php on line 3 I thought that it would display the $key as 0, then list all the values such as fabric_drop etc. then $key as 1 and list all the values and so on. What am I missing? Can a foreach work with a multidimensional array? Cheers Paul
  20. Antonio, Thanks for your answer it worked well. A further question if I may? Can I assign an existing session array to a multidimensional session array with the following syntax for example: $_SESSION['curtain_basket'][] = array ( 'fabric_drop' => $_SESSION['curtain_quote']['fabric_drop'] ); Is the above correct? Cheers Paul
  21. Hi all, I've been trying to do something for a few days and I can't get it to work. The background is as follows. I have a form which users can obtain a quote for making curtains. The form and the quote itself work fine. Once they have obtained a quote they can add it to their basket. I have created a session that contains all the relevant quote information that can then be taken to a basket page. I can't store this information in a DB because, at this stage, the user has not registered or logged in. I used print_r to see that the session works OK. A single quote has been added to the session. So far standard interent shopping stuff. However I need to write something that takes into account that the user may 'add to basket' and then go back a get a second curtain quote. Now, if that happens it will just overwrite the first quote and the user will still have only a single quote in the basket. So i thought about a multidimensional array for $_SESSION. So I created the following code: $_SESSION[] = array( 'fabric_drop' => $fabric_drop, 'track_pole' => $track_pole, 'track_pole_width' => $track_pole_width, 'heading_tape' => $heading_tape, 'lining' => $lining, 'fullness' => $fullness, 'interlined' => $interlined, 'fabric_width' => $fabric_width, 'final_curtain_quote' => $final_curtain_quote); If I run the script with one set of numbers it gives me a quote and adds it to the session perfectly. If I then run it again it overwrites the first quote. How do I get it to create a second array with the second quote in it? I checked Larry's chapter on e-commerce but that deals with a DB of fixed products. These curtain quotes can, and generally are, completely different. I did think about creating some sort of quote id that increments with each quote obtained. But how do I add it to the session array so that it shows: quote 1 = blah blah, quote 2 = blah, blh etc.? I feel that the answer is an easy one and reflects on my wooly knowledge of multidimensional arrays. Thanks Paul
  22. Hi all, I have a general question. I have worked through Larry's excellant book, I'm just starting the PHP5 Advanced one and also in the process of building a website for my wife business. It's quite ambitious (for a novice like me) with a few databases, a client login area, a shopping basket and e-commerce bit. The problem I'm having is seeing the big picture in coding terms. I spent many days coding one part (the user obtaining a quote and adding it to a basket) before I realised that what I was doing and the way I was doing it wasn't going to work procedurally. I know what I'm asking is a vast subject but does anyone have any tips, or know of any resource on how to begin a multipage, multidatabase project? I tried using Word to draw a 'flow chart' type of thing with arrows from one page to another but it ended up looking like a plate of spaghetti. I thought about creating all the individual files and commenting them before coding to make sure that the flow worked. Apologies for posting such a vague question and if the answer is, 'it comes with experience' then that's absolutely fine. Thanks Paul
  23. Hi, If I create a header include file (as per Larry's examples) why does it finish with the opening body tag and relevant div? Could it not just finish with the closing head tag and the individual pages start with a body tag and div? Does it make any difference? Cheers Paul
  24. Hi all, I have a form that a user fills in to register themselves. I start by making the form variables all FALSE. I then do all the checks, including that none of the variables are still FALSE, and then an INSERT into MySQL. Now 2 of the form fields are not mandatory, i.e. they can be left blank by the user. So, at this stage they will still be set at FALSE. It won't pass the test so won't INSERT. I then removed these two variables from the if($blah && $blah etc.) check. It worked and the INSERT worked as well. I then checked the DB table and the 2 non-mandatory fields that had no entry in them appear in the DB as blank fields. Now to the question (and a typical beginners question with regard to FALSE, NULL, 'blank?'). Are they really 'blank', i.e. empty? When I come to do SELECT queries later on and include those fields am I heading for trouble. What will come back? Would it be better to set the non-mandatory form variables to an empty string, i.e. $blah = ''; if the user did not use them. Then the if... test would work (none would be FALSE) and the DB would get an empty string insrted instead of FALSE. Does it make any difference? Cheers Paul PS. How can I set up this forum so that it emails me all the posts, instead of me having to log in?
×
×
  • Create New...