0

I have done with fetching and display the data. But the problem is ,its display the data of fetched row in the same row of html table. For example : i want to display like this

                  ID | Name | Desination
                   ......................
                   1 | ABC | Developer
                   2 | PQR | Tester
                   3 | XYZ | Developer

But its showing as -

                   ID | Name | Desination
                   ......................
                    1 | ABC | Developer   2 | PQR | Tester   3 | XYZ | Developer

I have done something like this-

$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_desc FROM candidate ".$join.' where '.$condition;
$result = mysql_query($sql) or die(mysql_error()); 

Correct me with the display format of table.

 <div class="box-body table-responsive no-padding">
     <table class="table table-hover">
          <tr>
              <th>ID</th>
              <th>Name</th>
              <th>Designation</th>
          </tr>
          <tr>
              <?php

                   If(mysql_num_rows($result)>0)
                   {
                     while($row=mysql_fetch_array($result))
                     {  

                ?>
                  <td><?php echo $row['cand_number']; ?></td> 
                  <td><?php echo $row['cand_fname']; ?></td> 
                  <td><?php echo $row['cand_desc']; ?></td> 
                <?php

                }
                }
                 ?>

              </tr>
       </table>
    </div>
vinod
  • 58
  • 1
  • 2
  • 12

4 Answers4

5

You need to repeat row not column

 <?php
If (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_array($result)) {
        ?>
        <tr>
            <td><?php echo $row['cand_number']; ?></td> 
            <td><?php echo $row['cand_fname']; ?></td> 
            <td><?php echo $row['cand_desc']; ?></td> 
        </tr>
        <?php
    }
}
?>
Saty
  • 22,443
  • 7
  • 33
  • 51
Pranay Aher
  • 444
  • 4
  • 11
  • @AbhiAhere..Thank you...it worked. How would i make the "name" as hyperlink and after on click of the link it should display the whole details of that candidate. – vinod May 02 '15 at 09:10
  • you are welcome . If you find the the answer helpful then check it as correct – abhi – Pranay Aher May 02 '15 at 09:13
  • As i have mentioned above You need to repeat row not column, all the explanation is in the line itself – Pranay Aher May 02 '15 at 09:19
  • @NarendraSisodia..ok the link too worked..but give some idea how would i display that particular candidate details when the link is clicked. i.e. it should execute another sql query on click. to display his whole details – vinod May 02 '15 at 09:25
  • ...how would i display that particular candidate details when the link is clicked. i.e. it should execute another sql query on click. to display his whole details – vinod May 02 '15 at 10:05
  • take row id to next page using ancor tag and fetch row of particular id – Pranay Aher May 02 '15 at 10:15
  • @abhiahere..http://stackoverflow.com/questions/30001748/how-could-i-execute-another-sql-query-on-hyperlink-click-to-display-his-whole-de ...please suggest some ideas on this. – vinod May 02 '15 at 11:30
0

You need to add into the while loop. Because of this your all data is printed inside first row.

<?php
If (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_array($result)) {
        ?>
        <tr>
            <td><?php echo $row['cand_number']; ?></td> 
            <td><?php echo $row['cand_fname']; ?></td> 
            <td><?php echo $row['cand_desc']; ?></td> 
        </tr>
        <?php
    }
}
?>
Saty
  • 22,443
  • 7
  • 33
  • 51
Codelord
  • 1,120
  • 2
  • 10
  • 21
  • @CodeLord..Thank You..it worked for me. :) How would i make the "name" as hyperlink and after on click of the link it should display the whole details of that candidate. – vinod May 02 '15 at 09:10
  • @vinod just add anchore tag for name like – Codelord May 02 '15 at 09:22
  • how would i display that particular candidate details when the link is clicked. i.e. it should execute another sql query on click. to display his whole details – vinod May 02 '15 at 09:27
  • You need to create another page for this send your candidate id in url parameter. This is broad question . You need to post this question so I can give brief answer. – Codelord May 02 '15 at 09:31
  • @CodeLord..wait I brief you with my code and send you the link. soon. Thank you – vinod May 02 '15 at 09:53
  • @CodeLord..the link here..i have posted the question..http://stackoverflow.com/questions/30001748/how-could-i-execute-another-sql-query-on-hyperlink-click-to-display-his-whole-de – vinod May 02 '15 at 11:29
0

Use this HTML and try

<div class="box-body table-responsive no-padding">
<table class="table table-hover">
<tr>
   <th>ID</th>
   <th>Name</th>
   <th>Designation</th>
</tr>

          <?php

               If(mysql_num_rows($result)>0)
               {
                 while($row=mysql_fetch_array($result))
                 {  

            ?>
             <tr>
              <td><?php echo $row['cand_number']; ?></td> 
              <td><?php echo $row['cand_fname']; ?></td> 
              <td><?php echo $row['cand_desc']; ?></td> 
            </tr>
            <?php

            }
            }
             ?>


   </table>
</div>
Sagar
  • 642
  • 3
  • 14
0

In addition to the other answers: don't forget to sanitise $join and $condition. This to prevent SQL injection.

Community
  • 1
  • 1