PHP MySQL HTML generating tables... after..fetching recods

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
Hi,

I am trying to generate an HTML table from a MySQL table using PHP

Code:
      $arr = mysql_fetch_rowsarr($result);
      $num_results = mysql_num_rows($result);
      for ($i=0; $i < $num_results; $i++) 
      {
      $d=explode("-",$arr[$i+1]["dob"]);
      $dd=$d[2];
      $mm=$d[1];
      $yy=$d[0];
      $d=$mm.'/'.$dd.'/'.$yy;
      $age=round(dateDiff("/", date("m/d/Y", time()), $d)/365, 0) ;
        echo '<tr> <td align="center">'.$arr[$i]['name'].'</td>
        <td align="center">'.$age.'</td>
        <td align="center"><div STYLE="word-wrap: break-word">'.$row['add'].'</div></td>
        <td align="center">'.$arr[$i]['dis'].'</td>
        <td align="center">'.$arr[$i]['divi'].'</td>
        <td align="center">'.$arr[$i]['phone'].'</td>
        <td align="center">'.$arr[$i]['email'].'</td> </tr>';
     }

$age is just to calculate age from every record entry
the code gives no output...i.e. no record is fetched from tables

thanks for ne help
 

LHVWB

New Member
Messages
1,308
Reaction score
0
Points
0
Please do not PM me for support if you have already posted, please be patient.

I think that maybe you should use the "mysql_fetch_array()" function instead of "mysql_fetch_rowsarr". ;)
 
Last edited:

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
Sorry for that...

I tried mysql_fetch_array

ok..i modified the code a little bit ..now what it did was repeat the same row again..instead of giving the next row from the resultset

Code:
 $arr = mysql_fetch_array($result);
      $num_results = mysql_num_rows($result);
      for ($i=0; $i < $num_results; $i++) 
      {
      $d=explode("-",$arr["dob"]);
      $dd=$d[2];
      $mm=$d[1];
      $yy=$d[0];
      $d=$mm.'/'.$dd.'/'.$yy;
      $age=round(dateDiff("/", date("m/d/Y", time()), $d)/365, 0) ;
        echo '<tr> <td align="center">'.$arr['name'].'</td>
        <td align="center">'.$age.'</td>
        <td align="center"><div STYLE="word-wrap: break-word">'.$arr['add'].'</div></td>
        <td align="center">'.$arr['dis'].'</td>
        <td align="center">'.$arr['divi'].'</td>
        <td align="center">'.$arr['phone'].'</td>
        <td align="center">'.$arr['email'].'</td> </tr>';
     }

The problem here is how to move to the next record in the resultset ??
 
Last edited:

marshian

New Member
Messages
526
Reaction score
9
Points
0
Possible errors:
- mysql_fetch_rowsarr: not found on php.net
- dateDiff: not found on php.net

Tips:
- processing each row of a result is easier with
PHP:
while($row = mysql_fetch_array($result))
- use the forum code
PHP:
 instead of [code]

Try this code:
[php]
while($onerow = mysql_fetch_array($result)) {
$d=explode("-",$onerow["dob"]);
$dd=$d[2];
$mm=$d[1];
$yy=$d[0];
$d=$mm.'/'.$dd.'/'.$yy;
$dd=(int)$dd;
$mm=(int)$mm;
$yy=(int)$yy;
$time = time();
//Note: this is if you're using a 4-digit year (yyyy), not a 2-digit year!
$age= ((int) date("Y", $time))-$yy;
if($mm > ((int) date("n", $time))) {
	$age--;
} elseif ($mm == ((int) date("n", $time))) {
	if($dd > ((int) date("j", $time))) {
		$age--;
	}
}
echo '<tr> <td align="center">'.$onerow['name'].'</td>
<td align="center">'.$age.'</td>';
//You must have defined a $row['add'] here!
echo '
<td align="center"><div STYLE="word-wrap: break-word">'.$row['add'].'</div></td>
<td align="center">'.$onerow['dis'].'</td>
<td align="center">'.$onerow['divi'].'</td>
<td align="center">'.$onerow['phone'].'</td>
<td align="center">'.$onerow['email'].'</td> </tr>';
}

Hope this was helpful,

Marshian
 

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
buddy , i am succesful in getting the record now.. from the code i mentioned in the previous post

but the problem is i cannot go to the next record in the resultset :(
how to navigate to the next record in the resultset using a for loop or nething


and can u tell me in case my table has got too many records from the table how can increase the page height along with it ??
or can i make the table scrollable ??
 
Last edited:

marshian

New Member
Messages
526
Reaction score
9
Points
0
Each time you call mysql_fetch_array($result), the cursor is moved to the next row.
An alternative is using mysql_data_seek()
 

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
thanks man....
i put that line inside the for loop...

there is one last question ..

and can u tell me in case my table has got too many records from the table how can increase the page height along with it ??
or can i make the table scrollable ??
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
add "overflow-y: scroll;" to the css of the table and it will make a scrollbar if the table is too long to fit in the defined box.
(a html page's length doesn't have to be enlarged if that's what you're asking)
 

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
wat box ??..it is just a table in a page
 
Last edited:

marshian

New Member
Messages
526
Reaction score
9
Points
0
if you don't specify a height (which makes the box), the y will never overflow, since the table can be as high as he wants
 

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
gr8 thanks
repped...you are a sweet marshian ..i bet there are humans ther too...:D

thanks a lot for your help

btw how can i close this thread...read a post by some MOD a day before..

it is not working ??

:(

Code:
<style type="text/css">
  table {
table-layout:fixed;
width:100%;
border:1px solid #f00;
word-wrap:break-word;
overflow-y: scroll;
}
 </style>


gr8 i got a scroll bar using this code

in CSS:

div.scroll {overflow: auto; width: 100%; height: 100%;}

In table DIV tag :

class="scroll"




not working :(
 
Last edited:

phpasks

New Member
Messages
145
Reaction score
0
Points
0
HTML:
<style type="text/css">
  table {
table-layout:fixed;
width:100%;
height:100%
border:1px solid #f00;
word-wrap:break-word;
overflow-y: scroll;
}
 </style>

I think your problem solved.

Asif
http://www.phpasks.com
 
Top