• Log in or sign up for an account here: https://www.sythe.org/login

Help with creating a table in php

TheKnight01

Newcomer
Joined
Dec 16, 2009
Sythe Gold
0
Messages
2
I was wondering if anyone can give me advice or point me to the right direction on how to code a table that can be edited via a registered user.
For example, if the columns were "Rank", "Name", "Total"

"Rank" is a positive real number(1,2,3..)
"Name" is just a name a user has added to the table
"Total" is also a positive real number which a registered user can edit by selecting the name then changing the value/number

I'm also thinking of having this table arrange the list of names depending on the column "Total". So if Name Three has more total than Name Two, then name three should be automatically ahead of name two in the table.

Finally i want this list published on an html page that can be viewed by anyone.

Sorry if i make it sound complicated but i really hope you get the idea. Can you help me with getting started please?
 
Very well, I'll update this post with the finished snippet.

<?php

$db_hostname = "localhost"; //Host
$db_username = ""; //Username
$db_pass = ""; //Password
$db_name = ""; //Database Name
$tbl_name = ""; //Name of the table to select from
$dbc = mysql_connect($db_hostname, $db_username, $db_pass) or die('Could not connect to mysql server.');
mysql_select_db($db_name, $dbc) or die('Could not select database.');

if ($_GET['sort'] == "total") {
$sort = "Total";
}
else {
$sort = "Rank";
}

if ($_GET['org'] == "asc") {
$by = "ASC";
$tbl = "desc";
}
else {
$by = "DESC";
$tbl = "asc";
}
?>

<table>
<tr>
<td><a href="?sort=rank&org=<?= $tbl; ?>">Rank</a></td><td>Name</td><td><a href="?sort=total&org=<?= $tbl; ?>">Total</a></td>
</tr>
<?php
$sql = "SELECT * FROM `$tbl_name` ORDER BY `$sort` $by";
$result = mysql_query($sql) or die('The error was: ' . mysql_error() . '<br />The query was: ' . $sql);

while($row = mysql_fetch_assoc($result)) {
?>

<tr>
<td><?= $row['Rank']; ?></td><td><?= $row['Name']; ?></td><td><?= $row['Total']; ?></td>
</tr>

<?php
}
?>
</table>
 
Thanks alot Deacon!:) Last question, how can i make the column rank automatically output numbers to the column? Because in the output screen, this is what i get:

Rank Name Total
2 Name Two 10
1 Name One 5

It did what i wanted(arrange the list according to column Total, but it also moves the value in Rank.

This is how i want it to look like:
Rank Name Total
1 Name Two 10
2 Name One 5

Let's say i want 50 rows, which is 50 numbers, so i want column Rank to print 1-50, and leave them unchanged as the list changes order according to column Total.
 
I updated the code with some links and a few extra variables to make it a bit more dynamic.

Now, you can click the text "Rank" or "Total" to sort by asc, or desc. :)
 
Runescape Gamblingrunescape gambling
Back
Top