NameSilo

PHP: Putting returned Mysql data in individual table cells

Spaceship Spaceship
Watch

whyme953

Established Member
Impact
1
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?
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
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:
<?

$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++){
		$cell_count++;
		if($cell_count<=$number_of_cells){
			echo $display_count+$j." ";
		}else{
			echo "X ";
		}
	}
	$display_count=$display_count+$j-1;
	$row_count++;
	echo "end of row\n";
}

?>

Lux
 
0
•••
PHP:
//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
}
?>
 
0
•••
compuXP said:
PHP:
//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
}
?>

That won't actually work the way the OP described.

Lux
 
0
•••
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:
<?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>";
 }
 // 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>");
 }
?>

Have fun! ;)
 
0
•••
Actually, lux, it works fine :) Just tweak that code a bit to fit your needs.
 
0
•••
compuXP said:
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 :D

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.
 
0
•••
It is the OP's job to explain his problem CLEARLY.
 
0
•••
Might I suggest not using multiple <td>'s... but rather... <div>'s?

PHP:
// setup the database query...
$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. :hehe:

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:
0
•••
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
 
0
•••
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:
	$result = mysql_query ($query);
	
	echo ('<table border="1">');
	
	
	$total_records = mysql_num_rows($result);
	
	$cell_max=4;
	$records = 1;
	$cell_num=1;
	

	while ($returned_row = mysql_fetch_array($result, MYSQL_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>ย </td>");
				}
				
			}
			if ($cell_num==$cell_max){ 
				echo ('</tr>');
				$cell_num=1; //resets $cols after every 3
			}
			
		}// end if records
			
	}// end of while loop

		echo ('</table>');
 
Last edited:
0
•••
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
 
0
•••
luxinterior said:
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.
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.
 
0
•••
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
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back