Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
Why are you unable to use break and continue within a foreach() loop? From the manual Note: While the description for continue doesn't explicitly state it works for foreach() loops, the examples show that it clearly does.
-
Php set a Checkbox as checked at start from page
Psycho replied to Foracc's topic in PHP Coding Help
In your if/then results, create a variable that will determine the checked status of the checkbox. I typically ise a ternary operator for these for brevity. Also, I would avoid using a name like "checkbox", give it a name as to what it is intended for. $Datei = file('test.txt')[2]; $checkboxChecked = ($Datei == "Line 2") ? 'checked' : ''; //If true, set value to 'ckecked' else value is empty string echo "<input type='checkbox' name='Checkbox' value='checkbox' {$checkboxChecked}>Checkbox</input>'; -
Little function like If $variable == 0 then echo "--"
Psycho replied to paulmagm's topic in PHP Coding Help
I think you meant for the condition to be ($max_supplu != 0). Either that, or the results should be swapped. -
<?php //Var to hold totCool result $totCool = false; //Query for an exact match on both gpw and ewt $query = "SELECT gpm, ewt, totCool from {$uniluxModel} where gpm = :uniluxGpm AND ewt = :uniluxEwt"; $stmt = $dbConn->prepare($queryStr); $stmt->execute(array(':uniluxGpm' => $uniluxGpm, ':uniluxEwt' => $uniluxEwt)); if($stmt->rowCount() > 0) { //There was an exact match $totCool = $stmt->fetchColumn() } //If no result try getting the two closest records matching gpm and high/low for ewt if (!$totCool) { $query = "( SELECT gpm, ewt, totCool FROM {$uniluxModel} WHERE gpm = :uniluxGpm1 AND ewt < :uniluxEwt1 ORDER BY gpm DESC LIMIT 1 ) UNION ( SELECT gpm, ewt, totCool FROM {$uniluxModel} WHERE gpm = :uniluxGpm2 AND ewt > :uniluxEwt2 ORDER BY gpm ASC LIMIT 1 )"; $stmt = $dbConn->prepare($queryStr); $stmt->execute(array(':uniluxGpm1' => $uniluxGpm, ':uniluxEwt1' => $uniluxEwt, ':uniluxGpm2' => $uniluxGpm, ':uniluxEwt2' => $uniluxEwt)); if($stmt->rowCount() > 0) { //There was a result, get results into an array $result = $stmt->fetchAll(); //Debug line to see the results echo "Results from DB: <pre>".print_r($result, true)."</pre>"; //Add logic here to calculate the totCool value from the results $totCool = "Need to calculate totCool value value"; } } //If no result try getting the two closes records matching ewt and high/low for gpm if (!$totCool) { $query = "( SELECT gpm, ewt, totCool FROM {$uniluxModel} WHERE gpm < :uniluxGpm1 AND ewt = :uniluxEwt1 ORDER BY gpm DESC LIMIT 1 ) UNION ( SELECT gpm, ewt, totCool FROM {$uniluxModel} WHERE gpm > :uniluxGpm2 AND ewt = :uniluxEwt2 ORDER BY gpm ASC LIMIT 1 )"; $stmt = $dbConn->prepare($queryStr); $stmt->execute(array(':uniluxGpm1' => $uniluxGpm, ':uniluxEwt1' => $uniluxEwt, ':uniluxGpm2' => $uniluxGpm, ':uniluxEwt2' => $uniluxEwt)); if($stmt->rowCount() > 0) { //There was a result, get results into an array $result = $stmt->fetchAll(); //Debug line to see the results echo "Results from DB: <pre>".print_r($result, true)."</pre>"; //Add logic here to calculate the totCool value from the results $totCool = "Need to calculate totCool value value"; } } //If no result get the two closest records that are both above gpm and ewt and both below gpm and ewt if (!$totCool) { $query = "( SELECT gpm, ewt, totCool FROM {$uniluxModel} WHERE gpm < :uniluxGpm1 AND ewt < :uniluxEwt1 ORDER BY gpm DESC, ewt DESC LIMIT 1 ) UNION ( SELECT gpm, ewt, totCool FROM {$uniluxModel} WHERE gpm > :uniluxGpm2 AND ewt > :uniluxEwt2 ORDER BY gpm ASC, ewt ASC LIMIT 1 )"; $stmt = $dbConn->prepare($queryStr); $stmt->execute(array(':uniluxGpm1' => $uniluxGpm, ':uniluxEwt1' => $uniluxEwt, ':uniluxGpm2' => $uniluxGpm, ':uniluxEwt2' => $uniluxEwt)); if($stmt->rowCount() > 0) { //There was a result, get results into an array $result = $stmt->fetchAll(); //Debug line to see the results echo "Results from DB: <pre>".print_r($result, true)."</pre>"; //Add logic here to calculate the totCool value from the results $totCool = "Need to calculate totCool value value"; } } //Determine if there were results if(!$totCool) { //No records that match either value echo "Not enough data to compute"; } else { //Do something with the retrieved\calculated value echo "totCool value = {$totCool}"; } ?>
-
You can give this a try. I think it's probably a little more verbose than it needs to be, but should do what you are asking. Not going to guarantee it is what you want, so run it through some tests first <?php //Var to hold result $result = false; //Query for an exact match on both gpw and ewt $query = "SELECT gpm, ewt, totCool from {$uniluxModel} where gpm = :uniluxGpm AND ewt = :uniluxEwt"; $stmt = $dbConn->prepare($queryStr); $stmt->execute(array(':uniluxGpm' => $uniluxGpm, ':uniluxEwt' => $uniluxEwt)); if($stmt->rowCount() > 0) { //There was an exact match $result = $stmt->fetchColumn() } //If no result try getting the two closest records matching gpm and high/low for ewt if (!$result) { $query = "( SELECT gpm, ewt, totCool FROM wxyz WHERE gpm = :uniluxGpm1 AND ewt < :uniluxEwt1 ORDER BY gpm DESC LIMIT 1 ) UNION ( SELECT gpm, ewt, totCool FROM wxyz WHERE gpm = :uniluxGpm2 AND ewt > :uniluxEwt2 ORDER BY gpm ASC LIMIT 1 )"; $stmt = $dbConn->prepare($queryStr); $stmt->execute(array(':uniluxGpm1' => $uniluxGpm, ':uniluxEwt1' => $uniluxEwt, ':uniluxGpm2' => $uniluxGpm, ':uniluxEwt2' => $uniluxEwt)); if($stmt->rowCount() > 0) { //There was a result $result = $stmt->fetchColumn() } } //If no result try getting the two closes records matching ewt and high/low for gpm if (!$result) { $query = "( SELECT gpm, ewt, totCool FROM wxyz WHERE gpm < :uniluxGpm1 AND ewt = :uniluxEwt1 ORDER BY gpm DESC LIMIT 1 ) UNION ( SELECT gpm, ewt, totCool FROM wxyz WHERE gpm > :uniluxGpm2 AND ewt = :uniluxEwt2 ORDER BY gpm ASC LIMIT 1 )"; $stmt = $dbConn->prepare($queryStr); $stmt->execute(array(':uniluxGpm1' => $uniluxGpm, ':uniluxEwt1' => $uniluxEwt, ':uniluxGpm2' => $uniluxGpm, ':uniluxEwt2' => $uniluxEwt)); if($stmt->rowCount() > 0) { //There was a result $result = $stmt->fetchColumn() } } //If no result get the two closest records that are both above gpm and ewt and both below gpm and ewt if (!$result) { $query = "( SELECT gpm, ewt, totCool FROM wxyz WHERE gpm < :uniluxGpm1 AND ewt < :uniluxEwt1 ORDER BY gpm DESC, ewt DESC LIMIT 1 ) UNION ( SELECT gpm, ewt, totCool FROM wxyz WHERE gpm > :uniluxGpm2 AND ewt > :uniluxEwt2 ORDER BY gpm ASC, ewt ASC LIMIT 1 )"; $stmt = $dbConn->prepare($queryStr); $stmt->execute(array(':uniluxGpm1' => $uniluxGpm, ':uniluxEwt1' => $uniluxEwt, ':uniluxGpm2' => $uniluxGpm, ':uniluxEwt2' => $uniluxEwt)); if($stmt->rowCount() > 0) { //There was a result $result = $stmt->fetchColumn() } } if(!$result) { //No records that match either value echo "Not enough data to compute"; } else { //Do something with the data } ?>
-
But, there is not necessarily a logical determination for those. Let's just take option #1 to illustrate the problem. The target values the user has entered are gpm = 1.5 and ewt = 40. Now, let's say there are the following records in the database id | gpm | ewt -------------- A 1.3 36 B 1.4 34 Which one do you choose for condition #1? Both of these records are below the two target values and are candidates for the record needed for #1. Record A has an ewt value that is closer to the target (40) than record B. But, the ewt value of record B is closer to the target (1.5) than record A That was why one solution @Barand provided resulted in four records. It has a deterministic approach based on the two variables and the two conditions.
-
No, it is not. I completely understand how to get min/max values from a database. There is a logic problem that I guess I am not doing well at explaining. Yes, determining the lower and upper bounds for ewt is trivial. However, you are not specifically looking for the ewt values - you are after the totCool values. And there are multiple records where ewt = 30 and multiple records where ewt = 40. So, what is the logic to determine which record where ewt = 30 (or where ewt = 40) should be retrieved? @Barand provides one solution where four records are retrieved. It finds: The record with the highest gpm value below the target AND the highest ewt value below the target The record with the lowest gpm value above the target AND the highest ewt value below the target The record with the highest gpm value below the target AND the lowest ewt value above the target The record with the lowest gpm value above the target AND the lowest ewt value above the target You would then have to determine in the code how you want to use those to calculate a relevant totCool value.
-
That really doesn't answer the question. What is the LOGIC to be implemented to determine which records to get? The sample values I copied are probably too "easy" for that question. Since there is a 'logical' record for both the low values and both the high records. But, taking the same example values of gpm, = 1.75 and ewt = 35, lets say these values exist gpm | ewt 1.5 | 50 --- Lower gpm but higher ewt 2.0 | 10 --- Higher gpm but very low ewt 4.5 | 30 --- Much higher gpm but ewt is only a little lower If you can explain the logic you want to use to determine which records to pull, I can look at providing something.
-
The first part in bold is easy enough. But, you need to provide more details to do the second item. If no records match ewt or gpm, then you need to define how you want to determine which records to return. For example, if the user enters gpm = 1.75 and ewt = 35, which records do you want returned from the sample data below? Do you want two records or four?
-
The challenge with programatically determining when cases are broken down or not is that it won't always match reality. A person fulfilling an order may break down a case to fulfill an order for individual units even when there were sufficient quantities of individual units on hand (e.g. one or more individual units may be damaged requiring another case to be opened). If the system must explicitly monitor physical cases vs assuming cases based on total quantity divided by case size, then the fulfillment process should be included in the application workflow where the person fulfilling the order confirms the programatically determined quantities of cases\single units or can edit based on what they actually execute. Otherwise, the actual inventory and the application inventory will get out of alignment. The OP needs to determine what should happen in a situation such as: There are no physical cases in the actual inventory, but there are 20 loose units (a case holds 15). Should the system allow a user to purchase a case and the fulfillment would simply send 15 loose units or can a user only purchase a case when there is an actual physical unopened case?
-
A potential problem is that such a system could never really "know" how many cartons you have vs. loose items unless the person fulfilling the order explicitly enters the number of cartons vs/ loose items. If that is not important, than I would define each product with a column to identify the "bulk quantity". Store the inventory in the total number of items (regardless of cartons). Then, whenever calculating a transaction just divide by the bulk quantity. The whole divisor would be the number of whole cartons and the remainder would be the individual units.
-
Taking reddit as an example, anyone makes an original comment to a post it would not be replying to another person's comment. So, the reply ID of those comments would be empty/zero. However, if anyone replies to one of those initial comments, then you need to know which comment they replied to - in that case you put the comment_id that someone is replying to as the reply_id. Then, if someone replies to one of those comments you use it as the reply_id
-
OK, here is how I would tackle this problem. Run a query with an AND condition. If a record is returned, then it is an exact match and use the totCool value. If no record is returned, then there was no record matching both. Proceed to step 2 Run a query to get the two records matching gpm with the closest (high and low) values for ewt and return the average of totCool for those two record. If no record is returned, then there was were not records matching gpm with one above and one below the ewt value. Proceed to step 3 Run a query to get the two records matching ewt with the closest (high and low) values for gpm and return the average of totCool for those two record. If no record is returned, then there was were not records matching ewt with one above and one below the gpm value. Exit with error condition. <?php //Var to hold result $result = false; //See if there is an exact match on both gpw and ewt $query = "SELECT totCool from {$uniluxModel} where gpm = :uniluxGpm AND ewt = :uniluxEwt"; $stmt = $dbConn->prepare($queryStr); $stmt->execute(array(':uniluxGpm' => $uniluxGpm, ':uniluxEwt' => $uniluxEwt)); if($stmt->rowCount() > 0) { //There was an exact match $result = $stmt->fetchColumn() } //If no result try getting the avereage records matching gpm and high/low ewt if (!$result) { $query = "SELECT AVG(totCool) FROM ( ( SELECT totCool FROM wxyz WHERE gpm = :uniluxGpm1 AND ewt < :uniluxEwt1 ORDER BY gpm DESC LIMIT 1 ) UNION ( SELECT totCool FROM wxyz WHERE gpm = :uniluxGpm2 AND ewt > :uniluxEwt2 ORDER BY gpm ASC LIMIT 1 ) ) as avgTable"; $stmt = $dbConn->prepare($queryStr); $stmt->execute(array(':uniluxGpm1' => $uniluxGpm, ':uniluxEwt1' => $uniluxEwt, ':uniluxGpm2' => $uniluxGpm, ':uniluxEwt2' => $uniluxEwt)); if($stmt->rowCount() > 0) { //There was a result $result = $stmt->fetchColumn() } } //If no result try getting the avereage records matching ewt and high/low gpm if (!$result) { $query = "SELECT AVG(totCool) FROM ( ( SELECT totCool FROM wxyz WHERE gpm < :uniluxGpm1 AND ewt = :uniluxEwt1 ORDER BY gpm DESC LIMIT 1 ) UNION ( SELECT totCool FROM wxyz WHERE gpm > :uniluxGpm2 AND ewt = :uniluxEwt2 ORDER BY gpm ASC LIMIT 1 ) ) as avgTable"; $stmt = $dbConn->prepare($queryStr); $stmt->execute(array(':uniluxGpm1' => $uniluxGpm, ':uniluxEwt1' => $uniluxEwt, ':uniluxGpm2' => $uniluxGpm, ':uniluxEwt2' => $uniluxEwt)); if($stmt->rowCount() > 0) { //There was a result $result = $stmt->fetchColumn() } } if(!$result) { //No records that match either value echo "Not enough data to compute"; } else { echo "totValue: {$result}"; } ?>
-
OK, let me see if I understand the problem correctly. If the user enters values for gpm and ewt AND there is a record that exactly matches both of those values, then you want the totCool value for that record. However, if there is no exact match, but either gpm or ewt does match records, then you want the average of totCool for the two records above and below the unmatched value. There are two scenarios that are unclear. What do you want to do if neither gpm nor ewt match any records? What should happen if one value matches, but for the unmatched value there is no record above (or no record below) to use to calculate an average?
-
Based on the request, I see that Barand's code works as required. However, I would do one thing differently. The lines to determine the afc and nfc positions in the loop are perfectly valid, but the calculations are not intuitively apparent. While maybe not as efficient, I prefer code that is more obvious, such as echo '<hr>Matrix table<br>'; for ($afcPos=0; $afcPos<10; $afcPos++) { for ($nfcPos=0; $nfcPos<10; $nfcPos++) { $coord = $afcPos . $nfcPos; $afcVal = $afc[$afcPos]; $nfcVal = $nfc[$nfcPos]; printf('| %02d | %d, %d<br>', $coord, $nfcVal, $afcVal); } } Although you state If this is going into a database, you should just need a table that defines each player and the coordinates of the position(s) they selected and a second table that defines the randomly generated numbers and their positions. You do not need to go back and update the player records with the number associated with their positions.
-
The lesson instructions state to create an associative array.
-
I am pretty certain that your source array is not constructed how the instructor intended. You are simply assigning an array of values to each associative key. I would think the source array should have an element for each employee with each value assigned to an associative key. Something like this: $employees = array ( array('name' => 'Employee-1', 'department' -> 'IT', 'salary' => '5000'), array('name' => 'Employee-2', 'department' -> 'HR', 'salary' => '4000'), array('name' => 'Employee-3', 'department' -> 'Marketing', 'salary' => '3000'), array('name' => 'Employee-4', 'department' -> 'Sales', 'salary' => '1500'), // etc. . . . ); The loop over each record in the $employees array foreach($employees as $employee) { echo "Name: {$employee['name']}<br>"; echo "Department: {$employee['department']}<br>"; echo "Salary: {$employee['salary']}<br>"; }
-
Have a table for comments which contains a unique record for each comment/reply: comment_id (id for each comment/reply) post_id (the ID of the post that the comment/reply belongs to) reply_id (The comment Id of the comment being replied to, or empty/0 if it was an initial comment) user_id datetime comment . . . any other relevant info
-
Here 0is how I would handle that function columninfo($table, $col = NULL) { //State the error condition check if(!$this->CheckTable($table)){ return false; } //State the error condition check if($col!=NULL && !$this->Checkfield($table, $col)){ return false; } //No errors $stmt = $this->db->prepare('SELECT * FROM '.$table); $stmt->execute(); $numCol = $stmt->columnCount(); $colAry = array(); for ($i = 0; $i < $numCol; $i++) { if($stmt->getColumnMeta($i)['name'] == $col) { //Return type for selected column return $stmt->getColumnMeta($i)[0]; } else { //Append current column to return arrray $colAry[] = $stmt->getColumnMeta($i); } } //Return array for all columns return $colAry; }
-
Generally, I would agree with that. However, I wonder if there would be legal reasons for keeping a copy of the invoice that was sent. Yes, another copy could be generated later and would be the same - if the code had not changed. But, if there was any type of legal action and the company needed to provide records of the invoices that were sent, I don't know if regenerating a copy would be adequate. They might have to also prove that none of the code changed that generated the invoice and if the code had changed they might have to prove what the invoice would have looked like at the time it was generated previously.
-
Most definitely different character sets (aka fonts). You can tell by the lower case L's in the variable name:
-
I'll add to @requinix's response. As he said, the easiest approach would be to have a save button that loops through all the rows in the table (except the header) and executes the save individually. Your current save operation only requires an id value. So, I would leave the current save_row() functiona unchanged and then do two things: In the script that builds the table, create a hidden field with the value being the ID Create a new function [e.g. save_all()]. Have that function iterate through every row in the table and get the ID value and execute the save_row() function However, as he said if you have "lots" of rows, this may not be a good approach because each AJAX call would have to be performed separately. You would have to do some testing with the maximum number of records you expect to have to see if it will be an issue. If so, I would take a different approach. I would make the values in the rows actual input fields - making the field names an array. Something like: echo "<input type='text' name='records[{$row_coun_fin['PRODUCT']}][remarks_val]' value='{$row_coun_fin['REMARKS'];}'>"; Then you can submit the whole form via a normal POST or via AJAX. Then the receiving page would just need to iterate over the $_POST['records'] array foreach($_POST['records'] as $productID => $productData) { //Update the data for the current record from the values in $productData }
-
You are making things harder than they need to be. Why are you including the sales_order table in your query when you don't use any data from that table and you can use the lookup value on the sales_order_lines table? Why do you iterate through the DB results to put them into other arrays just to iterate over those? $sql="SELECT `name`, `quantity`, `delivery_date`, DATE_ADD(`delivery_date`, INTERVAL 10 DAY) AS used_day FROM `sales_orders` so INNER JOIN `sales_order_lines` sol WHERE sol.`parent_id` IS NULL AND sales_orders_id = '$id'"; $result = mysqli_query($connect, $sql); $degree = "STORAGE: KEEP REFRIGERATED BELOW 5`C"; //var_dump($degree); $labels = ""; while($row = mysqli_fetch_array($result)) { $curent_label = sprintf( "%s\n%s %s\n%s %s\n%s\n%s", "{$row['name']}","<br>", 'Delivery Date', "<br>", 'Use By Date', "<br>", "{$row['delivery_date']}","<br>", "{$row['used_day']}","<br>", 'PRODUCT OF SOUTH AFRICA',"<br>", "{$degree}" ); $labels .= str_repeat($curent_label, $row['quantity']) } echo $labels;
-
Adding a try/catch or die to every single place that an error could potentially occur would probably be an exercise in futility. You say that your shared server has no error logs. You could try adding code to your page(s) to generate an error log. Put this code somewhere so that it will be executed on any page load: ini_set("log_errors", 1); ini_set("error_log", "php-errors.log"); You could then have a separate bowser window open to the location of the error log and refresh it upon any page load to see what errors may have occurred.
-
With a web application, you can certainly record when a user logs in, but even if you have a logout function a user can simply close the browser window. So, you do need a way to track that they are still there. Since this is a game where two people are playing each other, I think you need to do more than just track when they hit a new page. Here is what I would do: Create a last active field in the user table. Every time you see activity from the user, update the timestamp. Determine "how long" of a time period that you would consider someone as still active: 5 minutes, 10 minutes, ??? Then use that time period to query for active players based on those whose last active timestamp is within the last X minutes. This can also be used for two players already in a game to notify one that the other has dropped off. Once two users start a game use AJAX to both continually update their last active value AND to proactively notify them of anything relevant. E.g. when it is their turn, if the other user drops off, etc. AJAX is simply javascript that makes a server call and returns back data to the user which you can then implement additional logic to act on that returned data. You can update the content in their browser window based on the move the other player made, make a notification, whatever. In this case, I would have the AJAX make a call every X seconds (10, 20, 30 ???). That call would do several things: Update the last active timestamp for the user Check if the other user is still active. Since the other player will also have AJAX running, their last active timestamp should never be greater than the time you set for the AJAX calls to be made. So, if the other players last active timestamp is more than double that time period, I would let the user know that the other user has dropped Check if the other user has made a move (if it was their turn). If so, alert the user that the other user has made their move, show the results of that move, and tell the player it is now their turn. I'm sure there are some things I am leaving out, but the core of it is to use regular calls via AJAX to determine when users have completed a turn and to ensure they are still online. Be sure to use a JavaScript framework to do this (such as JQuery) instead of trying to build the AJAX code with native JavaScript.