0

I have this web based app with PHP (mysql) backend. I have a page to enter the grades and position received in a particular event. I need the page to show the checkboxes (for the grades) only if they have not been entered before. So I have this column P1 in my database and if this is NULL i need to show the checkboxes.

This is what i have on the php side

<?php
$PID = (int)$_GET['PID'];
$con = new mysqli("localhost","my_user","my_password",'events');
$result = $con->query("SELECT * FROM eventlist WHERE Eid=$PID");
global $output;
$row =  $result->fetch_assoc();
echo '<h1>'.$row["Ename"].'</h1>';
if($row["P1"]==NULL)
?>

and what i need to be displayed in P1 is NULL is as follows

<h2>Slot 1</h2>
<h3>Enter Result</h3>
<div id="c1" class="radio">
  <label><input type="radio" name="opt1radio" value=30>First</label>
  <label><input type="radio" name="opt1radio" value=20>Second</label>
  <label><input type="radio" name="opt1radio" value=10>Third</label>
</div>
<h3>Enter Grade</h3>
<div id="c2" class="radio">
  <label><input type="radio" name="opt2radio" value=5>A</label>
  <label><input type="radio" name="opt2radio" value=4>B</label>
  <label><input type="radio" name="opt2radio" value=3>C</label>
  <label><input type="radio" name="opt2radio" value=2>D</label>
  <label><input type="radio" name="opt2radio" value=0>DQ</label>
</div>
<div id="submit">
  <button onclick="myFunction()" type="button" class="btn btn-success">Submit</button>
</div>

How can i do this without putting echo in each and every HTML line.

Thanh Nguyen
  • 5,174
  • 11
  • 43
  • 74
Arshak Anjum
  • 142
  • 1
  • 10

2 Answers2

0

You can try this. If you don't want to use echo, just enclose the html in the if without including the html in the php area

<?php
 $PID = (int)$_GET['PID'];
    $con = new mysqli("localhost","my_user","my_password",'events');
    $result = $con->query("SELECT * FROM eventlist WHERE Eid=$PID");
    global $output;
    $row =  $result->fetch_assoc();
    echo '<h1>'.$row["Ename"].'</h1>';
    if($row["P1"]==NULL){
  ?>

<h2>Slot 1</h2>
    <h3>Enter Result</h3>
    <div id="c1" class="radio">
      <label><input type="radio" name="opt1radio" value=30>First</label>

      <label><input type="radio" name="opt1radio" value=20>Second</label>

      <label><input type="radio" name="opt1radio" value=10>Third</label>
    </div>

   <h3>Enter Grade</h3>  
   <div id="c2" class="radio">
      <label><input type="radio" name="opt2radio" value=5>A</label>

      <label><input type="radio" name="opt2radio" value=4>B</label>

      <label><input type="radio" name="opt2radio" value=3>C</label>

      <label><input type="radio" name="opt2radio" value=2>D</label>

       <label><input type="radio" name="opt2radio" value=0>DQ</label>
    </div>
    <div id="submit">

      <button onclick="myFunction()" type="button" class="btn btn-success">Submit</button>
    </div>
<?php }?>
Carl Binalla
  • 5,393
  • 5
  • 27
  • 46
0

I think that the only think that you are missing is to retrieve all rows one by one, you can do it with: while ($row = $result->fetch_assoc()) { and then put all your code like:

<?php
 $PID = (int)$_GET['PID'];
    $con = new mysqli("localhost","my_user","my_password",'events');
    $result = $con->query("SELECT * FROM eventlist WHERE Eid=$PID");
    global $output;
    while ($row = $result->fetch_assoc()) {
    echo '<h1>'.$row["Ename"].'</h1>';
    if($row["P1"]==NULL){
  ?>

<h2>Slot 1</h2>
    <h3>Enter Result</h3>
    <div id="c1" class="radio">
      <label><input type="radio" name="opt1radio" value=30>First</label>

      <label><input type="radio" name="opt1radio" value=20>Second</label>

      <label><input type="radio" name="opt1radio" value=10>Third</label>
    </div>

   <h3>Enter Grade</h3>  
   <div id="c2" class="radio">
      <label><input type="radio" name="opt2radio" value=5>A</label>

      <label><input type="radio" name="opt2radio" value=4>B</label>

      <label><input type="radio" name="opt2radio" value=3>C</label>

      <label><input type="radio" name="opt2radio" value=2>D</label>

       <label><input type="radio" name="opt2radio" value=0>DQ</label>
    </div>
    <div id="submit">

      <button onclick="myFunction()" type="button" class="btn btn-success">Submit</button>
    </div>
<?php } }?>

Whitout the while, your code only will show the first result.

Hope it helps!

JP. Aulet
  • 4,375
  • 4
  • 26
  • 39