Jump to content

Barand

Moderators
  • Posts

    22,438
  • Joined

  • Last visited

  • Days Won

    623

Community Answers

  1. Barand's post in create directory, rename file from source and place into new directory with date was marked as the answer   
    try
    echo "<a href='$latest_dir $latest_file'><button>"."continue</button></a><br>"; ^ ^ ... adding the single quotes, otherwise the href finishes at the space. (Are you sure you want the space?)
  2. Barand's post in Explode was marked as the answer   
    echo "$game[6] $game[7]/$game[4] $game[5] $game[9] $game[10] $game[11] - $game[12] $game[13] $game[14]";  
    is redundant
  3. Barand's post in returning the result of multidimentional array was marked as the answer   
    (This example uses the "house" and "pupil" tables from my SQL tutorials instead of your teams and players)
    $res = $db->query("SELECT h.house_name , p.fname , p.lname FROM house h JOIN pupil p USING (houseid) "); $data = []; foreach ($res as $row) { if (!isset($data[$row['house_name']])) { $data[$row['house_name']] = []; } $data[$row['house_name']][] = [$row['fname'], $row['lname']]; } foreach ($data as $house => $pupils) { echo "$house<ul>"; foreach ($pupils as $p) { echo "<li>{$p[0]} {$p[1]}</li>"; } echo "</ul>"; } giving

  4. Barand's post in   mysqli INSERT INTO from tabel not working was marked as the answer   
    Have you tried using mysqil's error reporting to find the reason?
  5. Barand's post in Trying to use php rand() function (newbie) was marked as the answer   
    Is the file that the code is in a .PHP file or a .HTML file?
    If it's not .php, rename it and try again.
  6. Barand's post in Suggestions welcomed on this? was marked as the answer   
    That is what you don't want to do. Don't increase or reduce values in tables. Instead store individual referals and individual offers. The number still required will be the difference between total referals and total offers which can be calculated when required by a query.
    So if you you had a db structure similar to this
    +-----------------+ +----------------+ | organisation | | volunteer | +-----------------+ +----------------+ | org_id |----+ | volunteer_id |----+ | org_name | | | name | | | contact | | | address | | | etc. | | | phone | | +-----------------+ | | etc | | | +------------------+ +----------------+ | +----------------+ | | referal | | | help_offer | | +------------------+ | +----------------+ | | ref_id | | | help_id | +-----<| org_id | +------<| volunteer_id | | referal_date | | offer_date | | sex | | sex | | age | | age | | name | | quantity | | address | +----------------+ | etc | +------------------+ ... then counting the referals by sex/age gives the requirement and summing the quantities of help offered by sex/age gives the aid total. The deferrences are how many you still require.
  7. Barand's post in Finding duplicate columns was marked as the answer   
    Like this?
    SELECT id as ids , job_number , line_item FROM production_data WHERE line_item = '' UNION SELECT GROUP_CONCAT(id separator ', ') as ids , job_number , line_item FROM production_data GROUP BY job_number, line_item HAVING COUNT(*) > 1; [edit] If you want the ids in their own rows, then
    SELECT id , job_number , line_item FROM production_data WHERE line_item = '' UNION SELECT p.id , dupes.job_number , dupes.line_item FROM production_data p JOIN ( SELECT job_number , line_item FROM production_data GROUP BY job_number, line_item HAVING COUNT(*) > 1 ) dupes USING (job_number, line_item);  
  8. Barand's post in Create alert in php and then remove from dom was marked as the answer   
    You could try something like this example
    <?php if (isset($_GET['ajax'])) { if (rand(0,1)) { exit("Bar Code Incorrect"); } else { exit("Everything OK"); } } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Test</title> <meta charset="utf-8"> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type='text/javascript'> $().ready( function() { $("#test").click( function() { $.get( "", {"ajax":1}, function(resp) { if (resp=="Bar Code Incorrect") { let err = $("<div>", {"id":"error", "class":"alert alert-warning", "html":resp}) $("#output").html(err) $("#error").fadeOut(4000) } else { $("#output").html(resp) } } ) }) }) </script> <style type='text/css'> .alert { background-color: red; color: white; padding: 16px; } </style> </head> <body> <button id='test'>Test</button> <div id='output'></div> </body> </html>  
  9. Barand's post in SQL Manipulation was marked as the answer   
    Your code seems to be assuming that $delete will be "false" if that first select query does not find any results. Wrong. It will be false only if the query fails with an error. Not finding records is not an error.
    I would use that first select query to find out how many books there were for that id. Here's a PDO example...
    if ($_SERVER['REQUEST_METHOD']=='POST') { $res = $conn->prepare("SELECT quantity FROM book WHERE idb = ? "); $res->execute( [$_POST['idb'] ] ); $available = intval($res->fetchColumn()); if ($available >= $_POST['quantity'] ) { $res = $conn->prepare("UPDATE book SET quantity = quantity - ? WHERE idb = ? "); $res->execute( [ $_POST['quantity'], $_POST['idb'] ] ); echo "Book/s successfully removed<br>"; } else { echo "There are only $available books in stock<br>"; } // Remove books with zero quantity $count = $conn->exec("DELETE FROM book WHERE quantity = 0"); echo "$count books were removed<br>"; }  
  10. Barand's post in How do I make a link go to a specific search, I'm new to this and I'm making a project was marked as the answer   
    Have you got
    if(isset($_POST['submit'])){ or
    if(isset($_GET['search'])){  
  11. Barand's post in Need Some Help Figuring Out Arrays was marked as the answer   
    It looks like your $AFC/$NFC array keys are not 0-based, as they need to be, so $AFC[0] and $NFC[0] do not exist. (It should be throwing out messages to that effect - are you reporting your errors?)
    Try
    $AFC = array_values($AFC); $NFC = array_values($NFC); just before the loop to update the matrix table.
  12. Barand's post in View a php script in pdf was marked as the answer   
    You could save the output as an image file and import that into the PDF file.
    FPDF supports png, jpeg, gif
    TCPDF supports the above plus SVG images (and you can also embed SVG script into the PDF)
  13. Barand's post in Web Scraping - Unstructured html table was marked as the answer   
    The data looks OK. It could do with some separation between the blocks of student data to make it easier to read. Perhaps...
    foreach($results_by_date as $name => $work) { foreach($work as $kdate => $date) { if($kdate == 'date'){ echo str_repeat('-', 20)."\n" ; echo $date. "\n\n"; }else{ foreach($date as $kdata => $date_data){ foreach($date_data as $kdatakey => $data){ echo "&nbsp;" . $data. "\n"; } echo "&nbsp;\n" ; } } } } FYI, this is my version for cli output
    // JSON from previous script saved in file $results = json_decode(file_get_contents('c:/inetpub/wwwroot/test/doc1355/rohan.json'), 1); $pad = str_repeat(' ', 8); $divs = $pad . str_repeat('-', 12) . PHP_EOL; $divd = str_repeat('-', 20) . PHP_EOL; echo '<pre>'; // test only - not required in CLI mode foreach ($results as $day) { echo $day['date'] . PHP_EOL . PHP_EOL; foreach ($day['students'] as $k => $sdata) { if ($k) echo $divs; foreach ($sdata as $val) { echo $pad . $val . PHP_EOL; } } echo $divd; }  
  14. Barand's post in Making Table head and its data dynamic in CodeIgniter was marked as the answer   
    I'd do it this way
    $res = $db->query("SELECT status_to , COUNT(*) as tot FROM crm_log GROUP BY status_to "); $totals = array_column($res->fetchAll(), 'tot', 'status_to'); $heads = "<tr><th>" . join('</th><th>', array_keys($totals)) . "</th></tr>\n"; $vals = "<tr><td>" . join('</td><td>', $totals) . "</td></tr>\n"; ?> <table border='1'> <?=$heads?> <?=$vals?> </table>
  15. Barand's post in W3 css with color value was marked as the answer   
    Of course you can
    <style type='text/css'> .w3-vomit { background-color: #f8b9ce; color: #c4ff4d; } </style> <h1 class="w3-vomit w3-padding">Example heading</h1>
  16. Barand's post in Getting all data in a particular timeframe for the respective agent_id was marked as the answer   
    Data
    +----+---------+----------+-----------+---------------------+ | id | refno | agent_id | status_to | logtime | +----+---------+----------+-----------+---------------------+ | 1 | LP01552 | 57 | Draft | 2021-10-05 10:33:12 | | 2 | LP02552 | 57 | Unpublish | 2021-10-04 10:33:12 | | 3 | LP03552 | 57 | Draft | 2021-10-05 10:33:12 | | 4 | LP04552 | 57 | Publish | 2021-10-09 10:33:12 | | 5 | LP05552 | 57 | Draft | 2021-10-10 10:33:12 | | 6 | LP06552 | 57 | Publish | 2021-10-11 10:33:12 | | 7 | LP07552 | 57 | Action | 2021-10-06 10:33:12 | | 8 | LP08552 | 58 | Draft | 2021-10-02 10:33:12 | | 9 | LP09552 | 58 | Unpublish | 2021-10-11 10:33:12 | | 10 | LP09652 | 58 | Publish | 2021-10-08 10:33:12 | | 11 | LP08542 | 59 | Draft | 2021-10-06 10:33:12 | | 12 | LP09542 | 59 | Unpublish | 2021-10-06 10:33:12 | | 13 | LP09642 | 59 | Draft | 2021-10-07 10:33:12 | +----+---------+----------+-----------+---------------------+ Code
    <?php $res = $db->prepare("SELECT agent_id as Agent , SUM(status_to = 'Draft') as Draft , SUM(status_to = 'Unpublish') as Unpublish , SUM(status_to = 'Publish') as Publish , SUM(status_to = 'Action') as Action FROM crm_log WHERE logtime BETWEEN ? AND ? GROUP BY agent_id "); $res->execute( [ '2021-10-01', '2021-10-31' ] ); $data = ''; $row = $res->fetch(); $heads = "<tr><th>" . join('</th><th>', array_keys($row)) . "</th></tr>\n"; do { $data .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n"; } while ($row = $res->fetch()); ?> <table border='1' style='border-collapse: collapse; width: 500px'> <?=$heads?> <?=$data?> </table> Output

  17. Barand's post in Unexpected behaviour while looping a table was marked as the answer   
    Instead of changing the display to "block" when you click the button, set it to "display: table-cell;"
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type='text/javascript'> function showDetails(cnt) { $(".more").css("display", "none"); // hide all var details = "details"+cnt; var x = document.getElementById(details); if (x.style.display === "none") { x.style.display = "table-cell"; // display requested one } else { x.style.display = "none"; } } </script>  
  18. Barand's post in seperate grouped radio button for each photo - keep/delete options was marked as the answer   
    Think of radio buttons as menu options. They need the same name (so the browser knows they are part of the same menu) but different values that the user selects.
    Use the id (image id I presume) as part of the name to group them
     
    <input type="radio" name="action[<?=$result['id']?>]" value="A"> Approve <input type="radio" name="action[<?=$result['id']?>]" value="D"> Delete Then to process the form data
    foreach ($_POST['action'] as $id => $action) { if ($action == 'D') { // delete image whose ID is $id } elseif ($action == 'A') { // approve image whose ID is $id } }  
  19. Barand's post in Loginform with session and Ldap was marked as the answer   
    That line is setting the session value to an array. did you mean  = $_POST['username'] ?
  20. Barand's post in PHP parsing XML data and draw HTML Table was marked as the answer   
    Plan B

  21. Barand's post in How to Mysql count only higest values per row was marked as the answer   
    The subquery calcultaing the maximum scores .behaves just like a temporary table. So you can the join it to tbale using user and count by country.
    SELECT a.country , sum(b.version=1) as V1 , sum(b.version=2) as V2 , sum(b.version=3) as V3 , sum(b.version=4) as V4 , sum(b.version=5) as V5 , sum(b.version=6) as V6 FROM table1 a JOIN ( SELECT user , MAX(version) as version FROM table2 GROUP BY user ) b USING (user) GROUP BY country ORDER BY country DESC; +---------+------+------+------+------+------+------+ | country | V1 | V2 | V3 | V4 | V5 | V6 | +---------+------+------+------+------+------+------+ | USA | 0 | 0 | 0 | 0 | 0 | 1 | | France | 0 | 0 | 2 | 0 | 0 | 0 | +---------+------+------+------+------+------+------+  
  22. Barand's post in Display options conditionally in <select> element was marked as the answer   
    This example will repopulate the category menu depending on the selected type
    <html> <head> <title>Sample</title> <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type='text/javascript'> // array of categories for each type var cat_types = JSON.parse('{"A":{"1":"Cat 1","2":"Cat 2","3":"Cat 3"},"B":{"4":"Cat 4","5":"Cat 5","6":"Cat 6"},"C":{"7":"Cat 7","8":"Cat 8","9":"Cat 9"}}'); $().ready(function() { // populate type menu $.each(cat_types, function(k,v) { $("#type").append($("<option>", {"val":k, "text":k})) }) // when type selected, populate cat menu $("#type").change( function() { $("#cat").html("<option value=''> - select category -</option"); var type = $(this).val() $.each(cat_types[type], function(k, v) { $("#cat").append($("<option>", {"val":k, "text":v})) }) }) }) </script> <style type='text/css'> </style> </head> <body> <form> Type <select name='type' id='type'> <option value=""> - select type -</option> </select> <br><br> Cat <select name='cat' id='cat'></select><br> </form> </body> </html>  
  23. Barand's post in Join error was marked as the answer   
    Well, the error disappeared when I did it that way.
    You can only do INNER JOINS with the FROM A, B, C method. You have to put the join conditions in the WHERE clause which mixes the table structure with selection criteria In my experience, explicit joins are faster.
  24. Barand's post in Create an array which is based on a given value was marked as the answer   
    Ah! Two arrays
    $amts_due = [ '15' => 1088.00, '13' => 2206.00, '11' => 1716.00, '9' => 1210.00, '7' => 140.00 ]; $amts_paid = []; $payment = 5000.00; ksort($amts_due); // oldest first foreach ($amts_due as $bid => &$due) { if ($payment >= $due) { $payment -= $due; $amts_paid[$bid] = $due; $due = 0; } else { $due -= $payment; $amts_paid[$bid] = $payment; $payment = 0; } } $amts_due = array_filter($amts_due); $amts_paid = array_filter($amts_paid); Giving
    Due Array ( [13] => 272 [15] => 1088 ) Paid Array ( [7] => 140 [9] => 1210 [11] => 1716 [13] => 1934 )  
  25. Barand's post in Introductions was marked as the answer   
    Good idea. Do you think we should have something like this ?
×
×
  • 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.