Current location: Hot Scripts Forums » Programming Languages » PHP » Create dynamic rows and columns in html using data from MYSQL table

Create dynamic rows and columns in html using data from MYSQL table

Reply
  #1 (permalink)  
Old
Newbie Coder
 
Join Date: Jan 2009
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
Create dynamic rows and columns in html using data from MYSQL table

Hi, I am trying to create rows and columns for some data but it is misbehaving, ie, creating one row with 6 columns instead of 'n' rows with only three columns each. Any help will be greatly appreciated. Here is the code:

<? $query = "SELECT * FROM friends WHERE friendid = '".$_SESSION['user_id']."' AND allow = 1 ORDER BY first_name ASC";
$result = mysql_query($query) or die ("Query failed");

//get the number of rows in our result so we can use it in a for loop
$numrows = (mysql_num_rows ($result));

echo "<table width = 100% border = '0' cellspacing = '2' cellpadding = '0'>";

// loop to create rows
if($numrows >= 1){
for ($r = 0; $r <= $numrows; $r++) {
echo "<tr>";

// loop to create columns
while ($friendList = mysql_fetch_array($result)) {

for ($c = 0; $c <= 2; $c++) {

echo "<td><a href='memberindex.php?id = ".$friendList['id']."'><img src='".$friendList['friendImg']."' title='".$friendList['first_name']."' alt='".$friendList['first_name']."'/><br />".$friendList['first_name']."</a><br /></td> "; }}
echo "</tr> "; }}

<? echo "</table> "; ?>
Reply With Quote

Featured Marketplace Scripts (view more)

  #2 (permalink)  
Old
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
PHP Code:

<?php

 $query 
"SELECT * FROM friends WHERE friendid = '".$_SESSION['user_id']."' AND allow = 1 ORDER BY first_name ASC";
$result mysql_query($query) or die ("Query failed");

echo 
"<table width = 100% border = '0' cellspacing = '2' cellpadding = '0'>";

// loop to create rows
if(mysql_affected_rows() > 0){
while (
$friendList mysql_fetch_array($result)) {

echo 
"<tr>"
"<td><a href='memberindex.php?id = ".$friendList['id']."'><img src='".$friendList['friendImg']."' title='".$friendList['first_name']."' alt='".$friendList['first_name']."'/><br />".$friendList['first_name']."</a><br /></td> " 
"</tr> ";
}

echo 
"</table> "?>
Reply With Quote
  #3 (permalink)  
Old
Newbie Coder
 
Join Date: Jan 2009
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
create dynamic rows and columns in html using data from mysql table

thanks a lot. I will check it out and let you know what happens.
Reply With Quote
  #4 (permalink)  
Old
Newbie Coder
 
Join Date: Jun 2009
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
a little sloppier, but should work.

<?
$query = "SELECT * FROM friends WHERE friendid = '".$_SESSION['user_id']."' AND allow = 1 ORDER BY first_name ASC";
$result = mysql_query($query) or die ("Query failed");

//get the number of rows in our result so we can use it in a for loop
$numrows = (mysql_num_rows ($result));


// loop to create rows
if($numrows >0){
echo "<table width = 100% border = '0' cellspacing = '2' cellpadding = '0'>";
// loop to create columns
$position = 1;
while ($friendList = mysql_fetch_array($result)){
if($position == 1){echo "<tr>";}
echo " <td><a href='memberindex.php?id=".$friendList['id']."'><img src='".$friendList['friendImg']."' title='".$friendList['first_name']."' alt='".$friendList['first_name']."'/><br />".$friendList['first_name']."</a><br /></td> ";
if($position == 3){echo "</tr> "; $position = 1;}else{ $position++;}
}//while

$end = "";
if($position != 1){
for($z=(3-$position); $z>0 ; $z--){
$end .= "<td></td>";
}
$end .= "</tr>";
}

echo $end."</table> ";
}//if
?>
Reply With Quote
  #5 (permalink)  
Old
Newbie Coder
 
Join Date: Jan 2009
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
kodekin

thanks a lot kodekin. I will use yours on another site as i got mine to finally work. Your assistance is much appreciated
Reply With Quote
  #6 (permalink)  
Old
New Member
 
Join Date: Sep 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
how to display in pagination

Hi, i m begginer in php and this webiste make me a lot os help to understodo basic script, now i m making an assignment, i wat display record in 3 column and pagination also. please can anybody help me
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Search engine [NEED HELP] HS Holiday PHP 22 10-26-08 10:42 AM
mysql data in html table zoliky PHP 2 02-14-08 03:21 PM
How to Display Data in Columns php-learner PHP 3 02-17-06 03:45 AM
Print MySQL resultset to five columns HTML-table morrowind PHP 1 05-03-04 08:13 AM


All times are GMT -5. The time now is 01:23 PM.
vBulletin® Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.