Basic php question

Discussion in 'PHP' started by bonjurkes, Feb 22, 2008.

  1. #1
    Hello,

    I am pretty new at php coding so i have a question now.

    <a href="test-<?php echo $row_bastir['page']-1; ?>.htm">&lt;&lt; Previous <?php echo $row_bastir['page']-1; ?>
    PHP:
    page is the name of the id table, at the table there is ids from 1 to 300

    And the code above is for previous id. For example if you are at id 5's page, by clicking the link at the bottom of the page, you can go to id 4's page. Practically it works but, when you are at id 1's page, that link will say go to id 0's page which does not exist.

    So i want to add some controlling way to code, so first it checks if id-1 is available, if it is not it wont display the link otherwise it will show the link for previous page.
     
    bonjurkes, Feb 22, 2008 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    Just check to see if its equal to 1 prior to the link. Do it before the a href, since you would skip the entire thing.
    <?php
    $page = $row_bastir['page'];
    if($page != 1) { echo "whole line"; }
    else { echo "previous link only"; }
     
    shallowink, Feb 22, 2008 IP
  3. bonjurkes

    bonjurkes Well-Known Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #3
    <?php $page = $row_bastir['page']; if($page != 1) {echo "<a href="test-<?php echo $row_bastir['page']-1; ?>-.html">&lt;&lt; Test <?php echo $row_bastir['page']-1; ?> Page</a>";} else { echo "previous link only"; } ?>
    PHP:
    Is it like that?

    Because i am using dreamweaver and i guess i made a mistake at somewhere because code colors are not changing anymore.
     
    bonjurkes, Feb 23, 2008 IP
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    You are mixing the tags up. If you put <?php you have to have ?> before you start another. Plus its pretty blasted confusing all on a single line.

    <?php
     $page = $row_bastir['page']; 
    if($page != 1) {
    echo "<a href=\"test-" . $page-1 . "-.html\">&lt;&lt; Test" .  $page-1 . "Page</a>";
    } else
     { echo "previous link only"; } ?>
    
    PHP:
    Which still isn't best but it should work. You have to put in the "next" page as well in the first half of the IF and replace the "previous link only" but get it working first. Further, if you use " to enclose the PHP statement, when you use " inside that statement it has to be escaped with the \. Which would have caused you more grief.
     
    shallowink, Feb 23, 2008 IP
  5. bonjurkes

    bonjurkes Well-Known Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #5
    Hello,

    I tested the code, but $page-1 part is making problem

    it is showing a text instead of url, when i change it to $page, it works like a charm

    so the problem is $page-1 part.
     
    bonjurkes, Feb 24, 2008 IP
  6. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #6
    well assign it to $prevPage:
    $prevPage = $page -1;
    Before you enter the if part. Then put $prevPage in place of the $page - 1
    
    <?php
     $page = $row_bastir['page'];
    $prevPage = $page -1;
    if($page != 1) {
    echo "<a href=\"test-$prevPage-.html\">Test $prevPage Page</a>";
    } else
     { echo "previous link only"; } ?>
    
    Code (markup):
     
    shallowink, Feb 24, 2008 IP
  7. bonjurkes

    bonjurkes Well-Known Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #7
    thanks :)

    how can i make it for the next page?

    because i tried to change - to + but even there is no record with the next id, it shows the link.
     
    bonjurkes, Feb 24, 2008 IP
  8. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #8
    <?php
    $page = $row_bastir['page'];
    $prevPage = $page -1;
    $nextPage = $page + 1;
    if($page != 1) {
    echo "<a href=\"test-$prevPage-.html\">Test $prevPage Page</a>";
    echo "<a href=\"test-$nextPage-.html\">Test $nextPage Page</a>";
    } else
    { echo "<a href=\"test-$nextPage-.html\">Test $nextPage Page</a>"; } ?>

    Should do it.
     
    shallowink, Feb 24, 2008 IP