Using PDO LIMIT with a for loop

Hi guys, I am trying to create a self seperating, divisional ladder for a baseball league. Code is as follows.

This tests the FOR loop, which works.
[embed=425,349]for($j=0; $j<24; $j=$j+6){

$stmt= $conn-> prepare('SELECT * FROM ddl WHERE ps4id!=" " ORDER BY pct DESC LIMIT 0 ,6 ');
$stmt->execute();[/embed]

This produces as a syntax error near 0,6
[embed=425,349]for($j=0; $j<24; $j=$j+6){

$stmt= $conn-> prepare('SELECT * FROM ddl WHERE ps4id!=" " ORDER BY pct DESC LIMIT ? ,6 ');
$stmt->execute(array($j));[/embed]

This produces undeclared variable $j
[embed=425,349]for($j=0; $j<24; $j=$j+6){

$stmt= $conn-> prepare('SELECT * FROM ddl WHERE ps4id!=" " ORDER BY pct DESC LIMIT $j ,6 ');
$stmt->execute();[/embed]

Please provide more details on what you are doing, not how you think it should be done.

[embed=425,349]$rs=$stmt-> fetchAll();

$i=0;
foreach($rs as $row) {
echo “

” . ’ ’ . “”;
echo “” . $row[‘ps4id’] . “”;
echo ‘’ . $row[‘wins’] . “”;
echo ‘’ . $row[‘loss’] . “”;
echo ‘’ . $row[‘pct’] . “”;
echo ‘’ . $row[‘rs’] . “”;
echo ‘’ . $row[‘ra’] . “”;

echo “”;
}
} [/embed]

Above is the rest of the code so you have a clearer picture of what i am producing… my apologies. I am looking for thus code to break a db of 24 rows, automatically into 4 divisions of 6 as it recalls and prints to a main page

You are trying to use PHP to do the heavy lifting (the work), but you should be having PDO or mysqli do the heavy lifting:

For example using PHP.NET’s world database, I wrote this:
[php]<?php
include ‘lib/includes/connect/connect.php’;

$db_options = [
/* important! use actual prepared statements (default: emulate prepared statements) /
PDO::ATTR_EMULATE_PREPARES => false
/
throw exceptions on errors (default: stay silent) /
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
/
fetch associative arrays (default: mixed arrays) /
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
];
$pdo = new PDO(‘mysql:host=’ . DATABASE_HOST . ‘;dbname=world;charset=utf8’, DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);
/

  • Grab the Amount of Records ($row) by setting a LIMIT (in this case it’s 50).
    */
    $query = ‘SELECT Name, CountryCode, District, Population FROM city ORDER BY CountryCode DESC LIMIT 50’;
    $stmt = $pdo->query($query);
    ?>
Display From Database Table div.container { display: block; width: 100%; max-width: 400px; height: auto; background-color: orange; padding: 10px; margin: 10px auto; } p.cityRow { font-family: Arial, Helvetica, sans-serif; font-size: 1.0rem; line-height: 1.5; color: #2e2e2e; } p.cityRow span.tabStyle { margin-left: 10px; } <?php /* * I used the World Database (from php.net) and the table city for an example. * I even styled it a little bit. Use class when it is needed and there is really no need to * increment it. Though I don't know exactly whatcha doing and I couldn't use your database, * so I subsituted. */ echo '
' . "\n"; while ($row = $stmt->fetch()) {
        echo '<p class="cityRow">'
        . '<span class="tabStyle">' . $row['Name'] . '</span>'
        . '<span class="tabStyle">' . $row['CountryCode'] . '</span>'
        . '<span class="tabStyle">' . $row['District'] . '</span>'
        . '<span class="tabStyle">' . $row['Population'] . '</span>'
        . '</p>' . "\n";
    }
    echo "</div>\n";
    ?>
</body>
[/php]

The important line to remember line to remember is line 54. All you really need to do is use a while loop. You should really be using prepared statements instead of putting the variable directly into the query statement.

It would also make your life easier if you could have a special field in the database table for the divisions (for example : American League Central Division = alcentral ) that way all you would need is an if statement or do it using MySQL. Just a thought.

HTH John

Thanks for input Strider64…
I guess i was looking for a way to increment limit so it looked something like this each time around.

LIMIT 0,6
LIMIT 6,6
LIMIT 12,6
LIMIT 18,6
With future eyes turned towards an app, having it being done in 1 line of code rather than mutiple uf statements, or replications of codes block, would have been ideal so as to save file size

Forath

What you want is the mudulo operator

Simple Example

[php]$i = 1;
foreach($records as $record){
echo $record;
if ($i % 6 == 0)
echo ‘


’;
$i++;
}[/php]

After a quick check of my php manual…

HOLY CRAP! THATS BRILLIANT!

Absolutely stunned at the elegant simplicity.

tops his hat To you sir, thankyou.

After a day hiking around the mountains, i sat down to address the code and realised it is too simple am answer. This prints a heading every 6 entries, unfortunately it does not address the PDO sorting…
the Query contains and relies upon
ORDER BY pct DESC LIMIT x, 6…
This allows the groups of six to be dealt with and rated amongst themselves…
And alas the problem still stands… for the moment i have copied and pasted 4 times modifying the LIMIT offset manually.
I would prefer to be able to do this by loop

Your goal should be to execute one query that gets all the related data that you want to display on one page, in the order that you want it. Your presentation code would then output the data the way you want.

Does your single query have ORDER BY pct DESC in it?

It appears you have left out more important information in the what because you were focused on the how. You have now mentioned groups and ratings. Why didn’t you mention this before? Again I ask, exactly WHAT are you trying to do? Leave the how to us.

It would be helpful if you posted an SQL dump of your DB so we can see what you’re working with.

Sponsor our Newsletter | Privacy Policy | Terms of Service