-
Posts
24,612 -
Joined
-
Last visited
-
Days Won
834
Everything posted by Barand
-
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());
-
Data normization Efficiency Spelling mistakes
-
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
-
display pedigree table fron mysql with php by levels
Barand replied to logandro's topic in PHP Coding Help
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 | +----+---------+----------+------+------+---------+------+---------+ -
There are examples here
-
Err500 when trying to open php page from browser
Barand replied to Abhinov's topic in PHP Coding Help
https://www.php.net/manual/en/oci8.setup.php -
display pedigree table fron mysql with php by levels
Barand replied to logandro's topic in PHP Coding Help
The basic processing is (pseudocode) function tree (id) { get id, name, sire, dam for id if not found return endif output id/name tree(sire) tree(dam) } // output pedigree tree for for horse X tree(X) -
display pedigree table fron mysql with php by levels
Barand replied to logandro's topic in PHP Coding Help
About 15 years ago I wrote a function to produce this from data very much like yours EG SELECT * FROM horse; +----+------+------+------+----------+ | id | name | sire | dam | gender | +----+------+------+------+----------+ | 26 | A | 27 | 39 | STALLION | | 27 | B | 28 | 40 | STALLION | | 28 | C | 29 | 40 | STALLION | | 29 | D | 30 | 40 | STALLION | | 30 | E | 31 | 41 | STALLION | | 31 | F | 32 | 41 | STALLION | | 32 | G | 33 | 42 | STALLION | | 33 | H | 33 | 42 | STALLION | | 34 | I | 35 | 43 | STALLION | | 35 | J | 36 | 43 | STALLION | | 36 | K | 37 | 44 | STALLION | | 37 | L | 38 | 44 | STALLION | | 38 | M | 34 | 45 | STALLION | | 39 | N | 29 | 46 | MARE | | 40 | O | 27 | 51 | MARE | | 41 | P | 28 | 51 | MARE | | 42 | Q | 29 | 50 | MARE | | 43 | R | 30 | 50 | MARE | | 44 | S | 31 | 49 | MARE | | 45 | T | 32 | 49 | MARE | | 46 | U | 33 | 48 | MARE | | 47 | V | 34 | 48 | MARE | | 48 | W | 35 | 46 | MARE | | 49 | X | 36 | 46 | MARE | | 50 | Y | 37 | 47 | MARE | | 51 | Z | 38 | 47 | MARE | +----+------+------+------+----------+ It uses a recursive* function which gets the sire and dam for the horse $id and outputs the name. It then calls itself for the sire and the dam which outputs their names and get their parents. This is repeated until the specified level of ancestry is reached. Is that the sort of thing you are trying to do? If so, give it a go using the method I described and come back if oyu get stuck * recursion : see recursion -
Setting option in a select dynamically with PHP
Barand replied to Adamhumbug's topic in PHP Coding Help
I find it easier to get options from a function, then the current value (in this case the "$user_role") can be passed as a parameter function roleOptions($conn, $current) { $sql = "SELECT role_id , role_name FROM ssm_role ORDER BY role_name ASC"; $opts = ''; if($result = mysqli_query($conn, $sql)) { if (mysqli_num_rows($result)>0){ while ($row = mysqli_fetch_array($result)){ $sel = $row['role_id'] == $current ? 'selected' : ''; $opts .= "<option $sel value='".$row['role_id']."'>".$row['role_name']."</option>"; } } } return $opts; } HTML <select name="role"> <?= roleOptions($conn, $user_role) ?> </select> -
OK, so if instead of 1 action you have three separate scripts specified as actions for the form - but you still have to write the code to do those actions. Each of those three would have to do step 1 (get the data) thus tripling that code. If one step fails, do you still want too do the others? How do you control that if they are now separated? What would you gain?
-
Remove the comma before the WHERE
-
You could take the code in your first post, make it a function and use the " wash, rinse, repeat" approach.
-
Also, your javascript looks decidedly dodgy to me. Two functions called on same event??? They refer to objects with id = 'det_X' -- you have no objects with ids beginning with "det_"
-
Does your print_r($attData); on line 8 show the correct data?
-
Perhaps you could give us a clue What is it doing that it shouldn't do What is it not doing that it should do?
-
Err500 when trying to open php page from browser
Barand replied to Abhinov's topic in PHP Coding Help
From the php manual... -
Then your "MyPHPScript.php" should retrieve the inputs from the $_GET array Send the email Update the database Use http_build_query() to rebuild the querystring then use header("Location: NEW URL WITH QUERYSTRING) to send to remote page. = euphemism for "if someone will write it for me (for free)", which is probably why you can't get the answer you really want.
-
Seriously!? You let anyone delete any record from any table just by putting values in a query string? http://.../delete.php?target=user&username=admin Brownie points for trying to use "prepare()" but your usage is wrong. The $id variable should not be in the query (that's the whole point of prepared statements). Use a placeholder instead and pass the id as a parameter $stmt = $pdo->prepare("DELETE FROM SOME_DATABASE.$table WHERE $idType = ?"); $status = $stmt->execute( [$id] );
-
How to make an associative array with $key => $value pairs
Barand replied to makamo66's topic in PHP Coding Help
Here's an example if ($_SERVER['REQUEST_METHOD']=='POST') { $post = array_map('trim', $_POST); if (!empty($post['productid']) && !empty($post['qty'])) { if (!isset($_SESSION['cart'][$_POST['productid']])) { $_SESSION['cart'][$_POST['productid']] = 0; } $_SESSION['cart'][$_POST['productid']] += $_POST['qty']; } } from an earlier topic of yours ! -
Display which row and which column has the biggest number of 1's
Barand replied to Dobby's topic in PHP Coding Help
I assume you've had a go at it yourself by now. Here's my effort... <?php $data = []; for ($r=0; $r<4; $r++) { for ($c=0; $c<4; $c++) { $data[$r][$c] = rand(0,1); } } $rowcounts = $colcounts = []; for ($r=0; $r<4; $r++) { $rowcounts[$r] = count(array_keys($data[$r], 1)); } for ($c=0; $c<4; $c++) { $colcounts[$c] = count(array_keys(array_column($data,$c), 1)); } $rmax = max($rowcounts); $cmax = max($colcounts); $rcmax = max($rmax, $cmax); $hirows = array_keys($rowcounts, $rcmax); $hicols = array_keys($colcounts, $rcmax); echo "<table border='1' style='border-collapse:collapse'>\n"; for ($r=0; $r<4; $r++) { echo "<tr>"; for ($c=0; $c<4; $c++) { $hilite = (in_array($r, $hirows) || in_array($c, $hicols)) ? 'class="max"' : ''; echo "<td $hilite> {$data[$r][$c]} </td>"; } echo "</tr>\n"; } echo "</table>\n"; ?> -
Err500 when trying to open php page from browser
Barand replied to Abhinov's topic in PHP Coding Help
Do you have this line in your php.ini file... ;extension=php_oci8_12c.dll If so, remove the ";" to enable the extension and check you have that dll file in your extensions folder. -
Err500 when trying to open php page from browser
Barand replied to Abhinov's topic in PHP Coding Help
Sounds like you don't the required library installed or enabled. Check the extensions in the php.ini file or use phpinfo() to see if there is an OCI section. https://www.php.net/manual/en/oci8.requirements.php