Jump to content

Creating a list from database


master82

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
https://forums.phpfreaks.com/topic/13863-creating-a-list-from-database/
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.


<?php
$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 :)


Regards
Liam
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.
[code]<?php
//connect to db here
$query="SELECT * FROM `market`";
$result=mysql_query($query);
while($row=mysql_fetch_array){
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>");
};
?>[/code]

Orio.
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  ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.