Jump to content

Barand

Moderators
  • Posts

    24,558
  • Joined

  • Last visited

  • Days Won

    822

Community Answers

  1. Barand's post in Object of class mysqli_result could not be converted to string was marked as the answer   
    Your problem is with $iduser

    It is the result of the first query yet you are attemping to insert it as a value in the second query. You need to fetch the id from the resultset to use its value.
  2. Barand's post in Searching and Calculating Minimum Years of experience was marked as the answer   
    Use DATE type columns for your dates, not varchar. Have your leaving dates either a valid date or NULL.
    SELECT eemp_id , fname , lname , AVG(timestampdiff(MONTH, joining_date, coalesce(leaving_date, curdate()))) as av_mths FROM employee_details ed JOIN employee e ON e.empid = ed.eemp_id GROUP BY eemp_id HAVING av_mths >= 36;  
  3. Barand's post in Database Design for hotel management system was marked as the answer   
    And you don't see a problem with that?
    For example
    1 2 3 4 5 6 7 8 9 10 Underline any number above that is both less than 5 and greater than 5  
  4. Barand's post in PHP: Returning and naming specific fields from a CSV in an array was marked as the answer   
    No idea what your csv data looks like, so going to use this (mytest.csv)...
    ,100,"Cancelled",, ,200,"Active",, ,300,"Pending",, then
    $fp = fopen('files/mytest.csv', 'r'); while ($row = fgetcsv($fp)) { $data[] = [ 'cost' => $row[1], 'status' => $row[2] ]; } fclose($fp); echo '<pre>' . print_r($data, 1) . '</pre>'; giving
    Array ( [0] => Array ( [cost] => 100 [status] => Cancelled ) [1] => Array ( [cost] => 200 [status] => Active ) [2] => Array ( [cost] => 300 [status] => Pending ) )  
  5. Barand's post in Need help streamlining multi table select and output was marked as the answer   
    Like this, maybe
    $res = $con->query("SELECT system_id , status_text_color , status_text , icon_color , status_img , link FROM status ORDER BY system_id "); foreach ($res as $row) { echo <<<OUTPUT <div class="col-xl-3 col-sm-6 grid-margin stretch-card"> <div class="card"> <div class="card-body"> <div class="row"> <div class="col-9"> <div class="d-flex align-items-center align-self-start"> <h6 class="mb-0"><?= $row['system_id']; ?></h6> <h6 class="{$row['status_text_color']} ml-2 mb-0 font-weight-medium">{$row['status_text']}</h6> </div> </div> <div class="col-3"> <div class="{$row['icon_color']}"> <span class="{$row['status_img']}"></span> </div> </div> </div> <h6 class="text-primary font-weight-normal"><a href="{$row['link']}" target="_blank">Access System</a></h6> </div> </div> </div> OUTPUT; }  
  6. Barand's post in You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version was marked as the answer   
    No it doesn't. Referencing field names that aren't in the table produces that error.
  7. Barand's post in Problem using a php variable in an html if..else was marked as the answer   
    Look again at my code.
  8. Barand's post in Sort by Date was marked as the answer   
    Example
    $arr = [ [ 'A', 'Jan. 22, 22'], [ 'B', 'Dec. 25, 21'], [ 'C', 'Feb. 22, 22'], [ 'D', 'Jan. 2, 22'] ]; usort($arr, function($a, $b) { $da = DateTime::createFromFormat('M. j, y', $a[1]); $db = DateTime::createFromFormat('M. j, y', $b[1]); return $db <=> $da; }); echo '<pre>' . print_r($arr, 1) . '</pre>'; outputs
    Array ( [0] => Array ( [0] => C [1] => Feb. 22 22 ) [1] => Array ( [0] => A [1] => Jan. 22 22 ) [2] => Array ( [0] => D [1] => Jan. 2 22 ) [3] => Array ( [0] => B [1] => Dec. 25 21 ) )  
  9. 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?)
  10. 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
  11. 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

  12. 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?
  13. 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.
  14. 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.
  15. 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);  
  16. 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>  
  17. 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>"; }  
  18. 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'])){  
  19. 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.
  20. 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)
  21. 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; }  
  22. 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>
  23. 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>
  24. 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

  25. 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>  
×
×
  • 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.