Jump to content


Photo

Creating a list from database


  • Please log in to reply
7 replies to this topic

#1 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 06 July 2006 - 04:32 PM

In my database I have the table market that contains the fields:

id(a unique field for the table)
sellerid(contains the selling members id)
qty(Number being sold)
price(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?

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 06 July 2006 - 04:39 PM

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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 06 July 2006 - 04:41 PM

Thats pretty simple:
// 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>';

Thats the basic code. Its untested but it should work.

#4 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 06 July 2006 - 04:41 PM

<?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>");
};
?>

Orio.
Think you're smarty?

(Gone until 20 to November)

#5 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 06 July 2006 - 05:36 PM

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

$result=mysql_query("SELECT * FROM mmarket ORDER BY price");

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


Once again guys - thanx  ;)

#6 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 06 July 2006 - 05:39 PM

Nevermind - it does it the way I want it automatically lol

#7 cmgmyr

cmgmyr
  • Members
  • PipPipPip
  • Advanced Member
  • 1,278 posts
  • LocationUSA

Posted 06 July 2006 - 05:43 PM

Ascending
$result=mysql_query("SELECT * FROM mmarket ORDER BY price ASC");

Descending
$result=mysql_query("SELECT * FROM mmarket ORDER BY price DESC");


#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 06 July 2006 - 05:44 PM

but just so you know, it would be like so:

select * from mmarket order by price asc
select * from mmarket order by price desc

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users