Jump to content

Recommended Posts

Hi all,

I need your help as I have got a problem with display the 50 rows in the search results. I want to limit 50 rows in per page and if I have less than 50 rows like 21 rows in the page 2 then display the 21 rows.

I have got a problem with the limit, because when I tried to use `LIMIT 0, 50`, it will display total 71 rows which I only want to display no more than 50 in per page.

Here is what I have tried:

    $search_mailbox = $link->prepare("SELECT * FROM $folder WHERE from_email LIKE ? OR subject LIKE ? OR message LIKE ? ORDER BY received_date DESC LIMIT 0, 50");
    $search_mailbox->execute([$searchemail, $searchsubject, $searchmessage]);


And I have also tried this:

    $search_mailbox = $link->prepare("SELECT * FROM $folder WHERE from_email LIKE ? OR subject LIKE ? OR message LIKE ? ORDER BY received_date DESC LIMIT $offset, $limit");
    $search_mailbox->execute([$searchemail, $searchsubject, $searchmessage]);


It will not limit the rows I want. It will still show the 71 rows.

Here is the full code:

   

<?php
    
    // Initialize the session
    session_start();
    
    //Connect to the database
    require_once('config.php');
    
    $searchemail = '%' . 'gmail.com' . '%';
    $searchsubject = '%' . 'gmail.com' . '%';
    $searchmessage = '%' . 'gmail.com' . '%';
    
    $mailbox = $link->prepare("SHOW TABLES");
    $mailbox->execute();
    $folders = array();
    $total = 0;
    $total_rows = 0;
    $i = 0;
            
    while ($folder = $mailbox->fetch(PDO::FETCH_NUM)) {
        $folder = $folder[0];
    
        if (strpos($folder, 'users') !== false) {
            $folder = str_replace('users', '', $folder);
        }
        else
        {
            $folders[$i] = $folder;
        }
        $i++;
    }
    
    foreach($folders as $folder) {
        $search_mailbox = $link->prepare("SELECT * FROM $folder WHERE from_email LIKE ? OR subject LIKE ? OR message LIKE ? ORDER BY received_date DESC LIMIT 0, 50");
        $search_mailbox->execute([$searchemail, $searchsubject, $searchmessage]);
        
        if ($search_mailbox->rowCount() > 0) {
            $total += $search_mailbox->rowCount();
        }
    }
    
    $page = 1;
    $limit = 50;
    //$per_page = 50;
    //$pages = ceil($total / $per_page);
    
    $offset = ($page - 1) * $limit;
    
    
    foreach($folders as $folder) {
        
        $search_mailbox = $link->prepare("SELECT * FROM $folder WHERE from_email LIKE ? OR subject LIKE ? OR message LIKE ? ORDER BY received_date DESC LIMIT $offset, $limit);
        $search_mailbox->execute([$searchemail, $searchsubject, $searchmessage]);
    
    
        if ($search_mailbox->rowCount() > 0) {
            foreach($search_mailbox->fetchAll() as $k => $row) {
                $email_number = $row['id'];
                $search_from = $row['from_email'];
                $search_subject = $row['subject'];
                $total_rows++;
                echo $search_subject . '.........................' . $total_rows;
                echo "<br>";
            }
        }
                        
    }
    
    ?>

I am unable to fetch the first 50 rows in the page 1 and I am also unable to fetch the 21 rows in the page 2. 

What I am trying to achieve is when I am in page 1, I want to display the first 50 rows, then in the page 2 if I have another 50 rows then display the 50 rows, but if I have less than 50 rows like 21 rows then display the 21 rows.

Can you please show me an example how I can start with first 50 rows when I am in the page 1 then in page 2 display 21 rows to make in total 71 rows?

Any advice would be much appreicated.

Thanks in advance.

Except for the fact that you hardcoded $page to 1 (and except for the syntax error), it looks right. Well, also except for the folder thing, that's really not good.

I see you have some debugging output there. What do you see?

12 minutes ago, requinix said:

Except for the fact that you hardcoded $page to 1 (and except for the syntax error), it looks right. Well, also except for the folder thing, that's really not good.

I see you have some debugging output there. What do you see?

There is no syntax error. So what you mean for the folder thing that's not really good?

I can see the full output for the email subjects. I cant be able to limit it to 50 when I'm searching for each folder. If I search one folder it will works fine, so I want to search for all folders.

Any idea how I can be able to limit it to allow me to display them in page 1, page 2 and so on?

You can use LIMIT in mysql

select rows from table limit 1,21

would return the first 21 results.  You'd have to build in some logic to determine which page you're on. Here's an example. 

https://www.myprogrammingtutorials.com/create-pagination-with-php-and-mysql.html

2 hours ago, mark107 said:

There is no syntax error.

There is in what you posted. But obviously it's just in the post, otherwise your script wouldn't run at all.

 

2 hours ago, mark107 said:

So what you mean for the folder thing that's not really good?

The way you set up folders by using additional database tables is not good. There is no reason why anything should ever be done dynamically like that. Instead, you should have a list of folders, all messages should be in the same database table, and each should be marked with the folder it's in.

 

2 hours ago, mark107 said:

I can see the full output for the email subjects. I cant be able to limit it to 50 when I'm searching for each folder. If I search one folder it will works fine, so I want to search for all folders.

...Are you saying you currently get 50 per folder and you don't want it to be per folder? Because that is what the code does.

The deal with using a list of folders and holding all the messages together would make this all very much easier.

 

2 hours ago, mark107 said:

Any idea how I can be able to limit it to allow me to display them in page 1, page 2 and so on?

taquitosensei already replied, but you'll have more luck researching the concept on your own if you use the term pagination.

14 hours ago, taquitosensei said:

You can use LIMIT in mysql

select rows from table limit 1,21

would return the first 21 results.  You'd have to build in some logic to determine which page you're on. Here's an example. 

https://www.myprogrammingtutorials.com/create-pagination-with-php-and-mysql.html

I have tried it but it doesn't work. Because of this I am using each folder under the loop so there is no way to avoid it unless I have to use one table or store the list of strings in the array or something like that. 

 

LIMIT most definitely does work. But it only applies to one single query. If you want to limit multiple queries then you "have" to do that yourself.

But what I said is still the best way to go. It will take some work to change to now, but it makes a great number of things much easier.

32 minutes ago, requinix said:

LIMIT most definitely does work. But it only applies to one single query. If you want to limit multiple queries then you "have" to do that yourself.

But what I said is still the best way to go. It will take some work to change to now, but it makes a great number of things much easier.

Already done it. It is a shame that no one could help me to solve it when I have to find my own way to solve it.

39 minutes ago, requinix said:

Ooh, you almost had me there. I didn't realize you were being sarcastic until I nearly hit the Submit Reply button.

Great joke. 👍

Im sorry but it is still not resolve as I have been checking on my keywords. I'm still getting more than 50 rows in per page so I dont know what to do and how to resolve it.

Do I have to use something is like the arrays or something like that if it would help or not?

If it would not help then what I need to do?

9 minutes ago, requinix said:

First, I need some confirmation: are you getting more than 50 per folder? Or up to 50 per folder and more than 50 total per page?

Yes, I am getting more than 50 per folder. So I want to display 50 total per page.

Then your code must not be the same as what you originally posted. Because what you showed there very clearly did 50 per folder.

You also had a couple echo statements in it. One included the value of $total_rows. Are you saying you get more than 50 because the value outputted goes beyond 50? Because you do not reset that value for each folder.
Reset the value to 0 before each folder's query, then see what output you get.

2 minutes ago, requinix said:

Then your code must not be the same as what you originally posted. Because what you showed there very clearly did 50 per folder.

You also had a couple echo statements in it. One included the value of $total_rows. Are you saying you get more than 50 because the value outputted goes beyond 50? Because you do not reset that value for each folder.
Reset the value to 0 before each folder's query, then see what output you get.

What value do you want me to reset before each folder?

$total_rows.

Or create a new variable just for the folder's rows.

    foreach($folders as $folder) {
        
        $search_mailbox = $link->prepare("SELECT * FROM $folder WHERE from_email LIKE ? OR subject LIKE ? OR message LIKE ? ORDER BY received_date DESC LIMIT $offset, $limit");
        $search_mailbox->execute([$searchemail, $searchsubject, $searchmessage]);
    
        $folder_rows = 0;
        if ($search_mailbox->rowCount() > 0) {
            foreach($search_mailbox->fetchAll() as $k => $row) {
                $email_number = $row['id'];
                $search_from = $row['from_email'];
                $search_subject = $row['subject'];
                $total_rows++;
                $folder_rows++;
                echo $search_subject . '.........................' . $folder_rows . ' / ' . $total_rows;
                echo "<br>";
            }
        }
                        
    }

You should see $folder_rows stay between 1-50.

31 minutes ago, requinix said:

$total_rows.

Or create a new variable just for the folder's rows.


    foreach($folders as $folder) {
        
        $search_mailbox = $link->prepare("SELECT * FROM $folder WHERE from_email LIKE ? OR subject LIKE ? OR message LIKE ? ORDER BY received_date DESC LIMIT $offset, $limit");
        $search_mailbox->execute([$searchemail, $searchsubject, $searchmessage]);
    
        $folder_rows = 0;
        if ($search_mailbox->rowCount() > 0) {
            foreach($search_mailbox->fetchAll() as $k => $row) {
                $email_number = $row['id'];
                $search_from = $row['from_email'];
                $search_subject = $row['subject'];
                $total_rows++;
                $folder_rows++;
                echo $search_subject . '.........................' . $folder_rows . ' / ' . $total_rows;
                echo "<br>";
            }
        }
                        
    }

You should see $folder_rows stay between 1-50.

Thanks, but it will do nothing as it will display more than 50 rows.

I find it very strange because when I store the list of strings in the array and when I use:

$test = array_slice($email_arr, 50, 50);

 

It works fine, but when I try this:

   

$search_mailbox = $link->prepare("SELECT * FROM $folder WHERE from_email LIKE ? OR subject LIKE ? OR message LIKE ? ORDER BY received_date DESC LIMIT 50, 50");


I will not be able to see the same output as above.

Any idea?
 

6 minutes ago, requinix said:

Let me know when you did it and what the result was.

here is the results for the array when I am on the page 1:

 

Image body........................1
Hey........................2
Hey........................3
Testing........................4
Re: Re: This weekend........................5
Re: This weekend........................6
This weekend........................7
Hey........................8
Meeting this weekend........................9
Meeting this weekend........................10
Re: Working on a project........................11
Hey Mark 7........................12
Hey Mark 6........................13
Hey Mark 5........................14
Hey Mark 4........................15
Hey Mark 3........................16
Hey Mark 2........................17
Hey Mark........................18
This lead Unsubscribed: ************@gmail.com........................19
This lead Unsubscribed: ************@gmail.com........................20
This lead Unsubscribed: ************@gmail.com........................21
This lead Unsubscribed: ************@gmail.com........................22
This lead Unsubscribed: ************@gmail.com........................23
This lead Unsubscribed: ************@gmail.com........................24
This lead Unsubscribed: ************@gmail.com........................25
This lead Unsubscribed: ************@gmail.com........................26
This lead Unsubscribed: ************@gmail.com........................27
This lead Unsubscribed: ************@gmail.com........................28
This lead Unsubscribed: ************@gmail.com........................29
This lead Unsubscribed: ************@gmail.com........................30
Hey........................31
Контрольные точки в переговорах........................32
Hey 2........................33
Hey........................34
Hey........................35
test spam........................36
[Earn 50 Credits] - Get A 10-Pack Of Ads At Ads........................37
[Earn 50 Credits] - Get A 10-Pack Of Ads At Ads........................38
[Earn 50 Credits] - Get A 10-Pack Of Ads At Ads........................39
Test 4 images........................40
Hey Mark........................41
Image body........................42
zip and rar attachment........................43
test image attachment no 5........................44
Hey........................45
Image in body and attachement........................46
checking message body........................47
New Email For Test........................48
Test Message From Shibbir........................49
Hey Mark 2........................50

 

here is the results for the array when I am on the page 2:

 

Re: Test Email by Koviri J........................1
Re: Test Email by Koviri J........................2
Test Email by Koviri J........................3
How are you........................4
Hey Mark........................5
Re: Meeting this weekend........................6
Hello........................7
Re: What’s the minimum?........................8
1,000 visitors per DAY.........................9
DIRTY CHEAP traffic!........................10
4 cent clicks. (OPEN NOW)........................11
Up to $400... per DAY...........................12
Turn a few bucks into $197+...........................13
How To Get An Extra $100-$500 in Your PayPal........................14
Re: Test Email........................15
GET MONEY in the next 24 hours - up to $200+ per day...........................16
3 steps to $200+ a day...........................17
$318+ per day with this 60 SECOND NICHE SITE GENERATOR...........................18
Booooooombarded............................19
THIS WEEKEND: Over $400 in less than 1 hour............................20
FREEEEEE videooooooooo traaaaaaffic............................21
THIS WEEKEND: $408 in 45 minutes..........................22
Weird system SPITS OUT $97 payments...........................23
$97 a pop pop pop...........................24
FREE SYSTEM: Affiliate marketing IN REVERSE...........................25
traaaaaaaaaaaaaaaaaaaffiiiiiiiiiiic........................26
dead business........................27
hey, YOU need this........................28
CLONE this!........................29
Your money........................30
Re: Hey........................31
Re: Hey........................32
AI: New FREE 7 figure traffic getting system............................33
FREE SYSTEM: Stuffs your pockets with $20 bills...........................34
FREEEEEEEEEE system sucks in $20 bills...........................35
DFY websites that suck in $12,000+ per month (can be YOURS)............................36
OPEN NOW: If you've never made money online (new $12K per month system)............................37
$100 to $200, day after day...........................38
CASH FAST: $200 a day...........................39
You bloody motherfucker didn't pay anyone...........................40
you're only $200-a-day away from FIRING your boss...........................41
-----> weird fun income video for ya...........................42
Re:........................43
Re:........................44
Re:........................45
Re:........................46
Re:........................47
........................48
my last email...........................49
crazy deal........................50

 

Here is the results for the database when I am on the page 1:
 

Image body.........................1
Hey.........................2
Hey.........................3
Testing.........................4
Re: Re: This weekend.........................5
Re: This weekend.........................6
This weekend.........................7
Hey.........................8
Meeting this weekend.........................9
Meeting this weekend.........................10
Re: Working on a project.........................11
Hey Mark 7.........................12
Hey Mark 6.........................13
Hey Mark 5.........................14
Hey Mark 4.........................15
Hey Mark 3.........................16
Hey Mark 2.........................17
Hey Mark.........................18
This lead Unsubscribed: ************@gmail.com.........................19
This lead Unsubscribed: ************@gmail.com.........................20
This lead Unsubscribed: ************@gmail.com.........................21
This lead Unsubscribed: ************@gmail.com.........................22
This lead Unsubscribed: ************@gmail.com.........................23
This lead Unsubscribed: ************@gmail.com.........................24
This lead Unsubscribed: ************@gmail.com.........................25
This lead Unsubscribed: ************@gmail.com.........................26
This lead Unsubscribed: ************@gmail.com.........................27
This lead Unsubscribed: ************@gmail.com.........................28
This lead Unsubscribed: ************@gmail.com.........................29
This lead Unsubscribed: ************@gmail.com.........................30
Hey.........................31
Контрольные точки в переговорах.........................32
Hey 2.........................33
Hey.........................34
Hey.........................35
test spam.........................36
[Earn 50 Credits] - Get A 10-Pack Of Ads At Ads.........................37
[Earn 50 Credits] - Get A 10-Pack Of Ads At Ads.........................38
[Earn 50 Credits] - Get A 10-Pack Of Ads At Ads.........................39
Test 4 images.........................40
Hey Mark.........................41
Image body.........................42
zip and rar attachment.........................43
test image attachment no 5.........................44
Hey.........................45
Image in body and attachement.........................46
checking message body.........................47
New Email For Test.........................48
Test Message From Shibbir.........................49
Hey Mark 2.........................50
Re: Test Email by Koviri J.........................51
Re: Test Email by Koviri J.........................52
Test Email by Koviri J.........................53
The computer must make money.........................54
Earnings on your PC.........................55
Программа пассивного дохода.........................56
How to use a PC to earn money?.........................57
test.........................58
How to use a PC to earn money?.........................59
Security Notice. mark@************.com was hacked! Change your password now!.........................60
Test signature.........................61
Try this.........................62
Hey.........................63
Hey.........................64
Hey.........................65
***SPAM*** [Earn 50 Credits] - Get A 10-Pack Of Ads At Ads.........................66
***SPAM*** [Earn 50 Credits] - Get A 10-Pack Of Ads At Ads.........................67
***SPAM*** [Earn 50 Credits] - Get A 10-Pack Of Ads At Ads.........................68
***SPAM*** [Earn 50 Credits] - Get A 10-Pack Of Ads At Ads.........................69
[Earn 50 Credits] - Get A 10-Pack Of Ads At EvolutionAds.........................70
***SPAM*** stuff $400 a day with this............................71
***SPAM*** .........FAST cash...................................72
***SPAM*** URGENT: Here's how my buddy Matt made over $50,000 last month............................73
***SPAM*** .........$500+ per day campaigns DONE FOR YOU....................................74
Testing 2.........................75
This weekend.........................76
This lead Unsubscribed: ************@gmail.com.........................77
This lead Unsubscribed: ************@gmail.com.........................78
test spam.........................79

 

 

Here is the results for the database when I am on the page 2:
 

How are you.........................1
Hey Mark.........................2
Re: Meeting this weekend.........................3
Hello.........................4
Re: What’s the minimum?.........................5
1,000 visitors per DAY..........................6
DIRTY CHEAP traffic!.........................7
4 cent clicks. (OPEN NOW).........................8
Up to $400... per DAY............................9
Turn a few bucks into $197+............................10
How To Get An Extra $100-$500 in Your PayPal.........................11
Re: Test Email.........................12
GET MONEY in the next 24 hours - up to $200+ per day............................13
3 steps to $200+ a day............................14
$318+ per day with this 60 SECOND NICHE SITE GENERATOR............................15
Booooooombarded.............................16
THIS WEEKEND: Over $400 in less than 1 hour.............................17
FREEEEEE videooooooooo traaaaaaffic.............................18
THIS WEEKEND: $408 in 45 minutes...........................19
Weird system SPITS OUT $97 payments............................20
$97 a pop pop pop............................21
FREE SYSTEM: Affiliate marketing IN REVERSE............................22
traaaaaaaaaaaaaaaaaaaffiiiiiiiiiiic.........................23
dead business.........................24
hey, YOU need this.........................25
CLONE this!.........................26
Your money.........................27
Re: Hey.........................28
Re: Hey.........................29
AI: New FREE 7 figure traffic getting system.............................30
FREE SYSTEM: Stuffs your pockets with $20 bills............................31
FREEEEEEEEEE system sucks in $20 bills............................32
DFY websites that suck in $12,000+ per month (can be YOURS).............................33
OPEN NOW: If you've never made money online (new $12K per month system).............................34
$100 to $200, day after day............................35
CASH FAST: $200 a day............................36
You bloody bad didn't pay anyone............................37
you're only $200-a-day away from FIRING your boss............................38
-----> weird fun income video for ya............................39
Re:.........................40
Re:.........................41
Re:.........................42
Re:.........................43
Re:.........................44
.........................45
my last email............................46
crazy deal.........................47
OUTTA NOWHERE: Newbie system rakes over $8,000 per mo. ...............................48
$8k per month for NEWBIES............................49
FAST $20-$80 within 24 hours (open up to start)............................50

 

Code for array:
 

$searchemail = '%' . 'gmail.com' . '%';
$searchsubject = '%' . 'gmail.com' . '%';
$searchmessage = '%' . 'gmail.com' . '%';

$mailbox = $link->prepare("SHOW TABLES");
$mailbox->execute();
$email_arr = array();
$folders = array();
$total_results = 0;
$i = 0;
        
while ($folder = $mailbox->fetch(PDO::FETCH_NUM)) {
    $folder = $folder[0];

    //if (strpos($folder, 'trash') !== false) {
        //$folder = str_replace('trash', '', $folder);
    //}
    if (strpos($folder, 'users') !== false) {
        $folder = str_replace('users', '', $folder);
    }
    else
    {
        $folders[$i] = $folder;
    }
    $i++;
}


foreach($folders as $folder) {
    $search_mailbox = $link->prepare("SELECT * FROM $folder WHERE from_email LIKE ? OR subject LIKE ? OR message LIKE ? ORDER BY received_date DESC");
    $search_mailbox->execute([$searchemail, $searchsubject, $searchmessage]);
    $total_results += $search_mailbox->rowCount();

    //Close connection
    $search_mailbox = null;
}

$i = 0;
$page = 1;
$limit = 50;
$pages = ceil($total_results / $limit);
$end = $limit;
$start = 0;

if ($page >= 2) {
    $start = $page * $limit - 50;
}


echo "the start in page 1 is........................." . $start;
echo "<br>";
echo "<br>";


foreach($folders as $folder) {
    $search_mailbox = $link->prepare("SELECT * FROM $folder WHERE from_email LIKE ? OR subject LIKE ? OR message LIKE ? ORDER BY received_date DESC");
    $search_mailbox->execute([$searchemail, $searchsubject, $searchmessage]);
    
    if ($search_mailbox->rowCount() > 0) {
        foreach($search_mailbox->fetchAll() as $k => $row) {
            
            
            
            $email_number = $row['id'];
            $search_from = $row['from_email'];
            $search_subject = $row['subject'];
            $email_arr[$i] = ['email_number' => $email_number, 'email_from' => $search_from, 'email_subject' => $search_subject];
            $i++;
        }
    }
}

$test = array_slice($email_arr, $start, $end);
$ii = 0;

foreach($test as $emails) {

    $ii++;
    echo $emails['email_subject'] . '........................' . $ii;
    echo "<br>";
    echo "<br>";
    echo "<br>";
}




Code for database:
 

$searchemail = '%' . 'gmail.com' . '%';
$searchsubject = '%' . 'gmail.com' . '%';
$searchmessage = '%' . 'gmail.com' . '%';
    
$mailbox = $link->prepare("SHOW TABLES");
$mailbox->execute();
$folders = array();
$total = 0;
$total_rows = 0;
$i = 0;
            
while ($folder = $mailbox->fetch(PDO::FETCH_NUM)) {
    $folder = $folder[0];
    
    if (strpos($folder, 'users') !== false) {
        $folder = str_replace('users', '', $folder);
    }
    else
    {
        $folders[$i] = $folder;
    }
    $i++;
}
    
foreach($folders as $folder) {
    $search_mailbox = $link->prepare("SELECT * FROM $folder WHERE from_email LIKE ? OR subject LIKE ? OR message LIKE ? ORDER BY received_date DESC LIMIT 0, 50");
    $search_mailbox->execute([$searchemail, $searchsubject, $searchmessage]);
        
    if ($search_mailbox->rowCount() > 0) {
        $total += $search_mailbox->rowCount();
    }
}
    
$i = 0;
$page = 1;
$limit = 50;
$pages = ceil($total_results / $limit);
$end = $limit;
$start = 0;

if ($page >= 2) {
    $start = $page * $limit - 50;
}
    
$offset = ($page - 1) * $limit;

echo 'start.........................' . $start;
echo "<br>";
echo 'end.........................' . $end;
echo "<br>";

    
    
foreach($folders as $folder) {
    $search_mailbox = $link->prepare("SELECT * FROM $folder WHERE from_email LIKE ? OR subject LIKE ? OR message LIKE ? ORDER BY received_date DESC LIMIT 50, 50");
    $search_mailbox->execute([$searchemail, $searchsubject, $searchmessage]);
    
    if ($search_mailbox->rowCount() > 0) {
        foreach($search_mailbox->fetchAll() as $k => $row) {
            $email_number = $row['id'];
            $search_from = $row['from_email'];
            $search_subject = $row['subject'];
            $limit--;
            $total_rows++;
            echo $search_subject . '.........................' . $total_rows;
            echo "<br>";
        }
    }
}

 

My math is better than my PHP.... so this stood out:
$start = $page * $limit - 50;
Is your math right?
Do you mean:
$page * ($limit - 50)
or do you mean:
($page * $limit) - 50

(Sorry I don't have enough time to read your code to figure out which one it should be....)

 

Edited by StevenOliver
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.