Jump to content

mac_gyver

Staff Alumni
  • Posts

    5,518
  • Joined

  • Days Won

    186

Everything posted by mac_gyver

  1. here is the sample code i posted above, with more details showing how it would control the building of the sections - // the business logic retrieves and stores the query results in an array $data. this serves as the input to this section of ocde if(empty($data)){ // there's no matching data, output an appropriate message here... } else { // there is data, start producing the output... $last_type = ''; // initialize this before the start of your loop that's display the data foreach($data as $row){ $strPrefix = substr($row["Prod_Type"], 0, 2); // output the product type heading each time it changes if($last_type != $strPrefix){ // the type changed if($last_type !=''){ // not the first section, close out any previous section here... // close the </div> for example } // start a new section here.... start the <div>, output any type image, .... $img = $types[$strPrefix]['img']; $alt = $types[$strPrefix]['alt']; echo "<img border='0' src='$img' alt='$alt'>"; $last_type = $strPrefix; // remember the new value } // output the data in each recored here.... } // you would close the last section here. whatever logic you have in the if($last_type !=''){ ... } above would be repeated or in a user written function that you call }
  2. i recommend to just rewrite the presentation portion of the code to produce the output you want. the example code i posted above is actually more relevant to this than just the switch/case statement. by detecting the change in the RB, BH, RA type, the code can finish off any html for the previous section and start a new section. the code that displays the data just goes right after the if(){} logic i posted. i do have another general suggestion. separate the business logic from the presentation logic. the business logic contains the php code that figures out what to do on the page and retrieves any data that's need to display the page. the result of the business logic should just be php variables. all the database specific statements should be in the business logic (you would pre-retrieve database data and store it in an array) and there should be no html markup in the business logic. the presentation logic should contain no database specific code, since it will receive any data it needs in php variables. the presentation logic should contain all the html/css/javascript.
  3. lol, spam in the spam filter thread ^^^. the GD based captcha works and should be use in the meantime. the complaint posts about the gd based captcha (from a member that had already written a couple of posts and never replied when asked a question about it and another member that never posted after the complaining post) were most likely from (the same) spammer(s) that just wanted an easier captcha.
  4. we don't really know what you want to do different. can you specifically show an example of what part of the data and exactly how you want it to be done differently? also, this code has several problems that you should fix or change first. the two biggest ones are you should be using css to style everything, not inline styling, and your two nested while(){} loops, that are looping over the same data. by having these two nested loops, the first one is really only acting like an if() conditional statement to run the rest of the code. however, by having these nested loops, the first row of your result set is not being displayed, because the nested loop that's actually displaying the data, is fetching and starting at the second row in the result set. here's a general suggestion that will help reduce that huge switch/case statement, by eliminating the repeated logic and converting the design into a data driven one, rather than a hard-code logic one, and make it easier to change or fix the html markup, because any particular piece of markup will only exist once in the code. define the type information - // define the type information (typically this would be stored in a 'config.php' include file or retrieved from a database table) $types['BH'] = array('img'=>'bulkheads.jpg','alt'=>'Bulkheads'); $types['RB'] = array('img'=>'roofracks.jpg','alt'=>'Roof-Bars'); // ... repeat for all product types initialization, before the start of the while(){} loop - $last_type = ''; // initialize this before the start of your ONE while(){} loop that's display the data at the point where you want to output the product type image inside the loop - // output the product type image each time it changes while displaying the products if($last_type != $strPrefix){ // the type changed, output the image $img = $types[$strPrefix]['img']; $alt = $types[$strPrefix]['alt']; echo "<img border='0' src='$img' alt='$alt'></td></tr>"; // since this markup and code only exists once, if you are changing something about this section of code, you only have to make the change in one place echo $title; $last_type = $strPrefix; // remember the new value }
  5. you actually need separate if() statements, for the if(!$skin)... tests. the value you are testing is dependent on the previous if() statement, not exclusive of it. an if/elseif/else string of test are exclusive of each other.
  6. my supposition about different databases/connections was just one of the many possible things that could be causing the problem. i/we could probably list a dozen different possible things, but won't, because your setup could be doing something different than what i/we have in mind, and the actual problem could be the 13th item that no one thought of. you need to 'close the loop' by providing some investigation and feedback in order to narrow down the possibilities. solving this will require that you actually add the things that i mentioned to the code in order to pin down where and what is actually happening.
  7. the following lines from that code are the relevant ones that you would need to call with the three different sets of $iWidth and $iHeight values, along with the appropriate path where you want the imagejpeg() to save the file - $vDstImg = @imagecreatetruecolor( $iWidth, $iHeight ); // copy and resize part of an image with resampling imagecopyresampled($vDstImg, $vImg, 0, 0, (int)$_POST['x1'], (int)$_POST['y1'], $iWidth, $iHeight, (int)$_POST['w'], (int)$_POST['h']); // define a result image filename $sResultFileName = $sTempFileName . $sExt; // output image to file imagejpeg($vDstImg, $sResultFileName, $iJpgQuality);
  8. you would dynamically build the WHERE part of the $sql = " ... "; string using php code. for something like (no pun intended) a repeated like term, it is easiest to build each term as entries in an array, then just implode the array with the OR keyword. you should have an array with the possible choices that you are using to both build the form and to build the WHERE part of the query (blindly looping over the submitted form data is not a good idea as hackers can submit hundreds of form fields.) as you loop over the defining array, if that particular form input isset(), to indicate the checkbox was checked, add the `manufacturer` LIKE '%abc' portion of the query into an array. when you get done looping over the choices, if the array is not empty, just implode it to form that part of the query (note: implode will produce the correct result even if there is only one entry in the array.) edit: for the example you posted, the code would look like this - $cars[] = 'BMW'; $cars[] = 'Audi'; $terms = array(); if(!empty($_POST['check_list'])) { foreach($cars as $choice){ if(isset($_POST['check_list'][$choice])){ $terms[] = "`manufacturer` LIKE '%$choice'"; } } } $where_clause = ''; if(!empty($terms)){ $where_clause = "WHERE ".implode(' OR ',$terms); } $sql = "SELECT * FROM `Cars` $where_clause ORDER BY `tier` ASC";
  9. sorry to jump in here, but this example code you found is poorly written. it's only claim to fame is it shows up on the first page of web search results or it's being suggested by php programming courses. using it, assuming you can even get it to work, isn't going to help you create web pages or learn how to program. you should just learn to use the underlying framework and write you own code. in short, if you don't have the programming skills to troubleshoot why this example code doesn't work and it doesn't have code already in it to tell you why it isn't working, you need to contact the author of the code to have him/her troubleshoot and fix the problems in it.
  10. in the non-working case, you need to actually pin down what the code and data is doing, before you can even locate what needs to be fixed. you could for example have some broken html on the page for the id = 4 case that's causing the form to be invalid markup and it does not submit any data. the data itself may be invalid (or empty) and is resulting in query errors or a query that's not matching anything. your code could even be running twice for some reason and updating the data correctly, but is also updating it again back to the original values. you need to determine exactly what code is running, what values are being used by the code, and what is being returned from statements. 1. make sure that the code where the update queries are at is actually running. you should be forming the sql statements in a php variable (always), then just use that variable as input to the function that's running the query. you can then echo/log the contents of that variable for debugging purposes so that you will know that the queries are being ran and what data they actually contain. 2. make sure that all the queries are running without any errors. you should (always) test the returned value from a query for errors. an UPDATE query will return a false value when there is a query error. you can echo/log the mysql_error() information to let you know what the error is. and as a side note: you are using both the tep_db_query() and mysql_query() statements to perform queries. you could have a case where there are multiple database connections (even a default one set up by the php installation) and each of those methods of running a query could be operating on different databases. perhaps the database connections for one id = value are all using the same database and the code works, whereas for the other id = value, there are errors or missing data due to multiple database connections being used in the code.
  11. that may be WHAT doesn't work, but exactly what about it doesn't work. are the values not displayed for picking, does the page just 'refresh' without apparently doing anything when the form is submitted, is the form data not present in the form processing code after the form was submitted, are there php errors, query errors, is the data just not updated in the database table, or is the data actually present in the database table but it is not having an affect when it's being used? in short, what symptom do you see in front of you, since we're are sitting there with you to see it, that leads you to believe 'it doesn't work'? there's very little backwardly incompatible differences between most of the php versions, and these things are generally obscure things that most code doesn't use (they are documented in the php.net documentation, appendix.) most problems like this are due to php configuration differences. in fact, only in the php5.4 version were a number of obsolete things finally removed (your use of $HTTP_GET_VARS for example aren't present as of php5.4+). edit: you also mentioned that the problem is associated with - vendors2_id = 16 works, vendors2_id = 4 does not work. the posted code only has vendors2_id hard coded as a 16. is the posted code the actual code that doesn't work and if not, why do you have different code that only differs in a value that's has hard coded that value rather than using one instance of the code and using a variable for the, well, variable value in it? if the code with a vendors2_id = 4 in it doesn't work, perhaps that's the code you need to post.
  12. you should store the raw data for each week/game/player as a separate row in a 'result' table, similar to what you do with a bank account statement. then simply do a sum() in a query to produce a score for any player(s) when needed. this will do two things - 1) keep a specific record of who, what, when, and where something occurred 2) prevent duplicate entries/processing from corrupting the data.
  13. sorry, but no you haven't. look at and compare the second line of code in both of the pieces of code in my reply #8 and in your reply #11. you should be able to notice what's logically different about those lines. in the line that's from your previous thread, you are assigning the result form the ->prepare() method call to a variable, $stmt, that then gets used in the bindParam() and the execute() method calls. programming is an exact science. the computer only does exactly what your code tells it to do. if a statement returns a value that you must then use in the following dependent statements, you must be able to 'get' that kind of information when you read the documentation and examples.
  14. user preferences (and privileges) are usually stored in a database table and retrieved on each page request. this allows them to be easily modifiable by site moderators/admins and they take effect immediately (on the next page request.) storing them in session variables means that only the visitor that the session belongs to can easily modify them or you must add a lot of unneeded complexity to make the session data find-able and editable by site moderators/admins. a 'remember me' login is usually accomplished by generating a unique and hard to guess token, that's not a fixed value tied to any user information, storing that token in a cookie and storing it in the user row in a database table. in this case, the logged in/logged out state is also stored in the user row in the database table so that the only way that someone who's logged out can become logged in is for them to submit their username/password.
  15. your program logic will default to the catch{...} block for all database errors, so even though the message being output says, "Connection failed: ..., you actually have an error at one of the query statements. i recommend that you change your code to consistently use one variable name to hold the sql query statements, something like $sql. then, in the catch{...} block, if $sql is empty, you know you reached that point due to a connection error. if $sql is not empty, you know you reached that point due to a query error. this logic will let you echo the $sql query statement as part of the error so that you can look to see what might be wrong with it. change your catch{...} block to something like the following - } catch(PDOException $e) { $status = empty($sql) ? 'Connection failed':" Query failed: $sql"; echo "$status, Error: {$e->getMessage()}, File: {$e->getFile()}, Line: {$e->getLine()}"; }
  16. There's probably 20,000 examples of what you are asking posted on the web. in fact that's what the web was created for, publishing and sharing information. your first stop should be to do a web search and try to accomplish this assignment yourself. programming help forums are for helping those that already have code and have tried.
  17. here's another hint. the following is your SELECT ... query from the previous thread, with the correct bind usage - $sql = "SELECT ...."; $stmt = $handler->prepare($sql); $stmt->bindParam(':username', $username); $stmt->bindParam(':password', $hash); $stmt->execute();these are the corresponding lines of code from this thread - $sql_string = "INSERT INTO users SET username = :a, password = :b"; $sql->prepare($sql_string); $sql->bindParam(':a', $username); $sql->bindParam(':b', $hash); $sql->execute();lines 2-5 of both of these pieces of code should be logically the same and in fact should be identical for consistency reasons (why keep writing/changing code that's performing the same actions.)
  18. we are really trying to help you, but when you don't bother to read the php.net documentation and its examples for the statements you are trying to use and really learn what each statement does, and in this case learn what each statement returns as a value, it's not possible for you to write code that does anything. you are operating in an uncontrolled random trial (and mostly error) mode, where you are not using the documentation as an input to determine the correct way of using any statement. i'll give you a hint: bindParam() is a method of the PDOStatement class. it is not a method of the PDO class. the following is the php.net documentation of the return value from the ->prepare() method - all of this information, including examples, can be found at in the php.net documentation. in your last thread, you were correctly doing everything to prepare and execute the query, except you were not using the proper bind statement. the code you posted above in this thread is nothing like what you used in the last thread, which says you didn't learn anything from what you were doing in the last thread. just going through the motion of copy/pasting lines of code, isn't programming and isn't learning. you must be able to generalize and make use of what you 'learned' to prepare and execute one type of query to do the same steps for any other type of query.
  19. i'm going to guess that you have a redirect-loop or similar that's requesting the page multiple times. the symptom you are seeing of random numbers other than a 1 causing the query to update is just the last random value being echoed, but in fact the code has ran several times, one of which had a 1 that caused the query to run. you may also have some other code that's always running the same query or have a logic error that's updating that value when it should be resetting it. at this point, you have a page that doesn't do what you expect. it will likely take seeing all the code on the page that reproduces the problem (less any database credentials) in order to help you.
  20. unfortunately, the OP is using the PDO library, based on the error and his connection code posted in the last thread on this forum, but isn't actually learning how to use it, and is therefore getting stuck on the basic steps over and over. @Tom10, the task in this thread is similar to your previous thread. you are trying to form and run a query, an insert query in this case, but are not using all the statements correctly. the only way to get all the statements to work together and correctly is to learn what each of the statements do, so that you will know how they are supposed to go together. in the last thread you were not using the correct bind statement that is part of the PDO library of functions. in this thread, you have a mix of code that is/was running a non prepared query using the pdo->query() method, then added a couple of lines of code trying to turn that into a prepared query, but not converting the sql statement to a prepared query, not using the correct pdo bind statement, and still leaving in the previous call to the pdo query() method. the reason i didn't post any fixed code or link to any php.net documentation in your previous thread, is because you are missing the basic understanding of what these statements and lines of code do and the only way you can gain that understanding is if you actually go and research, internalize, and learn this information. once you know how to use the pdo statements to prepare a query, bind input parameters, execute the query, and retrieve any results, you can then use that knowledge to form and run any kind of query.
  21. what are the actual $encounter values when you log/echo them right before the if(){} conditional statement and are you sure that's the actual code that's being ran on the server?
  22. it sounds like what you are trying to do isn't what session variables are intended for. the session is just a container for server-side variables that persist between page requests. it's called a session because it's intended to only last one browser session. it's actually not normal to extend the session cookie lifetime. perhaps if you state what some of these different values will be used for, someone can tell you the best way of handling each of them.
  23. no one stated there was a problem with your database connection, so i don't know why you posted the above code. the problem is that there is no pdo bind_param() method. to use the PDO database statements correctly, you must read the php.net documentation for what you are doing. just coping things you have have seen someplace, without knowing what they mean and what they do, isn't going to work.
  24. your prepare() statements are failing, probably because you haven't selected a database, and your code isn't doing anything when a prepare fails. your code keeps running and tries to use the result from the queries that have failed. your if(){} conditional test for the first two prepare() statements needs to do something when there's a failure, such as reporting that there is an error, prevent the rest of the code from running, and during development display all the information there is about the error. the third prepare() statement doesn't even have any logic around it to make sure it is working before trying to call the bind_param() method. the or trigger_error($mysqli->error); statement you have on the end of the $query = "..." string isn't doing anything (a string cannot produce a mysqli error.) perhaps you meant to have that in an else {} clause for the if(){} conditional test around the prepare() statements?
  25. you are making this much harder than it needs to be. you should also post in the javascrpt forum section if you are going to be using js/ajax. the reason your session variable isn't working is due to a logic error. there's no code setting or modifying the session variable. see the following sample code for how you could manage the process using php - <?php /* Create a new session to hold the step values */ session_start(); /* Force start on step 0 when page load, via session */ if(!isset($_SESSION['step'])){ $_SESSION['step'] = 0; } // form processing code if($_SERVER['REQUEST_METHOD'] == "POST"){ $errors = array(); // store any errors in this array $data = array_map('trim',$_POST); // get a trimmed copy of the form data /* Check WHICH form processing code to run */ switch($_SESSION['step']){ case 1: // step 1 form processing code // validate the form data //$errors[] = 'empty host name'; // dummy error for demo purposes // if no validation errors, use the form data if(empty($errors)){ // do something with the submitted data from this step } break; // processing code for the remaining steps would go here case 2: break; } // if there are no errors at this point, advance to the next step and do a header() redirect to the exact same url of this page to cause a get request if(empty($errors)){ $_SESSION['step']++; $host = $_SERVER['HTTP_HOST']; $uri = $_SERVER['REQUEST_URI']; header("Location: http://$host$uri"); exit; } // if there are errors, the code will continue and display the html document, where you would // display any errors, redisplay the current step's form, with any previous values } // any get method code would go here to get/produce data that the page needs, // such as if you are editing existing settings, to retrieve them and set the $data array with them // if the $data array doesn't already exist ?> <!DOCTYPE html> <html> <head> <title>CMS Website : Installer</title> <link rel="stylesheet" type="text/css" href="css/styles.css" /> <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,700' rel='stylesheet' type='text/css'> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> </head> <body> <div id="form-box"> <?php // display any errors if(!empty($errors)){ echo "Correct the following errors:<br>"; foreach($errors as $error){ echo "$error<br>"; } } switch($_SESSION['step']){ case 0: echo ' <h3>CMS Installer</h3> <p>You <small>MUST</small> install the website for it to function correctly.</p> <form method="post"> <div align="center"><input type="submit" value="Start Install" style="cursor: pointer;" /></div> </form> '; break; case 1: // note: to display any initial/existing form field data, test if the corresponding field // in the $data array isset() and echo it as needed to populate the form fields // this would be much easier if you dynamically built (and processed) each form by having a definition in an array that you simply step through echo ' <h3>CMS Installer ~ Step 1</h3> <p>Please fill in the form with the appropriate details</p> <form method="post"> <input type="text" name="db-host" placeholder="Database Host" /> <input type="text" name="db-username" placeholder="Database Username" /> <input type="password" name="db-password" placeholder="Database Password" /> <input type="text" name="db" placeholder="Database Name" /> <input type="submit" value="Submit Database" /> </form> '; break; // display code for the remaining steps would go here case 2: break; } ?> </div> </body> </html>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.