Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying records in 3 columns (horizontally & vertically) 1

Status
Not open for further replies.

WilliamMute

Programmer
Jan 4, 2006
117
Hi,

I've always wondered how this is done and cant seems to grasp it.

When I query my DB, how can I go about displaying each record with its attributes i.e Student one name DOB etc, Then Student 2 Name DOB etc, Student 3 Name DOB etc before creating a new row and repeat the process?

Basically I want to display three records vertically before creating another row for the next three.

Thanks again guys.
 
it's just a question of output logic

Code:
$num_cols = 3;
echo "<table>";
while ($row = mysql_fetch_assoc($result)){
   echo "<tr>";
   for($i=0; $i <$num_cols; $i++;){
      echo "<td>";
      echo $row['somefield'];
      echo "</td>";
      $row = @mysql_fetch_assoc($result);  
      if (!$row){
        for ($j=0; $j < ($num_cols-$i);$j++){
          echo "<td>&nbsp;</td>";
        }
        break(1);
      }
   }
   echo "</tr>";
}
echo "</table>";
 
Hi Justin,

Thanks for that.

I however get this error :

Parse error: parse error, unexpected ';', expecting ')' in /home/fhlinux175/s/seevision.co.uk/user/htdocs/New/pages/test2.php on line 12

Line 12 being:
Code:
for($i=0; $i <$num_cols; $i++;){

Here is another version I was toying around with

Code:
	<?php require_once('Connections/Connection.php');?>
		
		<?php
$sql = "SELECT * FROM videos";
$results = mysql_query($sql) or die("Something Wrong Mate");
$returnedrows = mysql_num_rows($results);
if($returnedrows > 0) {  //
		 $x=0;
print("<table cellspacing=0 cellspacing=0 align=\"center\">");	
while($returns = mysql_fetch_object($results)) {
$x=$x+1;
if ($x % 3 == 0) {
?>
<?php echo $returns['title']; 
//print("<td> $returns ['title']; </td></tr><tr>");
//print("<td><a href=\"[URL unfurl="true"]http://$returns->site\">www.$returns->site</a></td></tr><tr>");[/URL]
}else{
print("<td>NO SHOW</td>");
}

}//end while
print("</table>");
}//end if num >0
 ?>
and I get the error message: Something Wrong Mate which is the set error message. Can you happen to see any reason why this might be happening as well?

Thanks Justin.
 
Update:

I changed the code a little to
Code:
$sql = "SELECT * FROM videos";
$result=mysql_query($sql)
or die ("Query Error: " . mysql_error());

Now I just Get the error message: Query Error: No database selected

Which is very wiered. any sugestions?
Thanks
 
my fault. i typed straight into the tt box. remove the semicolon after $i++
 
Hi Justin,

I found it already, just did a qucick search on google and it came up. Thanks any way.

Now a different problem. This is what the code looks like now.

Code:
<?php require_once('Connections/Connection.php');?>
		
		<?php
		mysql_select_db($database_Connection, $Connection);
$sql = "SELECT * FROM videos";
$results = mysql_query($sql) or die("Something Wrong Mate");
$returnedrows = mysql_num_rows($results);

$num_cols = 3;
echo "<table>";
while ($row = mysql_fetch_assoc($results)){
   echo "<tr>";
   for($i=0;$i < $num_cols; $i++){
      echo "<td>";
      echo $row['title'];
      echo "</td>";
      $row = @mysql_fetch_assoc($result);  
      if (!$row){
        for ($j=0; $j < ($num_cols-$i);$j++){
          echo "<td>None Mate</td>";
        }
        break(1);
      }
   }
   echo "</tr>";
}
echo "</table>";

But it gives me the reverse of the expected result. There are 8 records on my database table, but it dosnt seems to be picking them up instead it giving me

None Mate None Mate None Mate
None Mate None Mate None Mate
None Mate None Mate None Mate

Any sugestions? Thanks
 
pluralise $result in the middle of the for loop

as to why you are not getting any output, i suspect it is either because there is no field called 'title' (remember case sensitive) or there are no records in the database.
 
Any idea why its not looping through?
its only coming up with the first 3 records even though there are 8 records in total and its suppose to move to the next three records and the next 3 etc.

 
Sorry there are 4 records on my table. but its still suppose to move to the next line dosnt it?
 
In addition to the above problem, the code is not code is not also allowing me to display associated data below each record which is the aim really ie,

Id:112 | Id002 | id005
William John Julie

Am not sure if you would understand the above example.

Thanks Justin
 
c'mon!!!
let's start with some more data.

what are the fields in your database and what is the data that you want to be output?
 
I've got,

user
title
image
id

As the fields in my database.

My point here is that, should it matter if I have odd number data on the tables? shouldnt it be that even if there were8 records there would be two rows with three records and the last row with just two?

What I want displayed was for example, the user and below that user, that users image then the next one and so on..
i.e

User1 | User2 | User2
User1's Pic |User2's Pic | User3's Pic

Sory to be a pain. I appreciate your patient
 
it was skipping a record too many at the end of a row.

try this instead

Code:
<?php
$sql = "SELECT id, user, title, image FROM people";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) == 0) {
	die ('no records');
}

$num_cols = 3;
$break = '<br/>';
echo "<table border=\"1\">";
while ($row = mysql_fetch_assoc($result)){
	echo "<tr>";
	for($i=0;$i < $num_cols; $i++){
		echo "<td>";
		echo $row['user'] . $break;
		echo $row['title'] . $break;
		echo $row['image'];
		echo "</td>";
		if($i !== 2) {
			$row = mysql_fetch_assoc($result);  
			if (!$row){
				for ($j=0; $j < ($num_cols-$i-1);$j++){
					echo "<td>None Mate</td>";
				}
			break;
			}
		}		
   }
   echo "</tr>";
}
echo "</table>";
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top