2
<?php   
$q=select * from students where (dynamic for user searching)   
$qry=mysql_fetch_array($q);   
while($row=mysql_fetch_array())   
{   
   **// i do not need data here?**   
}   
?>   
< table>   
< tr>< td><?php echo **$row[?][?];** ?>< /td>< td><?php echo **$row[?][?];** ?>< /td>...< /tr>   
< tr>< td><?php echo **$row[?][?];** ?>< /td>< td><?php echo **$row[?][?];** ?>< /td>...< /tr>   
....    
< /table> 

In need to generate a dynamic report in html table format the html table rows and columns are static for results so i cant use echo with in while loop i have to access it out side while loop i have an idea of selecting single row single column for each cell of table separatly but it will be time consuming and length any alternative or solution?

3
  • 2
    Can you clarify this more? I don't understand why you can't use echo Commented Jun 5, 2012 at 0:15
  • 1
    Why can't you use echo inside the while loop ? Commented Jun 5, 2012 at 0:15
  • You need to work on your HTML Commented Jun 5, 2012 at 0:31

2 Answers 2

4

You don't have to use a while loop. You can fetch the data as you need it.

$row1 = mysql_fetch_array($qry);
$row2 = mysql_fetch_array($qry);

I don't like doing this though because you have to keep track of the resource ($qry in this case) and you have to keep typing mysql_fetch_*() so I tend to load results into an array before I use them.

$result = array();
while($row=mysql_fetch_object($qry))
{
    $result[] = $row;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Build the table inside your loop, and then echo it to the page. For example, if you were building a table where each row had a user's ID and name, it would look like this:

$table = "";
while($row=mysql_fetch_array($qry)) {   
   $tablerow = "<tr><td>" . $row['id'] . "</td><td>" . $row['name'] . "</td></tr>";
   $table .= $tablerow;
}   

// you've built a string with the entire table. Now write it to the page.
echo($table);

Building your table outside the while loop is usually a bad idea, because you're repeating a lot of code and you don't know how many rows you need to put in. But if you really want to do it the way you showed in your question (maybe because you only want certain specific rows from the query?), build an array in your while loop and then refer to that array outside of it.

$results = array();
while($row=mysql_fetch_array()) {   
    $results[] = $row;
}       

Then you can use <td><?php echo($results[0]['id']) ?></td> to write a cell with the first user's ID number, and so forth.

6 Comments

thanx all who replied, i have done it as above method that is assigning data to an array and then access it where i need, using array i have flexibility of moving to previous and next record too. Thank u guyz
@octern Actually, building your table inside the while loop is usually a bad idea. Following the MVC model all the data you need should be loaded first. Only then should you start building the table. MVC's often result in loading mysql results into an array to be used later.
BTW mysql_fetch_array() takes in a resource.
Thanks, I fixed my answer. Been a while since I used the much-hated mysql library.
@FakhrAlam If you like this answer, you can "accept" it by clicking the checkmark icon just under the vote buttons. Accepting answers to your questions also makes people more likely to answer your other questions in the future.
|

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.