0

I have a code in HTML in a table. And I want the loop to just ignore them

<?php
$sel_admin = "query  ";
$rs_admin = mysql_query($sel_admin);
while($row = mysql_fetch_array($rs_admin))
{      
    echo "<th>". $row['a'].  "</th>";


    </thead> // This two line of code
    <tbody>  // is the one I want to exclude in the while loop


    $sel_admin2 = "query2  ";
    $rs_admin2 = mysql_query($sel_admin2);
    while($row2 = mysql_fetch_array($rs_admin2))
    {
        echo" <tr class='gradeX'> ";
        echo "<td>" . $row2['sched3_time'].  "</td>";
        echo"</tr>";
    }
}
?>

Is this even possible?

SOFe
  • 7,867
  • 4
  • 33
  • 61
Noobster
  • 93
  • 11

4 Answers4

1

You need to end your first loop, spit out the html and then start the loop again, havent tested but i think the below should now work.

 <?php
$sel_admin = "query  ";
$rs_admin  = mysql_query($sel_admin);
while ($row = mysql_fetch_array($rs_admin)) {
    echo "<th>" . $row['a'] . "</th>";
}
?>
</thead> 
<tbody>
<?php
$sel_admin2 = "query2  ";
$rs_admin2  = mysql_query($sel_admin2);
while ($row2 = mysql_fetch_array($rs_admin2)) {
    echo " <tr class='gradeX'> ";
    echo "<td>" . $row2['sched3_time'] . "</td>";
    echo "</tr>";
}
?> 
Matt The Ninja
  • 2,641
  • 4
  • 28
  • 58
  • I'm sorry, I already tried that, but the first `while` loop is important because it will increment so that the second query will move to the next column – Noobster Aug 10 '16 at 17:58
  • Ok understood, difficult without knowing what your pulling from MySQL. I would amend your MySQL statement (if possible, probably is with JOINS) so that each row reflect a row in your table and you can loop just that. Nesting them just seems odd. – Matt The Ninja Aug 10 '16 at 18:00
0

Please use mysqli instead of mysql. Take a look: MySQL vs MySQLi when using PHP + Your problem's answer too.

<?php
     $sel_admin = "query  ";
     $rs_admin = mysqli_query($connection,$sel_admin);
     while($row = mysqli_fetch_array($rs_admin))
     {      
        echo "<th>". $row['a'].  "</th>";

        ?>
        </thead>
        <tbody>
        <?php

        $sel_admin2 = "query2  ";
        $rs_admin2 = mysqli_query($connection, $sel_admin2);
        while($row2 = mysqli_fetch_array($rs_admin2))
        {      
           echo" <tr class='gradeX'> ";
           echo "<td>" . $row2['sched3_time'].  "</td>";
           echo"</tr>";
        }
    }
?> 
Community
  • 1
  • 1
Jitesh Sojitra
  • 3,655
  • 7
  • 27
  • 46
0

I'm guessing you want those lines printed once, not to be outside the loop, per se. You could use a variable to track it:

$linesNeeded = true;
while (...) {
  ...
  if ($linesNeeded) {
    echo $line1;
    echo $line2;
    $linesNeeded = false;
  }
  ...
}
Justin McAleer
  • 236
  • 1
  • 9
0

This is honestly just a guess but based on the code you provided you actually need to add more code after remove the code you do not want:

<?php
$sel_admin = "query  ";
$rs_admin = mysql_query($sel_admin);
while($row = mysql_fetch_array($rs_admin))
{      
    echo "<tr><th>". $row['a'].  "</th></tr>"; // Notice the <tr></tr>

    $sel_admin2 = "query2  ";
    $rs_admin2 = mysql_query($sel_admin2);
    while($row2 = mysql_fetch_array($rs_admin2))
    {
        echo" <tr class='gradeX'> ";
        echo "<td>" . $row2['sched3_time'].  "</td>";
        echo"</tr>";
    }
}
?>
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77