mike12255 Posted March 26, 2009 Share Posted March 26, 2009 I grabbing info from my db and throwing the info in a table, my problem is after 4 i need it to go down a row, but im not sure how to do this i've tried several things but cant get it, any ideas?? <table width="493" height="187" border="0" align="center"> <?php $sql = "SELECT * from tbl_product WHERE cat_name = '$area' ORDER BY pd_id DESC LIMIT $page, 14"; $i = 0; $row = 0; $result = mysql_query($sql) or die (mysql_error()); echo "<tr>"; while ($row = mysql_fetch_array($result)){ extract($row); /*if ($i ==4){ echo("<br>"); $row++; $i++; }*/ echo "<td align = \"center\">"; echo "<a href = \"item.php?id=$pd_id\"><img src=\"$th_path\" border=\"0\"><br>$pd_name</a>"; echo "</td>"; $i++; } echo "</tr>";?> </table></td> </tr> Quote Link to comment Share on other sites More sharing options...
gnawz Posted March 26, 2009 Share Posted March 26, 2009 I have done pagination code for you..You could use it or get an idea from there!! Put this in your functions file... //Pagination functions function getPagingQuery($sql, $itemPerPage = 10) { if (isset($_GET['page']) && (int)$_GET['page'] > 0) { $page = (int)$_GET['page']; } else { $page = 1; } // start fetching from this row number $offset = ($page - 1) * $itemPerPage; return $sql . " LIMIT $offset, $itemPerPage"; } /* Get the links to navigate between one result page to another. */ function getPagingLink($sql, $itemPerPage = 10, $strGet = '') { $result = dbQuery($sql); $pagingLink = ''; $totalResults = dbNumRows($result); $totalPages = ceil($totalResults / $itemPerPage); // how many link pages to show $numLinks = 10; // create the paging links only if theres more than one page of results if ($totalPages > 1) { $self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ; if (isset($_GET['page']) && (int)$_GET['page'] > 0) { $pageNumber = (int)$_GET['page']; } else { $pageNumber = 1; } // print 'previous' link only if its not // on page one if ($pageNumber > 1) { $page = $pageNumber - 1; if ($page > 1) { $prev = " <a href=\"$self?page=$page&$strGet\">[Prev]</a> "; } else { $prev = " <a href=\"$self?$strGet\">[Prev]</a> "; } $first = " <a href=\"$self?$strGet\">[First]</a> "; } else { $prev = ''; // on page one, don't show 'previous' link $first = ''; // nor 'first page' link } // print 'next' link only if its not // on the last page if ($pageNumber < $totalPages) { $page = $pageNumber + 1; $next = " <a href=\"$self?page=$page&$strGet\">[Next]</a> "; $last = " <a href=\"$self?page=$totalPages&$strGet\">[Last]</a> "; } else { $next = ''; // if on the last page, don't show 'next' link $last = ''; // nor 'last page' link } $start = $pageNumber - ($pageNumber % $numLinks) + 1; $end = $start + $numLinks - 1; $end = min($totalPages, $end); $pagingLink = array(); for($page = $start; $page <= $end; $page++) { if ($page == $pageNumber) { $pagingLink[] = " $page "; // no need to create a link to current page } else { if ($page == 1) { $pagingLink[] = " <a href=\"$self?$strGet\">$page</a> "; } else { $pagingLink[] = " <a href=\"$self?page=$page&$strGet\">$page</a> "; } } } $pagingLink = implode(' | ', $pagingLink); // return the page navigation link $pagingLink = $first . $prev . $pagingLink . $next . $last; } return $pagingLink; } //You SQL should be sth like this.. $sql = "SELECT * FROM dbtable WHERE blah blah blah' "; //Define number of items per page. Four in your case $rowsPerPage = 4; $result = dbQuery(getPagingQuery($sql, $rowsPerPage)); $pagingLink = getPagingLink($sql, $rowsPerPage); remember to display the pagination link. Its set to showing ten link items but you can change it to fewer ones in the pagination code <? echo $pagingLink; ?> Quote Link to comment Share on other sites More sharing options...
gnawz Posted March 26, 2009 Share Posted March 26, 2009 Sorry I gave you code for items per page. For your case, make a table in a loop and break it every four records echo " <p><center><h4>Your result(s):</center></h4>"; echo "<align='center'><table border=0 bgcolor='' >"; echo "<tr bgcolor='#FFFFFF' >"; for($i=0;$i<mysql_num_fields($result);$i++) { echo "<th>"; echo mysql_field_name($result, $i); echo "</th>"; } while($row=mysql_fetch_row($result)) { echo "<tr bgcolor='#FFFFFF'>"; for($j=0;$j<$i;$j++) { echo "<td>"; echo $row[$j]; echo "</td>"; } echo "</tr>"; } echo "</tr>"; echo "</table>"; put that table in a loop considering 4 records... Quote Link to comment Share on other sites More sharing options...
mike12255 Posted March 26, 2009 Author Share Posted March 26, 2009 im getting my db table (http://schoolworkanswers.com/kaon/products.php?a=Sofas) idk what could is producing that though because i edited it:(also not sure if putting this in a loop would do what i want, their are more then 4 fields in the db $sql = "SELECT * from tbl_product WHERE cat_name = '$area' ORDER BY pd_id DESC LIMIT $page, 14"; $row = 0; $result = mysql_query($sql) or die (mysql_error()); while ($row = mysql_fetch_array($result)){ extract($row); echo " <p><center><h4>Your result(s):</center></h4>"; echo "<align='center'><table border=0 bgcolor='' >"; echo "<tr bgcolor='#FFFFFF' >"; for($i=0;$i<mysql_num_fields($result);$i++) { } while($row=mysql_fetch_row($result)) { extract($row); echo "<tr bgcolor='#FFFFFF'>"; for($j=0;$j<$i;$j++) { echo "<td>"; echo $th_path; echo "</td>"; } echo "</tr>"; } echo "</tr>"; echo "</table>"; } ?> Quote Link to comment Share on other sites More sharing options...
gnawz Posted March 28, 2009 Share Posted March 28, 2009 Do that and tell me. 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.