 |
|
|
06-29-2011, 09:35 PM
|
Help with while loop...
|
Posts: 2,162
Name: ...
Location: ...
|
Hi All,
I need help with the correct while loop from the following script to display the categories from the db: (Note: the while loop is near the bottom of the php code.
HTML Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head> <title>Reunite My Site - Category Index</title>
<link rel="stylesheet" type="text/css" href="/css/default.css" media="screen">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<meta name="Description" content="The World's Most Trusted Source For Online Advertising!">
<style type="text/css">
.gradient {
border: 1px solid #000;
background: #c5deea; /* old browsers */
background: -moz-linear-gradient(top, #c5deea 0%, #8abbd7 31%, #066dab 100%); /* firefox */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#c5deea), color-stop(31%,#8abbd7), color-stop(100%,#066dab)); /* webkit */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#c5deea', endColorstr='#066dab',GradientType=0 ); /* ie */
}
</style>
</head>
<body>
<script type="text/javascript" src="http://www.reunitemysite.com/scripts/stats/track.php?mode=js"></script>
<noscript><img src="http://www.reunitemysite.com/scripts/stats/track.php?mode=img" border="0" alt="" width="1" height="1"></noscript>
<div class="gradient">
<br>
<a name="Top" target="_TOP"> </a>
<div class="pagination white">
<!-- The Following Php Code is the pagination script, which will create pages based on the number of banners in the database. Ex: 2 columns (across) x 5 Rows (down) the page -->
PHP Code:
<?php /* Include your code to connect to DB. */ include('cats_db.php');
/* Your DB table name */
$tbl_name="categories";
// How many adjacent pages should be shown on each side? $adjacents = 1;
/* First get total number of rows in data table. If you have a WHERE clause in your query, make sure you mirror it here. */
$query = "SELECT COUNT(*) as num FROM $tbl_name"; $total_pages = mysql_fetch_array(mysql_query($query)); $total_pages = $total_pages[num];
/* Setup vars for query. */ $targetpage = "categories.php"; //your file name (the name of this file)
$limit = 1; //how many items to show per page
$page = $_GET['page']; if($page) $st = ($page - 1) * $limit; //first item to display on this page
else $st = 0; //if no page var is given, set st to 0 /* Get data. */
$sql = "SELECT category_name FROM $tbl_name LIMIT $st, $limit"; $result = mysql_query($sql);
/* Setup page vars for display. */
if ($page == 0) $page = 1; // if no page var is given, default to 1. $prev = $page - 1; // previous page is page - 1 $next = $page + 1; // next page is page + 1 $lastpage = ceil($total_pages/$limit); // lastpage is = total pages / items per page, rounded up. $lpm1 = $lastpage - 1; // last page minus 1
/* Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once. */
$pagination = ""; if($lastpage > 1) { $pagination .= "";
//previous button
if ($page > 1) $pagination.= "<a href=\"$targetpage?page=$prev\" class=\"current\"> previous </a>"; else $pagination.= "<span class=\"disabled\"> previous </span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"number\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</a>"; } } elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some {
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2)) { for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) $pagination.= "<span class=\"number\">$counter </span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</a>"; } $pagination.= "<span class=\"dots\">...</span>"; $pagination.= "<a href=\"$targetpage?page=$lpm1\" class=\"number\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?page=$lastpage\" class=\"number current\">$lastpage</a>"; }
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination.= "<a href=\"$targetpage?page=1\" class=\"number\">1</a>"; $pagination.= "<a href=\"$targetpage?page=2\" class=\"number\">2</a>"; $pagination.= "<span class=\"dots\">...</span>"; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) $pagination.= "<span class=\"number\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</ a>"; } $pagination.= "<span class=\"dots\">...</span>"; $pagination.= "<a href=\"$targetpage?page=$lpm1\" class=\"number\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?page=$lastpage\" class=\"number current\">$lastpage</a>"; }
//close to end; only hide early pages
else { $pagination.= "<a href=\"$targetpage?page=1\" class=\"number\">1</a>"; $pagination.= "<a href=\"$targetpage?page=2\" class=\"number\">2</a>"; $pagination.= "<span class=\"dots\">...</span>"; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"number\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter\" class=\"number current\">$counter</a>"; } } } //next button
if ($page < $counter - 1) $pagination.= "<a href=\"$targetpage?page=$next\" class=\"number current\"> next </a>"; else $pagination.= "<span class=\"disabled\"> next </span>"; $pagination.= "</div>\n"; } // End of Pagination Script //
?>
<!-- Table Output Code -->
<?php
$result = mysql_query("SELECT * FROM categories") or die(mysql_error());
$numCols = 2; $numPerCol = ceil(mysql_num_rows($result) / $numCols);
echo "<table valign=\"top\"><tr>";
for($col = 1; $col <= $numCols; $col++) {
echo "<td valign=\"top\">";
for($row = 0; $row < $numPerCol; $row++) {
$resultRow = mysql_fetch_assoc($result);
if($resultRow == false) {
break; }
while($row = mysql_fetch_array($result)) { echo $row['category_name']; }
} echo "</td>"; } echo "</tr></table>";
?>
<?=$pagination?>
HTML Code:
</div>
<div align="center" valign="absbottom"><a href="http://www.reunitemysite.com/main.php#Top" target="_parent">Goto Top of Page</a>
</body>
</html>
</div>
<div align="center" valign="absbottom"><a href="http://www.reunitemysite.com/main.php#Top" target="_parent">Goto Top of Page</a>
</body>
</html>
Thank you!
Brian
__________________
Made2Own
Last edited by Brian07002; 06-29-2011 at 10:09 PM..
|
|
|
|
06-29-2011, 10:19 PM
|
Re: Help with while loop...
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
What's the problem?
Edit
Also, I know I mentioned this in another thread, but short tags are a bad idea.
PHP Code:
<?=$pagination?>
Even if you don't mean for this script to be portable, being in the habit of using short tags may cause problems in other scripts. It's better just to use the long form now as opposed to going through your code removing short tags when it comes time to upgrade your server.
Last edited by NullPointer; 06-30-2011 at 01:10 AM..
|
|
|
|
06-29-2011, 10:23 PM
|
Re: Help with while loop...
|
Posts: 2,162
Name: ...
Location: ...
|
Sorry, here's the result:
http://reunitemysite.com/categories.php
It's displaying all results, instead of the limit / per page. However, the result is giving the correct amount of pages.
Ex: 4 records in the db, 1 per page results in 4 pages, but all records are showing up on each page, not just one per page. I really hate errors like this, they are driving me mad
Thanks again!
Brian
__________________
Made2Own
Last edited by Brian07002; 06-29-2011 at 10:24 PM..
|
|
|
|
06-29-2011, 10:29 PM
|
Re: Help with while loop...
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
It looks like you're getting every row because you're selecting every row:
PHP Code:
$result = mysql_query("SELECT * FROM categories") or die(mysql_error());
|
|
|
|
06-29-2011, 10:41 PM
|
Re: Help with while loop...
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
If I think I what you are asking, then maybe this might help. If you want to display results in multiple table rows and columns, then I find it helpful to turn the db results into an array.
Code:
Array
(
[0] => Array
(
[0] => Array
(
[column1] => Value 1
[column2] => Value 2
[column3] => Value 3
)
[1] => Array
(
[column1] => Value 1
[column2] => Value 2
[column3] => Value 3
)
)
[1] => Array
(
[0] => Array
(
[column1] => Value 1
[column2] => Value 2
[column3] => Value 3
)
[1] => Array
(
[column1] => Value 1
[column2] => Value 2
[column3] => Value 3
)
)
[2] => Array
(
[0] => Array
(
[column1] => Value 1
[column2] => Value 2
[column3] => Value 3
)
)
)
Then you can display them into an html table
PHP Code:
echo '<table border="1">'; foreach ($array AS $row) { echo '<tr>'; for ($i = 0; $i < count($row); $i++) { echo '<td>' . implode('<br />', $row[$i]) . '</td>'; } if (--$i % 2 === 0) echo '<td>-</td>'; echo '</tr>'; } echo '</table>';
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
07-01-2011, 01:00 AM
|
Re: Help with while loop...
|
Posts: 2,162
Name: ...
Location: ...
|
Quote:
Originally Posted by NullPointer
It looks like you're getting every row because you're selecting every row:
PHP Code:
$result = mysql_query("SELECT * FROM categories") or die(mysql_error());
|
I had it working once before (pagination code) but using it for images instead of categories, and had no problems. The funny thing is now I am only getting 3 categories, but have the $result to select all (*). Of course only 4 categories in the table.
Can someone please help me out on this one?
__________________
Made2Own
|
|
|
|
07-01-2011, 06:05 PM
|
Re: Help with while loop...
|
Posts: 2,162
Name: ...
Location: ...
|
I am still having problems with the original code posted above. I also noticed that the output echo $row['category_name'] is supposed to have 4 rows (categories) but I am only getting 3 rows (it's skipping the first one) I don't know where that error is. :S All I am trying to do it display all records from the category_name row in the categories table which is inside the categories db. The pagination code is simply to seperate the pages based on the amount of categories per page. I have a working copy of the exact same pagination script that is working to display banners from my ad manager script, but I am using it now to display categories. Btw, I never thought of using the pagination script to begin with, would have made things alot easier, but no matter which way I choose, I am always having a problem 
__________________
Made2Own
Last edited by Brian07002; 07-01-2011 at 06:17 PM..
|
|
|
|
07-05-2011, 01:15 PM
|
Re: Help with while loop...
|
Posts: 3
|
Quote:
Originally Posted by Brian07002
I had it working once before (pagination code) but using it for images instead of categories, and had no problems. The funny thing is now I am only getting 3 categories, but have the $result to select all (*). Of course only 4 categories in the table.
Can someone please help me out on this one?
|
That's not completely clear. $result is a proper pagination query in the first instance. Why are you SELECTing everything again in the second $result?
|
|
|
|
07-07-2011, 06:30 AM
|
Re: Help with while loop...
|
Posts: 35
|
While Condition testing must be as == i mean while($row==)
this will work for While
|
|
|
|
07-07-2011, 06:41 AM
|
Re: Help with while loop...
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
Quote:
Originally Posted by shankar.adodis
While Condition testing must be as == i mean while($row==)
this will work for While
|
In PHP an assignment expression evaluates to the value being assigned. So if the value on the right side of the expression casts to false the expression will be considered false.
The following is valid and will evaluate to true until there are no more rows (in which case mysql_fetch_array returns false):
PHP Code:
while($row = mysql_fetch_array($result))
|
|
|
|
|
« Reply to Help with while loop...
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|