Jump to content

benanamen

Members
  • Posts

    2,134
  • Joined

  • Last visited

  • Days Won

    42

Posts posted by benanamen

  1. Your query is failing. You are using single quotes so the variables are not being interpolated. You should be using Prepared Statements anyways. NEVER EVER put variables in the query and don't create variables for nothing. There is no need to manually close the connection. Php will do it automatically.

    I would also recommend you use PDO. Here is a tutorial to get you going.

    https://phpdelusions.net/pdo

  2. OP was already advised about his code on another forum and did not implement any of the advice whatsoever. His response was "Could you please write it to me".

    OP, we are not a coding on demand service. You apparently didn't even attempt to copy and paste the complete working example from the tutorial you were referred to. If you just want someone to write it for you then post a job offer on any of the forums jobs section.

  3. 1 minute ago, SaranacLake said:

    I am just interested right now in refactoring this particular problem

    In that case, just remove the session start from your include files. You should have no need to check if a session has been started.

  4. 19 minutes ago, Jim R said:

    Can't I query it out and sort it on the backend rather than creating all that extra data input?  

    Sure you could but you are going to need code gymnastics to cover every possible case. Not really optimum IMO. The "extra data input" is a one time insert and you are done with it. At that point, it is as simple as selecting the column in your query.

  5. 14 minutes ago, Jim R said:

    Yes it does matters.  It matters to my Users, and it matters to those who want to see their school represented as the school is named.  It matters to me.

    That makes no sense at all. If you are  displaying the data from the school name column why would it not be what the school is named?

  6. A function also known as a method when in a Class should do one thing and do it well. It should also be easily re-usable and not have to be edited to use it elsewhere. What you have is none of that.

    What you will probably discover as the first problem is that you have hard coded the groups which means if you want a group other than uk you now have a problem on your hands. Duplicating the function for every group is not the answer.  What you want to do is pass the groups value to the function as a parameter. Now you can use the same function for any group. Same thing goes for the hard-coded table name. There is also no option to specify specific columns. Again you would run into problems with the hard coded column headers and every other hard coded item.

    Here is something you get you going towards a dynamic solution. (It's not a cut n paste solution)
     

    <?php declare(strict_types=1);
    
    $sql = 'YOUR-QUERY-HERE';
    $stmt = $db->pdoQuery($sql);
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$row)
    {
        echo '<H1>No Records Available</H1>';
    }
    else
    {
        $data = [];
        foreach ($row as $key => $val)
        {
            $data[ucwords(str_replace('_', ' ', $key))] = $val;
        }
        ?>
        <div class="table-responsive">
            <table id="myDataTable" class="table table-bordered table-striped table-hover table-sm">
                <thead class="thead-dark">
                <tr>
                    <th><?= join('</th><th>', array_keys($data)) . "</th>\n" ?>
                </tr>
                </thead>
                <tbody class="searchable">
                <?php
                do
                {
                    echo "<tr>\n<td>" . join("</td>\n<td>", $row) . "</td>\n";
                    echo " </tr> \n";
                } while ($row = $stmt->fetch(PDO::FETCH_ASSOC));
                ?>
                </tbody>
            </table>
    
        </div>
        <?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.