Jump to content

Barand

Moderators
  • Posts

    24,607
  • Joined

  • Last visited

  • Days Won

    831

Everything posted by Barand

  1. The thing about programming is that it requires some thought. Why would as user_id be equal to a date value? Why don't you do some reading about how to use SQL instead of taking the "infinite monkeys with typewriters" approach in the hope you eventually come up with a right answer?
  2. This time, read what I said.
  3. PS there is a perfectly good function in php already which does all this for you $file_data = file_get_contents($file); Which reminds me, your function needs to return the file data.
  4. If KB_TO_BYTES has not been defined then you need const KB_TO_BYTES = 1024; // We don't need to write to the file, so just open for reading $fp = fopen($file, 'r'); // open file for reading if ($fp) { $file_data = fread($fp, 8 * KB_TO_BYTES); fclose($fp); //close the file }
  5. Where did I say that? The comments are fine - you need to add the code that implements the comments. You also need to ensure that the constant KB_TO_BYTES has been defined and use it correctly as a constant (ie without the quotes).
  6. You have this comment... but you don't get around to actually opening the file - the comment won't do it for you. Therefore in the next line $fp has not been defined. Further, you have put 'KB_IN_BYTES' inside quotes thus making it a string value (which has a numeric value of 0). So I guess the problem is in trying to read 0 bytes from a file that doesn't exist. And what is the comment about being "good citizens"? You don't close it either. (Has KB_IN_BYTES been defined as constant anywhere?)
  7. Try this // Attempt delete query execution $stmt = $dbc->prepare("DELETE FROM users WHERE user_id = ? "); // prepare query with placeholder (?) for id value $stmt->bind_param('i', $_SESSION['user_id']); // bind the id value to the placeholder if ($stmt->execute()) { // execute the query echo "Records were deleted successfully."; } else { echo "ERROR: Not able to execute query " ; }
  8. That same phpinfo() output will tell you the status of your error reporting settings. If you are developing on a hosted site it is probable that any error reports go to your php error log instead being displayed.
  9. You can check the location of the php.ini file being used in the first section of the output from phpinfo(); EG
  10. I can see I have been talking to myself. I have better things to than waste more time on you.
  11. One way would be to add an "expiry_date" (default NULL) column to your user table. Instead of deleting the record, update the record setting the expiry date to CURRENT_DATE+3 days. Run a job every day that does a "DELETE FROM user WHERE expiry_date < CURRENT_DATE
  12. At the moment, your query says "DELETE all records from the user table where the value in column "user_id" is equal to the value in column "user_id". Is it obvious to you yet why every record gets deleted? You need to fix your query so it compares the value in column user_id against your session value.
  13. I suggest you read your query carefully, bearing in mind that the condition "user_id = user_id" is true for all records.
  14. When your button's name is "delete" why are you checking for $_REQUEST["remove_$i"] instead of $_REQUEST['delete'] ? Stop using REQUEST. Use POST or GET depending on your form's method. if you are fetching data to display, use method GET. If submitting your form has consequences (such as updating, deleting, emailing) then use POST method.
  15. For the record, the problem is an extra heading column, not an extra data column. You have <th> intead of a </th> thus adding an extra header cell. <th>VISITOR DOMAIN ADDRESS<th> ^
  16. Those ini-set()s at the beginning need to be in your php.ini file. If you have startup errors the code isn't executed, so how can it then set and report startup errors???
  17. An alternative is store the data in an array and sort the array. Pagination can be achieved using array_slice(). Example... <?php /* TEST DATA CREATE TABLE `employee` ( `empid` int(11) NOT NULL AUTO_INCREMENT, `fname` varchar(50) DEFAULT NULL, `lname` varchar(50) DEFAULT NULL, `dob` date DEFAULT NULL, PRIMARY KEY (`empid`) ) INSERT INTO `employee` VALUES (1,'Peter','Smith','1985-01-26'), (2,'Paul','Hartley','1973-12-02'), (3,'Mary','Baker','1980-04-11'), (4,'Jane','Doe','1990-11-28'); */ // GET DATA (assumes PDO connection) $res = $db->query("SELECT empid as `Employee ID` , fname as `First Name` , lname as `Last Name` , dob as `Date of Birth` FROM employee "); $data = $columns = []; $row = $res->fetch(); $columns = array_keys($row); do { $data[] = $row; } while ($row = $res->fetch()); // SORT THE DATA $sortby = $_GET['sortby'] ?? 'Employee ID'; $desc = $_GET['desc'] ?? 0; $chk = $desc==1 ? 'Checked' : ''; usort($data, function($a, $b) use ($sortby, $desc) { if ($desc) return $b[$sortby] <=> $a[$sortby]; else return $a[$sortby] <=> $b[$sortby]; }); // TABLE HEADINGS $theads = '<tr style="background-color: #EEE; font-weight: 600"><td>' . join('</td><td>', $columns) . "</td></tr>\n"; // TABLE DATA $tdata = ''; foreach ($data as $d) { $tdata .= "<tr><td>" . join('</td><td>', $d) . "</td></tr>\n"; } // SORT OPTIONS function sortOptions($columns, $current) { $opts = ''; foreach ($columns as $c) { $sel = $c==$current ? 'selected' : ''; $opts .= "<option $sel>$c</option>\n"; } return $opts; } ?> <html> <head> <title>Sample data sort</title> </head> <body> <form> <fieldset> Sort by <select name='sortby' onclick="this.form.submit()"> <?=sortoptions($columns, $sortby)?> </select> DESC <input type="checkbox" name="desc" value="1" <?=$chk?> onclick="this.form.submit()"> </fieldset> </form> <table style="width:80%; margin: 30px auto; font-family: sans-serif;"> <?=$theads?> <?=$tdata?> </table> </body> </html>
  18. What happens if you actually test if the execute() worked? Instead of $subscribe->execute([$name, $email]); if($subscribe){ echo .... try if ($subscribe->execute([$name, $email])) { echo ....
  19. Fred's id is 1, therefore listStaff(1, $users);
  20. If your problem is how to output tab separated data with column names /* SAMPLE DATA +-------+-------+---------+------------+ | empid | fname | lname | dob | +-------+-------+---------+------------+ | 1 | Peter | Smith | 1985-01-26 | | 2 | Paul | Hartley | 1973-12-02 | | 3 | Mary | Baker | 1980-04-11 | | 4 | Jane | Doe | 1990-11-28 | +-------+-------+---------+------------+ */ $res = $db->query("SELECT empid , fname , lname , dob FROM employee; "); echo '<pre>'; $row = $res->fetch(PDO::FETCH_ASSOC); echo join("\t", array_keys($row)) . "\n"; // headings do { echo join("\t", $row) . "\n"; // data } while ($row = $res->fetch()); echo '</pre>'; Which gives Similarly, if you want to write it to a csv file for export to Excel, then $res = $db->query("SELECT empid , fname , lname , dob FROM employee; "); $fp = fopen('AAA.csv', 'w'); $row = $res->fetch(PDO::FETCH_ASSOC); fputcsv($fp, array_keys($row), "\t"); // headings do { fputcsv($fp, $row, "\t"); // data } while ($row = $res->fetch());
  21. Data normization Efficiency Spelling mistakes
  22. Use ids, not names, to link records. TABLE: user +----+----------+------------+ | id | username | leader_id | +----+----------+------------+ | 1 | fred | 2 | | 2 | mo | NULL | | 3 | brian | 2 | | 4 | john | 2 | | 5 | peter | 1 | | 6 | curly | 1 | | 7 | joan | 1 | | 8 | Dennis | 6 | +----+----------+------------+ Recursion is your friend here. $res = $db->query("SELECT id , username , leader_id FROM usertest order by leader_id "); $users = []; // store users in array for each leader foreach ($res as $r) { $users[$r['leader_id']][] = [ 'id' => $r['id'], 'username' => $r['username'] ]; } echo '<pre>'; listStaff(null, $users, 0); // list staff for leader "null" echo '</pre>'; /** * recursive function to list staff * * @param int $id * @param array $users * @param int $level */ function listStaff($id, &$users, $level=0) { $indent = str_repeat("\t", $level); foreach ($users[$id] as $u) { // for each of their staff echo "$indent{$u['username']}<br>"; // outout the name if (isset($users[$u['id']])) { // if they have staff listStaff($u['id'], $users, $level+1); // list their staff } } } Giving mo fred peter curly Dennis joan brian john
  23. Input data (horsetest) +----+---------+------+------+----------+------------+ | id | name | sire | dam | gender | dob | +----+---------+------+------+----------+------------+ | 1 | Horse A | NULL | NULL | STALLION | 2005-01-15 | | 2 | Horse B | NULL | NULL | MARE | 2005-03-19 | | 3 | Horse C | NULL | NULL | MARE | 2006-03-11 | | 4 | Horse D | 1 | 2 | STALLION | 2009-05-10 | | 5 | Horse E | 1 | 3 | MARE | 2010-08-25 | +----+---------+------+------+----------+------------+ then to get mother and father SELECT h.id , h.name , h.gender , TIMESTAMPDIFF(YEAR, h.dob, CURDATE()) as age , h.sire as f_id , s.name as father , h.dam as m_id , d.name as mmother FROM horsetest h JOIN horsetest s ON h.sire = s.id JOIN horsetest d ON h.dam = d.id; giving +----+---------+----------+------+------+---------+------+---------+ | id | name | gender | age | f_id | father | m_id | mmother | +----+---------+----------+------+------+---------+------+---------+ | 4 | Horse D | STALLION | 10 | 1 | Horse A | 2 | Horse B | | 5 | Horse E | MARE | 9 | 1 | Horse A | 3 | Horse C | +----+---------+----------+------+------+---------+------+---------+
  24. There are examples here
  25. https://www.php.net/manual/en/oci8.setup.php
×
×
  • 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.