1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

help with while loop

Discussion in 'HTML & Website Design' started by pshaw, Feb 2, 2021.

  1. #1
    Hi, in changeing from MYSQL to MYSQLi (that's where the blame goes) I can't get a clear understanding as to the proper code
    for the while loop. help please. code follows:
    ------------------------------------------------------
    <html><body>
    <hr>
    <center><b><font size=+2> 515 Certification Expiration Report</font><br>06/22/2020 - 06/22/2021<p>
    
    <hr>
    
    <?php
    //Open a new connection to the MySQL server
    require_once "getprerentdb.php";
    
    //MySqli Select Query
    $results = $mysqli->query (SELECT * FROM waitlist);
    
    ?>
      <table border=1>
      <th>unit#</th>
      <th>resident name</th>
      <th>movein date</th>
      <th>effect date</th>
      <th>expire date</th>
      <th>days left</th>
      <th colspan=3>recertification notification</th>
      </tr>
    
    <?php
    // ---------------------------------------
    while($row = mysqli_fetch_array($result))
      {
    // ----------------------------------------  
    echo "<tr>";
    echo "<td>" . $row['apt'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['moveindate'] . "</td>";
    echo "<td>" . $row['effdate'] . "</td>";
    echo "<td>" . $row['expdate'] . "</td>";
    echo "<td>" . $row['daysleft'] . "</td>";
    echo "<td>" . $row['90date'] . "</td>";
    echo "<td>" . $row['60date'] . "</td>";
    echo "<td>" . $row['30date'] . "</td>";
    echo "</tr>";
    
    echo "</table>";
      }
      ?>
    </body></html>
    PHP:

     
    Last edited by a moderator: Feb 2, 2021
    pshaw, Feb 2, 2021 IP
  2. seomanualsubmission

    seomanualsubmission Well-Known Member

    Messages:
    904
    Likes Received:
    128
    Best Answers:
    4
    Trophy Points:
    165
    #2
    Why are you using Table under while loop.

    <?php
    // ---------------------------------------
    while($row = mysqli_fetch_array($result))
    {
    // ----------------------------------------
    echo "<tr>";
    echo "<td>" . $row['apt'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['moveindate'] . "</td>";
    echo "<td>" . $row['effdate'] . "</td>";
    echo "<td>" . $row['expdate'] . "</td>";
    echo "<td>" . $row['daysleft'] . "</td>";
    echo "<td>" . $row['90date'] . "</td>";
    echo "<td>" . $row['60date'] . "</td>";
    echo "<td>" . $row['30date'] . "</td>";
    echo "</tr>";

    }
    echo "</table>";
    ?>
    Now format will be fine.
     
    seomanualsubmission, Feb 2, 2021 IP
  3. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    this t
    515 Certification Expiration Report
    06/22/2020 - 06/22/2021



    //Open a new connection to the MySQL server require_once "getprerentdb.php"; //MySqli Select Query $results = $mysqli->query (SELECT * FROM waitlist); ?>
    Notice: Undefined variable: result in C:\xampp\htdocs\property\certlog.php on line 25

    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\property\certlog.php on line 25
    unit# resident name movein date effect date expire date days left recertification notification

    he result:
     
    pshaw, Feb 2, 2021 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    this
    $results = $mysqli->query (SELECT * FROM waitlist);
    PHP:
    should be
    $results = $mysqli->query ('SELECT * FROM `waitlist`');
    PHP:
    and this
    while($row = mysqli_fetch_array($result))
    PHP:
    should be
    while($row = mysqli_fetch_array($results))
    PHP:
     
    sarahk, Feb 2, 2021 IP
    seomanualsubmission likes this.
  5. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    a
    515 Certification Expiration Report
    06/22/2020 - 06/22/2021



    //Open a new connection to the MySQL server require_once "getprerentdb.php"; //MySqli Select Query $results = $mysqli->query ('SELECT * FROM `waitlist`'); ?>
    Notice: Undefined variable: results in C:\xampp\htdocs\property\certlog.php on line 24

    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\property\certlog.php on line 24
    unit# resident name movein date effect date expire date days left recertification notification

    fter change:
     
    pshaw, Feb 2, 2021 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #6
    I wasn't coping looking at that code so I tidied it up a bit.
    Compare your original to mine and learn a different way of doing things

    We'll start by adding error display, remove it when it goes live but error messages help you understand your code
    Put the setup code at the top and only have presentation PHP code in the markup section. You'll read stuff about MVC and separation of code from presentation. This is the best we can do with a single page script.

    Use H1, H2 etc for your headings and learn how to use css to style them appropriately.

    Add thead and tbody to your tables, it helps the browser and anyone using a screen reader. May not apply in this case but it's "best practice".

    Don't jump in and out of echo statements constantly. Just write a single statement that is readable. The {} tell PHP where to find variables in the string.
    
    <?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    
    //Open a new connection to the MySQL server
    require_once "getprerentdb.php";
    
    //MySqli Select Query
    $results = $mysqli->query ('SELECT * FROM `waitlist`');
    
    
    ?><html>
    <head></head>
    <body>
      <hr>
      <h1>515 Certification Expiration Report: 06/22/2020 - 06/22/2021</h1>
      <hr>
    
      <table border='1' cellpadding="4">
        <thead>
          <tr>
            <th>unit#</th>
            <th>resident name</th>
            <th>movein date</th>
            <th>effect date</th>
            <th>expire date</th>
            <th>days left</th>
            <th colspan=3>recertification notification</th>
          </tr>
        </thead>
        <tbody>
          <?php
    // ---------------------------------------
          while($row = mysqli_fetch_array($results))  {
    // ----------------------------------------
            echo "<tr>
              <td>{$row['apt']}</td>
              <td>{$row['name']}</td>
              <td>{$row['moveindate']}</td>
              <td>{$row['effdate']}</td>
              <td>{$row['expdate']}</td>
              <td>{$row['daysleft']}</td>
              <td>{$row['90date']}</td>
              <td>{$row['60date']}</td>
              <td>{$row['30date']}</td>
            </tr>";
          }
          ?>
        </tbody>
      </table>
    </body>
    </html>
    
    PHP:
     
    Last edited: Feb 3, 2021
    sarahk, Feb 3, 2021 IP
  7. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    whatParse error: syntax error, unexpected 'waitlist' (T_STRING), expecting ')' in C:\xampp\htdocs\property\certlog.php on line 10's wrong?
     
    pshaw, Feb 3, 2021 IP
  8. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #8
    Aaah good catch. I didn't put the quotes around the SQL query. I'm surprised you didn't pick up on that. I've updated my code above.
     
    sarahk, Feb 3, 2021 IP
  9. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #9
    Thank you very much - finally
     
    pshaw, Feb 3, 2021 IP
  10. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #10
    How do I use the select if I add a where clause?
     
    pshaw, Feb 3, 2021 IP
  11. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #11
    You need to go and hunt down some mysqli tutorials and read how to do prepared statements.

    If the where clause has user-entered data then you have to ensure that the data is safe.
     
    sarahk, Feb 3, 2021 IP
  12. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #12
    $results = $mysqli->query (SELECT * FROM waitlist);
    Code (markup):
    That's not a string. Where are your quotes? You're using fetch_array instead of fetch_assoc so your results are numerically indexed not field indexed, you're mixing object and non-object willy-nilly...

    Overall your code is just missing a bunch of things... and the markup is like the worst of "4 transitional" -- aka in transition from 1997 to 1998 practices.

    Though @sarahk, that {} nonsense is NOT going to do this poor sod any favors.

    Fixing the gibberish to non-existent semantics and your incomplete code, it goes something more like this:

    <!DOCTYPE HTML><html lang="en"><body><meta charset="utf-8">
    <title>515 Certification Expiration Report</title>
    </head><body>
    
    <table>
    	<caption>
    		515 Certification Expiration Report<br>
    		06/22/2020 - 06/22/2021
    	</caption>
    	<thead>
    		<tr>
    			<th scope="col">unit#</th>
    			<th scope="col">resident name</th>
    			<th scope="col">movein date</th>
    			<th scope="col">effect date</th>
    			<th scope="col">expire date</th>
    			<th scope="col">days left</th>
    			<th scope="col" colspan=3>recertification notification</th>
    		</tr>
    	</thead><tbody>
    <?php
    
    //Open a new connection to the MySQL server
    require_once('getprerentdb.php');
    
    $results = $mysqli->query('
    	SELECT *
    	FROM waitlist
    ');
    
    while ($row = $result->fetch_assoc) echo '
    		<tr>
    			<th scope="row">', $row['apt'], '</th>
    			<td>', $row['name'], '</td>
    			<td>', $row['moveindate'], '</td>
    			<td>', $row['effdate'], '</td>
    			<td>', $row['expdate'], '</td>
    			<td>', $row['daysleft'], '</td>
    			<td>', $row['90date'], '</td>
    			<td>', $row['60date'], '</td>
    			<td>', $row['30date'], '</td>
    		</tr>';
    		
    ?>
    	</tbody>
    </table>
    
    </body></html>
    Code (markup):
    Guessing slightly on the body row headers...

    And remember, H1 doesn't mean "draw a line across the screen" it means a block level change in subject or topic (negating something like H1)... and tags like FONT and CENTER haven't been valid/proper HTML since 1997... just like the outmoded border and cellpadding attributes. You want to control that sort of stuff, do it from your stylesheet!
     
    Last edited: Feb 4, 2021
    deathshadow, Feb 3, 2021 IP
  13. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #13
    @deathshadow it's waitlist, not waitli

    Poor fella again will say it throws an error.
     
    qwikad.com, Feb 4, 2021 IP
  14. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #14
    oops, fixed. Not sure how I lost two characters there... but then I probably shouldn't be trying to touch-type with a band-aid across the end of my right hand middle finger. Sliced it open on the tear-strip for a aluminum foil package.
     
    deathshadow, Feb 4, 2021 IP
  15. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #15
    Dead shade long time since I've been lambasted so thoroughly. See you're still at it. I meant to tell you guys I straightened it out.
    It isn't as pretty as yours looks but slightly less coding and mine works - yours doesn't. Hang in there and don't hurt your friends
    too bad.
     
    pshaw, Feb 4, 2021 IP
  16. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #16
    That's epic.

    Do you mind posting the working solution? It's always nice to know what's worked.


     
    qwikad.com, Feb 4, 2021 IP
  17. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #17
    So pompous.
     
    sarahk, Feb 4, 2021 IP