Home>

I am a beginner.
We are currently making simulated shopping and want to implement the paging function on the product screen.
The number of pages can be implemented with the current code, but the first and second pages are the same screen.
How should we improve it?
Please write the details of the question here.

Error message

I'm not paging well

Source code

        <? php
            try {
                $dsn = 'mysql: dbname = Photo;host;charaset = utf8';
                $user = 'root';
                $password = '';
                $dbh = new PDO ($dsn, $user, $password);
                $dbh->
                setAttribute (PDO :: ATTR_ERRMODE, PDO :: ERRMODE_EXCEPTION);
                $sql = 'SELECT code, name, image FROM photo_product
                        WHERE 1 ';
                $stmt = $dbh->prepare ($sql);
                $stmt->execute ();

                print '<b>Photo list<br /><br /></b>';
                while (true) {
                    $rec = $stmt->fetch (PDO :: FETCH_ASSOC);
                    if ($rec == false) {
                        break;
                    }
                    print ' ';
                    print $rec ['name'];
                    print '<br />';
                print ' ';
                    print '<br /><br/>';
                    print '';
                    print '<br />';
                }
                $photo_page = 3;
                $page = empty ($_ GET ['page'])? 1: (int) $_GET ['page'];

                $offset = $photo_page * ($page-1);
                $sql = '"select * from photo_product limit". $offset. ",". $photo_page.';

                $total = $dbh->query ("select count (*)
, (From photo_product ")->fetchColumn ();
                $totalPages = ceil ($total/$photo_page);
                $dbh = null;
            }
            catch (Exception $e) {
                print '<br/>There is a problem right now.
                       <br/>Please wait for a while.  ';
                exit ();
            }
            ?>
          <? php for ($i = 1;$i<= $totalPages;$i ++):?>
             "><? php print $i;?>
            <? php endfor;?>
  • Answer # 1

    $sql = '"select * from photo_product limit". $offset. ",". $photo_page.';

    I'm not running this SQL, and here is a grammatical mistake.

  • Answer # 2

      

    $sql = '"select * from photo_product limit". $offset. ",". $photo_page.';
      $total = $dbh->query ("select count (*)
      from photo_product ")->fetchColumn ();

    A story like a story
    I put it in a variable with $sql, but I write a SQL statement statically at the next execution ...

    Originally, sql statements should be processed with prepare, and double quotes within single quotes
    I don't know what it means to double. Also, if you cut by limit, at least
    Requires order by.