Web Hosting Talk







View Full Version : eh...


brcolow
11-30-2002, 05:14 PM
Alrighty, I am trying to list things from a database, look here www.flashstand.com/itemdis.php
As you see, it works ok but it only lists one of the items from the database, how do i loop thru and list them all? this is the current code I am using...

<?
@ $db = mysql_pconnect("localhost", "", "");
if (!$db)
{

echo "<b>Could not connect to database, try again later.</b>";

exit;

}
mysql_select_db("forums");
$result = mysql_query("select * from phpbb_items WHERE type='1'") or die(mysql_error());
$numrows = mysql_num_rows($result);
if ($numrows > 0)
{
$row = mysql_fetch_object($result);;
print"<table cellspacing=\"1\" cellpadding=\"8\" width=\"100%\" BORDERCOLOR=\"c0c0c0\" bordercolordark=\"#c0c0c0\" bordercolorlight=\"#c0c0c0\" border=\"1\">
<tr align=\"left\">
<th>Weapon Name</th>
<th>Picture</th>
<th>Description</th>
<th>Cost</th>
</tr>

<tr align=\"left\">
<td><font size=\"2\">$row->name</td>
<td><font size=\"2\"><img src=\"$row->picture\" width=\"20\" height=\"20\"></td>
<td><font size=\"2\">$row->description</td>
<td><font size=\"2\">$row->cost</td></font>
</tr>";
}
?>

Aralanthir
11-30-2002, 05:17 PM
Try this?

while ($row=mysql_fetch_object($result))
{

block of code here

}

Although usually when I loop, I use the mysql_fetch_array method.

brcolow
11-30-2002, 05:18 PM
No that wont work because I need everything else there to....

brcolow
11-30-2002, 05:21 PM
besides that i already tried it and it leaves blank table cells lol cmon

Lippy
11-30-2002, 05:48 PM
did you have it echo the results at the end of each loop?

brcolow
11-30-2002, 05:51 PM
how do i do that... post the code?

Lippy
11-30-2002, 06:06 PM
I am still rather new to php, and can edit it quite well writting it is another story as I lack experaince. Hopefully some one else can provide you with how an echo statement can be used to show the results you want.

brcolow
11-30-2002, 07:19 PM
bump, cmon people i need help here!

nozol
11-30-2002, 07:37 PM
Here is a modified version of your code that I think will work for you.

<?
@ $db = mysql_pconnect("localhost", "", "");
if (!$db)
{

echo "<b>Could not connect to database, try again later.</b>";

exit;

}
mysql_select_db("forums");
$result = mysql_query("select * from phpbb_items WHERE type='1'") or die(mysql_error());

while ($row = mysql_fetch_array($result))
{
$name=$row["name"];
$pict=$row["picture"];
$desc=$row["description"];
$cost=$row["cost"];
print"<table cellspacing=\"1\" cellpadding=\"8\" width=\"100%\" BORDERCOLOR=\"c0c0c0\" bordercolordark=\"#c0c0c0\" bordercolorlight=\"#c0c0c0\" border=\"1\">
<tr align=\"left\">
<th>Weapon Name</th>
<th>Picture</th>
<th>Description</th>
<th>Cost</th>
</tr>

<tr align=\"left\">
<td><font size=\"2\">$name</td>
<td><font size=\"2\"><img src=\"$pict\" width=\"20\" height=\"20\"></td>
<td><font size=\"2\">$desc</td>
<td><font size=\"2\">$cost</td></font>
</tr>";
}
?>

hope this helps

nozol
11-30-2002, 07:42 PM
echo is very easy, just use 'echo' instead of 'print' above.

brcolow
11-30-2002, 07:55 PM
thanks alot.

nozol
11-30-2002, 07:57 PM
you are welcome. did it work?

jtrovato
11-30-2002, 10:43 PM
Here you go, this should work. I didn't test it due the fact I don't have a datbase to query nor want to create one to test it on. You can do this. I corrected some logical errors you had. I hope you enjoy this


<?
// ********** Connect to MySQL server ********** //
@ $db = mysql_pconnect("localhost", "", "");
if (!$db)
{
echo "<b>Could not connect to database, try again later.</b>";
exit;
}

// ********** Select and Query Database ********** //
mysql_select_db("forums");
$result = mysql_query("SELECT * FROM phpbb_items WHERE type='1'") or die(mysql_error());

// ********** Get the Number of Rows Return From Query ********** //
$numrows = mysql_num_rows($result);

// ********** Create Table ********** //
echo "<table cellspacing=1 cellpadding=8 width=100% BORDERCOLOR=#C0C0C0 bordercolordark=#C0C0C0 bordercolorlight=#C0C0C0 border=1>";
echo "<tr align=left>";
echo "<th>Weapon Name</th><th>Picture</th><th>Description</th><th>Cost</th></tr>";

// ********** Display Query ********** //
for ($i=0;$i<$numrows;$i++)
{
$row = mysql_fetch_object($result);
echo "<tr>"
echo "<td><font size=2>".stripslashes ($row->name)."</font></td>";
echo "<td><font size=2><img src=\"".$row->picture."\" width=20 height=20></font></td>";
echo "<td><font size=2>".stripslashes ($row->description)."</font></td>";
echo "<td><font size=2>".$row->cost."</font></td>";
echo "</tr>";
}
echo "</table>";
?>


have a nice day

jtrovato
11-30-2002, 10:49 PM
I went to your link and noticed that every roq had it's own table. The code above will line everything up nicely. Also if there are no results returned it will only display a header. Post a thread when you try this code so I can see it work?

John

jtrovato
12-02-2002, 01:43 AM
Did the code work for you?