Jump to content

print total sum in array


kurtos123

Recommended Posts

 [ code ]<?php
$iKolommen = 5;
$iGamesPerPagina = 60;
$aSpelNamen = array(
  "bratz",
    "yasmine ski dressup",
         );
?> [ / code ]

 

I use above code to determine all games with name bratz and yasmin ski dressup in my database and to print them on my webpage.

 

How can i print the exact number of all the games with those names ?

Can you give me the exact code because i am a newbie in php.

I tried a code but it only gives the number 2 because it counts the names and not the amount.

 

Many thanks for your help in advance.

Link to comment
Share on other sites

figured this out and it works but is there a much shorter way?

 

Figured this out and it works, but is there a much shorter way ?

 

<?php
                           $sWhere = "";
                            $i = 0;
                            foreach($aSpelNamen AS $sSpelNaam)
                            {
                              $sWhere .= "name LIKE '%" . $sSpelNaam . "%'";
                              if(++$i < count($aSpelNamen))
                              {
                                $sWhere .= " OR ";
                              }
                            }

                            $rQuery = mysql_query("SELECT id FROM " . $pre . $sqlgame . " WHERE (" . $sWhere . ")");
                            $iCount = mysql_num_rows($rQuery);
                            ?>                
                             bratz spellen (<?php echo $iCount; ?>)
Link to comment
Share on other sites

Tried inplementing your code Barand but i am sorry it did not give any result. I need to print on my webpage the total number of games with the name "Bratz" and "yasmine ski dressup" which are in my mysql database.  (SpelNamen means GameNames, SpelNaam means GameName).

 

 

<?php
                           $sWhere = "";
                            $i = 0;
                            foreach($aSpelNamen AS $sSpelNaam)
                            {
                              $sWhere .= "name LIKE '%" . $sSpelNaam . "%'";
                              if(++$i < count($aSpelNamen))
                              {
                                $sWhere .= " OR ";
                              }
                            }

                            $rQuery = mysql_query("SELECT COUNT(id) as total FROM " . $pre . $sqlgame . " WHERE (" . $sWhere . ")");
                            $iCount = mysql_num_rows($rQuery);
                            ?>                
                             bratz games (<?php echo $iCount; ?>)
Link to comment
Share on other sites

As I said in my reply, the query will return one row with the required count.

 

You need to get the result from the total field returned in the query, not count the rows.

$rQuery = mysql_query("SELECT COUNT(id) as total FROM " . $pre . $sqlgame . " WHERE (" . $sWhere . ")");
$row = mysql_fetch_assoc($rQuery);
$iCount = $row['total'];

Stop using the mysql_ functions (they will not be available in future versions of php) and start using mysqli_ functions or PDO.

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.