Jump to content

foreach


DEVILofDARKNESS

Recommended Posts

This gives me only the first row, and I guess it should be with foreach, but I have no experience with foreach can someone help me?

 

<?php
#leden
#gedichten
#gedichten / lid
#top 10 posters
#top 10 categories
#...
/*DATABASE SETTINGS */

$query = "SELECT COUNT(user_id) FROM users";
$result = mysql_query($query);
list($users) = mysql_fetch_row($result);  # Number of users

$query = "SELECT COUNT(poem_id) FROM poems";
$result = mysql_query($query);
list($poems) = mysql_fetch_row($result);  #  Number of poems

$ppu = $poems / $users;                   # $ppu = $poems per user

$query = "SELECT user_name, ammount FROM users ORDER BY ammount DESC LIMIT 0,10";
$result = mysql_query($query);
while($topusers = mysql_fetch_array($result)) : # SELECT all users descending with on 1 the user with the most posts.

$query = "SELECT p.*, c.* FROM poems p INNER JOIN categories c ON p.category_id = c.category_id ORDER BY c.ammount DESC LIMIT 0,10";
$result = mysql_query($query);
list($categories) = array($result);   #SELECT all categories descending with on 1 the category with the most posts.
?>
<html dir="ltr">
    <head>
        <title>STATISTIEKEN</title>
        <link href="../../standard.css" type="text/css" rel="stylesheet" />
<link href='dichtkunst.css' type='text/css' rel="stylesheet" />
    </head>
    <body>
        <table class="look" height="100%" width="100%" border="1">
            <tbody>
                <tr>
                    <td width="10%" height="100%"><iframe class="frames" src="../../functieknoppen/functieknoppendichtkunst.htm" frameborder="0" width="100%" height="100%" scrolling="no"></iframe>
                    </td>
                    <td><center>
<table border="0"><tr><td colspan="2"><b>STATISTIEKEN</b></td></tr>
<tr><td>Aantal gebruikers:</td><td><b><?php echo $users; ?></b></td></tr>
<tr><td>Aantal gedichten:</td><td><b><?php echo $poems; ?></b></td></tr>
<tr><td>Aantal gedichten per gebruiker:</td><td><b><?php echo $ppu; ?></b></td></tr>
<tr><td>Top 10 gebruikers:</td><td><b><?php {echo "<ul><li>{$topusers['user_name']} : {$topusers['ammount']}</li></ul>"; ?></b></td></tr>
</table>
                    </center></td>
                </tr>
            </tbody>
        </table>
    </body>
</html>
<?php
endwhile;
?>

Link to comment
Share on other sites

Doing it the way you are you would need another while within the one you already have. You might want to look at our UNION/JOINS tutorial on the main site, executing queries within loops is generally not needed and is very inifficient.

Link to comment
Share on other sites

Ok, I have renamed one, now it gives me an eroor that T_ENDWHILE was unexpected, but I can't see where it went wrong

 

<?php
#leden
#gedichten
#gedichten / lid
#top 10 posters
#top 10 categories
#...

/* DATABASE SETTINGS */

$query = "SELECT COUNT(user_id) FROM users";
$result = mysql_query($query);
list($users) = mysql_fetch_row($result);  # Number of users

$query = "SELECT COUNT(poem_id) FROM poems";
$result = mysql_query($query);
list($poems) = mysql_fetch_row($result);  #  Number of poems

$ppu = $poems / $users;                   # $ppu = $poems per user

$query = "SELECT user_name, ammount FROM users ORDER BY ammount DESC LIMIT 0,10";
$result = mysql_query($query);
while($topusers = mysql_fetch_array($result)) : # SELECT all users descending with on 1 the user with the most posts.

$query = "SELECT p.*, c.* FROM poems p INNER JOIN categories c ON p.category_id = c.category_id ORDER BY c.ammount DESC LIMIT 0,10";
$res = mysql_query($query);
list($categories) = array($res);   #SELECT all categories descending with on 1 the category with the most posts.
?>
<html dir="ltr">
    <head>
        <title>STATISTIEKEN</title>
        <link href="../../standard.css" type="text/css" rel="stylesheet" />
<link href='dichtkunst.css' type='text/css' rel="stylesheet" />
    </head>
    <body>
        <table class="look" height="100%" width="100%" border="1">
            <tbody>
                <tr>
                    <td width="10%" height="100%"><iframe class="frames" src="../../functieknoppen/functieknoppendichtkunst.htm" frameborder="0" width="100%" height="100%" scrolling="no"></iframe>
                    </td>
                    <td><center>
<table border="0"><tr><td colspan="2"><b>STATISTIEKEN</b></td></tr>
<tr><td>Aantal gebruikers:</td><td><b><?php echo $users; ?></b></td></tr>
<tr><td>Aantal gedichten:</td><td><b><?php echo $poems; ?></b></td></tr>
<tr><td>Aantal gedichten per gebruiker:</td><td><b><?php echo $ppu; ?></b></td></tr>
<tr><td>Top 10 gebruikers:</td><td><b><?php {echo "<ul><li>{$topusers['user_name']} : {$topusers['ammount']}</li></ul>"; ?></b></td></tr>
</table>
                    </center></td>
                </tr>
            </tbody>
        </table>
    </body>
</html>
<?php
endwhile;
?>

Link to comment
Share on other sites

Try replacing the while loop with the following:

while($topusers = mysql_fetch_array($result)) {} # SELECT all users descending with on 1 the user with the most posts.

Not sure what that while loop is ment to do but I believe it was that : symbol after the while loop causing the problem.

Link to comment
Share on other sites

Damn, Still doesn't work... it keep giving me only the first row... Can someone explain me how I can show them all?

 

<?php
#leden
#gedichten
#gedichten / lid
#top 10 posters
#top 10 categories
#...
/*DATABASE SETTINGS */

$query = "SELECT COUNT(user_id) FROM users";
$result = mysql_query($query);
list($users) = mysql_fetch_row($result);  # Number of users

$query = "SELECT COUNT(poem_id) FROM poems";
$result = mysql_query($query);
list($poems) = mysql_fetch_row($result);  #  Number of poems

$ppu = $poems / $users;                   # $ppu = $poems per user

$query = "SELECT user_name, ammount FROM users ORDER BY ammount DESC LIMIT 0,10";
$result = mysql_query($query);
while($topusers = mysql_fetch_array($result)) : # SELECT top 10 users descending with on 1 the user with the most posts.

$query = "SELECT p.*, c.* FROM poems p INNER JOIN categories c ON p.category_id = c.category_id ORDER BY c.ammount DESC LIMIT 0,10";
$res = mysql_query($query);
while($categories = mysql_fetch_array($res)) :  #SELECT top 10 categories descending with on 1 the category with the most posts.
?>
<html dir="ltr">
    <head>
        <title>STATISTIEKEN</title>
        <link href="../../standard.css" type="text/css" rel="stylesheet" />
<link href='dichtkunst.css' type='text/css' rel="stylesheet" />
    </head>
    <body>
        <table class="look" height="100%" width="100%" border="1">
            <tbody>
                <tr>
                    <td width="10%" height="100%"><iframe class="frames" src="../../functieknoppen/functieknoppendichtkunst.htm" frameborder="0" width="100%" height="100%" scrolling="no"></iframe>
                    </td>
                    <td><center>
<table border="0"><tr><td colspan="2"><b>STATISTIEKEN</b></td></tr>
<tr><td>Aantal gebruikers:</td><td><b><?php echo $users; ?></b></td></tr>
<tr><td>Aantal gedichten:</td><td><b><?php echo $poems; ?></b></td></tr>
<tr><td>Aantal gedichten per gebruiker:</td><td><b><?php echo $ppu; ?></b></td></tr>
<tr><td>Top 10 gebruikers:</td><td><b><?php echo "<ul><li>{$topusers['user_name']} : {$topusers['ammount']}</li></ul>"; ?></b></td></tr>
<tr><td>Top 10 categorien:</td><td><b><?php echo "<ul><li>{$categories['category_name']} : {$categories['ammount']}</li></ul>"; ?></b></td></tr>
</table>
                    </center></td>
                </tr>
            </tbody>
        </table>
    </body>
</html>
<?php
endwhile;
endwhile;
?>

Link to comment
Share on other sites

 

Try this:

<?php
#leden
#gedichten
#gedichten / lid
#top 10 posters
#top 10 categories
#...
/*DATABASE SETTINGS */

$query = "SELECT COUNT(user_id) FROM users";
$result = mysql_query($query);
list($users) = mysql_fetch_row($result);  # Number of users

$query = "SELECT COUNT(poem_id) FROM poems";
$result = mysql_query($query);
list($poems) = mysql_fetch_row($result);  #  Number of poems

$ppu = $poems / $users;                   # $ppu = $poems per user

$query = "SELECT user_name, ammount FROM users ORDER BY ammount DESC LIMIT 0,10";
$result = mysql_query($query);
while($topusers = mysql_fetch_array($result)) { # SELECT top 10 users descending with on 1 the user with the most posts.

$query = "SELECT p.*, c.* FROM poems p INNER JOIN categories c ON p.category_id = c.category_id ORDER BY c.ammount DESC LIMIT 0,10";
$res = mysql_query($query);
while($categories = mysql_fetch_array($res)) {  #SELECT top 10 categories descending with on 1 the category with the most posts.
?>
<html dir="ltr">
    <head>
        <title>STATISTIEKEN</title>
        <link href="../../standard.css" type="text/css" rel="stylesheet" />
<link href='dichtkunst.css' type='text/css' rel="stylesheet" />
    </head>
    <body>
        <table class="look" height="100%" width="100%" border="1">
            <tbody>
                <tr>
                    <td width="10%" height="100%"><iframe class="frames" src="../../functieknoppen/functieknoppendichtkunst.htm" frameborder="0" width="100%" height="100%" scrolling="no"></iframe>
                    </td>
                    <td><center>
<table border="0"><tr><td colspan="2"><b>STATISTIEKEN</b></td></tr>
<tr><td>Aantal gebruikers:</td><td><b><?php echo $users; ?></b></td></tr>
<tr><td>Aantal gedichten:</td><td><b><?php echo $poems; ?></b></td></tr>
<tr><td>Aantal gedichten per gebruiker:</td><td><b><?php echo $ppu; ?></b></td></tr>
<tr><td>Top 10 gebruikers:</td><td><b><?php echo "<ul><li>{$topusers['user_name']} : {$topusers['ammount']}</li></ul>"; ?></b></td></tr>
<tr><td>Top 10 categorien:</td><td><b><?php echo "<ul><li>{$categories['category_name']} : {$categories['ammount']}</li></ul>"; ?></b></td></tr>
</table>
                    </center></td>
                </tr>
            </tbody>
        </table>
    </body>
</html>
<?php
}}
?>

See if the old fashion way works.

Link to comment
Share on other sites

sorry, but can anybody help me? it's quit urgent...

 

 

I just did a test and the reason why the method of while loops you are using isn't working is because you can't have loops inside loops using that method. Below is an example of the bug.

<?
$i = 1;
$a = 5;
while ($a <= 10) :
$a++;
echo"test<br>";
while ($i <= 10) :
    echo $i."<br>";
    $i++;
endwhile;
endwhile;
?>

Depending on what the documentation describes the usage of this style of loop, perhaps is should be reported as a bug under bugs.php.net

Link to comment
Share on other sites

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.