Jump to content

Barand

Moderators
  • Posts

    24,566
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Alternatively $pop = 54321; $res = $pdo->prepare("SELECT ROUND( ? / DAYOFYEAR(CURDATE()))"); $res->execute([$pop]); echo $res->fetchColumn(); //--> 1025 or (no database required)... echo round($pop / (date('z')+1)); //--> 1025
  2. You can do with a single INSERT SELECT statement. INSERT INTO Images (name, person_id, filename, description, medium) SELECT name , table_id , image_name , description , medium FROM tbl_person_data; Doing it with a prepared statement, as you are, requires either execute with a numerically indexed array and ? as placeholders, or execute with an associative array and named placeholders matching the array keys
  3. If you are so error-prone, use your form which maintains those answer_group/answer tables to validate. If you create a group for 5, only show 5 answer rows. If editing and you accidentally have 6 answers, display the sixth in say, red, to flag the error.
  4. try foreach ($array as $k => $d) { if ($k > 0) { if (strtotime($d) > strtotime($array[$k-1])+6) { $new[] = "-------------------"; } } $new[] = $d; } $new = Array ( [0] => 2021-02-10 09:04:48 [1] => 2021-02-10 09:04:54 [2] => 2021-02-10 09:05:00 [3] => 2021-02-10 09:05:06 [4] => 2021-02-10 09:05:12 [5] => 2021-02-10 09:05:18 [6] => ------------------- [7] => 2021-02-10 09:06:18 [8] => 2021-02-10 09:06:24 ) [edit...] Alternative solution... $new = []; $newkey = 0; foreach ($array as $k => $d) { if ($k > 0) { if (strtotime($d) > strtotime($array[$k-1])+6) { $newkey++; } } $new[$newkey][] = $d; } gives $new = Array ( [0] => Array ( [0] => 2021-02-10 09:04:48 [1] => 2021-02-10 09:04:54 [2] => 2021-02-10 09:05:00 [3] => 2021-02-10 09:05:06 [4] => 2021-02-10 09:05:12 [5] => 2021-02-10 09:05:18 ) [1] => Array ( [0] => 2021-02-10 09:06:18 [1] => 2021-02-10 09:06:24 ) )
  5. In PHP, string indexes for arrays should be quoted. I am talking about the HTML in your form. You are naming the input field as "['title']" instead of "[title]"
  6. name="1['title']" ^ ^ remove those single quotes in all inout names
  7. I wouldn't bother. It's not as though your users will be creating these "answer" records. It's completely under your control and testing will show any "accidents". And what if it really is a 5-answer group but you accidentally enter "4" or "6" in the group table? It's equivalent to storing a derived total instead of getting the total from the data. Let the number of answers dictate the group size.
  8. print_r() doesn't normally put quotes around keys, just [..]. EG Array ( [fname] => joe [lname] => bloggs ) If you have them, your form must be adding them to the input field names. Show your form code.
  9. Are you sure it isn't referring to line 64 in a different file? It's not like the error reports to quote an empty line number.
  10. Define customer/product as UNIQUE... CREATE TABLE `cart` ( `cart_id` int(11) NOT NULL AUTO_INCREMENT, `customerID` int(11) DEFAULT NULL, `productID` varchar(5) DEFAULT NULL, `quantity` int(11) DEFAULT NULL, PRIMARY KEY (`cart_id`), UNIQUE KEY `UNQ_cart_product` (`customerID`,`productID`) ) BEFORE +---------+------------+-----------+----------+ | cart_id | customerID | productID | quantity | +---------+------------+-----------+----------+ | 1 | 1 | ABC12 | 3 | | 2 | 1 | DEF23 | 5 | +---------+------------+-----------+----------+ UPDATE INSERT INTO cart (customerID, productID, quantity) VALUES (1, 'ABC12', 2), (1, 'DEF23', 5), (1, 'GHI34', 10) ON DUPLICATE KEY UPDATE quantity = quantity + VALUES(quantity); AFTER +---------+------------+-----------+----------+ | cart_id | customerID | productID | quantity | +---------+------------+-----------+----------+ | 1 | 1 | ABC12 | 5 | | 2 | 1 | DEF23 | 10 | | 3 | 1 | GHI34 | 10 | +---------+------------+-----------+----------+
  11. This the content of my include file ("db_inc.php") <?php define("HOST",'localhost'); define("USERNAME",'????'); define("PASSWORD",'????'); define("DATABASE", 'test'); // default database name function pdoConnect($dbname=DATABASE) { $db = new PDO("mysql:host=".HOST.";dbname=$dbname;charset=utf8",USERNAME,PASSWORD); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); return $db; } function myConnect($dbname=DATABASE) { mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); $db = mysqli_connect(HOST,USERNAME,PASSWORD,$dbname); $db->set_charset('utf8'); return $db; } To use include 'db_inc.php'; $conn = pdoConnect(); // pdo connection to default database or, for example, to connect a different database using mysqli $conn = myConnect('sakila'); I recommend using PDO.
  12. TIP: If you are creating home-grown charts, plotting the values is the easy bit. 95% of the coding effort will be in the drawing of chart area, plot area, axes, axis labels, scaling, titles etc. You can sidestep this with a simple table with horizontal bars. EG CODE EXAMPLE... <?php $values = [ 'Strongly Disagree' => 7, 'Disagree' => 10, 'Neither' => 12, 'Agree' => 25, 'Strongly Agree' => 41 ]; function valueChart(&$values) { $out = "<table class='chartTable'> <tr><th>Response</th> <th>Total</th> <th>Percent</th> </tr> "; $totalVal = array_sum($values); foreach ($values as $resp => $n) { $out .= "<tr><td>$resp</td> <td class='ra'>$n</td> <td>" . bar($n / $totalVal * 100) . "</td></tr>\n"; } $out .= "</table\n"; return $out; } function bar($val=0) { $a = '#3399ff'; $b = '#e6f2ff'; $c = '#0066cc'; $bg = '#eee'; $width = 300; $height = 25; $svg = <<<SVG <svg width='$width' height='$height' viewBox='0 0 $width $height'>"; <defs> <linearGradient id="bargrad" x1="0" y1="0" x2="0" y2="1"> <stop offset="0%" style="stop-color:$a"/> <stop offset="25%" style="stop-color:$b"/> <stop offset="100%" style="stop-color:$c"/> </linearGradient> </defs> <rect x='0' y='0' width='$width' height='$height' style='fill:$bg' stroke='#999'/> SVG; $w = $val/100 * $width; $svg .= "<rect x='0' y='0' width='$w' height='$height' style='fill:url(#bargrad)' />"; $svg .= "</svg>\n"; return $svg; } ?> <!DOCTYPE html> <html lang="en"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Chart Example</title> <head> <style type='text/css'> .chartTable { font-family: arial, sans-serif; font-size: 11pt; } th { padding: 4px 16px ; } td { padding: 0 16px; } .ra { text-align: right; } </style> </head> <body> <?=valueChart($values)?> </body> </html> Hope this helps.
  13. Have you got PHP error reporting turned on and have you set your PDO options to report exceptions?
  14. Why don't you RTFM to see what $$ does? try $stmt = $conn->prepare("INSERT INTO mytable (title, medium, person_id, name) VALUES (:title, :medium, :person_id, :name) "); foreach ($titles as $title_elements) { if ($title_elements['title'] != '') { $stmt->execute($title_elements); } }
  15. That is NOT a facsimile of your titles array. Your $titles array has keys of "title", "medium" etc. That array above has values of "title", medium" etc with numeric keys. Use print_r(array) on both to seee the difference.
  16. Primary keys are unique identifiers for each entity and therefore, by definition, are already UNIQUE. (Or did you really mean monkeys? 🙂 )
  17. You send and receive the array OK. That just leaves your processing of it inside the function.
  18. What's your code that calls generateSurveyItem() ?
  19. It doesn't like anything less than 0 as an offset value in the LIMIT clause.
  20. On line 601 you set $page to "" (empty string) You only set it to an int value if $_GET['page'] is set. (603) Change 601 to $page = 0;
  21. Which particular bit of your code is giving you a problem?
  22. No, but it it gives you a way of viewing the data in order you want. To make it permanent you woud have to ALTER the table or create a view with required order and use that instead of the table.
  23. My theory bites the dust then. I thought it might be a string.
  24. original table Query... ALTER TABLE `test`.`user_demo` CHANGE COLUMN `username` `username` VARCHAR(20) NULL DEFAULT NULL AFTER `userid`; new table [EDIT....] There are a couple of alternatives. 1 ) Use a query to specify the required column sequence SELECT fname , lname , username , userid FROM test.user_demo; 2 ) Create a "view" on the table with the required column sequence.
×
×
  • 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.