Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help with while loop...
Old 06-29-2011, 09:35 PM Help with while loop...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
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">&nbsp;</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 + ($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 + ($adjacents 2))  //enough pages to hide some
        
{

        
//close to beginning; only hide later pages

        
if($page + ($adjacents 2))        
        {
        for (
$counter 1$counter + ($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 - (+ ($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

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 06-29-2011 at 10:09 PM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-29-2011, 10:19 PM Re: Help with while loop...
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 06-30-2011 at 01:10 AM..
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 06-29-2011, 10:23 PM Re: Help with while loop...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
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

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 06-29-2011 at 10:24 PM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 06-29-2011, 10:29 PM Re: Help with while loop...
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
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()); 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 06-29-2011, 10:41 PM Re: Help with while loop...
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
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 === 0) echo '<td>-</td>';
    
    echo 
'</tr>';
}
echo 
'</table>'
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-01-2011, 01:00 AM Re: Help with while loop...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Quote:
Originally Posted by NullPointer View Post
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

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 07-01-2011, 06:05 PM Re: Help with while loop...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
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

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 07-01-2011 at 06:17 PM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 07-05-2011, 01:15 PM Re: Help with while loop...
Junior Talker

Posts: 3
Trades: 0
Quote:
Originally Posted by Brian07002 View Post
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?
adityamenon90 is offline
Reply With Quote
View Public Profile
 
Old 07-07-2011, 06:30 AM Re: Help with while loop...
Experienced Talker

Posts: 35
Trades: 0
While Condition testing must be as == i mean while($row==)

this will work for While
__________________
Magento Themes Experts

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
shankar.adodis is offline
Reply With Quote
View Public Profile
 
Old 07-07-2011, 06:41 AM Re: Help with while loop...
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by shankar.adodis View Post
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)) 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to Help with while loop...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.31933 seconds with 12 queries