Jump to content


Printing records from a mySQL database in PHP


6 replies to this topic

#1 jakai

    Young Padawan

  • Members
  • Pip
  • 4 posts
  • Gender:Male

Posted 18 March 2007 - 05:54 AM

Hello, the topic sounds much more easy then it is. Im making a gallery for a page.
The trick is that I want to have 4 records per row. I have tried with div's but the css seems to work different with every damn browser. So then I figured the only way out was tables. and I got to this conclusion:

<table width="530" border="0" cellspacing="0" cellpadding="0" >

	<?php								
 
 $i = 1;

while($row = mysql_fetch_array($result))
 
  {
 
 
  if ( $i==1 ) {

  echo "<tr><td>" . $row['Test'] . "</td>";
  $i++;
  }
  else if ( $i == 2 ) {
  echo "<td>" . $row['Test'] . "</td>";
  $i++;
  }
   else if ( $i == 3 ) {
  echo "<td>" . $row['Test'] . "</td>";
  $i++;
  }
 else if ($i == 4 ) {
 echo "<td>" . $row['Test'] . "</td></tr>";
 $i = 1;

}

else {}

}
mysql_close($con);
?>

</table>

This works until it's less than 3 records left to print or just one record to print. So thats where im stuck.

Thanks ;)

#2 jakai

    Young Padawan

  • Members
  • Pip
  • 4 posts
  • Gender:Male

Posted 18 March 2007 - 06:32 AM

View Post. Adam ., on Mar 18 2007, 01:04 PM, said:

Why not try:

<table width="530" border="0" cellspacing="0" cellpadding="0" >

<?php

$i = 1;

while($row = mysql_fetch_array($result))

{


if ( $i == 1 ) {

echo "<tr><td>" . $row['Test'] . "</td>";
$i = 2;
}
else if ( $i == 2 ) {
echo "<td>" . $row['Test'] . "</td>";
$i = 3;
}
else if ( $i == 3 ) {
echo "<td>" . $row['Test'] . "</td>";
$i = 4;
}
else if ($i == 4 ) {
echo "<td>" . $row['Test'] . "</td></tr>";
$i = 1;

}
}
}
mysql_close($con);
?>

</table>

Not too sure, to early for me lol ;)

- Adam B)


Thanks Adam, but im pretty sure this would have the same problem. the problem is that if i have less than 3 records it don't have a "</tr>" at the end which ends the row. It works without it, but i want a correct way.

Edited by jakai, 18 March 2007 - 06:35 AM.


#3 Av-

    I Feel Left Out

  • Members
  • PipPipPipPip
  • 1,971 posts
  • Gender:Male
  • Location:10 ft. below sea level

Posted 18 March 2007 - 09:28 AM

Try this

<?php
$i = 1;
print('<table><tr>');
while($row = mysql_fetch_array($result)){
	print('<td>'. $i .'</td>'. (($i%3 == 0) ? '</tr><tr>' : ''));
	$i++;
}
print('</tr></table>');
?>


#4 jakai

    Young Padawan

  • Members
  • Pip
  • 4 posts
  • Gender:Male

Posted 18 March 2007 - 10:07 AM

View PostAv-, on Mar 18 2007, 04:28 PM, said:

Try this

<?php
$i = 1;
print('<table><tr>');
while($row = mysql_fetch_array($result)){
	print('<td>'. $i .'</td>'. (($i%3 == 0) ? '</tr><tr>' : ''));
	$i++;
}
print('</tr></table>');
?>

Thanks so much av-!B) This works. But can i ask you what the ? and : means? ;)

Edited by jakai, 18 March 2007 - 10:13 AM.


#5 bay

    Young Padawan

  • Members
  • Pip
  • 105 posts
  • Gender:Male
  • Location:Chicago, IL USA

Posted 18 March 2007 - 10:35 AM

its a ternary operator

it shortens if statements: for example

( expression ) ? executute_if_true : execute_if_false;

So for example

<?php

$i = 2;

echo ( $i = 2 ) ? 'true' : 'false';
// will echo 'true'

?>

it is an alternative to

if ( $i = 2 )
{
echo 'true';
}
else
{
echo 'false';
}

its a matter of preference

#6 jakai

    Young Padawan

  • Members
  • Pip
  • 4 posts
  • Gender:Male

Posted 18 March 2007 - 10:39 AM

Thanks! ;)

#7 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 18 March 2007 - 05:42 PM

I would recommend a more dynamic solution, but if that works then that's all that's needed.

Either way, this is a frequently asked question, please use the forum search in the future.
http://www.pixel2life.com/forums/index.php...1858&hl=row





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users