Print display help and pagination.

Discussion in 'PHP' started by OreoKnight, Feb 18, 2008.

  1. #1
    So with the help of you guys and working on my own I'm down to two final things I would like to get done before I really start creating a ton of content for my site. So here is what we got.

    This is setup so I can call certain articles from certain categories as I need them. Is there a way for me to format the text that is being brought up by the query. Maybe to display it in a table of three columns with the articles filling it. I'd like the text size to be smaller as well to get it to work/look right.

    Also I've been reading up on pagination but I still don't understand it. Would you create a pagination file say pagination.php and do an include to where you want it to paginate it or do you have to put it in the print coding to display it.

    Thanks in advance!

    Here is the code for test that I have right now.
    <?
    // get all the articles for this category
    $sql = mysql_query("select * from articles where cat_id='51' order by id DESC");
    $count = mysql_num_rows($sql);
    if($count > 0){
    while($row = mysql_fetch_assoc($sql)){
    $id = $row['id'];
    $title = $row['title'];
    /*if(strlen($image) > 5)
    print '<a href="article.php/'.$id.'/'.$title.'" class="article_style" style="clear: both;"><img src="'.$image.'" style="float: left; margin-right: 2px;" /><b>'.$title.'</b><br /></a>';
    else*/
    print '<a href="../../article.php/'.$id.'/'.$title.'" class="article_style" style="width: 500px; margin-bottom: 5px;"><b>'.$title.'</b><br /></a>';
    }
    }
    else{
    print "<br /><b style=\"font-size: 11px; text-decoration: underline;\">No articles currently in this category.</b><br />Currently, no articles are listed in this category. Please check back shortly for our
    constantly updated and listed articles!";
    }
    ?>

    I have a page that test runs the codes I change, you can find the test run of this information here.
    www.buildurbody.com/test4.php
     
    OreoKnight, Feb 18, 2008 IP
  2. Cobnut

    Cobnut Peon

    Messages:
    184
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    In terms of pagination for printing, the simplest option is probably to use the CSS page-break-before property. I've used this a few times and it works well.

    How I did it was to define an unused element, e.g. h3, with this property and then insert <h3 /> wherever I needed a page break. You can script for it by counting elements/lines/whatever and throw one in when needed.

    Jon
     
    Cobnut, Feb 18, 2008 IP
  3. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    My eyes! Do NOT use SELECT *, it's bad bad bad. Just select id,title since that's the only data you are using here. Second, you're not realling doing pagination, which is more the standard "1 2 3 4 5 6 7" etc crap, you're just printing out info into a table. There isn't any need to put this into a pagination.php file, that would be pretty much pointless for this unless you're going to be using it on a lot of pages, in which case creating a function for it isn't a bad idea either. Also $id = $row['id']; is bad, you already have a variable for it, so use it, all you're doing is creating more work for yourself, more chances for things to go wrong, and in the whole scheme of things if you were writing a large site, creating just a little more overhead for the system. If you already have a variable that contains the data you want: use it. If you want to manipulate it, that's fine to put in another variable, but don't copy variables for no reason.

    Okay, if you're just wanting to put styling in it, it's really quite simple, just put whatever tags you want, in your echo statement (use echo instead of print too, it's supposed to be just a little more efficient on the system ;)). In your loop just do echo '<b>'.$title.'</b>:<br />'; if you wanted to print out the title in bold with a line break, and then for every single part of the loop it's going to print it out bold in a line break. If you want to do a table with 3 columns, it's a little more tricky but here's how you'd do it:
    
    echo '<table><tr>';
    
    for ($i=0; $i<blah; $i++){
      blah;
      echo '<td>data here</td>';
    
      if ($i%3 == 0 && $i != 0) //this is if you want 3 columns
        echo '</tr><tr>'; //this makes a new row every 3rd column
    }
    
    echo '</tr></table>';
    PHP:
    You've just got to play with some code and get it out how you want, but it's really not all that hard. Play with some css classes as well.
     
    projectshifter, Feb 18, 2008 IP
  4. OreoKnight

    OreoKnight Peon

    Messages:
    95
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    So how would that look script wise with my original code, and I'm assuming I replace the blahs right... I'm not so much worried about the pagination right now for each of the main categories as I am getting this thing to print out into table format... The goal is to have it print each exercise article's title in a row which will be under a main heading. Take chest for instance, under my little blurb about the chest I'll have this print(or echo) the articles under in table format as to take up less space and look more attractive. Also I have moved onto the next test page for what I'm looking for.
    www.buildurbody.com/test5.php

    I'm probably gonna add another row, and in this row I'd like the table to generate the links to those articles. Whereby I stick the code in the <tr> code </tr>, this way it looks nice neat and closed. I'll be doing this for all the major parts of the body. I know it might be more efficient to do it another way, but know that I don't know php and I'm trying to find more of an html solution (which I do know). I figured I can query specific articles from categories and have them print in tabular format, thus eliminating the need for sub-cats.(I didn't code this and am not gonna pay to recode it until I start making some money, again html is all I really know, plus what I've discerned from analyzing the php and playing with how it works, thus all the test pages.....)

    As per the code right now to get the query and print it looks like this...

    <?
    // get all the articles for this category
    $sql = mysql_query("select * from articles where cat_id='51' order by id DESC");
    $count = mysql_num_rows($sql);
    if($count > 0){
    while($row = mysql_fetch_assoc($sql)){
    $id = $row['id'];
    $title = $row['title'];
    /*if(strlen($image) > 5)
    print '<a href="article.php/'.$id.'/'.$title.'">'.$title.'<br /></a>';
    else*/
    print '<a href="../../article.php/'.$id.'/'.$title.'">'.$title.'<br /></a>';
    }
    }
    else{
    print "<br /><b style=\"font-size: 11px; text-decoration: underline;\">No articles currently in this category.</b><br />Currently, no articles are listed in this category. Please check back shortly for our
    constantly updated and listed articles!";
    }
    ?>
    Hopefully this test page will give you guys a better idea on how to help....
    Say for instance I create three articles in the category chest: Flat bench, Incline Fly, and DB Decline Press.... They'll all show up right under the little blurb. This page will again, have all the major body parts, and at the top I will add a jump to tag for each body part so users can jump to the particular section of interest. This eliminates the need to have multiple pages like a chest.php, a back.php, ect. ect. As long as users can flow and navigate its the key, doesn't matter if the coding is not "optimal" as long as it gets the job done. :) Especially when you don't know php...

    But you guys have been extremely helpful as always!
     
    OreoKnight, Feb 19, 2008 IP
  5. OreoKnight

    OreoKnight Peon

    Messages:
    95
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    So I edited my code.... www.buildurbody.com/test6.php and I'm getting
    Parse error: syntax error, unexpected '=' in /home/jujubai/public_html/test6.php on line 111

    What's wrong with the coding? I'm trying to get the 3 column print out right now.

    <div class="article_menu"><br><?
    // get all the articles for this category
    $sql = mysql_query("select * from articles where cat_id='51' order by id DESC");
    $count = mysql_num_rows($sql);
    if($count > 0){
    while($row = mysql_fetch_assoc($sql)){
    $id = $row['id'];
    $title = $row['title'];
    /*if(strlen($image) > 5)

    echo '<table><tr>';for ($i=0; $i<blah; $i++){ blah; echo '<td><a href="article.php/'.$id.'/'.$title.'">'.$title.'<br /></a>';</td>'; if ($i%3 == 0 && $i != 0) //this is if you want 3 columns echo '</tr><tr>'; //this makes a new row every 3rd column}echo '</tr></table>';

    }
    }
    else{
    print "<br /><b style=\"font-size: 11px; text-decoration: underline;\">No articles currently in this category.</b><br />Currently, no articles are listed in this category. Please check back shortly for our
    constantly updated and listed articles!";
    }
    ?>
     
    OreoKnight, Feb 20, 2008 IP
  6. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    For starts you actually left "blah" in there. Look at the error, it tells you what line is messed up, go to it and look at what it's saying is wrong and try to figure it out.
     
    projectshifter, Feb 20, 2008 IP