Jump to content

Barand

Moderators
  • Posts

    24,389
  • Joined

  • Last visited

  • Days Won

    803

Posts posted by Barand

  1. Change

    function PDF($orientation='P', $unit='mm', $size='A4')

    to

    function __construct($orientation='P', $unit='mm', $size='A4')

    Giving the constructor function the same name as the class is very PHP version 4 and you can't get away with it any more.

    You probably have to make the same amendment in your FPDF class also if it's an old version.

  2. First off, I'd put just the connection code in the included file. You also need to add a few options when connecting with PDO. EG

        $db = new PDO("mysql:host=".HOST.";dbname=$dbname;charset=utf8",USERNAME,PASSWORD);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    Put the query and result processing in the main file.

    $userQuery = $db->prepare("
        SELECT idUsers, uidUsers, emailUsers, pwdUsers, premium
        FROM users 
        WHERE idUsers =:user_id
        ");
    $userQuery->execute(['user_id' => $_SESSION['user_id']]);
        
    while ($user = $userQuery->fetchObject()) {
        echo  $user->premium ? '<img src="../../img/premium.png" alt="Swiftify+">' : '<img src="../../img/swiftify.png" alt="Swiftify+">' ;
        echo '<br>';
    }
    

     

  3. Are those the two queries you mentioned, or two of your attempts at combining the queries.

    Your first references Visits.Va2  yet there is no mention of a Visits table.

    You also mention LEFT/Right JOIN and UNION but they seem to be absent too, as is any kind of JOIN.

    Why is the second query being run in a loop?

    In other words, can you provide a better description of what you are trying to, preferably with the table structures.

  4. Here's an example which uses the "sakila" film database, displaying film titles for the selected guidance rating

    <?php
    include 'db_inc.php';                //  DATABASE 
    $db = pdoConnect('sakila');          //  CONNECTION STUFF
    
    //
    //  PROCESS AJAX REQUESTS
    //
        if (isset($_GET['ajax'])) {
            $res = $db->prepare("SELECT title
                                 FROM film
                                 WHERE rating = ?
                                 ORDER BY title
                                ");
            $res->execute( [ $_GET['rating'] ] ) ;
            $films = $res->fetchAll();
            exit(json_encode($films)) ;
        }
    
    //
    //  GET LIST OF RATINGS FOR SELECTION
    //
        $res = $db->query("SELECT DISTINCT rating
                           FROM film
                           ORDER BY rating
                          ");
        $rating_list = '';
        foreach ($res as $r) {
            $rating_list .= '<li class="w3-light-gray w3-hover-blue">'.$r['rating'].'</li>';
        }
        
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="creation-date" content="05/10/2019">
    <title>Movies by Rating</title>
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript">
        $().ready( function() {
            
            $("li").click(function() {
                var rating = $(this).html()
                $.get(
                    "" ,
                    {"ajax":1, "rating":rating} ,
                    function (response) {
                        $("#search-results").html("")
                        $.each(response, function(k,v) {
                            $("#search-results").append(v.title+"<br>")
                        })
                    } ,
                    "JSON"
                )
            })
        })
    </script>
    </head>
    <body>
        <h1>Films by Rating</h1>
        <div class="w3-ul w3-card-4 w3-margin w3-hoverable" style="width:25%; float:left; position: fixed">
            <?=$rating_list?>
        </div>
        <div id="search-results" class="w3-content w3-margin w3-padding "style="float:right; width:70%;">
        
        </div>
    </body>
    </html>

    image.png.cd3c72ca6268e7bbffad877f45a1ea3e.png

  5. 3 hours ago, Barand said:

    if ( !isset($_SESSION['defaultseasonid_']) || !isset($_SESSION['defaultmatchtypeid_']) || !isset($_SESSION['defaultlanguage_']) )

    For the sake of completeness, an alternative way to write this is

    if (!isset($_SESSION['defaultseasonid_'], $_SESSION['defaultmatchtypeid_'], $_SESSION['defaultlanguage_']))
  6. 1 minute ago, AlMch said:

    now only the season drop-down is working. 

    That's the only one that you check with isset(). Should be

    if ( !isset($_SESSION['defaultseasonid_']) || !isset($_SESSION['defaultmatchtypeid_']) || !isset($_SESSION['defaultlanguage_']) )

    but the ?? method is a lot simpler.

    • Great Answer 1
  7. As of PHP 7.0 you could replace this code...

    if(!session_is_registered('defaultseasonid_') || !session_is_registered('defaultmatchtypeid_') || !session_is_registered('defaultlanguage_'))
        
    {
        $_SESSION['defaultseasonid_'] = $pdata['DefaultSeasonID'];
        $_SESSION['defaultmatchtypeid_'] = $pdata['DefaultMatchTypeID'];
        $_SESSION['defaultlanguage_'] = $pdata['DefaultLanguage'];
        $defaultseasonid = $_SESSION['defaultseasonid_'];
        $defaultmatchtypeid = $_SESSION['defaultmatchtypeid_'];
        $defaultlanguage = $_SESSION['defaultlanguage_'];
    }
    else
    {
        $defaultseasonid = $_SESSION['defaultseasonid_'];
        $defaultmatchtypeid = $_SESSION['defaultmatchtypeid_'];
        $defaultlanguage = $_SESSION['defaultlanguage_'];
    }

    with ...

        $defaultseasonid = $_SESSION['defaultseasonid_'] ?? $pdata['DefaultSeasonID'];
        $defaultmatchtypeid = $_SESSION['defaultmatchtypeid_'] ?? $pdata['DefaultMatchTypeID'];
        $defaultlanguage = $_SESSION['defaultlanguage_'] ?? $pdata['DefaultLanguage'];

    "??" is the "null coalesce" operator, so

    $a = $b ?? $c ?? $d;

    will set $a to the first of the three values that is not  null.

    • Great Answer 1
  8. if the purpose is to find the class given the sectional# then it would be easier if the array were inverted...

    <?php
    
    foreach (range(1,16) as $sect) $sectional_class[$sect] = 4;
    foreach (range(17,32) as $sect) $sectional_class[$sect] = 3;
    foreach (range(33,48) as $sect) $sectional_class[$sect] = 2;
    foreach (range(49,64) as $sect) $sectional_class[$sect] = 1;
    
    echo $sectional_class[25];                //--> 3
    ?>

    or you can get them direct form the database without using array to look them up

    SELECT t.team_name
         , s.sectional_id
         , c.class_id
    FROM team t
         JOIN
         sectional_team st USING (team_id)
         JOIN
         sectional s USING (sectional_id)
         JOIN
         class c USING (class_id)
    WHERE season = ?

     

    • Like 1
  9. 10 minutes ago, Jim R said:

    The code would be stagnant.  We know every year in basketball, teams in sectional 1-16 are 4A.  What we don't know is which class or sectional every team is in from year to year.  

    In which case you have in intermediate file to record, each season, which sectional a team is in

    image.thumb.png.4d670a82def84759b19629bf138ebffd.png

  10. 1 hour ago, Jim R said:

    So I have a Schools table that has a BSectionals column (B = basketball), and I'll eventually have a column for each sport.

    Adding columns for each sport is not the way to design database tables.

    From what you described above,

    • Each sport has several classes
    • Each class has several sectionals
    • Each sectional has several teams
    • Each school has several teams(sports)

    so your data should reflect this structure

    image.png.500089d1937a3c52866aad53cb93e002.png

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