-
Posts
24,599 -
Joined
-
Last visited
-
Days Won
829
Everything posted by Barand
-
SELECT DATE_FORMAT(mydatefield, '%d-%m-%Y') as formattedDate If you are sorting or comparing dates, use the original and not the formatted version
-
Getting data from a previous row, based on the current row
Barand replied to frankchester's topic in MySQL Help
Don't add another column for the calculated gains. Derived data should not be stored but calculated when required. For example Data mysql> SELECT * FROM frankchester; +----+-----------+------------+-----------+ | id | accountid | date | followers | +----+-----------+------------+-----------+ | 1 | 1 | 2014-11-10 | 50 | | 2 | 2 | 2014-11-10 | 45 | | 3 | 1 | 2014-11-11 | 55 | | 4 | 2 | 2014-11-11 | 60 | | 5 | 1 | 2014-11-12 | 65 | | 6 | 2 | 2014-11-12 | 55 | +----+-----------+------------+-----------+ Query SELECT accountid, date, followers, gain FROM ( SELECT date , IF(@preva=accountid,followers-@prevf,NULL) as gain , @prevf:= followers as followers , @preva:= accountid as accountid FROM frankchester JOIN (SELECT @prevf:=NULL, @preva:=NULL) as init ORDER BY accountid, date ) as calc; Results +-----------+------------+-----------+------+ | accountid | date | followers | gain | +-----------+------------+-----------+------+ | 1 | 2014-11-10 | 50 | NULL | | 1 | 2014-11-11 | 55 | 5 | | 1 | 2014-11-12 | 65 | 10 | | 2 | 2014-11-10 | 45 | NULL | | 2 | 2014-11-11 | 60 | 15 | | 2 | 2014-11-12 | 55 | -5 | +-----------+------------+-----------+------+ -
Remove the "%"s
- 11 replies
-
Calculating Inbreeding on a 10 Generation Pedigree
Barand replied to Triple_Deuce's topic in PHP Coding Help
A couple of years back I was working on dog pedigree charts so still had some test data. The chart of the data is attached and, as you can see, there are common ancestors. To find them you need to use a recursive function so I would load your data into an array and use that for the recursive search rather bombard your server with dozens of queries. This code will give the common ancestors and the generational distance on the sire and dam sides. You would then need to calc the IC value for each of these ancestors. $db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE); $sql = "SELECT id, dogname, sire, dam FROM dogtable"; $dogs = $sires = $dams = array(); $res = $db->query($sql); while (list($id, $nm, $s, $d) = $res->fetch_row()) { $dogs[$id] = [$s,$d,$nm]; } function getAncestors($id, $key, &$dogs, &$ancests, $dist) { if ($id==0) return; $ancests[$id] = $dist; if (isset($dogs[$id]) ) { getAncestors($dogs[$id][$key], 0, $dogs, $ancests, $dist+1); getAncestors($dogs[$id][$key], 1, $dogs, $ancests, $dist+1); } } $dogid = 1; getAncestors($dogs[$dogid][0], 0, $dogs, $sires, 0); getAncestors($dogs[$dogid][1], 1, $dogs, $dams, 0); ksort($sires); ksort($dams); $common = array_intersect_key($sires,$dams); echo "<pre>"; echo "| ID | NAME | SIRE | DAM |\n"; echo "| | | DIST | DIST |\n"; echo "|-----|--------------------|------|------|\n"; foreach ($common as $id => $dist) { printf("|%4d | %-18s | %4d | %4d |\n", $id, $dogs[$id][2], $sires[$id], $dams[$id]); } Outputs | ID | NAME | SIRE | DAM | | | | DIST | DIST | |-----|--------------------|------|------| | 8 | dog I | 2 | 1 | | 16 | dog Q | 3 | 2 | | 17 | dog R | 3 | 2 |- 40 replies
-
- 1
-
-
- inbreeding
- pedigree
-
(and 2 more)
Tagged with:
-
Have you tried var_dump($password); to check it contains what you think it contains?
-
There is no column "res" in your results (you didn't select one with that name or use it as an alias) so $row['res'] has no value. Select the id along with the other columns and use that as the value.
-
What do you mean by "fails". Not returning any records is not a query failure, it merely means that no records match both conditions. Why are you using LIKE and not = in the password check?
-
I know the feeling. I can always fall back on "senility" when the need arises.
-
checking if the all the values in rows are equal in table
Barand replied to mythri's topic in PHP Coding Help
Test data mysql> SELECT * FROM orders; +----------+------------+--------+ | order_id | order_date | status | +----------+------------+--------+ | 1 | 2014-11-01 | | | 2 | 2014-11-02 | | | 3 | 2014-11-03 | | | 4 | 2014-11-04 | | | 5 | 2014-11-05 | | +----------+------------+--------+ mysql> SELECT * FROM line_items; +--------------+----------+------------+ | line_item_id | order_id | despatched | +--------------+----------+------------+ | 1 | 1 | 0 | | 2 | 1 | 1 | | 3 | 1 | 1 | | 4 | 2 | 1 | | 5 | 2 | 1 | | 6 | 2 | 1 | | 7 | 3 | 0 | | 8 | 3 | 0 | | 9 | 3 | 0 | | 10 | 4 | 0 | | 11 | 5 | 1 | +--------------+----------+------------+ Query to update orders to "COMPLETED" id all items despatched UPDATE orders LEFT JOIN line_items ON orders.order_id = line_items.order_id AND despatched = 0 SET orders.status = 'COMPLETED' WHERE line_items.order_id IS NULL; Resulting orders table mysql> SELECT * FROM orders; +----------+------------+-----------+ | order_id | order_date | status | +----------+------------+-----------+ | 1 | 2014-11-01 | | | 2 | 2014-11-02 | COMPLETED | | 3 | 2014-11-03 | | | 4 | 2014-11-04 | | | 5 | 2014-11-05 | COMPLETED | +----------+------------+-----------+ -
I shortened your code for you <?php $result = ''; if (isset($_POST['conversionType'])) { $conversionType = $_POST['conversionType']; $conversionInput = $_POST['conversionInput']; $conversionValue = 0; list ($from, $to) = explode('-', $conversionType); $scales = array ( 'C' => 'Celcius', 'F' => 'Fahrenheit', 'K' => 'Kevin', 'R' => 'Rankine' ); define('DEGREES', '°'); switch ($conversionType) { case 'C-F': $conversionValue = $conversionInput * (9/5) + 32; break; case 'C-K': $conversionValue = $conversionInput + 273.15; break; case 'C-R': $conversionValue = ($conversionInput + 273.15) * (9/5); break; case 'K-C': $conversionValue = $conversionInput - 273.15; break; case 'K-F': $conversionValue = $conversionInput * (9/5) - 459.67; break; case 'K-R': $conversionValue = $conversionInput * (9/5); break; case 'F-C': $conversionValue = ($conversionInput - 32) * (5/9); break; case 'F-K': $conversionValue = ($conversionInput + 459.67) * (5/9); break; case 'F-R': $conversionValue = $conversionInput + 459.67; break; case 'R-C': $conversionValue = ($conversionInput + 491.67) * (5/9); break; case 'R-F': $conversionValue = $conversionInput - 459.67; break; case 'R-K': $conversionValue = $conversionInput * (5/9); break; } $result = sprintf('%s%s %s = %0.2f%s %s', $conversionInput, DEGREES, $scales[$from], $conversionValue, DEGREES, $scales[$to]); } ?> <html> <head> <title>Unit Converter</title> </head> <body> <h1>CONVERSION</h1> <form action="" method="post"> <link rel="StyleSheet" media="screen" href="convert.css"> Type of conversion: <br> <select name="conversionType"> <option value="null" selected="selected">Select type...</option> <option value="null"></option> <option value="null">--Temperature--</option> <option value="C-F">Celsius to Fahrenheit</option> <option value="C-K">Celsius to Kelvin</option> <option value="C-R">Celsius to Rankine</option> <option value="K-C">Kelvin to Celsius</option> <option value="K-F">Kelvin to Fahrenheit</option> <option value="K-R">Kelvin to Rankine</option> <option value="F-C">Fahrenheit to Celsius</option> <option value="F-K">Fahrenheit to Kelvin</option> <option value="F-R">Fahrenheit to Rankine</option> <option value="R-C">Rankine to Celsius</option> <option value="R-K">Rankine to Kelvin</option> <option value="R-F">Rankine to Fahrenheit</option> </select> <br><br> Value to convert:<br> <input type="text" name="conversionInput"> <br><br> <input type="submit" value="Convert"> </form> <br><br> <?php echo $result;?> </body> </html>
-
Check your formulae. C-F should be $conversionValue = $conversionInput * (9/5) + 32; You have (5/9). I haven't checked any other
-
Change $line = rtrim($line) . " Be careful with cats!" . PHP_EOL; to $lines[$n] = rtrim($line) . " Be careful with cats!" . PHP_EOL;
-
You have a mysqli connection but you are using mysql_query(). You can't mix the two libraries. Use mysqli_query(). Better would by to create a mysqli prepared statement and repeatedly execute that with the new data values each time.
-
Best way of organizing dropdown menus please
Barand replied to benoit1980's topic in PHP Coding Help
Here's an example <?php $a_towns = array(1=>'Town A','Town B','Town C','Town D','Town E'); $opts="<option value=''>- Select Town -</option>\n"; foreach($a_towns as $k=>$t) { $opts .= "<option value='$k'>$t</option>\n"; } ?> <html> <head> <style type='text/css'> div.hideable { border: 1px solid gray; width: 200px; height: 50px; padding: 20px; font-size: 20pt; } #div1 { background-color: red; } #div2 { background-color: green; } #div3 { background-color: blue; } </style> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript"> $().ready(function() { $(".hideable").hide(); $("#menuA").change(function() { $(".hideable").hide(); switch ($("#menuA").val()) { case '1': case '2': $("#div1").show(); break; case '3': $("#div2").show(); break; case '4': $("#div3").show(); break; case '5': $("#div2").show(); break; } }) }) </script> </head> <body> <select name='menuA' id='menuA'> <?=$opts?> </select> <br> <div id="div1" class='hideable'>DIV 1</div> <div id="div2" class='hideable'>DIV 2</div> <div id="div3" class='hideable'>DIV 3</div> </body> </html> -
Best way of organizing dropdown menus please
Barand replied to benoit1980's topic in PHP Coding Help
You would initially build you page content and dropdown options with PHP, but the showing of the div content would be done client-side using javascript/jquery -
Quantity in shopping basket add and subtract buttons not working
Barand replied to mik_se7's topic in PHP Coding Help
How do you know which quantity belongs to which item, all you save is one $qty -
It was a bespoke application targeted specifically for the client's database and table relationships. It would not be of general use.
-
Quantity in shopping basket add and subtract buttons not working
Barand replied to mik_se7's topic in PHP Coding Help
if ($qty < 5){ $addqty = $qty + 1; Where is $qty defined? -
You could try removing 'message' from the $required array $required = array('name','email');
-
split array by first part of key name into other arrays
Barand replied to AdRock's topic in PHP Coding Help
try $data = array(); foreach ($rows as $arr) { foreach ($arr as $k => $v) { list($cat, $attr) = explode('.', $k); if ($attr == 'name') { $name = $v; $data[$cat][$name] = array(); } else { $data[$cat][$name][$attr] = $v; } } } RESULT $data = Array ( [fruit] => Array ( [Apple] => Array ( [colour] => Red [weight] => 0.1 ) [Banana] => Array ( [colour] => Yellow [weight] => 0.7 ) ) [vegetable] => Array ( [Carrot] => Array ( [colour] => Orange [weight] => 0.05 ) [Potato] => Array ( [colour] => Brown [weight] => 0.6 ) ) ) -
My query above did not take into account new clients who had no previous test results. SELECT a.iduser , CONCAT(first_name,' ',last_name) as name , DATE_FORMAT(apptime, '%a %D %l:%i%p') as time , a.idtreatment , a.idstylist , t.idtreatmentTest , tu.dateOfTreatment , tt.treatmentTestExpiry , CASE WHEN DATEDIFF(a.apptime, IFNULL(tu.dateOfTreatment,'0000-01-01')) > treatmentTestExpiry THEN 'TEST REQUIRED' ELSE '' END as testReq FROM appointment a INNER JOIN user u USING (iduser) INNER JOIN treatment t ON a.idtreatment = t.idtreatment LEFT JOIN treatmenttest tt ON t.idtreatmenttest = tt.idtreatmenttest LEFT JOIN ( SELECT iduser, idtreatment, MAX(dateOfTreatment) as dateOfTreatment FROM treatmentuser GROUP BY iduser, idtreatment ) tu ON a.idtreatment = tu.idtreatment AND a.iduser = tu.iduser ORDER BY time;
-
mysqli display field name / conditionally format table
Barand replied to TheAlmightyOS's topic in PHP Coding Help
I use this to print query results into a table with headings (useful for testing queries) echo query2HTMLtable($conn, "SELECT * FROM tablename"); function query2HTMLtable($db, $sql) { $output = "<table border='1' cellpadding='2' style='border-collapse:collapse'>\n"; // Query the database $result = $db->query($sql); // check for errors if (!$result) return ("$db->error <pre>$sql</pre>"); if ($result->num_rows == 0) return "No matching records"; // get the first row and display headings $row = $result->fetch_assoc(); $output .= "<tr><th>" . join('</th><th>', array_keys($row)) . "</th></tr>\n"; // display the data do { $output .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n"; } while ($row = $result->fetch_assoc()); $output .= "</table>\n"; return $output; } -
I used your initial tables and added an appointment table CREATE TABLE treatmentUser ( idtreatmentUser INT NOT NULL PRIMARY KEY, idtreatment INT, iduser INT, dateOfTreatment DATE -- yyyy-mm-dd ); CREATE TABLE treatmentTest ( idtreatmentTest INT NOT NULL PRIMARY KEY, treatmentTestExpiry INT -- days for which the test is valid ); CREATE TABLE treatment ( idtreatment INT NOT NULL PRIMARY KEY, idtreatmentTest INT ); CREATE TABLE appointment ( idappointment INT NOT NULL PRIMARY KEY, iduser INT, apptime DATETIME, -- yyyy-mm-dd hh:ii:ss idstylist INT ); And used this query. (The subquery is used so you use only the latest test date for a user as they may have have several tests on record) SELECT a.iduser , CONCAT(first_name,' ',last_name) as name , DATE_FORMAT(apptime, '%a %D %l:%i%p') as time , a.idtreatment , a.idstylist , t.idtreatmentTest , tt.idtreatmentTest , tu.dateOfTreatment , tt.treatmentTestExpiry , CASE WHEN DATEDIFF(a.apptime, tu.dateOfTreatment) > treatmentTestExpiry THEN 'TEST REQUIRED' ELSE '' END as testReq FROM appointment a INNER JOIN user u USING (iduser) INNER JOIN treatment t ON a.idtreatment = t.idtreatment LEFT JOIN treatmenttest tt ON t.idtreatmenttest = tt.idtreatmenttest LEFT JOIN treatmentuser tu ON a.idtreatment = tu.idtreatment AND a.iduser = tu.iduser LEFT JOIN ( SELECT iduser, idtreatment, MAX(dateOfTreatment) as dateOfTreatment FROM treatmentuser GROUP BY iduser, idtreatment ) latest ON tu.iduser = latest.iduser AND tu.idtreatment = latest.idtreatment AND tu.dateOfTreatment = latest.dateOfTreatment ORDER BY time; edit - my data These were my results
-
Your other table structure in your previous post was better, once you get round the notion that users in your tables are actually clients Now you have lost both the association of a test with a treatment and the valid duration for a test before it expires. "Expired" is a derived value (and therefore should not be in a table) based on the duration and when the test was last given. How do you now know if a test has expired without going through and manually updating those "expired" flags?
-
Something like this $tabledata = ''; while($row=mysql_fetch_array($query)) { $subtotal=$_SESSION['cart'][$row['product_id']]['quantity']*$row['price']; $totalprice+=$subtotal; $tabledata .= <<<ROW <tr> <td>{$row['name']}</td> <td><input type="text" name="quantity[{$row['product_id']}]" size="5" value="{$_SESSION['cart'][$row['product_id']]['quantity']}"/></td> <td>{$row['price']}€</td> <td>$subtotal</td> </tr> ROW; }