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 the table output...
Old 07-02-2011, 12:58 PM Help with the table output...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
In the code below, particularly at the bottom, where the table output code is, I think the curly brackets are the problem { these } I was changing them last night, and I had the code at a point where I got all the categories displaying on the page, but the columns were not correct. Could someone have a look and see if you fix the problem?

Note: I don't want to change the function code to another way, I want to make it as simple as can be, don't make it harder than it actually is.

Thank you
Brian


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 2;              //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");

$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?>
__________________
Made2Own

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

Last edited by Brian07002; 07-02-2011 at 01:00 PM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-02-2011, 02:01 PM Re: Help with the table output...
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Seriously, you need to stop making new threads all the time about the same subject.
Also, it seems you need to read up on tables. A table is set up in this manner
HTML Code:
<table>
   <tr> <!-- row -->
      <td>A cell in the row</td>
      <td>A cell in the row</td>
      <td>A cell in the row</td>
   </tr> <!-- end of row -->
   <tr> <!-- new row -->
      <td>A cell in the row</td>
      <td>A cell in the row</td>
      <td>A cell in the row</td>
   </tr> <!-- end of row -->
</table>
Tables are always built with rows containing cells, not columns containing rows.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 07-02-2011, 03:11 PM Re: Help with the table output...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Yes, I am aware of that, what I don't get is in the php code, the curly brackets. I didn't know that by placing the curly brackets in the incorrect place (and not far from there original spot) that caused a couple of major display problems. The first problem was that the while loop / echo part of the code was not echoing all of the rows in db, (skipping the first row) and when I moved the curly brackets around, I managed to get all the categories displayed from the db. I am still having that issue because of changing the code around for another issue which was the table columns. I understand that columns run across the page, while rows run down the page. It's the php part that I can't seem to master. I will continue to pound at the code some more, but if you have the answer, please do post.

Thank you
Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 07-02-2011, 04:11 PM Re: Help with the table output...
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Alright, here's an example that I think will work. Took some of your code and added some of my own.

PHP Code:
$result mysql_query("SELECT * FROM categories");

$numCols 2;
$numPerCol ceil(mysql_num_rows($result) / $numCols);

// I'm just gonna put the results into an array, so we can access rows by index
$categories = array();
while(
$row mysql_fetch_array($result)) {
   
$categories[] = $row['category_name'];
}

echo 
'<table>';
for (
$i 0$i $numPerCol$i++) {
   echo 
'<tr>'// new row
   
for ($j 0$j $numCols$j++) {
      
$index = ($i numCols) + $j;
      
// if the number of rows is not evenly dividable by $numPerCol,
      // the last row will not be full. Then just output an empty cell
      
if (isset($categories[$index])) {
         echo 
'<td>' $categories[$index] . '</td>';
      } else {
         echo 
'<td>-</td>';
      }
   }
}
echo 
'</table>'
And if you want to add in pagination, you'd better look into the SQL LIMIT clause. You'll find plenty of resources if you search for "mysql limit" or similar. I can also recommend the tutorials on tizag.com.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 07-02-2011 at 04:13 PM.. Reason: found small mistake in code
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to Help with the table output...
 

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 1.76893 seconds with 12 queries