Jump to content

Barand

Moderators
  • Posts

    24,511
  • Joined

  • Last visited

  • Days Won

    819

Posts posted by Barand

  1. <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>4 Queens</title>
    <meta charset="utf-8">
    <style type='text/css'>
        table {
            border-collapse: collapse;
        }
        td {
            width: 40px;
            height: 40px;
            text-align: center;
            font-size: 16pt;
        }
        tr:nth-child(odd) td:nth-child(even),
        tr:nth-child(even) td:nth-child(odd) {
            background-color: lightgreen;
        }
    </style>
    </head>
    <body>
        <?php
            echo '<table border="1">';
            for ($i = 1; $i <= 4; $i++) {
                echo "<tr>";
                for ($j = 1; $j <= 4; $j++) {
                    echo '<td>' . pow($i,2) + pow($j,2) . '</td>';
                } 
                echo '</tr>';
            }
            echo "</table>\n";
        ?>
    </body>
    </html>

    image.png.9604c2a268a8a5e392acd5bbebf2629b.png

    Dear Supreme Being,

    Can you put aside your arrogance for a moment and assume that we are 5 year olds (not a wall) and

    1. explain the theory behind your Pythagorean solution to the problem and why it "works"
    2. explain how the above square then tells us where the four queens should be positioned?
    • I've found the 5s but there are no "opposite 10s", only 20s opposite the 5s, so the description is somewhat vague..
    • Having found the 5s, 10s, 20s and 25s (why those values in particular?), I now have 8 squares. Given that these are the candidates, the code then needs to find which 4 of these 8 fit the bill.
  2. Why the Freckle didn't you post the code that actually used to get the results you are complaining about. Once I got the data loaded, your query wouldn't even run without corrections to column names.

    Anyway - the answer to your question...

    They are in the wrong order because you order by your generated qNo column. I'd give up on that method.

    image.png.6d718925c353ed818a18d9e07fe154bf.png

    If you are using MariaDB, you can ORDER BY NATURAL_SORT_KEY(Q_id)

    If MySQL (which doesn't have that function), use FetchAll() to get an array of your results then natsort($results) use a custom sort which does a strnatcmp() on the Q_id column
     

    $res = $pdo->query(" ... ");
    $result = $res->FetchAll();
    usort($results, fn($a,$b)=>strnatcmp($a['Q_id'], $b['Q_id']));

    (Using sort($results) would have sorted using the values of the first column in each row - I assumed natsort() would do the same (silly me) )

  3. 1 hour ago, shadd said:
    create table tblQuestions
    (
    	QueId		varchar(255),
    	instruction  	text  ,
    	question  	text  ,
    	Primary key(Que_Id)
    
    );

    FYI - it helps if the column defined as the primary key exists in the table!

    Same goes for the unique key in user table.

    Plus other syntax errors - I was trying to help but I gave up.

  4. Your main problem is that mysql_*** functions have been deprecated for years and now no longer exist (unless you are using a really ancient version of PHP, in which case you should upgrade)

    You need to use mysqli or PDO (PDO is definitely recommended).

  5. ... or ...

        $res = $pdo->query("SELECT Lvl
                                 , Sub
                            FROM shadd_1
                            ORDER BY Lvl DESC, Sub
                            ");
        $results = $res->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_COLUMN);
        
        foreach ($results as $lvl => $arr)  {
            echo "$lvl <UL>";
            foreach ($arr as $sub)  {
                echo "<li>$sub</li>";
            }
            echo "</ul>";
        }

    FYI - the $results array (slightly simpler than the $data array in above post) looks like this..

    Array
    (
        [ULE] => Array
            (
                [0] => BASIC lug
                [1] => BASIC lug
            )
    
        [ACE] => Array
            (
                [0] => ger
            )
    
    )

     

    • Like 1
  6. 1 hour ago, shadd said:

    how can i make sure it does not go back to start time duration on page refresh?

    Store the completion time instead of the duration. Count down until the end time is reached.

     

    1 hour ago, shadd said:

    how can i accustom it to recieve the count down duration using php variable?

    My usual method is to store a variable in a hidden input field in PHP  then access that field's value from javascript.

  7. @Psycho the PDO result/statement object is traversable whether you use query() or execute().

    The mysqli result object is traversable (when the result of a query) but the statement object as a result of execute() is not (as well as having a different set of methods to process).

    This adds to my theory that mysqli result class and statement class were developed by two teams who never spoke to one another.

    EG

    $res = $pdo->query("SELECT name FROM reindeer");
    foreach ($res as $r) {
        echo $r['name'] . "<br>";
    }

    results...

    Comet
    Cupid
    Dasher
    Vixen
    Prancer
    Dancer
    Donner
    Blitzen

     

    • Like 1
    • Haha 1
  8. Don't use "select *". Specify the columns you need.

    Then use a foreach loop, not a for loop.

    Don't embed variables in the sql string, use prepared queries with placeholders, then execute with the variables passed as parameters.

    $sql_list = "SELECT ID FROM dados_socios WHERE usuario = ? ORDER BY ID";
    $result_list = $conn->prepare($sql_list);
    $result_list->execute([$usario]);
    
    echo "<select>";
      foreach {$result_list as $row)
        echo "<option>ID: {$row['ID']}</option>";
      }
     echo "</select>";

     

    • Like 1
  9. PHP runs on the server. - Javascript runs on the client.

    On completion of the PHP it sends the page to the client where the javascript runs.

    At the time you "echo $array;" the js variable "i" does not yet exist. (Error thrown if turn on error reporting)

    By the time the javascript runs "$array" no longer exists.

    • Like 1
  10. 3 hours ago, jodunno said:

    However, i also recommend creating only two possible doors: bottom and right.

    I agree those are sufficient to define and draw the cell connections but, to be able to trace the paths through the network, it's a lot easier to know that A connects to B and also that B connects to A - that requires the top and left ones too.

    The database gave me an easy way to check for uniqueness and that I wasn't just recreating the same grid over and over.

    • Like 1
  11. Consider a student who has a maths exam at the end of each term. They score 60% in each of the first 2 terms but they are ill for the third so no score can be entered.

    If the "score" column in the result table is defined

    score int NOT NULL DEFAULT '0'

    then not inputting the score inserts a value of 0%. When the average score is calculated at the end of the year it is 40% ((60 + 60 + 0) / 3).

    On the other hand, if it is defined as

    score int

    thus allowing a NULL value if omitted, the the average is a much fairer 60% ((60 + 60) / 2).

    Consider NULL to be the absence of known value.

    Use NOT NULL when it is mandatory that a value is provided.

  12. Because your table has 8 columns but you only want to insert into 3 of them, it will attempt to insert NULL values (or the columns' specified default values) into the remaining 5.

    Therefore, if a column is specified as NOT NULL, it should have a default value if you don't always populate it when inserting a new record.

    In addition, the id column should be an auto_incremented INT value by default and also excluded from the insert.

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