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?

6
  • Remove them from the loop? If you delete the lines of code, they won't be executed. Commented Aug 10, 2016 at 17:49
  • I really can't remove them from the loop because the <table> needed it, for proper displaying of data Commented Aug 10, 2016 at 17:50
  • 1
    Then what exactly are you trying to do here? You want them to be executed in the loop, but you don't want them to be executed in the loop? Commented Aug 10, 2016 at 17:51
  • I just like them to be as ease, like they won't be carried by the loop Commented Aug 10, 2016 at 17:52
  • What about showing an example of the output result you want to see? Commented Aug 10, 2016 at 17:53

4 Answers 4

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>";
}
?> 
Sign up to request clarification or add additional context in comments.

2 Comments

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
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.
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;
  }
  ...
}

Comments

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>";
        }
    }
?> 

Comments

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>";
    }
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.