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 take a look at the following: <td><input name="display_y" type="radio" value="Yes" checked='"if($snippets_list['display']='y'){echo."checked".;}"'/></td> I have queried a database, assigned the values to an array and now I'm trying to preset a pair of radio buttons with the result of part of the query. The query works, I've tested that so I reckon I've messed up my quotes somewhere along the line. I've read the section in the start of Larry's book about single quotes being literal and double quotes being interpreted. I would have thought that the 'if' section needs to be interpreted. The whole lot is in the middle of an echo that contains the test of the HTML table. Also the array value is a string, not a boolean. Thanks Paul
  2. Larry, Thanks for the quick response but I was just about to post that I've sorted it with the following code: if (isset($_POST['submitted'])) //Has the form been submitted? { if(isset($_SESSION['snippets_requested'])) { $snippet_ref = $_POST['snippet_ref']; $_SESSION['snippets_requested'][] = $snippet_ref; } else { $snippet_requested = array(); $_SESSION['snippets_requested'] = $snippet_requested; $snippet_ref = $_POST['snippet_ref']; $_SESSION['snippets_requested'][] = $snippet_ref; } } I figured that I was creating a new session each time the user clicked on a button, so it was always going to stay on the first key. So I've added a check to see if it is the first time or subsequent times. I'm sorry if I wasted anyones time with this. Cheers Paul
  3. Aaargh, the screen dump hasn't appeared. Sorry, I don't know how else to add it. Hopefully you can get the idea from the code. Paul
  4. Hi all, I think the problem may lie with me not explaining myself properly. Please take a look at the following screen dump. This was created with the following code: $q = "SELECT * FROM snippets WHERE (display = 'y') AND (price <= 8) ORDER BY price LIMIT 12 "; $r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); //check that a match was made with the return of 1 row of data //Assign the results to an array if (mysqli_num_rows($r) >= 1) { $snippets_1 = array(); while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $snippets_1[] = $row; } mysqli_free_result($r); } else { echo' <p>There are no snippets in this price range</p> '; } //Create a table for the results echo' <h2>Snippet info</h2> <table width="780px"> '; //Take the array of snippet info and display in a block in a grid //Include a form and button in order for the user to request a snippet foreach($snippets_1 as $snippet_info) { echo' <div class="each_snippet"> <img src="images/snippets/' . $snippet_info['image'].'.jpg" width="180px" height="180px" ><br/> <a href="#" id="openModal">View larger image....</a> <div id="modal"> <div id="modalMask"> </div> <div id="modalContent"> <img src="images/snippets/' . $snippet_info['image'].'.jpg" width="500px" height="500px" > <input type="button" id="closeModal" value="Close"> </div> </div> <p>'. $snippet_info['price'] . '</p> <p>'. $snippet_info['name'] . '</p> <p>'. $snippet_info['description'] . '</p> <form action="snippets_1a.php" method="post" name="snippets_1a" > <input name="snippet_ref" type="hidden" value="' . $snippet_info['snippet_ref'] . '" /> <input name="submitted" type="hidden" value="TRUE" /> <input name="submit" type="submit" value="Request this snippet" /> </form> </div> '; } echo' </table> <br />'; The idea is that a user can click on a button and it'll add the 'snippet_ref' to a session. They can then click on a second button and it'll add that 'snippet_ref' to the session and so on. I attempted to do this with the following code: $snippet_requested = array(); $_SESSION['snippets_requested'] = $snippet_requested; //If a user has requested a snippet place it in a session if (isset($_POST['submitted'])) //Has the form been submitted? { $snippet_ref = $_POST['snippet_ref']; } $_SESSION['snippets_requested'][] = $snippet_ref; echo'<pre>'; print_r($_SESSION); '</pre>'; ?> The problem being that the session keeps getting overwritten and all that changes is the value of first key, as follows: Array ( [snippets_requested] => Array ( [0] => 8 ) ) I hope that makes more sense. Any help would be great. Would Ajax work better in this situation? If so I hope it's not the only solution, I've not learnt about Ajax yet!! Cheers Paul
  5. Edward, Thanks for the quick reponse. I tried what you suggested by doing the following: if (isset($_POST['submitted'])) //Has the form been submitted? { $snippet_ref = $_POST['snippet_ref']; $snippet_requested = array(); $snippet_requested[] = $snippet_ref; echo' <p>The snippet requested is ref. ' . $snippet_ref . '</p> '; $_SESSION[$snippet_requested]; print_r($_SESSION); } When I run it it comes up with an illegal offset error. Paul
  6. Hi all, Please see the following: <form action="snippets_1a.php" method="post" name="snippets_1a" > <input name="snippet_ref" type="hidden" value="' . $snippet_info['snippet_ref'] . '" /> <input name="submitted" type="hidden" value="TRUE" /> <input name="submit" type="submit" value="Request this snippet" /> </form> </div> '; } echo' </table> <br />'; //If a user has requested a snippet place it in a session if (isset($_POST['submitted'])) //Has the form been submitted? { $snippet_ref = $_POST['snippet_ref']; echo' <p>The snippet requested is ref. ' . $snippet_ref . '</p> '; $_SESSION['requested_snippet'] = $snippet_ref; print_r($_SESSION); } The form is generated from a database query and there will be many of them on a single page, each with a different 'snippet_ref'. That bit works OK. The user can then click on any of the buttons. If they do so the code above is meant to take the relevant 'snippet_ref' from the form and add it to a session. The idea being that the page will reload, the user can then click another button with a different 'snippet_ref' and this will be added to the session. A bit like adding stuff to a basket. The echo is there just to check that each button calls the correct 'snippet_ref' and it does. The problem is that the session is just overwritting the last request with the new one instead of adding it on. My feeling is that it has something to do with the page reloading and just creating a new 'snippet_ref' with key zero. Thanks for any help. Cheers Paul
  7. Hi all, Hope you're all well. Please take a look at the following code: if (mysqli_num_rows($r) >= 1) { echo' <h2>Snippet info</h2> <table width="780px"> '; while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { echo' <div class="each_snippet"> <img src="images/snippets/' . $row['image'].'.jpg" width="180px" height="180px" ><br/> <a href="#" id="openModal">View larger image....</a> <div id="modal"> <div id="modalMask"> </div> <div id="modalContent"> <img src="images/snippets/' . $row['image'].'.jpg" width="500px" height="500px" > <input type="button" id="closeModal" value="Close"> </div> </div> <p>'. $row['price'] . '</p> <p>'. $row['name'] . '</p> <p>'. $row['description'] . '</p> </div> '; } echo' </table> <br />'; mysqli_free_result($r); } (I tried using the 'Special BBCode' button to post this but it interpreted the code instead of showing it, if that makes sense. So I had to just cut and paste). I have a database of information which I have queried and styled so that the results appear as a grid, as opposed to a list. That worked fine, it displayed 6 rows of data displayed as a grid. I have then added Larry's 'modal window' example from his book modifying it so that when the link is clicked it shows a larger picture than the thumbnail being displayed by default. Much to my surprise, given my understanding of JS, it worked. However it only worked for the first row of data returned from the while loop. The link in subsequent 'rows' doesn't do anything. I can copy in the JS and CSS code if anyone needs it but it's straight out of Larry's book. I'm struggling to understand why it would work once only as the while loop is returning the same code. As always any help is much appreciated. Cheers Paul
  8. Margaux, Thanks for the answer and sorry for the slow response, I got sidetracked for a while. Your suggestion worked great. I used CSS to absolute position the div whereever I wanted. The only slight change I made (for anyone who's interested) is that I wrapped the whole lot in a div, including the foreach bit, otherwise if there was more than 1 error they just sat on top of one another as opposed to a list. Cheers Paul
  9. Hi all, I have a form with an array for error messages. If there is a problem the error messages appear at the top of the page. I would like to place them halfway down the page. I'm using the following piece of code to display the errors: else { foreach ($registration_error_messages as $reg_error) { echo "$reg_error"; } } In order to do this would I need to assign the $reg_error to another array and then use printr to display this array wherever I want? Or is there a better way? Cheers Paul
  10. Ah, I see that know. Thanks for that. Cheers Paul
  11. sorry the HTML seems to have disappeared. Does this work? <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>A modal window</title> <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link href="modal.css" rel="stylesheet" id="css" type="text/css"> </head> <body> <input type="button" id="openModal" value="Show window"> <div id="modal"> <div id="modalMask"> </div> <div id="modalContent"> <p>This is the modal content</p> <input type="button" id="closeModal" value="Close"> </div> </div> <script src="modal.js"></script> </body> </html>
  12. Hey all, I'm working on Larry's example of a modal window in chapter 9. I've typed up all the code and tested it and the 'show window' button doesn't show the window. I've checked and rechecked the code and I can't see what I've done wrong. This is the modal content @charset "utf-8"; /* CSS Document */ #modal { display:none; position:absolute; left:0px; right:0px; width:100%; height:100% } #modalMask { position:absolute; left:0px; top:0px; width:100%; height:100% background-color:#eee; z-index:1000; opacity:0.9; filter:alpha(opacity=90); -moz-opacity: 0.9; } #modalContent { position:relative; width:3000px; margin:15px auto; padding:15px; background-color#fff; border:1px solid #000; text-align:center; z-index:9999; } // Javascript Document function openModal() { 'use strict' document.getElementById('closeModal').onclick = closeModal; document.getElementById('modal').style.disply = 'inline-block'; document.getElementById('openModal').onclick = null; } function closeModal() { 'use strict' document.getElementById('openModal').onclick = openModal; document.getElementById('modal').style.disply = 'none'; document.getElementById('closeModal').onclick = null; } window.onload = function() { 'use strict' document.getElementById('openModal').onclick = openModal; }; Thanks for any help anyone can give. Paul
  13. Natt, Hi. What is the error message that comes up? Or are they just 'missing'? Paul
  14. Larry Thanks for that, at least I can stay out of the corner for typos, although arguably I should be there for lack of common sense. That's what happens when you copy without understanding. Sorry to have bothered everyone. Paul
  15. Hi all, I have been working on the above excercise and cannot get it to work. According to the Firebug console 'U.$ is not a function ' on the following line of code: U.addEvent(U.$('output'), 'mouseover', updateDuration); I'll put all the code below but including the utilities.js file there's quite a bit of it. I suspect it may be the HTML file (it's the only one I had to do myself!). If it turns out to be a typo I shall go and stand in the corner, I've checked and rechecked the typing on all 3 files. Cheers Paul First the HTML file: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Epoch</title> <link rel="stylesheet" href="default_CSS_stylesheet.css"> <script src="utilities.js"></script> <script src="epoch.js"></script> <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> </head> <body> <p id="output"></p> </body> </html> The epoch.js file: // Javascript Document function updateDuration() { 'use strict'; var now = new Date(); var message = 'It has been ' + now.getTime(); message+= ' seconds since the epoch. (mouse over to update)'; U.setText('output', message); } window.onload = function() { 'use strict' U.addEvent(U.$('output'), 'mouseover', updateDuration); updateDuration(); }; Finally the utilities.js file: // Javascript Document //Utilities script for Javascript //To be added to every HTML page that requires Javascript //Create one global object var U = { //Define the $ method //This method is used to return a document by id $: function(id) { 'use strict'; if (typeof id == 'string') { return document.getElementById(id); } }, //This method sets text in a HTML id setText: function(id, message) { 'use strict' if ( (typeof id == 'string') && (typeof message == 'string') ) { var output = this.$(id); if (!output) return FALSE; if (output.textContent !== undefined) { output.textContent = message; } else { output.innerText = message; } return TRUE; } }, //This method adds and removes event listeners for browsers and IE prior to IE9 addEvent: function(obj, type, fn) { 'user strict' if (obj && obj.addEventListener) { obj.addEventListener(type, fn, false); } else if (obj && obj.attachEvent) { obj.attachEvent('on' + type, fn); } }, removeEvent: function(obj, type, fn) { 'user strict' if (obj && obj.removeEventListener) { obj.removeEventListener(type, fn, false); } else if (obj && obj.detachEvent) { obj.detachEvent('on' + type, fn); } } }; var U = { /* functions */};
  16. Hi all, hope you're all well. A general question regarding constructors and destructors. I can see (I think) why constructors are useful: in that they are automatically and immediantly called when an object is created, so you know that it'll always be done. However I'm confused about what destructors are for. If unset removes objects and it all gets removed when the script finishes anyway what do destructors do? I've worked through Larry's example (script 6.7) so I can see the how and when but I'm missing the why! Cheers Paul
  17. abigail, Great. I shall raise a ticket with my hosting company. Thanks for your help. Paul
  18. Hi all, I'm about to make a site live for the first time that includes a config file and a mysqli_connect script. In Larry's examples of a config file he defines an absolute path to the mysqli login script. In local testing it's "C/blah/blah/etc." In live it will be http:// something. However Larry recommends that the mysqli script is kept outside of the main web directory. I've managed to do that on the hosting companies file structure but how do I write an absolute path to it. If it was in the same directory as say the home page it would be http://www.mywebsite.co.uk/mysqli_connect.php. But how do I write it so it's one directory 'up'? Cheers Paul
  19. Antonio, Ahh, I see now. The date formatting worked and I extended the same logic to the ROUND function for 2 decimal places and that worked. Learnt something today. Thanks Paul
  20. Whoa guys, now I'm confused. HartleySan, thanks for your suggestion I appreciate being pointed in the right direction, don't feel you need to spend time testing stuff. I hope you had a good night! Margaux, I tried your suggestion and it didn't work, the date still came out as the complete date and time. But Antonio's post has got me. If I use DATE_FORMAT in the sql command (and it worked) then why do I need an alias or to do the same formatting in $row. Once it's formatted in the SQL query doesn't it just output that way? I also thought alias's were just a way to save typing a long name. Larry quotes 'An alias is merely a symbolic renaming of a thing in a query.' I'm not understanding the connection between this query and an alias. I also tried looking up whether I could format the date in PHP (as part of $row) . I used: <td>'. $row['date_format(saved_date, 'd/m/y')'] .'</td> It didn't like it. Cheers Paul
  21. HartleySan, I wasn't sure what you meant. I thought an alias was for particular columns or groups of columns that had been selected. But I tried: $q = "SELECT * AS sc FROM saved_curtains WHERE quote_no = '$quote_no' DATE_FORMAT(saved_date, '%D %M %Y') "; It didn't work but thanks for the suggestion. If I don't hear anything I'll try formatting the output in the table itself but I'll be surprised if it can't be done. Cheers Paul
  22. Hi all, I have the following code: $q = "SELECT * FROM saved_curtains WHERE quote_no = '$quote_no' DATE_FORMAT(saved_date, '%D %M %Y') "; It does not come up with an error but it doesn't format the saved_date column either. Now I've had a rummage around Google etc. to try and find out what I'm doing wrong and I'm getting the impression that if I want to format a single column whilst wanting all I need to list out ever single column and format the date one instead of using '*'. Larry's book only gives examples of selecting specific columns and formatting them, not all columns and formatting one of them. Is that correct? I can't believe it is. There must be another way. Cheers Paul
×
×
  • Create New...