giraffemedia Posted June 18, 2008 Share Posted June 18, 2008 Hello i'm trying to split my query results into 3 or 4 columns within a table so that the results look ordered and not random. The data returned from the query is similar for each row in the mysql table and what I would like to do is basically count the number of returned records, divide it by 3 or 4 and then echo the results making sure each entry is in it's own <td> in a grid like format. I don't know the best place to start so I really would appreciate some help. Here is what I have so far. It works fine except that obviously all the results are printed inline and are not in a grid format... <? $issues_query="SELECT * FROM issues WHERE issue_on_sale BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 500 DAY)"; $issues_result=mysql_query($issues_query); if(!$issues_result) { print("Query Error: ".mysql_error()); } while($row=mysql_fetch_array($issues_result)) { $issue_number = $row['issue_number']; $issue_month = $row['issue_month']; $issue_year = $row['issue_year']; echo 'Issue <strong>' . $issue_number . '</strong> ' . $issue_month . ' ' . $issue_year . '<input name="issue_number" type="checkbox" value="' . $issue_number . '" />'; } ?> Regards James Quote Link to comment https://forums.phpfreaks.com/topic/110731-split-results-into-columns/ Share on other sites More sharing options...
rarebit Posted June 18, 2008 Share Posted June 18, 2008 The phpfreaks tutorials disappeared a while ago (they had a few examples), so here's one of mine. Quote Link to comment https://forums.phpfreaks.com/topic/110731-split-results-into-columns/#findComment-568116 Share on other sites More sharing options...
giraffemedia Posted June 18, 2008 Author Share Posted June 18, 2008 Rarebit, thanks for your help. I've read through your tutorial and have got my table working by combining the database rows into the array, but i'm a bit stumped how to create 3 or more columns. Which is the part that determines how many columns there would be? Here is what I have now... while($row=mysql_fetch_array($issues_result)) { $issues[]= 'Issue' . ' <strong>' .$row['issue_number'] . '</strong> ' . $row['issue_month'] . ' ' . $row['issue_year'] . '<input name="issue_number" type="checkbox" value="' . $issue_number . '" />'; } function table_02($data, $details="") { $sret = "<table ".$details.">\n"; $all = count($data)-1; for($i=0; $i <= $all; $i++) { if(($i % 2)==0) { $sret .= "<tr><td>".$data[$i]."</td>"; } else { $sret .= "<td>".$data[$i]."</td></tr>\n"; } } // Catch if an odd cell if(($all % 2) == 0) { $sret .= "<td><br></td></tr>\n"; } $sret .= "</table>\n"; return $sret; } $data = $issues; print table_02($data, "border='1' width='100%'"); Regards James Quote Link to comment https://forums.phpfreaks.com/topic/110731-split-results-into-columns/#findComment-568159 Share on other sites More sharing options...
rarebit Posted June 18, 2008 Share Posted June 18, 2008 A quick look and I think you just need to change the mod (%) to anther number, e.g. change '% 2' to '% 3' or '% 4', etc... Quote Link to comment https://forums.phpfreaks.com/topic/110731-split-results-into-columns/#findComment-568162 Share on other sites More sharing options...
giraffemedia Posted June 18, 2008 Author Share Posted June 18, 2008 A quick look and I think you just need to change the mod (%) to anther number, e.g. change '% 2' to '% 3' or '% 4', etc... I've tried that and it doesn't work. I have tried changing: if(($i % 2)==0) { $sret .= "<tr><td>".$data[$i]."</td>"; } to this if(($i % 2)==0) { $sret .= "<tr><td>".$data[$i]."</td><td>".$data[$i]."</td>"; } but all that happens is the first two columns are duplicates, not different. Can I use a variable as placeholder or container that gets filled by the query results until there are no more in the array? James Quote Link to comment https://forums.phpfreaks.com/topic/110731-split-results-into-columns/#findComment-568177 Share on other sites More sharing options...
rarebit Posted June 18, 2008 Share Posted June 18, 2008 no, like this (and I think theres two usages...) if(($i % 2)==0) { $sret .= "<tr><td>".$data[$i]."</td>"; } to if(($i % 3)==0) { $sret .= "<tr><td>".$data[$i]."</td>"; } Quote Link to comment https://forums.phpfreaks.com/topic/110731-split-results-into-columns/#findComment-568181 Share on other sites More sharing options...
giraffemedia Posted June 18, 2008 Author Share Posted June 18, 2008 I have tried that already Rarebit. Perhaps I didn't explain myself in the previous post. That was the first thing i tried. James Quote Link to comment https://forums.phpfreaks.com/topic/110731-split-results-into-columns/#findComment-568184 Share on other sites More sharing options...
rarebit Posted June 18, 2008 Share Posted June 18, 2008 Sorry, only half reading... Have a look version 4, it's multi columns... function table_04($data, $cols, $details="") { $sret = "<table ".$details.">\n"; $all = count($data); $offset = ceil($all/$cols); for($i=0; $i < $offset; $i++) { $sret .= "<tr>"; for($j=0; $j < $cols; $j++) { $sret .= "<td>".$data[($i+($j*$offset))]."</td>"; } $sret .= "</tr>\n"; } $sret .= "</table>\n"; return $sret; } Quote Link to comment https://forums.phpfreaks.com/topic/110731-split-results-into-columns/#findComment-568193 Share on other sites More sharing options...
giraffemedia Posted June 18, 2008 Author Share Posted June 18, 2008 I've had a look, but I can't see where I would put the array. I've tried using this but I get no results... while($row=mysql_fetch_array($issues_result)) { $issues[]= '<input name="issue_number" type="checkbox" value="' . $issue_number . '" />Issue <strong>' .$row['issue_number'] . '</strong> ' . $row['issue_month'] . ' ' . $row['issue_year'] ; } function table_04($data, $cols, $details="") { $sret = "<table ".$details.">\n"; $all = count($data); $offset = ceil($all/$cols); for($i=0; $i < $offset; $i++) { $sret .= "<tr>"; for($j=0; $j < $cols; $j++) { $sret .= "<td>".$data[($i+($j*$offset))]."</td>"; } $sret .= "</tr>\n"; } $sret .= "</table>\n"; return $sret; } $data = $issues; echo table_04($data, "border='0' width='100%'"); Sorry for being such a pain!! James Quote Link to comment https://forums.phpfreaks.com/topic/110731-split-results-into-columns/#findComment-568202 Share on other sites More sharing options...
rarebit Posted June 18, 2008 Share Posted June 18, 2008 This puts your data into 3 parts, before it was a single dimension array... while($row=mysql_fetch_array($issues_result)) { $issues[]= array('<input name="issue_number" type="checkbox" value="' . $issue_number . '" />Issue <strong>' .$row['issue_number'] . '</strong> ' , $row['issue_month'] , $row['issue_year']) ; } see if that helps, also your missing the number of columns... echo table_04($data, 3, "border='0' width='100%'"); Quote Link to comment https://forums.phpfreaks.com/topic/110731-split-results-into-columns/#findComment-568218 Share on other sites More sharing options...
giraffemedia Posted June 18, 2008 Author Share Posted June 18, 2008 By doing that it just outputs the results as the word 'Array' for each result and that's it. I think i'll just leave it as a 2 column layout for now. Thanks for your help rarebit I appreciate it. Regards James Quote Link to comment https://forums.phpfreaks.com/topic/110731-split-results-into-columns/#findComment-568244 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.