need help with table

Reply

Join Date: Sep 2009
Posts: 22
Reputation: MDanz is an unknown quantity at this point 
Solved Threads: 0
MDanz MDanz is offline Offline
Newbie Poster

need help with table

 
0
  #1
Nov 4th, 2009
i have a table with 14 columns and 4 rows. i know how to echo a result with a table from a query but how do i input results into a table thats already there? results go into cell 1, cell 2, cell 3 etc


been having trouble on this. table id= stack

its pretty long but i'll post the example so i can relate it to my code..

  1. mysql_connect("localhost", "Master", "password;
  2. mysql_select_db("Ustack");
  3.  
  4. $report = mysql_query("SELECT * FROM Reply ")or die (mysql_error());
  5.  
  6. while($row = mysql_fetch_array( $report)) {
  7.  
  8.  
  9. $id = $row['id'];
  10.  
  11. }
  12.  
  13. <table border="0">
  14. <tr>
  15. <td width="70" height="70">&nbsp;</td>
  16. <td width="70" height="70">&nbsp;</td>
  17. <td width="70" height="70">&nbsp;</td>
  18. <td width="70" height="70">&nbsp;</td>
  19. <td width="70" height="70">&nbsp;</td>
  20. <td width="70" height="70">&nbsp;</td>
  21. <td width="70" height="70">&nbsp;</td>
  22. <td width="70" height="70">&nbsp;</td>
  23. <td width="70" height="70">&nbsp;</td>
  24. <td width="70" height="70">&nbsp;</td>
  25. <td width="70" height="70">&nbsp;</td>
  26. <td width="70" height="70">&nbsp;</td>
  27. <td width="70" height="70">&nbsp;</td>
  28. <td width="70" height="70">&nbsp;</td>
  29. </tr>
  30.  
  31. </table>
  32.  

so how do i get $id to go to cell 1, cell 2, cell 3 etc and fill up the table. its easy when you echo it into a table but i need the table there before.
Last edited by MDanz; Nov 4th, 2009 at 1:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 144
Reputation: HITMANOF44th is an unknown quantity at this point 
Solved Threads: 19
HITMANOF44th HITMANOF44th is offline Offline
Junior Poster
 
0
  #2
Nov 4th, 2009
im not really following what you are trying to do if you wanna tell me with a little bit better of a explanation i can def help but i think this is what you are trying to do let me know

  1. echo "<table border='0'><tr>";
  2. for ( $counter = 0 ; $counter <= 1000; $counter += 1) {
  3. echo "<td width='70' height='70'>&nbsp;$id[$counter]</td>";
  4. }
  5. echo "</tr></table>";
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 885
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 127
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark
 
0
  #3
Nov 4th, 2009
MDanz,

You must build the table as you go, in two nested loops :-
  • Outer loop - table rows
  • Inner loop - table cells (forming colums)
On the assumption that your $report rows should each be represented as a table row, then the following php code should build the table you want :-
  1. <?php
  2. mysql_connect("localhost", "Master", "password;
  3. mysql_select_db("Ustack");
  4.  
  5. $report = mysql_query("SELECT * FROM Reply ")or die (mysql_error());
  6.  
  7. echo "<table border=\"0\">\n";
  8. while($row = mysql_fetch_array( $report)){
  9. echo "<tr>\n";
  10. foreach ($row as $value){
  11. echo "<td width=\"70\" height=\"70\">$value</td>\n";
  12. }
  13. echo "</tr>\n";
  14. }
  15. echo "</table>\n";
  16. ?>
Off-the-cuff; not tested

Airshow
Last edited by Airshow; Nov 4th, 2009 at 3:16 pm.
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 22
Reputation: MDanz is an unknown quantity at this point 
Solved Threads: 0
MDanz MDanz is offline Offline
Newbie Poster
 
0
  #4
Nov 4th, 2009
heres a better question..

how do i echo 1st result, 2nd result, 3rd result? i know how to echo all of them, how do i echo the specific... e.g. 2nd result in query. or the 3rd result in the query


  1. $addz = mysql_query("SELECT * FROM Stacks WHERE username = 'Admin'")or die (mysql_error());
  2.  
  3. while($rowrun = mysql_fetch_array( $addz)) {
  4.  
  5. $idz = $rowrun['id'];
  6.  
  7.  
  8. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 885
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 127
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark
 
0
  #5
Nov 4th, 2009
Originally Posted by MDanz View Post
how do i echo 1st result, 2nd result, 3rd result? i know how to echo all of them, how do i echo the specific... e.g. 2nd result in query. or the 3rd result in the query
This doesn't make sense.

Your query yields only one result ($result), which is (in all probability) "2-dimensional"; an array of arrays. The top level array should be indexed by integer ([1], [2], [3], [4]), while the level-2 arrays should be indexed by column name (as established in the database).

Therefore any particular item of data should be addressable as $result[n]['columnId']. This is effectively what happens (via different syntax) in my nested loops example above.

Airshow
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,080
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 137
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster
 
-1
  #6
Nov 4th, 2009
You should use the LIMIT keyword in the sql query.

SELECT ... FROM ... WHERE .... ORDER BY .... LIMIT(x,y)

The WHERE and ORDER BY clauses are optional
LIMIT has two parameters (both integers)
The first integer is the offset - i.e. start returning records from record number (x) of the query - the first record is 0 - not 1!
The second integer (y) will limit the resulting recordset to that number of records.

To return the only 2nd record, just do: LIMIT(1,1)
To return the 2nd and 3rd record, do: LIMIT(1,2)
To return the first 10 records, do: LIMIT(0,10)

I don't know if this is what you're looking for.
Happy Humbugging Christmas
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 49
Reputation: EvolutionFallen is an unknown quantity at this point 
Solved Threads: 8
EvolutionFallen EvolutionFallen is offline Offline
Light Poster
 
0
  #7
Nov 4th, 2009
Originally Posted by MDanz View Post
  1. while($row = mysql_fetch_array( $report)) {
  2.  
  3.  
  4. $id = $row['id'];
  5.  
  6. }
This won't work, by the way. You'll end up with only the $row['id'] of the last row of your results. You're not preserving the previous values. To do that, either $id would need to be an array, or you'd need to build the table within the while loop.

As for having the table already there and then later inserting the data, that sounds more like something you'd need JavaScript or AJAX to do -- though I'm not sure if that's even possible, as I'm no expert in either.
Last edited by EvolutionFallen; Nov 4th, 2009 at 5:17 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 885
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 127
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark
 
0
  #8
Nov 4th, 2009
Originally Posted by ardav View Post
the first record is 0 - not 1!
That's a good point Ardav, which alerts me to an error in my post above.

My " [1], [2], [3], [4] " should read " [0], [1], [2], [3] "

Airshow
Last edited by Airshow; Nov 4th, 2009 at 5:45 pm.
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 885
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 127
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark
 
0
  #9
Nov 4th, 2009
Originally Posted by EvolutionFallen View Post
As for having the table already there and then later inserting the data, that sounds more like something you'd need JavaScript or AJAX to do -- though I'm not sure if that's even possible, as I'm no expert in either.
That's exactly what I thought Evo but I sort of ignored it because the question is posed in the PHP forum.

It's certainly possible to replace the contents of individual table cells using AJAX/DHTML methods in Javascript. As far as I can tell, that does not apply here but I may be wrong.

Airshow
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 49
Reputation: EvolutionFallen is an unknown quantity at this point 
Solved Threads: 8
EvolutionFallen EvolutionFallen is offline Offline
Light Poster
 
0
  #10
Nov 4th, 2009
Originally Posted by Airshow View Post
That's exactly what I thought Evo but I sort of ignored it because the question is posed in the PHP forum.

It's certainly possible to replace the contents of individual table cells using AJAX/DHTML methods in Javascript. As far as I can tell, that does not apply here but I may be wrong.
I thought I'd mention it in case the OP didn't know it was an option, since PHP certainly cannot do what he/she is asking for =)
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 357 | Replies: 12
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC