displaying records in a table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
naturepoonam
Forum Newbie
Posts: 3
Joined: Sat Oct 29, 2011 1:20 pm

displaying records in a table

Post by naturepoonam »

Hi,

I have this code it displays records from the table in 2 columns

So right now the records are displayed in table like this

Code: Select all

<table>
<tr>
<td></td>
<td></td>
</tr>
</table>

I want to show 500 records in first and second table cell and 500 records in other 2 table cell so then i want my table to look like this

Code: Select all

<table>
<tr>
<td>first 500 records</td>
<td>first 500 records</td>
<td>Other 500 records</td>
<td>Other 500 records</td>
</tr>
</table>

Here is my actual code

Code: Select all

<?php
$db_host = 'mysql12.abc.com';
$db_user = 'test1';
$db_pwd = 'Test123';

$database = 'testdatabase';
$table = 'pricing';

if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");

if (!mysql_select_db($database))
die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";

// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";

echo "</tr>\n";
}
mysql_free_result($result);
echo "</table>\n";
?>
Last edited by Benjamin on Sat Oct 29, 2011 9:12 pm, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: displaying records in a table

Post by califdon »

I'm afraid you haven't told us what your question is. We don't just write code for people. If you have tried to do it and you had a problem, tell us what the problem is. Also, I don't understand what you are even trying to do. "500 records" doesn't mean much when we have no idea what is in each record. Trying to display 500 records of anything in one <td> is probably not going to look very good.
naturepoonam
Forum Newbie
Posts: 3
Joined: Sat Oct 29, 2011 1:20 pm

Re: displaying records in a table

Post by naturepoonam »

IHi,

I have a table called pricing. It has 2 fields called amount and premium

There are 1000 records in the table

So the way i wan it to be displayed is like this

Code: Select all

<table>
<tr>
<td class="1">Amount</td>
<td class="1">Premiun</td>
<td>Amount</td>
<td>Premiun</td>
</tr>
</table>

The first 500 records will be displayed in table cell with class 1

thanks for your help

poonam
Last edited by Benjamin on Sat Oct 29, 2011 10:27 pm, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: displaying records in a table

Post by califdon »

naturepoonam wrote:IHi,

I have a table called pricing. It has 2 fields called amount and premium

There are 1000 records in the table

So the way i wan it to be displayed is like this

Code: Select all

<table>
<tr>
<td class="1">Amount</td>
<td class="1">Premiun</td>
<td>Amount</td>
<td>Premiun</td>
</tr>
</table>

The first 500 records will be displayed in table cell with class 1

thanks for your help

poonam
So you have 4 columns in each row and you want to display the word "Amount" in the first and third columns, and the word "Premium" in the second and fourth columns?? That's what you will get with that code. I'm sorry, but I just can't see what you expect your output to look like.

But even before the layout issues, this sounds like you are doing something with a database that maybe should be simply a formula.
Post Reply