wchamber22 Posted February 7, 2012 Share Posted February 7, 2012 Hello freaks, Got a task here which is displaying correctly (I believe). I only have 3 data entries in the db right now. Like I said (I think) the display of the code below yields the right layout but it repeats the first db entry over and over. I got the display to work correctly like this: 1 2 3 4 5 6 7 8 9 I am trying to get this display result (so it is more eligable for the end user!) 1 4 7 2 5 8 3 6 9 Thanks in advance! CODE <?php include_once "connect_to_mysql.php"; $cols = 3; $result = mysql_query("SELECT plantID, botanicalName FROM plants ORDER BY botanicalName"); $numrows = mysql_num_rows($result); $rows_per_col = ceil($numrows / $cols); $c = 1; $r = 1; while ($row = mysql_fetch_array($result)) { $plantID = $row["plantID"]; $botanicalName = $row["botanicalName"]; if ($r == $rows_per_col) { $c++; $r = 1; } else { $r++; } } $dyn_table = '<table width="750" cellpadding="0" cellspacing="0" border="0">'; for ($r = 1; $r <= $rows_per_col; $r++) { $dyn_table .= '<tr>'; for ($c = 1; $c <= $cols; $c++) { $dyn_table .= '<td><a href="plant_details.php?plantID = ' . $plantID . '" id="plantLink">' . $botanicalName . '</a></td>'; } $dyn_table .= '</tr>'; } $dyn_table .= '</table>'; ?> Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted February 7, 2012 Share Posted February 7, 2012 You can't really do that, HTML tables are drawn row by row. You could try to pivot the result set so that it goes in the order: 1,4,7,2,5,8,3,6,9. You could also print three tables side by side. Most sites just do what you're already doing: display is left-to-right and then top-to-bottom. It cuts down on scrolling as well. Quote Link to comment Share on other sites More sharing options...
wchamber22 Posted February 7, 2012 Author Share Posted February 7, 2012 Yo ManiacDan, I like the split into three tables option! How do I put 1/3 of the results in one dataset, a 1/3 of the results in another, and 1/3 in another? Could you direct me to an example of such. Thanks! Quote Link to comment Share on other sites More sharing options...
sasa Posted February 7, 2012 Share Posted February 7, 2012 try <?php include_once "connect_to_mysql.php"; $cols = 3; $result = mysql_query("SELECT plantID, botanicalName FROM plants ORDER BY botanicalName"); $numrows = mysql_num_rows($result); $rows_per_col = ceil($numrows / $cols); $c = 1; $r = 1; while ($row = mysql_fetch_array($result)) { $plantID[$r][$c] = $row["plantID"]; $botanicalName[$r][$c] = $row["botanicalName"]; if ($r == $rows_per_col) { $c++; $r = 1; } else { $r++; } } $dyn_table = '<table width="750" cellpadding="0" cellspacing="0" border="0">'; for ($r = 1; $r <= $rows_per_col; $r++) { $dyn_table .= '<tr>'; for ($c = 1; $c <= $cols; $c++) { $dyn_table .= '<td><a href="plant_details.php?plantID = ' . $plantID[$r][$c] . '" id="plantLink">' . $botanicalName[$r][$c] . '</a></td>'; } $dyn_table .= '</tr>'; } $dyn_table .= '</table>'; ?> Quote Link to comment Share on other sites More sharing options...
wchamber22 Posted February 7, 2012 Author Share Posted February 7, 2012 Sasa >= THE MAN Thanks! I think it is working, as I only have three rows in my db as of now. Looks like i was missing some array logic. Thanks again. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 7, 2012 Share Posted February 7, 2012 I think this is a little more strait-forward. include_once "connect_to_mysql.php"; $result = mysql_query("SELECT plantID, botanicalName FROM plants ORDER BY botanicalName"); $numrows = mysql_num_rows($result); $max_cols = 3; $max_rows = ceil($numrows / $max_cols); $count = 0; $tdData = array(); while ($row = mysql_fetch_array($result)) { $colIdx = floor($count/$max_rows); $rowIdx = $count - ($colIdx * $max_rows); $tdData[$rowIdx][$colIdx] = "<td><a href=\"plant_details.php?plantID={$row['plantID']}\" id=\"plantLink_{$row['plantID']}\">{$row['botanicalName']}</a></td>\n"; $count++; } $dyn_table = '<table width="750" cellpadding="0" cellspacing="0" border="0">'; foreach ($tdData as $rowAry) { $dyn_table .= "<tr>\n" . implode("", $rowAry) . "</tr>\n"; } $dyn_table .= '</table>'; Note: I changed the output to make the ID parameter of the anchor tags unique. You are not supposed to have multiple elements on a page with the same ID. Quote Link to comment Share on other sites More sharing options...
wchamber22 Posted February 8, 2012 Author Share Posted February 8, 2012 Thanks to you too Psycho! Everyone here has been so helpful, and it is much appreciated! Quote Link to comment 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.