haruki Posted July 19, 2007 Share Posted July 19, 2007 Hi. This is my first question here. echo "</tr>\n"; // printing table rows while($row = mysql_fetch_row($result)) { ?> <tr> <?php // $row is array... foreach( .. ) puts every element // of $row to $cell variable foreach($row as $cell) echo "<td>$cell</td>"; echo "</tr>\n"; } mysql_free_result($result); ?> This will print the array like: ([ ] <- cell) [value1] [value2] [value3] [value4] [value5] ..etc I would like it to it to print and sort all value in 4 cell(collumn) then add a new row and do so on.. Like this [val1][val2][val3][val4] (add 4cells with the first 4 arrayvalues then a new row) [val5][val6][val7][val8] (add 4cells with the 4 next arrayvalues then a new) [val9] etc.. Can anyone assist? Quote Link to comment https://forums.phpfreaks.com/topic/60708-solved-foreach-array/ Share on other sites More sharing options...
sasa Posted July 19, 2007 Share Posted July 19, 2007 try <?php echo "</tr>\n"; // printing table rows foreach ($rows as $row) while($row = mysql_fetch_row($result)){ // $row is array... foreach( .. ) puts every element // of $row to $cell variable $count = 0; foreach($row as $cell){ if ($count % 4 == 0) echo '<tr>'; echo "<td>$cell</td>"; $count++; if ($count % 4 == 0) echo '</tr>'; } while ($count % 4 > 0){ $count++; echo '<td> </td>'; } echo "</tr>\n"; } mysql_free_result($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302024 Share on other sites More sharing options...
haruki Posted July 19, 2007 Author Share Posted July 19, 2007 I get the following errors. Im a n00b. Notice: Undefined variable: rows in C:\Program Files\Apache\htdocs\test.php on line 34 Warning: Invalid argument supplied for foreach() in C:\Program Files\Apache\htdocs\test.php on line 34 Any ideas! Quote Link to comment https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302038 Share on other sites More sharing options...
sasa Posted July 19, 2007 Share Posted July 19, 2007 ups i don't remove line for debug just remove line foreach ($rows as $row) Quote Link to comment https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302051 Share on other sites More sharing options...
haruki Posted July 19, 2007 Author Share Posted July 19, 2007 Ahh.. It works. But....... yes a but.. The thing is that i want input the array info on all cells.. if the array looks like example. $row= array(1, 2, 3, 4, 5, 6, 7, 8, etc..); i want the first four cells to have get the first 4 values from the array, then on the next row i want the next 4 cells to have the next 4 values in the array and so on. I cant manage to get this to work. To be honest i have little clue of how i shall make it work. Quote Link to comment https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302056 Share on other sites More sharing options...
DeadEvil Posted July 19, 2007 Share Posted July 19, 2007 Hi! Can I see the query? Quote Link to comment https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302079 Share on other sites More sharing options...
haruki Posted July 19, 2007 Author Share Posted July 19, 2007 Here is the whole thing.. <html><body> <?php $db_host = 'localhost'; $db_user = 'root'; $db_pwd = ''; $database = 'test'; $table = 'test'; if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); // sending query $result = mysql_query("SELECT * FROM {$table} limit 0, 2"); if (!$result) { die("Query to show fields from table failed"); } $fields_num = mysql_num_fields($result); echo "<h1>Table: {$table}</h1>"; echo "<table border='1'><tr>"; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); echo "<td>{$field->name}</td>"; } echo "</tr>\n"; // printing table rows while($row = mysql_fetch_row($result)){ // $row is array... foreach( .. ) puts every element // of $row to $cell variable $count = 0; foreach($row as $cell){ if ($count % 4 == 0) echo '<tr>'; echo "<td>$cell</td>"; $count++; if ($count % 4 == 0) echo '</tr>'; } while ($count % 4 > 0){ $count++; echo '<td>$cell</td>'; } echo "</tr>\n"; } mysql_free_result($result); ?> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302081 Share on other sites More sharing options...
DeadEvil Posted July 19, 2007 Share Posted July 19, 2007 Try this... <html><body> <?php $db_host = 'localhost'; $db_user = 'root'; $db_pwd = ''; $database = 'test'; $table = 'test'; if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); // sending query $result = mysql_query("SELECT * FROM {$table} limit 0, 2"); if (!$result) { die("Query to show fields from table failed"); } $fields_num = mysql_num_fields($result); echo "<h1>Table: {$table}</h1>"; echo "<table border='1'><tr>"; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); echo "<td>{$field->name}</td>"; } echo "</tr>\n"; // printing table rows while($row = mysql_fetch_array($result)){ // $row is array... foreach( .. ) puts every element // of $row to $cell variable $count = 0; foreach($row as $cell){ if ($count % 4 == 0) echo '<tr>'; echo "<td>$cell</td>"; $count++; if ($count % 4 == 0) echo '</tr>'; } while ($count % 4 > 0){ $count++; echo "<td>$cell</td>"; } echo "</tr>\n"; } ?> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302086 Share on other sites More sharing options...
haruki Posted July 19, 2007 Author Share Posted July 19, 2007 [ ] <- lets pretend that is a cell. this is how the output comes out when i try the source above. [1] [1] [1] [1] [2] [2] [2] [2] [3] etc.. what im aiming for is [1] [2] [3] [4] [5] [6] [7] [8] [9] etc.. Got any clues? Quote Link to comment https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302098 Share on other sites More sharing options...
DeadEvil Posted July 19, 2007 Share Posted July 19, 2007 I want to know. How many fields in your test table? Quote Link to comment https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302107 Share on other sites More sharing options...
haruki Posted July 19, 2007 Author Share Posted July 19, 2007 How many records there is in my table?? The database is called test and i got one table called test there. Inside the table i have fields (columns), the one i have now is called test. I have only one so far. But i will add more later on. I have 6 records on test, and more will be added later aswell. Quote Link to comment https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302117 Share on other sites More sharing options...
DeadEvil Posted July 19, 2007 Share Posted July 19, 2007 Ok, I see.... try this one <html><body> <?php $db_host = 'localhost'; $db_user = 'root'; $db_pwd = ''; $database = 'test'; $table = 'test'; if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); // sending query $result = mysql_query("SELECT * FROM {$table} limit 0, 2"); if (!$result) { die("Query to show fields from table failed"); } $fields_num = mysql_num_fields($result); echo "<h1>Table: {$table}</h1>"; echo "<table border='1'>"; echo "<tr>"; echo "<td>Test Column 1</td>"; echo "<td>Test Column 2</td>"; echo "<td>Test Column 3</td>"; echo "<td>Test Column 4</td>"; echo "</tr>"; while($row = mysql_fetch_array($result)) { if ($count % 4 == 0) echo '<tr>'; echo "<td>".$row['test']."</td>"; $count++; if ($count % 4 == 0) echo '</tr>'; } echo "</table>"; ?> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302127 Share on other sites More sharing options...
haruki Posted July 19, 2007 Author Share Posted July 19, 2007 hey it works!!!! thanx for the help!!!! Quote Link to comment https://forums.phpfreaks.com/topic/60708-solved-foreach-array/#findComment-302138 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.