Jump to content


Photo

I have a question


  • Please log in to reply
6 replies to this topic

#1 Renlok

Renlok
  • Members
  • PipPipPip
  • Advanced Member
  • 258 posts
  • Locationnewbury

Posted 21 October 2006 - 07:52 PM

If you have a database with a number of values in it and then if you take them from the database with php how would you have it so you have a table showing each value in the database, showing number that each particular value has been entered.

Thanks for any help.

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 21 October 2006 - 08:39 PM

you lost me with the part about "showing number that each particular value has been entered", but i'm guessing you simply mean echoing them in chronological order. first off, you need to have an id column that is auto_incremented that you can then reference to see what order they are in. once that is in place, just do the following:
<?php
$sql = mysql_query("SELECT * FROM myTableName ORDER BY id");
echo "<table>\n";
if (mysql_num_rows($sql) > 0) {
  while ($x = mysql_fetch_array($sql)) {
    echo "<tr>\n";
    echo "<td>$x[id]</td>\n";
    echo "<td>$x[value]</td>\n";
    echo "</tr>\n";
  }
} else {
  echo "<tr><td>No records returned</td></tr>\n";
}
echo "</table>\n";
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Renlok

Renlok
  • Members
  • PipPipPip
  • Advanced Member
  • 258 posts
  • Locationnewbury

Posted 21 October 2006 - 09:01 PM

oh no i now how to do that, sorry for not making it clear with "showing number that each particular value has been entered"
Ok urm heres an example of what i what it to do:
The Database table:

ID          ¦        Name
1          ¦          John
2          ¦          Mike
3          ¦          Joe
4          ¦          Mike
5          ¦          John
6          ¦          John

That should then show:

Rank  ¦  Name  ¦  Enties
--------------------------
1        ¦  John    ¦    3
2        ¦  Mike    ¦    2
3        ¦  Joe      ¦    1

Hope thats clearer

#4 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,891 posts
  • LocationCanada

Posted 21 October 2006 - 09:23 PM

<?php
<?php
$sql = mysql_query("SELECT COUNT('ID') as Number, Name
    FROM myTableName
    GROUP BY Name
    ORDER BY number DESC");
echo "<table>\n";
if (mysql_num_rows($sql) > 0) {
    echo "<tr>\n";
    echo "<td>Rank</td>\n";
    echo "<td>Name</td>\n";
    echo "<td>Entries</td>\n";
    echo "</tr>\n";
  $count = 0;
  while ($x = mysql_fetch_array($sql)) {
    $count++;
    echo "<tr>\n";
    echo "<td>$count</td>\n";
    echo "<td>$x[Name]</td>\n";
    echo "<td>$x[Number]</td>\n";
    echo "</tr>\n";
  }
} else {
  echo "<tr><td>No records returned</td></tr>\n";
}
echo "</table>\n";
?>
?>

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#5 Renlok

Renlok
  • Members
  • PipPipPip
  • Advanced Member
  • 258 posts
  • Locationnewbury

Posted 21 October 2006 - 11:05 PM

ok i changed the code to work with my page
<?php                                                                                           # line 115
                $sql = mysqli_query("SELECT COUNT('ID') as Number, searchterm  
                    FROM search
                        GROUP BY searchterm
                        ORDER BY number");
                    echo "<table>\n";                                                           # line 120
                if (mysqli_num_rows($sql) > 0) {
                    echo "<tr>\n";
                    echo "<td>Rank</td>\n";
                    echo "<td>Search Term</td>\n";
                    echo "<td>Entries</td>\n";                                                  # line 125
                    echo "</tr>\n";
                $count = 0;
                while ($x = mysqli_fetch_array($sql)) {
                    $count++;
                    echo "<tr>\n";
                    echo "<td>$count</td>\n";
                    echo "<td>$x[Searchterm]</td>\n";
                    echo "<td>$x[Number]</td>\n";
                    echo "</tr>\n";
                }
            } else {
                echo "<tr><td>No records returned</td></tr>\n";
            }
            echo "</table>\n";
            ?>                                                                                  # line 140
</center>
    <?php
    mysqli_free_result($result);
    $db->close();
    ?>                                                                                          # line 145

comes out with the errors

Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/renlok/public_html/topsearch.php on line 119

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/renlok/public_html/topsearch.php on line 121
No records returned

Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, null given in /home/renlok/public_html/topsearch.php on line 143



#6 Renlok

Renlok
  • Members
  • PipPipPip
  • Advanced Member
  • 258 posts
  • Locationnewbury

Posted 22 October 2006 - 09:22 AM

??? anyone have anything?

#7 JasonLewis

JasonLewis
  • Members
  • PipPipPip
  • Advanced Member
  • 3,351 posts
  • LocationVictoria, Australia

Posted 22 October 2006 - 12:16 PM

obviously your missing a parameter in your query.
when using mysqli your first parameter must be your mysqli connection.
example:

$link = mysqli_connect("localhost", "user", "password", "db");
$result = mysqli_query($link, "SELECT blah FROM table");

hope this helps.
Good luck with your coding.
Jason / ProjectFear / Jaysonic




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users