Results 1 to 4 of 4
  1. #1

    Converting Pagination Procedural Style Code To PREP STMT

    Converting Pagination Procedural Style Code To PREP STMT


    Folks,

    Do have a look at my pagination code using procedural style that is working fine:

    PHP Code:
    <?php

    //Required PHP Files.
    include 'config.php';
    include
    'header.php';

    //Check if User is already logged-in or not. Get the login_check() FUNCTION to check.
    if (login_check() === FALSE)
    {
    //Redirect User to Log-in Page after 2 secs.
    header("refresh:2; url=login.php");
    exit();
    }
    else
    {
    $user = $_SESSION["user"];

    $id = $_SESSION["id"];
    $account_activation_status = $_SESSION["account_activation_status"];
    $id_video_verification_status = $_SESSION["id_video_verification_status"];
    $id_video_verification_url = $_SESSION["id_video_verification_url"];
    $sponsor_username = $_SESSION["sponsor_username"];
    $recruits_number = $_SESSION["recruits_number"];
    $on_day_number_on_7_days_wish_list = $_SESSION["on_day_number_on_7_days_wish_list"];
    $primary_website_domain = $_SESSION["primary_website_domain"];
    $primary_website_email = $_SESSION["primary_website_email"];
    $username = $_SESSION["username"];
    $first_name = $_SESSION["first_name"];
    $middle_name = $_SESSION["middle_name"];
    $surname = $_SESSION["surname"];
    $gender = $_SESSION["gender"];
    $age_range = $_SESSION["age_range"];
    $religion = $_SESSION["religion"];
    $marital_status = $_SESSION["marital_status"];
    $working_status = $_SESSION["working_status"];
    $profession = $_SESSION["profession"];

    $recipient_username = $user;



  2. #2

    Converting Pagination Procedural Style Code To PREP STMT

    is working fine:

    Are you sure about that? The $page_size variable being used in the LIMIT clause in the query is the wrong variable. It should be the $page_count variable.

    If you name your variables better, you can avoid this type of mistake. The $page_count variable is actually the number of rows/records per page and the $page_size variable is the total number of pages. Neither of these variables are named anything to do with what they are being used for.

    Your limit clause is currently trying to do - LIMIT offset, total number of pages. Does that make any sense? Shouldn't it be - LIMIT offset, number of rows per page?

    As to the error, you can do a web search for that error message and find what it means. Short-version - you have not fetched all the result(s) from one query before trying to run another query.

    If you re-read my reply outlining what the two sql queries need to be, you will find that the first query should be using COUNT(*) to get a count of matching rows, not by selecting all the matching rows. If you use COUNT(*) to do this, there will only be a single row in the result set and once you fetch that, this error will go-a-way.

  3. #3

    Converting Pagination Procedural Style Code To PREP STMT

    Originally Posted by DSmabismad
    Are you sure about that? The $page_size variable being used in the LIMIT clause in the query is the wrong variable. It should be the $page_count variable.

    If you name your variables better, you can avoid this type of mistake. The $page_count variable is actually the number of rows/records per page and the $page_size variable is the total number of pages. Neither of these variables are named anything to do with what they are being used for.

    Your limit clause is currently trying to do - LIMIT offset, total number of pages. Does that make any sense? Shouldn't it be - LIMIT offset, number of rows per page?

    As to the error, you can do a web search for that error message and find what it means. Short-version - you have not fetched all the result(s) from one query before trying to run another query.

    If you re-read my reply outlining what the two sql queries need to be, you will find that the first query should be using COUNT(*) to get a count of matching rows, not by selecting all the matching rows. If you use COUNT(*) to do this, there will only be a single row in the result set and once you fetch that, this error will go-a-way.


    Thanks! The non PREP STMT version that I said was working, is not actually my code. I found it somewhere online. Made use of it for over a year on other projects.
    I think the way that script was coded is that, programmer picks how many pages he want all records spread-over. In my case 10 pages. And then the script checks how many rows of records there are and spreads it over 10 pages. And so, the row count on each page are likely to differ on each search. Some searches will yield, for example, 10 records per page while other kw searches display different number of records per page.
    As of right now, I don't care whether 10 records is displayed per page or 100 records as I can always change the values of variables now and then. I just want to convert the script to PREP STMT. Saying all this, if you deem there is an error in the non-PREP STMT version (which I picked from somewhere) then you have done good in pointing-out the errors to me. Cheers for that!

    On the current project, tried making use of it and it does working. Maybe co-incidently. But, since my whole project revolves around PREP STMT then must convert this page's code to it too.

    EDIT: I thought the original programmer was using the $rows_num = mysqli_stmt_num_rows($stmt) to count the rows fetched. What do you think ?
    On line 73, the following shows no pagination numbers:

  4. #4

    Converting Pagination Procedural Style Code To PREP STMT

    DSmabismad,

    Anyway, saying all this, I would like to see the following from you IN ORDER to LEARN your STYLE of doing things.
    And so, if you don't mind, may we see 6 samples (if it's not too much to ask):

    1. A code sample where each page shows only 10 records. (Use NON-PREP STMT).
    2. A code sample where all records are spreadover 10 pages. (Use NON-PREP STMT).

    3. A code sample where each page shows only 10 records. (Use PREP STMT - mysqli procedural style).
    4. A code sample where all records are spreadover 10 pages. (Use PREP STMT - mysqli - procedural style).

    5. A code sample where each page shows only 10 records. (Use PREP STMT - PDO oop style).
    6. A code sample where all records are spreadover 10 pages. (Use PREP STMT - PDO oop style).

    Or, if that was too much to ask, then how-about 2 samples (bold ones mentioned obove). And, if that is too much too then how-about 1 sample from your end (the bold red one) ?
    I'm pretty much sure other future newbies would appreciate your hard work. Alternatively, open a separate tutorial thread on the subject and show your code samples there. And then mention the link on this thread for me to check it out.
    I'll wait for your sample. Thanks.
    As for me, this is the 3rd day I'm gonna google for a proper tutorial on this subject. All tutorials found so far do not show PREP STMT sample and the ones that do show don't show it procedural style. I have not got into oop style yet and not a good idea to jump into it half-way through the procedural style.

    PS - Don't worry. Other gurus are likely to chime in on the samples and so best you make the first move since I asked you to. I'll try charming the others. Benanamen loves pdo. I'll see if I can put him upto doing the pdo versions. He's helped me out once with a pdo sample.

    Cheers!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •