Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
1. Why not set the 'time' field to have a default value of NOW()? Then you don't need to include the field in your INSERT query at all! 2. You do not need to do anything with the INSERT statements. You need to modify your SELECT statement to have the DB format the date when it returns it to you 3. mac_gyver provided a link to to manual page that explains how to format the data in MySQL. Is there something on that page you don't understand?
-
Your code makes no sense. If I am reading it correctly the table has a single column being used called 'privilege'. I would assume then that each value is the name of a privilege? Heck, I don't even see anything in the query to only return the records for a specific user. But, you should have a finite list of privileges. I would have a table with a column for each privileged then a single record for each user with a 1 or zero in each column to identify if the user has that right or not. You should think long and hard about the structure of what you are building. You could very well be taking yourself down a long dark alley where nothing good will come.
-
Changed an else/if but now fails... What did I miss?
Psycho replied to totallytech's topic in PHP Coding Help
Is this PHP? I've never seen that syntax before. Is this, perhaps, a framework you are using? -
@Gizmola You obviously know this better than I and you've provided some good information in this thread and the linked page. But I question one thing that you've stated multiple times. For example: There is no need to normalize the DB to be GT time. MySQL will always store a timestamp relative to GMT. http://dev.mysql.com/doc/refman/5.5/en/datetime.html As you stated, timestamps are agnostic with respect to timezones. The only thing that changing the timezone of the server affects is how the values are retrieved from the database. If you retrieve the value as a date string (which is the default format: YYYY-MM-DD HH:MM:SS) it will be formatted based upon the timezone of the server. However, since you most likely want a numeric timestamp anyway so it can be formatted in PHP, it would be easiest to use UNIX_TIMESTAMP(fieldname) when selecting the data. To reiterate, there is no need to force the value to be GMT when saving a timestamp, but it should be specifically retrieved as a GMT value when used. Doing this it doesn't matter what timezone the server is set for.
-
The above is PHP code. But, that condition is invalid - you are missing the dollar sign at the beginning of the variable name: $title Plus, to do something "onclick" requires that you either submit the page and process the POST/GET data or you need to implement JavaScript to perform an AJAX request. I assume you want the latter. But, there's too much to try and tell you how to do this in a forum post. I would suggest you look up on how to implement JQuery's AJAX functionality.
-
Because the variables you are using to add the values to the query are either not defined or are defined incorrectly. THe other errors you reported clearly showed that.
-
You did NOT use backticks => `````````. You used regular single ticks => '''''''' fastsol even told you exactly where on the keyboard the character was! Also, the variables you are using to insert data are not defined!
-
There's nothing wrong with using backticks for the column names as used above. If any column name was a reserved word they would be required.
-
I think you are taking the wrong approach as this will require three queries instead of one, but here, give this a tryu <?php //Make DB connection include('connection.php'); //Set the ID $the_id = (isset($_GET['id']) && ctype_digit($_GET['id'])) ? $_GET['id'] : 0; //Vars to hold the dynamic output $output = ''; $pagination = ''; //Run query to get the selected record data $query = "SELECT the_title, the_subject, summary, view, Image FROM the-table WHERE the_id = $the_id"; $result = mysqli_query($con, $query) or die(mysqli_error($con)); $record = mysqli_fetch_assoc($result); //Check that there was a match if(!$record) { $output .= 'No record found'; $pagination = ''; } else { //Create record output $output .= "<div class='containerDetail'>\n"; $output .= " <h1>title {$record['the_title']}</h1>\n"; $output .= " <ul>\n"; $output .= " <li>subject: {$record['the_subject']}</li>\n"; $output .= " <li>summary: {$record['summary']}</li>\n"; if(!empty($record['view'])) { $output .= " <li><a href='{$record['view']}'>View</a></li>\n"; } $output .= " </ul>\n"; $output .= "</div>\n"; $output .= "<img src='{$record['Image']}' /> \n"; //Run query to get the prev record id $query = "SELECT MAX(the_id) FROM the-table WHERE the_id < $the_id"; $result = mysqli_query($con, $query) or die(mysqli_error($con)); $prevID = mysqli_fetch_assoc($result); //Run query to get the next record id $query = "SELECT MIN(the_id) FROM the-table WHERE the_id > $the_id"; $result = mysqli_query($con, $query) or die(mysqli_error($con)); $nextID = mysqli_fetch_assoc($result); //Create the pagination output $pagination .= "<div class='pagination'>\n"; $pagination .= " <div>\n"; $pagination .= " <div id='paglinks'>\n"; $pagination .= ($prevID) ? "<a href='./thepage.php?id={$prevID}'>« Previous </a>" : "« Previous"; $pagination .= " | "; $pagination .= ($nextID) ? "<a href='./thepage.php?id={$nextID}'>Next »</a>" : "Next »"; $pagination .= " </div>\n"; $pagination .= " </div>\n"; $pagination .= "</div>\n"; } ?> <?php include("head.php"); ?> <body class=""> <?php include("navigation.php"); ?> <section class="container"> <?php echo $output; ?> <?php echo $pagination; ?> </section> </body> </html>
-
Try adding a "type" to the add row button as <button onclick="addRow()" type="button">Add Row</button>
-
Can you provide the "output" of the script - i.e. the HTML source that is generated? I don' have your database in order to run the script. Since this is a JS problem, only the HTML is needed.
-
Why would that button cause the form to be submitted? It isn't created as a submit button - so it wouldn't submit the form unless you were to apply JS to the button to force it to submit the form
-
How can I provide that? You should already have code to display the data from the "selected" record. I've given you the solution, but apparently there is some confusion on that solution. I think you are to wrapped up in what you think you need to be doing that you are not understanding what I have provided. The solution is to implement the "normal" pagination process that allows the use to navigate from 'page' to 'page'. In this case the number of records for each page will be 1 (as opposed to 20 or some other set number). You do NOT need to determine the ID of the prev or next pages. That is only making this harder than it needs to be. EDIT: The ONLY reason you would need to use a different process is if you need to display "data" regarding the Prev & Next records on the current page. Please provide the code to output the complete page and I'll see if I can rewrite as needed.
-
I guess you didn't read my first post. You are making this more difficult than it needs to be. Just follow the "normal" pagination logic and make the page size 1. You do not need to know the ID of the next record to make this work. If you know there is another "page" of data (in this case at least a single record), then pass the next page number to the script. Then it doesn't matter if there is a gap of one or multiple numbers between record IDs - it just works! To reiterate: You should be passing the PAGE number - not the record ID. It will work - I promise. Also, I created the pagination controls as a variable for a reason. It gets unwieldy to manage code when you are going in and out of PHP/HTML repeatedly. Put all of your PHP code at the top of your script and create variables to hold the respective output. Then put the HTML at the bottom of the script just using echo's to output the dynamical generated content.
-
There is not going to be an easy way to just drop in some code to make this happen. Before you even start you need to fully understand your database schema. Otherwise, you run the risk of creating orphaned records that could be benign or could create real problems. For example, if you were to delete a client - that client may have placed order or had other records associated with them. What should happen to that associated data when the client is "deleted". If you delete the client and the associated data, then you lose all history of that associated data as well. For example, if you were to run an order history report it would be wrong. Also, if any inventory is based on order history the inventory would be wrong. Or, if you delete the client and not the associated data, how would it be used? If you ran an order history report you could still have a problem where the orders are not reported depending on how the query is built. Or, if it is included, what should display in those reports where the client is included in the output. Way, way too many issues you need to understand and plan for before creating any processes for deleting records. You will likely find that some records can be permanently deleted, while some can be "soft" deleted and other cannot be deleted at all. Here is an example of one possible scenario: Let's say you have a value for "Region" that is applied to clients. If any clients are assigned to a region AND region is a required value, then you can allow a region to be deleted if no clients are assigned to it. Or, during the delete operation, you could give the option to reassign any clients to a new region. If region is not required, then you can probably just allow any to be deleted without any checks - but you need to update the existing clients that reference the region to an appropriate Nu// or Default value.
-
Here is a rough outline of how you could redo the code. This is not tested and written "off the cuff" so there may be a few syntax errors. But, it should point you in the right direction. <?php $recordsPerPage = 1; include('connection.php'); //Get the current page to display $currentPage = isset($_GET['page']) ? intval($_GET['page']) : 1; //Calculate the limit start value $limitStart = ($currentPage-1) * $recordsPerPage; //Get the current page records ## NOTE the SQL_CALC_FOUND_ROWS allows you to use a query to get the ## current page records and calculate the total records (w/o the limit) $query = "SELECT SQL_CALC_FOUND_ROWS field1, field2 FROM thetable ORDER BY field1 LIMIT {$limitStart}, {$recordsPerPage}"; $result = mysqli_query($con, $query); //Run second query to get the total records calculated from 1st query $query = "SELECT FOUND_ROWS()"; $result_count = mysqli_query($con, $query); $rowResult = mysqli_fetch_array($result_count); $totalRecords = $rowResult[0]; //Calculate total pages $totalPages = ceil($totalRecords / $recordsPerPage); //Create pagination controls $paginationControls = ''; //Previous page if($currentPage > 1) { $prevPage = $page - 1; $paginationControls .= "<a href=\"./thepage.php?page={$prevPage}\">« Previous </a>"; } else { $paginationControls .= "« Previous"; } //Next page if($currentPage < $totalPages) { $nextPage = $page + 1; $paginationControls .= "<a href=\"./thepage.php?page{$nextPage}\">Next »</a>"; } else { $paginationControls .= "Next »";; } //ADD CODE TO DISPLAY THE RECORD(S) mysqli_close($con); ?>
-
Lot's of things wrong in that code: 1. NEVER use data directly from the user (e.g. $_POST, $_GET, etc.) in a query. You are opening yourself up to SQL injection. Either verify the data is 'valid' or, better yet, use prepared statements. 2. Don't use mysql_ functions - they are deprecated. You are mixing those and mysqli_ functions. 3. Don't use "*" in your SELECT query unless you really need all the fields. Plus, don't query all the rows and then use num_rows. Instead run a query to return the number of rows. 4. That code would not work as you have it. You are running a query to get the number of rows, and then using the result as a new query. ??? Since each "page" is a single record, you can change your logic to use a more traditional pagination approach and just limit the page size to one record. The prev/next links do NOT need the ID of the records. That way you can change the records per page at will.
-
I don't see anything in your code that is trying to clone the select list. You should NOT hard-code the fields to be copied/cloned into the JavaScript code. Instead you should put the 'group' of fields to be copied into a DIV or other container and then dynamically copy the fields in that container. That way, when you make changes to the fields in the future, you only have to change the fields in the initial form (i.e. PHP) and you don't have to change the JavaScript. Here is a very "rough" example: http://jsfiddle.net/sEtmN/3/
-
My website is displaying an error that I need some help on
Psycho replied to SpeedyMods's topic in Third Party Scripts
Where's the code from the file state.php? Please star out the password when you do. But, to me it looks like there isn't a connection made. If you post the code we can add some debugging logic to hopefully provide a better idea of the problem. -
It would be helpful to see the logic you are using to get a page of records and output it to the page (including the queries). It could be you are running queries in loops, the query you are using is inefficient, or you need to add indexes on specific fields used in the query. (or a combination of the three).
-
As indicated by chriscloyd's code - do not duplicated code. You apparently only want to change the content of the page and there is no need to replicate the creation of the HTML, HEAD and other tags. Use php to determine the dynamic content and "plug it in" to the template of the page. I would make one change to chriscloyd's code and determine the content BEFORE the output is started. As for a blank page, did you view the HTML source of the page? I'm not very good with flash and don't know if the code you have would produce and output. I modified the above code to add some debugging logic so you can verify if the page is getting executed correctly. <?php $ouput = "Please login to access this page!"; $debug = "Condition is false"; if($user->data['is_registered']) { $ouput = '<script type="text/javascript"> var multiplayer = new SWFObject("player.swf?xml=config.xml&player_skin=skin.swf", "multiVideoPlayer", "361", "283", "7", "#FFFFFF", true); multiplayer.write("flashcontent"); </script>'; $debug = "Condition is true"; } ?> <html> <head> <script type="text/javascript" src="swfobject.js"></script> </head> <body> <?php echo $output; ?> <br> <?php echo $debug; ?> </body> </html>
-
I made some assumptions and made this so it would be very flexible $functions = array('SUM', 'MAX', 'MIN', 'AVG'); $patterns = array(); $replacemetns = array(); foreach($functions as $func) { $patterns[] = "#({$func})\(([^\)]*)\) as (\"){0,1}([^\"]*)(\"){0,1}#"; $replacements[] = "\\1(\\2) over() \\3\\4\\5"; } $output = preg_replace($patterns, $replacements, $input); If this is your input SELECT field1, field2, field3, SUM('field3') as 'field3sum', --Single quotes MAX("field4") as "field4max", --Double quote AVG(field5) as field5avg --No quotes FROM table_name This will be the output SELECT field1, field2, field3, SUM('field3') over() 'field3sum', --Single quotes MAX("field4") over() "field4max", --Double quote AVG(field5) over() field5avg --No quotes FROM table_name
-
Are "foo" and "bar" always going to be "foo" and "bar" or can they be variable text values? EDIT: Also, you say the value inside the aggregate function may or may not have quotes. Can it use single quotes as well? And what about the quotes around "Bar" can that use single or double quotes - or no quotes?
-
Yeah, it's kind of hard for me to test a query without the requisite DB. I don't use CASE statements all that often and I goofed on the format - it should just be "END" not "END CASE". Although that's not what the manual says. Also, I found it could be simplified a little: //Define the WHEN conditions for WHERE clause CASE statement $cutoff_scores = array(25=>7,50=>14,75=>28,90=>40); $WHENs = ''; foreach ($cutoff_scores as $score => $rebox_interval) { $WHENs .= "WHEN last_reboxed_date < DATE_SUB(CURDATE(), INTERVAL {$rebox_interval} DAY) THEN (right_count / (right_count + wrong_count)) <= '$score'1\n"; } //Create the query $query = "UPDATE leitner_vcard_boxes SET box = (box + 1), last_reboxed_date = CURDATE() WHERE box < 7 AND CASE {$WHENs} ELSE 0 END"; $result= mysqli_query($link, $query); Resulting query UPDATE leitner_vcard_boxes SET box = (box + 1), last_reboxed_date = CURDATE() WHERE box < 7 AND CASE WHEN last_reboxed_date < DATE_SUB(CURDATE(), INTERVAL 7 DAY) THEN (right_count / (right_count + wrong_count)) <= '25'1 WHEN last_reboxed_date < DATE_SUB(CURDATE(), INTERVAL 14 DAY) THEN (right_count / (right_count + wrong_count)) <= '50'1 WHEN last_reboxed_date < DATE_SUB(CURDATE(), INTERVAL 28 DAY) THEN (right_count / (right_count + wrong_count)) <= '75'1 WHEN last_reboxed_date < DATE_SUB(CURDATE(), INTERVAL 40 DAY) THEN (right_count / (right_count + wrong_count)) <= '90'1 ELSE 0 END CASE If a WHEN condition is met it will return the result of the THEN condition.
-
OK, after a little research, I see you can use a CASE statement in the WHERE clause. I don't know if this is any more efficient or not. But, I think it makes the logic and the resulting query a little easier to understand //Define the WHEN conditions for WHERE clause CASE statement $cutoff_scores = array(25=>7,50=>14,75=>28,90=>40); $WHEN = ''; foreach ($cutoff_scores as $score => $rebox_interval) { $WHEN .= "WHEN last_reboxed_date < DATE_SUB(CURDATE(), INTERVAL {$rebox_interval} DAY) AND (right_count / (right_count + wrong_count)) <= '$score' THEN 1\n"; } //Create the query $query = "UPDATE leitner_vcard_boxes SET box = (box + 1), last_reboxed_date = CURDATE() WHERE box < 7 AND CASE {$WHEN} ELSE 0 END CASE"; $result= mysqli_query($link, $query); Resulting Query UPDATE leitner_vcard_boxes SET box = (box + 1), last_reboxed_date = CURDATE() WHERE box < 7 AND CASE WHEN last_reboxed_date < DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND (right_count / (right_count + wrong_count)) <= '25' THEN 1 WHEN last_reboxed_date < DATE_SUB(CURDATE(), INTERVAL 14 DAY) AND (right_count / (right_count + wrong_count)) <= '50' THEN 1 WHEN last_reboxed_date < DATE_SUB(CURDATE(), INTERVAL 28 DAY) AND (right_count / (right_count + wrong_count)) <= '75' THEN 1 WHEN last_reboxed_date < DATE_SUB(CURDATE(), INTERVAL 40 DAY) AND (right_count / (right_count + wrong_count)) <= '90' THEN 1 ELSE 0 END CASE