raj76 Posted February 3, 2010 Share Posted February 3, 2010 Hi, Going mad with mysql record display in html table....I want to display the mysql records as below User1 User2 User3 User4 aa bb cc dd - - - ss So this is my objective... and my code is.... <? $sql=mysql_query("select admin_id,admin_name from admin_table where admin_id in('22','23','24','25') ") or die(mysql_error()); $count=mysql_num_rows($sql); $i=1; while($row=mysql_fetch_array($sql)) { $admin_name=$row['1']; ?> <td><?=$admin_name;?></td> <? if(($i % $count) == 0) { echo '</tr> '; } $sql1=mysql_query("select * from report where admin_id='".$row[0]."' and which_day='2010-2-2' ") or die(mysql_error()); while($row1=mysql_fetch_array($sql1)) { $admin_id=$row1['admin_id']; $activity=$row1['activity']; $start_time=explode(" ",$row1['start_time']); $end_time=explode(" ",$row1['end_time']); $time=$start_time[1]."---".$end_time[1]; $total_value=$row1['total_value']; ?> <tr><td><?=$activity;?></td></tr> <? $i++; } } ?> </tr></table> it displays as User1 aa User2 bb User3 cc User4 dd ss plz suggest any method....going crazy Thanks, Raj Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/ Share on other sites More sharing options...
jl5501 Posted February 3, 2010 Share Posted February 3, 2010 I think you are missing a } in the correct place to close your first while loop, as, if I understand what you are trying to do, you need to close that while before the 3nd query. Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1006084 Share on other sites More sharing options...
raj76 Posted February 3, 2010 Author Share Posted February 3, 2010 sorry mate i didnt understand.. I have this table in mind <table width="200" border="1"> <tr> <td>anis</td> <td>mujeeb</td> <td>aziz</td> <td>raj</td> </tr> <tr> <td>ds</td> <td>ds</td> <td>dsd</td> <td>dsa</td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td>cc</td> </tr> </table> and i have only 2 queries running...one to take admin names from admin table and second to display other values from report table... Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1006137 Share on other sites More sharing options...
aeroswat Posted February 3, 2010 Share Posted February 3, 2010 Why do you have a mod operator when your count is just the total number of entries in your query result? This is kind of redundant. If you are going through your max then you don't need to mod. Just place the table row closing tag outside of your loop. Also I believe in order to accomplish a display like this the best way would be to store your record set in a new array then print from there. It gives you much more control to cycle through the records. Will give you example code on how to do that in one sec Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1006145 Share on other sites More sharing options...
aeroswat Posted February 3, 2010 Share Posted February 3, 2010 $rs = array(); while ($row = mysql_fetch_assoc($sql)) { $rs[] = $row; } Doing that should set your record set in a php array so that you can easily traverse it however you need it. I think you can figure out the logic of the loops. If not let me know and i'll help you with that. Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1006151 Share on other sites More sharing options...
raj76 Posted February 3, 2010 Author Share Posted February 3, 2010 hi aeroswat, Many thanks for your suggestion...yes I followed the way you have told but there is the same problem in loop..I need to know the logic of the looping. here is the code $sql=mysql_query("select * from report,admin_table where report.admin_id=admin_table.admin_id and which_day='2010-2-2' ") or die(mysql_error()); while($row=mysql_fetch_array($sql)) { $admin_name=$row['admin_name']; $activity=$row['activity']; $start_time=explode(" ",$row['start_time']); $end_time=explode(" ",$row['end_time']); $time=$start_time[1]."---".$end_time[1]; $total_value=$row['total_value']; $test_array[$admin_name][] = array('work'=> $activity,'time'=>$time,'total'=>$total_value); } so when i print echo '<pre>'; print_r($test_array); echo '</pre>'; it gives me the below array structure. Array ( [az] => Array ( [0] => Array ( [work] => dsdsds [time] => 2:47---2:55 [total] => 8 ) ) [an] => Array ( [0] => Array ( [work] => sdsdsdsdsd [time] => 1:47---2:47 [total] => 60 ) ) [mu] => Array ( [0] => Array ( [work] => sdsdsd [time] => 1:30---2:48 [total] => 78 ) ) [raj] => Array ( [0] => Array ( [work] => dsdwew [time] => 3:34---3:40 [total] => 6 ) [1] => Array ( [work] => cdsfdfdfd [time] => 3:25---3:35 [total] => 10 ) ) ) previously i did in this way to achieve the above html table..but thought in this way might be difficult to place each key and value of an element under tr and td...so i was looping mysql queries. I think it will be easier to display the first array keys as columns and rest of the elements as rows from test_array so it display as az an mu raj time aa sss fff ggg 4.45-6.54 - - - ooo 5.46-7.34 please suggest... Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1006166 Share on other sites More sharing options...
aeroswat Posted February 3, 2010 Share Posted February 3, 2010 $rs = array(); while ($row = mysql_fetch_assoc($sql)) { $rs[] = $row; } for($a=0;$a<(amount of columns goes here);$a++) { echo "<tr>"; for($i=0;$i<count($rs);$i++) { echo "<td>" . $rs[$a][$i] . "</td>"; } echo "</tr>"; } I think that'll do what you want it to. I'm pretty sure you can access the array by index still when it's set by column name. Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1006170 Share on other sites More sharing options...
aeroswat Posted February 3, 2010 Share Posted February 3, 2010 Please let us know if this worked for you for other users who stumble upon this thread. Also click the mark solved button if your question was answered. Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1006177 Share on other sites More sharing options...
raj76 Posted February 3, 2010 Author Share Posted February 3, 2010 I tried with that array looping but displayed a blank output...I am sure that the resultset is containing values from the db. here is the code $sql=mysql_query("select admin_table.admin_name,report.activity,report.start_time,report.end_time,report.total_value from report,admin_table where report.admin_id=admin_table.admin_id and which_day='2010-2-2'"); $tot=mysql_num_rows($sql); $rs = array(); while ($row = mysql_fetch_assoc($sql)) { $rs[] = $row; } echo '<table>'; for($a=0;$a<$tot;$a++) { echo "<tr>"; for($i=0;$i<count($rs);$i++) { echo "<td>" . $rs[$a][$i] . "</td>"; } echo "</tr>"; } echo '</table>'; Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1006193 Share on other sites More sharing options...
raj76 Posted February 3, 2010 Author Share Posted February 3, 2010 i changed from mysql_fetch_assoc to mysql_fetch_array....its displaying the records but still the usernames are not showing as header columns... Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1006219 Share on other sites More sharing options...
aeroswat Posted February 3, 2010 Share Posted February 3, 2010 i changed from mysql_fetch_assoc to mysql_fetch_array....its displaying the records but still the usernames are not showing as header columns... If you want them to be header columns then you will either have to throw in like a switch kinda like this $rs = array(); while ($row = mysql_fetch_assoc($sql)) { $rs[] = $row; } for($a=0;$a<(amount of columns goes here);$a++) { switch($a){ case:1 echo "<th>"; break; default: echo "<tr>"; break; } for($i=0;$i<count($rs);$i++) { echo "<td>" . $rs[$a][$i] . "</td>"; } switch($a){ case:1 echo "</th>"; break; default: echo "</tr>"; break; } } Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1006231 Share on other sites More sharing options...
raj76 Posted February 3, 2010 Author Share Posted February 3, 2010 let me try mate... Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1006256 Share on other sites More sharing options...
raj76 Posted February 3, 2010 Author Share Posted February 3, 2010 no hope... its a game of tr td /n or <br>...and loops... But not coming from head.. Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1006276 Share on other sites More sharing options...
aeroswat Posted February 3, 2010 Share Posted February 3, 2010 no hope... its a game of tr td /n or <br>...and loops... But not coming from head.. Can you do a view source on the html page that you are looking at when you visit that php page. That will help me see what the formatting is printing. Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1006279 Share on other sites More sharing options...
raj76 Posted February 3, 2010 Author Share Posted February 3, 2010 its printing as <table><tr><td>aziz</td><td>dsdsds</td><td>2010-02-02 2:47 PM</td><td>2010-02-02 2:55 PM</td><td>8</td></tr><td><td>anis</td><td>sdsdsdsdsd</td><td>2010-02-02 1:47 PM</td><td>2010-02-02 2:47 PM</td><td>60</td></td><tr><td>mujeeb</td><td>sdsdsd</td><td>2010-02-02 1:30 PM</td><td>2010-02-02 2:48 PM</td><td>78</td></tr><tr><td>raj</td><td>dsdwew</td><td>2010-02-02 3:34 PM</td><td>2010-02-02 3:40 PM</td><td>6</td></tr><tr><td>raj</td><td>cdsfdfdfd</td><td>2010-02-02 3:25 PM</td><td>2010-02-02 3:35 PM</td><td>10</td></tr></table> Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1006284 Share on other sites More sharing options...
aeroswat Posted February 3, 2010 Share Posted February 3, 2010 its printing as <table><tr><td>aziz</td><td>dsdsds</td><td>2010-02-02 2:47 PM</td><td>2010-02-02 2:55 PM</td><td>8</td></tr><td><td>anis</td><td>sdsdsdsdsd</td><td>2010-02-02 1:47 PM</td><td>2010-02-02 2:47 PM</td><td>60</td></td><tr><td>mujeeb</td><td>sdsdsd</td><td>2010-02-02 1:30 PM</td><td>2010-02-02 2:48 PM</td><td>78</td></tr><tr><td>raj</td><td>dsdwew</td><td>2010-02-02 3:34 PM</td><td>2010-02-02 3:40 PM</td><td>6</td></tr><tr><td>raj</td><td>cdsfdfdfd</td><td>2010-02-02 3:25 PM</td><td>2010-02-02 3:35 PM</td><td>10</td></tr></table> don't know why i wrote what i did... forgot html for a second rofl $rs = array(); while ($row = mysql_fetch_assoc($sql)) { $rs[] = $row; } for($a=0;$a<(amount of columns goes here);$a++) { echo "<tr>"; for($i=0;$i<count($rs);$i++) { switch($a){ case:1 echo "<th>"; break; default: echo "<tr>"; break; } echo $rs[$a][$i]; switch($a){ case:1 echo "</th>"; break; default: echo "</td>"; break; } } echo "</tr> } Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1006290 Share on other sites More sharing options...
raj76 Posted February 3, 2010 Author Share Posted February 3, 2010 its giving like below <table><tr><tr>aziz</tr><tr>dsdsds</tr><tr>2010-02-02 2:47 PM</tr><tr>2010-02-02 2:55 PM</tr><tr>8</tr></tr><tr><th>anis</th><th>sdsdsdsdsd</th><th>2010-02-02 1:47 PM</th><th>2010-02-02 2:47 PM</th><th>60</th></tr><tr><tr>mujeeb</tr><tr>sdsdsd</tr><tr>2010-02-02 1:30 PM</tr><tr>2010-02-02 2:48 PM</tr><tr>78</tr></tr><tr><tr>raj</tr><tr>dsdwew</tr><tr>2010-02-02 3:34 PM</tr><tr>2010-02-02 3:40 PM</tr><tr>6</tr></tr><tr><tr>raj</tr><tr>cdsfdfdfd</tr><tr>2010-02-02 3:25 PM</tr><tr>2010-02-02 3:35 PM</tr><tr>10</tr></tr></table> Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1006333 Share on other sites More sharing options...
raj76 Posted February 6, 2010 Author Share Posted February 6, 2010 Till now can't get sorted it out....plz let me know if there is some other method/process by which i can achieve the desired output Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1007823 Share on other sites More sharing options...
aeroswat Posted February 8, 2010 Share Posted February 8, 2010 Till now can't get sorted it out....please let me know if there is some other method/process by which i can achieve the desired output Sorry messed up again. Here is the correct code $rs = array(); while ($row = mysql_fetch_assoc($sql)) { $rs[] = $row; } for($a=0;$a<(amount of columns goes here);$a++) { for($i=0;$i<count($rs);$i++) { switch($i){ case:0 echo "<th>"; break; default: echo "<tr>"; break; } echo $rs[$a][$i]; switch($a){ case:1 echo "</th>"; break; default: echo "</td>"; break; } } echo "</tr> } Link to comment https://forums.phpfreaks.com/topic/190791-strugling-with-row-columnsplz-help/#findComment-1008854 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.