Jump to content

Barand

Moderators
  • Posts

    24,566
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Regarding CASE statements, yes you can use them in an insert. BUT whether the one you propose is feasible is impossible to say without knowing what you are trying to accomplish. Where do "home_id", "away_id" and "l_team_id" come from in that statement?
  2. So there could be old tracklog records for a user that were targetted against a previous goal. At present, the calories burned in these would also be included against the latest goal.
  3. You shouldn't be putting $userid in the query string. It looks as though the actual query being executed is SELECT COUNT(udid) AS `rank` FROM myTable WHERE score > ( SELECT score from myTable WHERE udid = myLongUdidStringHere Because "myLongUdidStringHere" isn't in quotes, sql treats it as a column name. Try function getUserRanks($udid, $conn) { $sql = "SELECT COUNT(udid) AS `rank` FROM myTable WHERE score > ( SELECT score from myTable WHERE udid = :udid )"; $stmt = $conn->prepare($sql); $stmt->execute( [ ':udid' => $udid ] ); $ranks = $stmt->fetchObject(); return $ranks->rank; }
  4. Here's a replacemnt progress_bar function function progress_bar($width, $height, $calories, $target) { $bar = "<svg width='$width' height='$height' view_box='0 0 $width $height'>\n <rect x='0' y='0' width='$width' height='$height' fill='#CCC' />\n"; // calc width of bar for calories already burned if ($target==0) { $target = 1; } if ($calories > 0) { $cal_width = $calories * $width / $target; $bar .= "<rect x='0' y='0' width='$cal_width' height='$height' stroke='#ccc' fill='#3C3' />\n"; } else { $bar .= "<text x='5' y='16' font-size='9pt'>No data for user</text>\n"; } $bar .= "</svg>\n"; return $bar; }
  5. I get that I get that if the userid in the session data has no data in the tables Looks like there are 3 goal records for the user. I assumed 1. If there are several, how are they to be processed?
  6. Could be there is no target "weightlost" in the user's goal record
  7. If you are doing wildcard searches, using LIKE, you are stuck with the long version I'm afraid. Alternative is FULLTEXT index search. (Moving to MySql forum)
  8. Prefix the table name with the database name $sql = "SELECT ID, FirstName, Last Name, Email, Mobile, PromoCode FROM database1.table1 WHERE (Email LIKE ?) OR (Mobile LIKE ?) OR (PromoCode LIKE ?) UNION SELECT ID, FirstName, Last Name, Email, Mobile, PromoCode FROM database2.table1 WHERE (Email LIKE ?) OR (Mobile LIKE ?) OR (PromoCode LIKE ?) "; Databases must be on the same server.
  9. I created some test data. Try this session_start(); $_SESSION['userid'] = 2; // your's is probably set already $con=mysqli_connect($host,$username,$password,$dbname); if (mysqli_connect_errno()) { die("Failed to connect to MySQL: " . mysqli_connect_error()); } $bar_length = 400; $bar_height = 20; $sql = "SELECT SUM(caloriesburned) as calories_burned , CASE g.weightunit WHEN 'lbs' THEN 3500 ELSE 7700 END * weightlost as target_calories FROM goal g INNER JOIN tracklog t USING (userid) WHERE userid = ?"; $stmt = $con->prepare($sql); $stmt->bind_param('i', $_SESSION['userid']); $stmt->execute(); $stmt->bind_result($calories, $target); $res = $stmt->fetch(); function progress_bar($width, $height, $calories, $target) { $bar = "<svg width='$width' height='$height' view_box='0 0 $width $height'>\n <rect x='0' y='0' width='$width' height='$height' fill='#CCC' />\n"; // calc width of bar for calories already burned $cal_width = $calories * $width / $target; $bar .= "<rect x='0' y='0' width='$cal_width' height='$height' stroke='#ccc' fill='#3C3' />\n"; $bar .= "</svg>\n"; return $bar; } ?> <!DOCTYPE html> <html> <head> <title>Sample Progress Bar</title> </head> <body> Calories burned to date : <?=$calories?><br> Target calories : <?=$target?><br> <?= progress_bar($bar_length, $bar_height, $calories, $target) ?> </body> </html>
  10. The quotes should be around the 'userid' bit.
  11. Why have you added the quotes around $_SESSION['userid'] ?
  12. The definition of the progress_bar function EDIT: You need to turn on error reporting in your php.ini file
  13. Keep it simple $inside = []; while($row = $result->fetch_array()) { $hour = date('G', strtotime($row['time'])); $inside[$hour] = $row['temp']; }
  14. My code assumed a PDO connection. I have changed it to mysqli using a prepared statement. $con=mysqli_connect("localhost","root","password","registration"); if (mysqli_connect_errno()) { die("Failed to connect to MySQL: " . mysqli_connect_error()); } $bar_length = 400; $bar_height = 20; $sql = "SELECT SUM(caloriesburned) as calories_burned , CASE g.weightunit WHEN 'lbs' THEN 3500 ELSE 7700 END * weightlost as target_calories FROM goal g INNER JOIN tracklog t USING (userid) WHERE userid = ?"; $stmt = $con->prepare($sql); $stmt->bind_param('i', $_SESSION[userid]); $stmt->execute(); $stmt->bind_result($calories, $target); $res = $stmt->fetch(); echo progress_bar($bar_length, $bar_height, $calories, $target);
  15. Something like this $userid = somevalue; // however you decide which user $bar_length = 400; $bar_height = 20; $sql = "SELECT SUM(caloriesburned) as calories_burned , CASE g.weightunit WHEN 'lbs' THEN 3500 ELSE 7700 END * weightlost as target_calories FROM goal g INNER JOIN tracklog t USING (userid) WHERE userid = ?"; $stmt = $pdo->prepare($sql); $stmt->execute( [$userid] ); $res = $stmt->fetch(); echo progress_bar($bar_length, $bar_height, $res['calories_burned'], $res['target_calories']);
  16. We would need to know the structure of your tables to answer that
  17. Given the target calories and calories to date, then this will give a simple progress bar $bar_length = 400; $bar_height = 20; $target = 1000; // target calories need to burned off $calories = 650; // calculated total burned off so far echo progress_bar($bar_length, $bar_height, $calories, $target); // output progress bar function progress_bar($width, $height, $calories, $target) { $bar = "<svg width='$width' height='$height' view_box='0 0 $width $height'>\n <rect x='0' y='0' width='$width' height='$height' fill='#CCC' />\n"; // calc width of bar for calories already burned $cal_width = $calories * $width / $target; $bar .= "<rect x='0' y='0' width='$cal_width' height='$height' stroke='#ccc' fill='#3C3' />\n"; $bar .= "</svg>\n"; return $bar; }
  18. How are you calculating the calories needed to burned from the target weight loss?
  19. Your final ")" is in the wrong place. It needs to be in the position marked below echo $this->date->add($this->set_interval($del_tim_holder_1[count($del_tim_holder_1)] * 7))->format("d.m.Y"); ^
  20. try foreach (range('A','Z') as $alpha) { echo "<li data-text='$alpha'>$alpha</li>\n"; }
  21. $date = '16.02.2017'; $days = 21; // create dateTime object $dt = new DateTime($date); // create a dateInterval $di = new DateInterval("P{$days}D"); // add interval to the date echo $dt->add($di)->format('d.m.Y'); //--> 09.03.2017
  22. Do you have php error reporting switched on? You certainly aren't checking if there was a mysql error.
  23. Looks like the "space" is some other whitespace character. What does SELECT HEX(price) as hexprice; give you?
  24. That gives mysql> SELECT price -> , CAST(REPLACE(price,' ','') AS signed) as number -> FROM test -> ORDER BY CAST(REPLACE(price,' ','') AS signed); +--------+--------+ | price | number | +--------+--------+ | 8 082 | 8082 | | 8 791 | 8791 | | 8 791 | 8791 | | 9 374 | 9374 | | 9 823 | 9823 | | 10 186 | 10186 | | 12 257 | 12257 | | 12 698 | 12698 | | 13 959 | 13959 | | 14 463 | 14463 | | 14 920 | 14920 | | 15 132 | 15132 | | 16 023 | 16023 | | 16 117 | 16117 | | 16 606 | 16606 | +--------+--------+ So, except for "table" being a reserved word and not to be used as a table name, I see no problem. edit: But why are you storing prices as text? Why not just use DECIMAL.
×
×
  • 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.