Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 2 of 2
  1. #1
    Junior Member
    Join Date
    Jul 2018
    Posts
    3
    Member #
    59668

    how to echo $row in the html section

    Hey guys!

    I am trying to get my $row to be output in the html section but it doesn't work. I can get it to work in my php section. Must I put the whole code in my html section or can I just do it as follows? Someone did it with object orienting but how can I get this to work with procedure?

    PHP Code:
    <?php

    include_once 'includes/dbh.php';
    //Set question number

    $number = (int) $_GET['n'];

    // Run the query to get the question

    $sql "SELECT * FROM questions WHERE question_number = ?;";

     
    $stmt mysqli_stmt_init($conn);
            if(!
    mysqli_stmt_prepare($stmt$sql)) {
               echo 
    "SQL error";
            } else {
              
    mysqli_stmt_bind_param($stmt"i"$number);
              
    mysqli_stmt_execute($stmt);

              
    $result mysqli_stmt_get_result($stmt);
              while(
    $row mysqli_fetch_assoc($result)) {

             
    // echo $row['text'];
            
    }
          }



    ?>
    Code:
    <!DOCTYPE html>
    <html>
    <head>
       <title>PHP QUIZZER</title>
       <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
       <header>
          <div class="quiz_container">
             <h1> PHP Quizzer</h1>
          </div>
       </header>
       <main>
          <div class="quiz_container">
             <div class="current">Question 1 of 5</div>
             <p class-"question">
                
                <?php echo $row['text'];
               
                ?>
             </p>
             <form action="process.php" method="POST">
                <ul class="choices">
                   <li><input name="choice" type="radio" value="1" />Bar Line</li>
                   <li><input name="choice" type="radio" value="1" />Double Bar Line</li>
                   <li><input name="choice" type="radio" value="1" />Triple Bar Line</li>
                   <li><input name="choice" type="radio" value="1" />Bar</li>
                </ul>
                <input type="submit" value="submit">
             </form>
          </div>
       </main>
    
       
    </body>
    </html>

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    Yes, you have to put the whole thing into your HTML and the page will need to have .php extension.

    PHP Code:
    <?php 
    include_once 'includes/dbh.php'
    //Get question number
    //You're doing this at the top because you need to know which question it is, example:  Question 1 of 5
    $number = (int) $_GET['n']; 
    ?>
    <!DOCTYPE html>
    <html>
    <head>
       <title>PHP QUIZZER</title>
       <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
       <header>
          <div class="quiz_container">
             <h1> PHP Quizzer</h1>
          </div>
       </header>
       <main>
          <div class="quiz_container">
             <div class="current">Question <?=$number?> of 5</div>
             <p class-"question">
                
     <?php 
    // Run the query to get the question 
    $sql "SELECT * FROM questions WHERE question_number = ?;"
     
    $stmt mysqli_stmt_init($conn); 
            if(!
    mysqli_stmt_prepare($stmt$sql)) { 
               echo 
    "SQL error"
            } else { 
              
    mysqli_stmt_bind_param($stmt"i"$number); 
              
    mysqli_stmt_execute($stmt); 
              
    $result mysqli_stmt_get_result($stmt); 
              while(
    $row mysqli_fetch_assoc($result)) { 
             echo 
    $row['text']; 
            } 
          } 
    ?> 
              </p>
             <form action="process.php" method="POST">
                <ul class="choices">
                   <li><input name="choice" type="radio" value="1" />Bar Line</li>
                   <li><input name="choice" type="radio" value="1" />Double Bar Line</li>
                   <li><input name="choice" type="radio" value="1" />Triple Bar Line</li>
                   <li><input name="choice" type="radio" value="1" />Bar</li>
                </ul>
                <input type="submit" value="submit">
             </form>
          </div>
       </main>
       
    </body>
    </html>



Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:14 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com