Jump to content
Larry Ullman's Book Forums

100yen

Members
  • Posts

    42
  • Joined

  • Last visited

  • Days Won

    1

Everything posted by 100yen

  1. hi HartleySan. Thanks for helping out ! Sorry my explaination might not be clear enough. Here is html code in the problem area, <div id="content_area"> </div> Before a category is selected, the target area is empty. After I selected a category, ajax responses fill in the empty content_area with product data. <div id="content_area"> <table cellspacing="3" cellpadding="3" align="left"> <tbody> <tr class="product_row"> <td> <table class="p_frame"> <tbody> <tr> <td> <img width="80px" src="images/product/resized/DiamondGRP_110x110.jpg"> </td> <td> <p class="add_small">DM100</p> <p> </td> </tr> </tbody> </table> </td> <td> <td> </tr> <tr class="product_row"> <tr class="product_row"> </tbody> </table> <p id="test">get this value</p> </div> </div> Then inside the javascript init function, I add document.getElementsByClassName( ) trying to select the product_sku (<p class="add_small">). I am not able to select it. I tried to select different id or class from same ajax response content, all of them failed. If I select a html element that is not a return from ajax response, that would work. I think that the problem is when init function runs on window just loaded, the <p class="add_small"> does not exists. It only exist later when I choose a category. Therefore, getElementsByClassName( ) returns false. Is that the case? Any possible solutions? thanks again.
  2. full codes on the problematic page window.addEventListener('load', ajax_init, false); function ajax_init(){ // btn to close modal window if(utility.$('close_window_btn')){ utility.$('close_window_btn').onclick = close_window; } // btn for open modal window if(utility.$('add_product_modal')){ utility.$('add_product_modal').onclick = open_window; } var ajax = getXMLHttpRequestObject(); if(ajax){ // selecting primary category if(utility.$('primary_cat_modal')){ utility.$('primary_cat_modal').onchange = function(){ // get the value from select tag var parent_id = utility.$('primary_cat_modal').value; //alert(document.getElementsByTagName("option")[parent_id].value); ajax.open('get', 'sub_cat_ajax.php?pid=' + encodeURIComponent(parent_id)); //alert(parent_id); ajax.onreadystatechange = function(){ handleResponse(ajax); } ajax.send(null); return false; } } // End of check DOM // selecting subcategory if(utility.$('sub_category_modal')){ utility.$('sub_category_modal').onchange = function(){ var category_id = utility.$('sub_category_modal').value; ajax.open('get', 'get_products_ajax.php?cid=' + encodeURIComponent(category_id)); ajax.onreadystatechange = function(){ handleResponse_product(ajax); } ajax.send(null); return false; } } // add products if(document.getElementsByClassName("add_small")){ var add_product = document.getElementsByClassName("add_small"); for(var a = 0; a < add_product.length; a++){ add_product[a].onclick = function(){ alert(this.value); } } } } // End of ajax IF } // And of initAjax function function handleResponse(ajax){ if(ajax.readyState == 4){ if((ajax.status == 200) || (ajax.status == 304)){ var sub_cat = utility.$('sub_category_modal'); //alert(ajax.responseText); sub_cat.innerHTML = ajax.responseText; } } } function handleResponse_product(ajax){ if(ajax.readyState == 4){ if((ajax.status == 200) || (ajax.status == 304)){ var products = utility.$('content_area'); products.innerHTML = ajax.responseText; } } } function close_window(){ var close_win = utility.$('add_product'); close_win.style.visibility = "hidden"; } function open_window(){ var open_win = utility.$('add_product'); open_win.style.visibility = 'visible'; }
  3. I have been making a menu to select primary category and ajax returns the subcategory. when I picks an option in subcategory category, the ajax then return the products under the subcategory. So far so good those steps do works. My next step is to click a specific product that is returned by Ajax response and have it calling ajax again to update its newsletter setting in the product DB. The problem here is that I am not able to select the product. Each product_sku have a <p>tag with a class "add_small". So I want to know any possible solution to deal this situation. thanks. // Not able to select add_small class which comes from ajax response if(document.getElementsByClassName("add_small")){ var add_product = document.getElementsByClassName("add_small"); for(var a = 0; a < add_product.length; a++){ add_product[a].onclick = function(){ alert(this.value); } } }
  4. Thank you Larry . Excellent books helps a lot . Can't wait to buy your new Effortless E-Commerce with PHP and MySQL (2nd Edition).
  5. oh,, just figure out the right method to do. Thanks anyway. In case someone else looking for something, I post the code here $_SESSION['redirect_to'] = $_SERVER['REQUEST_URI']; header("Location: http://example.com/login.php"); exit(); // On successful login $redirect = $_SESSION['redirect_to']; // unset the session var unset($_SESSION['redirect_to']); header("Location: http://example.com/$redirect"); exit(); source: http://stackoverflow.com/questions/7004530/redirect-to-page-user-was-trying-to-get-to-after-logging-in
  6. Hi, Here the case is when user clicks a link, if he/she is not signed in yet, then he/she will be redirected to a login page. After the user successfully logged in, user should be redirected to the page url he/she intended to visit. I have no clue how to do this, but I found article online suggesting to store url into $_SESSION array before user has logged in. So I tried to store what url visitor clicked into $_SESSION['targetPage'], then redirect to login page. When user does a successful login, then $_SESSION ['targetPage'] is plugged into header function to redirect user back to the page intend. This method doesn't work, but I think the direction might be right. So please kindly give me some thought how to make this working. thank you very much! if(!isset($_SESSION['user_id'])){ // try storing the url that user clicked in $_SESSION $_SESSION['targetPage'] = $_SERVER['REQUEST_URI']; $url = 'login_page.php'; header("Location: $url"); exit(); } else { // save the session user id into $user_id $user_id = $_SESSION['user_id']; }
  7. Hi HartleySan, thanks again for help. I am trying to do a category menu in the front page that visitor can mouse over the menu and display the category and subcategory. I used Larry's method to sort out the category array by the parent_id, and then apply a JQuery plugin called super fish to create this menu. Everything seems work except the extra yielded <a> cause the blank space in the first space ( the red rectangle highlight). I still want to solve this problem.. if I can. or leave as it is since the most part of menu is functioning.
  8. HI, again I tried to use the array looping technique from PHP advance book (chapter 1) on my project to output products category into <ul><li> list then apply superfish menu on them. But the output created extra <a>element which slightly messed up the superfish menu. I double check the make_list() function script and I can't tell why the extra <a> tabs are created. the code: <?php require ('includes/config.inc.php'); require (MYSQL); function make_list($parent) { global $cat_list; echo '<ul>'; foreach($parent as $catid => $c_list){ echo "<li>"; echo "<a href=\"index.php?cid=$catid\">"; echo $c_list; if(isset($cat_list[$catid])){ //$catid = parent_id make_list($cat_list[$catid]); } echo '</a></li>'; } echo '</ul>'; //close the ordered list } // end of make_list function $query = "SELECT cc.category_id, cc.parent_id, c.category FROM categories AS c INNER JOIN category_categories AS cc USING (category_id) WHERE c.publish = 1"; $result = mysqli_query($dbc, $query); if(!$result) echo mysqli_error($dbc); if(mysqli_num_rows($result)>=1){ $cat_list = []; while (list($c_id, $p_id, $cat) = mysqli_fetch_array($result, MYSQLI_NUM)){ $cat_list[$p_id][$c_id] = $cat; } } make_list($cat_list[0]); ?> the problem output(first few lines) <ul> <li> <a href="index.php?cid=1">Box Collection</a> <ul> <a href="index.php?cid=1"></a> <li> <a href="index.php?cid=1"></a> <a href="index.php?cid=32">Classic Preminum Brown Hardwood Boxes</a> </li> <li> <a href="index.php?cid=35">Hat Boxes</a> </li> <li> <a href="index.php?cid=36">Leatherette Boxes</a> </li> I expect the output to be: <ul> <li> <a href="index.php?cid=1">Box Collection</a> <ul> <li> <a href="index.php?cid=32">Classic Preminum Brown Hardwood Boxes</a> </li> <li> <a href="index.php?cid=35">Hat Boxes</a> </li> <li> <a href="index.php?cid=36">Leatherette Boxes</a> </li> From the make_list() function, which seems fine, I can't tell why the extra <a> is created. Please anyone can tell where is the problem?
  9. hi, HartleySan, thank you for replying, and sorry for the duplicated post. I tried to modify the menu script that Larry wrote in his Modern Javascript book and to hook up with php script for supplying the data into the options array. However I was only able to do that for the top category menu. The subcategory menu, as you mentioned, is very difficult to do so. Is that possible to achieve the dynamic menu with Ajax? I haven't studied Ajax yet, so I am not sure how much Ajax can do. Please let me know and thank you again.
  10. Hi, Larry In your Modern Javascript book, there is a dynamic select menu for picking operation system. Is that possible to create a similar menu in php? or manipulate that menu with php. I have 2 tables table 1: category contains category_id, category table 2: category_categories contains id, category_id, parent_id so I want to create a primary menu to have all categories that the parent_id equals to 0 (the top), when user select a category from primary menu, the subcategory menu become selectable and load the subcategory under the primary category that user has selected. Thank you all
  11. problem solve after taking break, just move the $iid = array() out the foreach loop, then it can hold all the results from foreach loop.
  12. Hello, I tried to insert multiple image into image table using a foreach loop. The images insertion have returned no problem. But I wanted to catch the image_id of each insert and putted them into an array ($iid). Here is problem, no matter what I tried, the result always return 2 separated array: Array([0]=>9193) Array([1]=>9194). Is there anyway to solve this problem? I want Array([0]=>9193 [1]=>9194). Thank you all!. CODE: // query for media $query = 'INSERT INTO media (file_title, file_mimetype, file_type, file_url, file_url_thumb, file_is_product_image, created_on) VALUES (?,?,?,?,?,?,NOW())'; $stmt = mysqli_prepare($dbc, $query); $i = 0; foreach ($image_name as $key => $value){ $dot = strpos($value, '.'); $file_title = substr($value, 0, $dot); $extension = str_replace('.','',substr($value, $dot)); $file_mime = 'image/'.$extension; $file_type = 'product'; $file_url = $value; $file_url_thumb = 'images/product/resized/'.$file_title. $upload->getSuffix().$extension; $fipi = 1; mysqli_stmt_bind_param($stmt, 'sssssi', $file_title, $file_mime, $file_type, $file_url, $file_url_thumb, $fipi); mysqli_stmt_execute($stmt); // check result: if(mysqli_stmt_affected_rows($stmt) == 1){ // get the image id $iid = array(); $iid[$i++] = mysqli_stmt_insert_id($stmt); print_r($iid); // RESULT: Array([0]=>9193)Array([0]=>9194) } else { echo 'Insertion failed' . mysqli_stmt_error($stmt); } } // End of foreach
  13. oh.... I just figure out... I should quote the $file_title instead of plainly passing it into VALUES. sorry for such stupid problem..
  14. Hi, I get a strange problem when I try to insert images data into media table. I decided to break down the problems section by sections. I rewrite the query using basic mysqli_query() function and insert one row at a time for testing purpose. Here is the strange thing, when I pass the value as variable into the VALUES, the insertion will fail and return an error: 'insertion failedUnknown column 'edvard' in 'field list''. However, If I just pass the string into VALUES(), the insertion goes without problem. I haven't run into this sort of problem before, can anyone explain the reason please. Thanks again! Failed Code: $file_title = 'edvard-much-the-scream'; $file_mime = '.jpg'; $product_type = 'product'; $file_url = 'edvard-much-the-scream.jpg'; $fipi = 1; $query = "INSERT INTO media (file_title) VALUES ($file_title)"; // this fails mysqli_query($dbc, $query); if(mysqli_affected_rows($dbc)==1){ $iid = mysqli_insert_id($dbc); echo $iid; } else { echo 'insertion failed' . mysqli_error($dbc); } Success Code: $file_title = 'edvard-much-the-scream'; $file_mime = '.jpg'; $product_type = 'product'; $file_url = 'edvard-much-the-scream.jpg'; $fipi = 1; $query = "INSERT INTO media (file_url) VALUES ('edvard-much-the-scream')"; // this works mysqli_query($dbc, $query); if(mysqli_affected_rows($dbc)==1){ $iid = mysqli_insert_id($dbc); echo $iid; } else { echo 'insertion failed' . mysqli_error($dbc); }
  15. Hi, HartleySan. The code is working now thank you very much for helping out. I have question following about inserting multiple product images into image table and get multiple image_id using mysqli_insert_id. Should I ask in this thread or open a new one specific for it?
  16. Hello there, It's my first time asking question here, so I want to introduce myself a little bit. I was a graphic designer and 3D artist and I have never done any website except using iWeb. The company I work for needs to rebuild its website which has roughly four thousand products in the database. The website was built by hosting company using a open source CMS called OScommerce. My given project was to create a independent website using php and mysql that can be easily modified to suit the need when time is changing. When I receive the project around beginning of this year, I knew nothing about php and mysql so I went to amazon to buy 2 books. One was "PHP Solutions Dynamic Web Design Made Easy" and other one "Modern Javascript". I studied through both books. The PHP Solution is a good book, but not for absolute beginner. The way it organizes its content, mixing basic and advance concerts and methods together into one book keeps new learner confused and frustrated. So I wasn't really fully understand the material I have learned. As comparison, Modern Javascript is much clear to me and easy to learn. After finished Modern Javascript, I found Mr. Larry also wrote other php books, so I decided to pick up PHP and MySQL for Dynamic from Amazon. As I studied through (followed all examples), I found this book really helped me clear up the concert that I wasn't really clear of. So ends up, I purchased all php book from Larry and finished all of them. Now back to the question, I run into problem to insert timestamp into prepare statement, after some goolge search, there is answer in stackoverflowhttp://stackoverflow.com/questions/14096220/datetimes-now-function-does-not-work-with-pdo-mysql-prepared-statements I change my script according to it, but it returns an error: mysqli_stmt_bind_param(): Number of elements in type definition string doesn't match number of bind variables. I have no idea now, can anyone show me the right way to this please. Thanks. CODE: // query for products table $query = 'INSERT INTO products (product_sku, product_name, product_size, product_color, product_packaging, product_desc, created_on, new_arrival, feature, publish) VALUES (?,?,?,?,?,?,NOW(),?,?,?)'; $stmt = mysqli_prepare($dbc, $query); mysqli_stmt_bind_param($stmt,'ssssissiii', $sku,$name,$size,$color, $packaging,$desc, $new_arrival,$feature,$publish ); mysqli_stmt_execute($stmt);
×
×
  • Create New...