Jump to content

Creating a list from database


Recommended Posts

In my database I have the table [b]market[/b] that contains the fields:

[b]id[/b](a unique field for the table)
[b]sellerid[/b](contains the selling members id)
[b]qty[/b](Number being sold)
[b]price[/b](how much its on the market for)

I need two things,

Firstly, I need to create the php script to show the list

and secondly, next to each row in the list a hyperlink to seller.php?id=??  (where ?? is the id of the row in the database)

Can it be done?
Link to comment
Share on other sites

This should work mate, you also need to make the connection to database but i'm taking it that you have already done the connection part.

$result=mysql_query("SELECT * FROM market");
while ($row=mysql_fetch_array($result)) {
echo "$row[sellerid], $row[qty], $row[price] <a href='seller.php?id=$row[id]>the link</a><Br>";

Just basic, you can add formatting yourself :)

Link to comment
Share on other sites

Thats pretty simple:
[code=php:0]// connect to db here

$sql = 'SELECT * FROM market';
$result = mysql_query($sql);

echo '<table border="0" cellpadding="0" cellspacing="0">';
echo '<tr><th>id</th>';
echo '<th>Seller id</th>';
echo '<th>Price</th>';
echo '<th>Qty</th>';

while ($row = mysql_fetch_row($result))
    echo '<tr><td>' . $row['id'] . '</td>';
    echo '<td><a href="seller.php?id=' . $row['sellerid'] . '">Seller id</a></td>';
    echo '<td>' . $row['price'] . '</td>';
    echo '<td>' . $row['qty'] . '</td></tr>';

echo '</table>';[/code]

Thats the basic code. Its untested but it should work.
Link to comment
Share on other sites

//connect to db here
$query="SELECT * FROM `market`";
echo("------------------<br><strong>ID: </strong>".$row['id']."<br><strong>Seller-ID: </strong>".$row['sellerid']."<br><strong>Quantity: </strong>".$row['qty']."<br><strong>Price: </strong>".$row['price']."<br><strong>Link: <a href=\"seller.php?id=".$row['id']."\">Click here</a><br><br>");

Link to comment
Share on other sites

Work great guys - thank you.

Just one other small thing I would like to know,

I want to sort the list by price, how do I go about that?

I've got as far as this line, adding order by...

[b]$result=mysql_query("SELECT * FROM mmarket ORDER BY price");[/b]

...but how do I specify it to be ordered ascending or decending?

Once again guys - thanx  ;)
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.

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.