NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page PHP: putting returned mysql data in individual table cells

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
2 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 08-19-2005, 06:54 PM THREAD STARTER               #1 (permalink)
NamePros Member
 
whyme953's Avatar
Join Date: Jun 2004
Posts: 92
whyme953 is an unknown quantity at this point
 



PHP: putting returned mysql data in individual table cells


how do i loop through returned mysql data, putting each element in its own <td> using multiple rows?
i.e. if i have thumbnails which i want displayed three to a row, how do i do the loops?

can somebody please give me some advice on how to do this?
__________________
Bais Menachem
Last edited by whyme953; 08-19-2005 at 07:24 PM.
whyme953 is offline  
Old 08-19-2005, 07:39 PM   #2 (permalink)
Senior Member
 
luxinterior's Avatar
Join Date: Jul 2004
Location: Kizmiaz
Posts: 1,088
luxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of light
 



I just put together a quick example of one way to handle formatting and table layouts. It should be enough to get you on your way.

PHP Code:
<?

$number_of_cells 
9;
$number_of_cells_per_row 4;
$number_of_rows ceil($number_of_cells $number_of_cells_per_row);
$cell_count 0;
$display_count 0;

while(
$row_count!=$number_of_rows){
    
    for(
$j=1;$j<=$number_of_cells_per_row;$j++){
????: NamePros.com http://www.namepros.com/programming/116972-php-putting-returned-mysql-data-individual.html
        
$cell_count++;
        if(
$cell_count<=$number_of_cells){
            echo 
$display_count+$j." ";
        }else{
            echo 
"X ";
        }
    }
    
$display_count=$display_count+$j-1;
    
$row_count++;
????: NamePros.com http://www.namepros.com/showthread.php?t=116972
    echo 
"end of row\n";
}

?>
Lux
luxinterior is offline  
Old 08-19-2005, 07:42 PM   #3 (permalink)
DNOA Member
Join Date: May 2004
Posts: 5,040
mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future
 


Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
PHP Code:
//Before this, make a msyql_query return a value to variable $result.
while ($row = mysql_fetch_array($result))
{
   $value = $row['column_name'];
   //do this as much as necessary to retrieve
   //the data for this particular result.
?>
<tr><td>Put data here.</td></tr>
<?php
}
????: NamePros.com http://www.namepros.com/showthread.php?t=116972
?>
mholt is offline  
Old 08-19-2005, 07:44 PM   #4 (permalink)
Senior Member
 
luxinterior's Avatar
Join Date: Jul 2004
Location: Kizmiaz
Posts: 1,088
luxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of light
 



Originally Posted by compuXP
PHP Code:
//Before this, make a msyql_query return a value to variable $result.
????: NamePros.com http://www.namepros.com/showthread.php?t=116972
while ($row = mysql_fetch_array($result))
{
   $value = $row['column_name'];
   //do this as much as necessary to retrieve
   //the data for this particular result.
?>
<tr><td>Put data here.</td></tr>
<?php
}
?>
That won't actually work the way the OP described.

Lux
luxinterior is offline  
Old 08-19-2005, 08:46 PM   #5 (permalink)
NamePros Regular
 
Zubair1's Avatar
Join Date: Mar 2005
Posts: 921
Zubair1 is a glorious beacon of lightZubair1 is a glorious beacon of lightZubair1 is a glorious beacon of lightZubair1 is a glorious beacon of lightZubair1 is a glorious beacon of light
 


AIDS/HIV
you can use this way together all the information from your table.
just replace the important variables & table/column names to the owns you have or want.
PHP Code:
<?php
 mysql_connect 
("localhost""username""password")
 or die(
"MySQL Error : <b>" mysql_error(); . "</b>");

 
mysql_select_db ("dbname")
 or die(
"MySQL Error : <b>" mysql_error(); . "</b>");

 
// finally we start the query
 
$sql mysql_query("SELECT * FROM tablename");

 while (
$result mysql_fetch_array($sql)) {
 
// Escaping from php mode to html mode
?>

<table widht="90%" align="center" cellpadding="1" cellspacing="1" border="0">

<?php
  
// back to php mode
  
print "<tr>";
  print 
"<td>data :" $result['columnname'] . "</td>";
  print 
"<tr>";
 }
????: NamePros.com http://www.namepros.com/showthread.php?t=116972
 
// Escaping from php mode to html mode
?>
</table>

<?php
 
// back to php mode
 
if (count($result) < 1) {
  die (
"<b>Error : No record are to be found in the database table</b>");
????: NamePros.com http://www.namepros.com/showthread.php?t=116972
 }
?>
Have fun!
__________________
Live Support : Zubair11 [at] hotmail.com
Free SEO Directory! || Free Online TV || Tech Blog
Web Design & Web Development Services || Reliable Web Hosting
Zubair1 is offline  
Old 08-19-2005, 09:24 PM   #6 (permalink)
DNOA Member
Join Date: May 2004
Posts: 5,040
mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future
 


Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
Actually, lux, it works fine Just tweak that code a bit to fit your needs.
mholt is offline  
Old 08-19-2005, 10:03 PM   #7 (permalink)
Senior Member
 
luxinterior's Avatar
Join Date: Jul 2004
Location: Kizmiaz
Posts: 1,088
luxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of light
 



Originally Posted by compuXP
Actually, lux, it works fine Just tweak that code a bit to fit your needs.
With respect, I disagree.

The OP's question was not how to display data in A (as in one) cell but how to display it in a variable number of cells and rows. The example I gave shows a way to display any number of cells in any format you choose.

Sorry to be pedantic but attention to detail is part of being a good programmer

So while your code obviously will work it doesn't address the OP's problem.

Lux

PS: Zubair1 - Your code is wrong for the same reason.
luxinterior is offline  
Old 08-20-2005, 07:36 AM   #8 (permalink)
DNOA Member
Join Date: May 2004
Posts: 5,040
mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future
 


Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
It is the OP's job to explain his problem CLEARLY.
mholt is offline  
Old 08-20-2005, 09:50 AM   #9 (permalink)
Don
Live Chat Operator
 
Don's Avatar
Join Date: Jun 2005
Location: NY
Posts: 881
Don has a brilliant futureDon has a brilliant futureDon has a brilliant futureDon has a brilliant futureDon has a brilliant futureDon has a brilliant futureDon has a brilliant futureDon has a brilliant futureDon has a brilliant futureDon has a brilliant futureDon has a brilliant future
 

Member of the Month
July 2007
Parkinson's Disease Diabetes Breast Cancer Save a Life Animal Cruelty Save a Life Animal Cruelty Cancer Survivorship Breast Cancer Ethan Allen Fund Ethan Allen Fund Ethan Allen Fund Special Olympics Animal Rescue Wildlife Marrow Donor Program Special Olympics Autism Adoption Cancer Survivorship Baby Health Myanmar Relief Alzheimer's Child Abuse AIDS/HIV Cystic Fibrosis Multiple Sclerosis
Might I suggest not using multiple <td>'s... but rather... <div>'s?

PHP Code:
// setup the database query...
????: NamePros.com http://www.namepros.com/showthread.php?t=116972
$query "Your Query Here"
$result mysql_query($query);

// Extract info for each result...
while($row mysql_fetch_array($result)){ 

// give a name to each of the database table fields...
     
$variableone $row["field 1"];
    
$variabletwo $row["field 2"];
    
$variablethree $row["field 3"];

// display the results...
echo "<div class=\"rowdiv\">
<div class=\"tddivone\">
$variableone</div>
<div class=\"tddivtwo\">
$variabletwo</div>
<div class=\"tddivthree\">
$variablethree</div> 
</div>"
;

Just another way to do it.

I'm using the code above on a domain portfolio site that I'm currently working on...

http://www.gdzo.com...

As you can see... you can easily give the illusion of having multiple <tr>'s & <td>'s this way.

Hope this helps.
__________________
`
...
.
Last edited by greatdomainz; 08-20-2005 at 11:23 AM.
Don is offline  
Old 08-20-2005, 11:22 PM THREAD STARTER               #10 (permalink)
NamePros Member
 
whyme953's Avatar
Join Date: Jun 2004
Posts: 92
whyme953 is an unknown quantity at this point
 



thank you very much to all of you who responded.

to make my problem clear: i need multiple rows with multiple objects in each row.
i.e. how do i put every 3 <td>'s within a <tr>??
(my issue is not how to print the <td> tag, its how to make multiple rows)

to luxinterior:
thanx a lot, i tried something using that same basic idea, the problem is that it printed out each record times the total number of records instead of looping through all the records (if there were 3 records it would print each one three times etc.)
i will try fooling around with your code a bit and see if i get anywhere...

once again thanks to everybody for your help
__________________
Bais Menachem
whyme953 is offline  
Old 08-21-2005, 02:49 AM THREAD STARTER               #11 (permalink)
NamePros Member
 
whyme953's Avatar
Join Date: Jun 2004
Posts: 92
whyme953 is an unknown quantity at this point
 



figured it out.
there is probably a easier/simpler/better way than what i did but it does the trick.

(i didnt make a max. num of rows and pagination as i will never have too many records for a page)

here goes:
PHP Code:
    $result mysql_query ($query);
    
    echo (
'<table border="1">');
????: NamePros.com http://www.namepros.com/showthread.php?t=116972
    
    
    
$total_records mysql_num_rows($result);
    
    
$cell_max=4;
    
$records 1;
    
$cell_num=1;
    

    while (
$returned_row mysql_fetch_array($resultMYSQL_ASSOC)){
        
            if  (
$cell_num==1){
                echo (
'<tr>');
            }
        if (
$records <= $total_records){
                    
            echo (
"<td><p>{$returned_row[name]}{$returned_row[img_name]}</p></td>\n");
            
$records++;
            
$cell_num++;
            
            
//put empty td's where needed
            
if (($records $total_records) and ($cell_num $cell_max)){
                for (
$remaining $cell_num$remaining $cell_max$remaining++){
                echo (
"<td>&nbsp;</td>");
                }
                
            }
            if (
$cell_num==$cell_max){ 
                echo (
'</tr>');
                
$cell_num=1//resets $cols after every 3
????: NamePros.com http://www.namepros.com/showthread.php?t=116972
            
}
            
        }
// end if records
            
    
}// end of while loop

        
echo ('</table>'); 
__________________
Bais Menachem
Last edited by whyme953; 08-21-2005 at 03:40 AM.
whyme953 is offline  
Old 08-21-2005, 05:27 AM   #12 (permalink)
Senior Member
 
luxinterior's Avatar
Join Date: Jul 2004
Location: Kizmiaz
Posts: 1,088
luxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of light
 



Glad you got it sorted whyme953, well done! Something else you may want to look into and try to adopt into your normal devlopment process is seperating functionality from design. Theres' loads of reasons to do it so I won't go over old ground. Here's a really useful link that will open your eyes to the idea of templating and I promise you once you get used to it you'll wonder why you didn't use it before.

http://www.massassi.com/php/articles/template_engines/

Take it easy!

Lux
luxinterior is offline  
Old 08-21-2005, 05:58 AM THREAD STARTER               #13 (permalink)
NamePros Member
 
whyme953's Avatar
Join Date: Jun 2004
Posts: 92
whyme953 is an unknown quantity at this point
 



Originally Posted by luxinterior
Something else you may want to look into and try to adopt into your normal devlopment process is seperating functionality from design.
good point, thanks for the advice and link.
????: NamePros.com http://www.namepros.com/showthread.php?t=116972
thats something i've been working on doing lately. if i'm not mistaken (correct me if i'm wrong) the only thing that would apply to in this case is the <table></table> tags, it actualy crossed my mind to fix that, i just got a bit lazy.
__________________
Bais Menachem
whyme953 is offline  
Old 08-21-2005, 06:32 AM   #14 (permalink)
Senior Member
 
luxinterior's Avatar
Join Date: Jul 2004
Location: Kizmiaz
Posts: 1,088
luxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of lightluxinterior is a glorious beacon of light
 



Well what I'd do is use that templating system and load variables. Take out ALL html. Your objective should be to remove as much html as possible. When you have to change the look of your site etc you'll be glad you developed using templates. One of the suggestions above was also pretty good and something I do. Having perhaps a 'row.tpl' with three cells and variables like var1, var2 and var3 then just do a loop loading each variable, build a html string (using the templates) and then sending it to another .tpl. Sounds complicated but once you get the hang of it it's pretty straight forward.

Good luck

Lux
luxinterior is offline  
Closed Thread


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Googlism - What does google think of you? deadserious The Break Room 55 12-15-2005 09:09 AM
Tutorial: How to Install Apache2 MySQL and PHP on Windows deadserious Webmaster Tutorials 35 09-21-2005 09:46 PM
Dedicated Servers from just £45 $90 + [UK] 1 TB Data Transfer P4 3.0GHZ Only £119 blackicehosting Web Hosting Offers 0 07-12-2005 06:24 PM
Tutorial: Getting Started With MySQL (The Basics) deadserious Webmaster Tutorials 3 04-18-2004 01:17 PM

 
All times are GMT -7. The time now is 12:10 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger