Jump to content

Barand

Moderators
  • Posts

    24,614
  • Joined

  • Last visited

  • Days Won

    835

Everything posted by Barand

  1. That is not the same as the code I posted and should be generating several errors - if you have your error display/reporting turned on.
  2. You were close with your original method but it needed a couple of tweaks while fetch { if $type != $t { if $t != '' output </optgroup> // only close previous group if there was one output <optgroup> $t = $type end if output <option> end while output </optgroup> // close final group
  3. I have an inflateable baseball bat which I often use on such occasions.
  4. To expand on what @kicken said, numeric values do not get quoted and string values only get quoted if they contain one or more commas. If you really do need everything quoted then you could... function generateCSV($data, $filename) { $fp = fopen($filename, 'w'); foreach ($data as $k => $v) { if ($k==0) { fputcsv($fp, array_keys($v)); } fprintf($fp, '"%s","%s","%s","%s","%s","%s"'."\n", ...array_values($v)); } fclose($fp); } OUTPUT: Date,time,Device,label,throughput_read,throughput_write "11/21/2020","00:00","audigysg36s6101","192905199","1742118968","11.073020990149" "11/21/2020","00:15","audigysg36s6101","192914200","1742253920","11.072679922568"
  5. Using your original form, and mysqli ... if ($_SERVER['REQUEST_METHOD']=='POST') { $stmt = $db->prepare("INSERT INTO player(uniform, nameFirst, nameLast) VALUES (?,?,?)"); $stmt->bind_param('sss', $uniform, $fname, $lname); foreach ($_POST['uniform'] as $k => $uniform) { $fname = $_POST['nameFirst'][$k]; $lname = $_POST['nameLast'][$k]; $stmt->execute(); } }
  6. $data = []; foreach ($_POST['uniform'] as $k => $uniform) { $fname = $_POST['nameFirst'][$k]; $lname = $_POST['nameLast'][$k]; $data[$k] = [$uniform, $fname, $lname]; } // to view the $data echo '<pre>' . print_r[$data, 1] . '</pre>'; // insert each $data element into your table (as shown) foreach ($data as $rec) { // insert record }
  7. try $arr = array("test", "test", "hello", "test", "world", "world", "world", "hello", "test"); $prev = ''; $results = []; $j = 0; foreach ($arr as $i => $v) { if ($v != $prev) { $j = $i; $results[$j] = 1; $prev = $v; } else { $results[$j]++; } } foreach ($results as $k => $v) { echo "$arr[$k] : $v <br>"; } Result: test : 2 hello : 1 test : 1 world : 3 hello : 1 test : 1
  8. You've had some clues. What have you tried?
  9. It was relevant to your problem - if you were using PDO (Sorry, not clairvoyant) Any code given are examples, not necessarily writing your code for you. Rather that you should emulate, not copy. The onus is on you read them, see what they are doing and apply to your situation.
  10. if array == array[i-1] then you have adjacent duplicates
  11. Had you consider starting with an index of 0, then, whenever you add a new player, add 1 to get the index of the new array? This way you can have as few or many as you like. As for your error, have you just blindly copy/pasted my coded without regard to whatever variable contains your mysql connection - or even if yours is pdo or mysqli?
  12. So? To insert $stmt = $pdo->prepare("INSERT INTO mytable (uniform, nameFirst, nameLast) VALUES (:uniform, :nameFirst, :nameLast)"); foreach ($_POST['data'] as $data) { $stmt->execute($data); }
  13. Alternatively, a change to your input naming... <?php if (isset($_GET['data'])) { echo '<pre>', print_r($_GET, 1), '</pre>'; } ?> <form> <input class="form-control" name="data[1][uniform]" type="text" placeholder="Uni #" /> <input class="form-control" name="data[1][nameFirst]" type="text" placeholder="First Name" /> <input class="form-control" name="data[1][nameLast]" type="text" placeholder="Last Name" /> <br> <input class="form-control" name="data[2][uniform]" type="text" placeholder="Uni #" /> <input class="form-control" name="data[2][nameFirst]" type="text" placeholder="First Name" /> <input class="form-control" name="data[2][nameLast]" type="text" placeholder="Last Name" /> <br> <input class="form-control" name="data[3][uniform]" type="text" placeholder="Uni #" /> <input class="form-control" name="data[3][nameFirst]" type="text" placeholder="First Name" /> <input class="form-control" name="data[3][nameLast]" type="text" placeholder="Last Name" /> <br> <input type='submit' name='btnSub' value='Submit'> </form> giving GET = Array ( [data] => Array ( [1] => Array ( [uniform] => 1 [nameFirst] => aaa [nameLast] => bbb ) [2] => Array ( [uniform] => 2 [nameFirst] => ccc [nameLast] => ddd ) [3] => Array ( [uniform] => 3 [nameFirst] => eee [nameLast] => fff ) ) [btnSub] => Submit )
  14. Something like this?... <?php $data = []; foreach ($_GET['uniform'] as $k => $uniform) { $fname = $_GET['nameFirst'][$k]; $lname = $_GET['nameLast'][$k]; $data[$k] = [$uniform, $fname, $lastname]; } ?>
  15. yes, and make sure your php error reporting is on.
  16. I don't see any error check after the query() call. Easier than checking all mysqli calls is to put this line of code before the "new mysqli()" line ... mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
  17. What if you echo the query string to examine the syntax? echo "SELECT * FROM MK_migration_details WHERE mig_bid='".$_SESSION['bid']."'";
  18. Check for mysql error messages print_r($conn->errorInfo()) // PDO echo $conn->error; // mysqli
  19. Probably for a very similar reason as in your previous post. Check for mysqli errors. Put this line of code before you create your mysqli connection then they will all be reported automatically mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
  20. I'm going to say your $stmt->prepare failed, so $stmt does not contain a valid statement object, hence the $stmt->close() failure. Reason for it failing - maybe because you create a table called "todolist" then try to insert into a table called "employees"
  21. Name your inputs as name='str[1]', name='str[2] etc instead os str1, str2 ... Then it's a simple loop when you process the posted data foreach ($_POST['str'] as $i => $value) { if (trim($value) == '') { echo "$i is blank</br>"; // or whatever you want to do with empty ones } }
  22. Telling us what the problem is goes a long way towards getting it resolved.
  23. Simple and easy to debug it may be, but unfortunately it doesn't get past the syntax parser
  24. Use {..} around the complex variables, e.g. echo "<li><a href='{$row['url']}' title='{$row['title']}'><i class='fas fa-user site-nav--icon'></i> Help</a></li>"; ^___________^ ^_____________^
  25. Here's an example showing bookings/available times for 17th Nov 2020 DATA TABLE: jib_reservation TABLE: jib_booking_period +-------+--------+--------+---------------------+---------------------+--------------+ +-----------+------------+----------+ | resid | roomid | userid | start_time | end_time | participants | | period_id | start_time | end_time | +-------+--------+--------+---------------------+---------------------+--------------+ +-----------+------------+----------+ | 1 | 1 | 1 | 2020-11-17 09:00:00 | 2020-11-17 10:30:00 | 6 | | 1 | 08:00:00 | 08:30:00 | | 2 | 2 | 2 | 2020-11-17 08:30:00 | 2020-11-17 10:30:00 | 15 | | 2 | 08:30:00 | 09:00:00 | | 3 | 2 | 3 | 2020-11-17 12:00:00 | 2020-11-17 14:00:00 | 12 | | 3 | 09:00:00 | 09:30:00 | | 4 | 1 | 1 | 2020-11-17 11:00:00 | 2020-11-17 12:00:00 | 5 | | 4 | 09:30:00 | 10:00:00 | | 5 | 3 | 2 | 2020-11-17 15:00:00 | 2020-11-17 17:00:00 | 6 | | 5 | 10:00:00 | 10:30:00 | +-------+--------+--------+---------------------+---------------------+--------------+ | 6 | 10:30:00 | 11:00:00 | | 7 | 11:00:00 | 11:30:00 | | 8 | 11:30:00 | 12:00:00 | | 9 | 12:00:00 | 12:30:00 | | 10 | 12:30:00 | 13:00:00 | | 11 | 13:00:00 | 13:30:00 | | 12 | 13:30:00 | 14:00:00 | | 13 | 14:00:00 | 14:30:00 | | 14 | 14:30:00 | 15:00:00 | | 15 | 15:00:00 | 15:30:00 | | 16 | 15:30:00 | 16:00:00 | | 17 | 16:00:00 | 16:30:00 | | 18 | 16:30:00 | 17:00:00 | +-----------+------------+----------+ CODE <?php require 'db_inc.php'; $db = pdoConnect('test'); $chosen_date = '2020-11-17'; ### ### Get status of bookings on chosen date ### $res = $db->prepare("SELECT r.roomid , concat(p.start_time, ' - ', p.end_time) as slot , u.firstname , v.participants FROM jib_room r CROSS JOIN jib_booking_period p LEFT JOIN jib_reservation v ON r.roomid = v.roomid AND TIME(v.start_time) < p.end_time AND TIME(v.end_time) > p.start_time AND DATE(v.start_time) = ? LEFT JOIN jib_user u USING (userid) ORDER BY slot, roomid" ); $res->execute([ $chosen_date ]); $slots = []; // arrange results into an array foreach ($res as $r) { if (!isset($slots[$r['slot']])) { $slots[$r['slot']] = array_fill_keys(range(1,4),''); // empty room array } $slots[$r['slot']][$r['roomid']] = $r['firstname'] ? "{$r['firstname']} ({$r['participants']})" : '';; } // output array to table $bookdata = ''; foreach ($slots as $s => $rooms) { $bookdata .= "<tr><td>$s</td>"; foreach ($rooms as $user) { $cls = $user ? 'booked' : ''; $bookdata .= "<td class='$cls'>$user</td>"; } $bookdata .= "</tr>\n"; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="creation-date" content="11/16/2020"> <title>Bookings</title> <style type='text/css'> table { width: 800px; font-family: verdana, sans-serif; font-size: 11pt; border-collapse: collapse; } th { background-color: black; color: white; padding: 8px; } td { padding: 4px 8px; } td.booked { background-color: red; color: white } </style> </head> <body> <table border='1'> <tr><th>Booking slot</th><th>Room 1</th><th>Room 2</th><th>Room 3</th><th>Room 4</th></tr> <?=$bookdata?> </table> </body> </html> OUTPUT
×
×
  • 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.