0

Following is my code in which i am trying to display the values from the same table but in different columns of my HTML table with different ids like lets suppose my table has three columns then in each column the distinct value get displayed

All i want that my respective id changes on each column of the data being displayed from same table

Something like : Values of ID1 | Values of ID2 | Values of ID3

But currently my do while is working something like this: Values of ID1 | Values of ID1 | Values of ID1

Kindly let me know how can i fix my following code:

          <table width="714" height="318" border="0" align="right" cellpadding="2" cellspacing="0">
          <?php $count=0; do { 
                  { ++$count;

          ?> <tr>
            <td width="231" height="288" valign="top" background="images/viv.png"><div align="center"><img src="get.php?id=<?php echo $row_Recordset1['ID']; ?>"  width="270" height="270" /><br />
                <br />
              <a href="get_deal.php?id=<?php echo $row_Recordset1['ID']; ?>"><img src="button.png" width="182" height="36" border="0" /></a><br />
              <br />
            </div></td>
            <td width="231" valign="top" background="images/viv.png"><div align="center"><img src="get.php?id=<?php echo $row_Recordset1['ID']; ?>"  width="270" height="270" /><br />
                    <br />
                    <a href="get_deal.php?id=<?php echo $row_Recordset1['ID']; ?>"><img src="button.png" width="182" height="36" border="0" /></a><br />
                    <br />
            </div></td>
            <td width="231" valign="top" background="images/viv.png"><div align="center"><img src="get.php?id=<?php echo $row_Recordset1['ID']; ?>"  width="270" height="270" /><br />
                    <br />
                    <a href="get_deal.php?id=<?php echo $row_Recordset1['ID']; ?>"><img src="button.png" width="182" height="36" border="0" /></a><br />
                    <br />
            </div></td>
          </tr>
              <tr>
                <td height="23" valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
                <td valign="top">&nbsp;</td>
              </tr>
              <?php }

              } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
if ($count == 0)
            echo '<span style="font-family:Tahoma;color:white;font-size:200%">No Deals Available</span>';   ?>
        </table></td>
      </tr>
    </table>

SOLUTION: http://www.qualitycodes.com/tutorial.php?articleid=33&title=Displaying-Records-in-Multiple-Columns-Using-PHP

2
  • If you are displaying using tables, why not use <td></td> instead of <span>? Commented Jun 14, 2012 at 20:32
  • Are you sure this works as you expect? It seems like for the first iteration of the loop, $row_Recordset1 will not be set and you'll end up with links like <a href="get_deal.php?id=">. This is because you're using a do{...} while() loop instead of a standard while(){...} loop. Commented Jun 14, 2012 at 20:33

1 Answer 1

3

You keep echoing $row_Recordset1['ID']; which means you keep showing the ID column. You must change ID to the name of the column you want to display.

In this case they keep doing ID1 so if all three values are ID then you need to do aliases (call them different names) in your select so you can use ID1, ID2 and ID3

For instance if your current sql query is:

select *
from tablex.....

Then you now have to extract the columns and name them:

select tablex.ID as ID1, secondTable.ID as ID2, thirdTable.ID as ID3
from tablex.....

and then you can refer to them as ID1, ID2 and ID3.

Sign up to request clarification or add additional context in comments.

11 Comments

hmmm yes you are right all three are ID so creating an alias will resolve the issue can you kindly give a hint of such query
That depends on your query. But the general idea is to use the AS keyword. For example: SELECT table1.ID AS ID1, table2.ID AS ID2...
the table is same.. So in that case if i do table1.ID AS ID1, table1.ID As ID2 i dont think it'll gonna display the distinct values
Why do you have the same column name 3 times in your table? You should always have different column names. Because of that the database is probably just grabbing the first ID and not seeing the others.
how can i create such script that on second column id changes?
|

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.