473,326 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

[CLOSED] Database results in columns

I'm a newbie (less than a week of php) and I'm curious about how results can lay out. Is it possible to have the results layed out in 4 columns?

Usual

RECORD 1 GOES HERE
RECORD 2 GOES HERE
RECORD 3 GOES HERE

I need

RECORD 1 RECORD 2 RECORD 3 RECORD 4
RECORD 5 RECORD 6 ETC.

If this is possible, please don't forget that I'm REALLY new at this. :)

THANKS!!!
Oct 7 '06 #1
13 1714
I'm a newbie (less than a week of php) and I'm curious about how results can lay out. Is it possible to have the results layed out in 4 columns?

Usual

RECORD 1 GOES HERE
RECORD 2 GOES HERE
RECORD 3 GOES HERE

I need

RECORD 1 RECORD 2 RECORD 3 RECORD 4
RECORD 5 RECORD 6 ETC.

If this is possible, please don't forget that I'm REALLY new at this. :)

THANKS!!!
The answer is yes. It is possible. I am also a noob so hopefully someone might have a better/cleaner solution than I do. The only reason I am replying is because I *just* managed to accomplish the same effect that you are describing. How I did it may not be the best way available but my knowledge is limited so I work with what I have.

Here's what I did

I started off with setting up my query and getting the results and the numrows

then..when it came to echoing the results
I declared two variables: the standard $i and $columncountSTORE YOUR CODE BETWEEN code or PHP TAGS!!!!!

Expand|Select|Wrap|Line Numbers
  1. $i = 0;
  2. $columncount = 0:
  3.  
  4. //I started my table
  5. echo "<table><tr>";
  6.  
  7. //I created a while loop for the recordsets
  8.  
  9. while ($i < $num) {
  10. $field=mysql_result($result,$i,"field");
  11.  
  12. echo "<td>".$field."</td>";
  13.  
  14. //inside the loop, I check the column counter to see if I reached the 3rd column
  15.  
  16. if ($columncount = 2) {
  17. //close that row and open a new one
  18. echo "</tr><tr>";
  19. }
  20. //increment my counts by 1
  21. $i++;
  22. $columncount++;
  23. }
  24. //then close my table
  25. echo "</tr></table>";
  26.  
STORE YOUR CODE BETWEEN code or PHP TAGS!!!!!
Like I said before, there may be better ways to do this so keep looking for an answer, this just happened to be the only one I could think of under my time constraints with my knowledge
Hope it helps.
Oct 7 '06 #2
ronverdonk
4,258 Expert 4TB
See my reply to this forum's thread http://www.thescripts.com/forum/thread545705.html
and the code posted there is:
[PHP] echo "<table">;
$i = 0;
while ($rec = mysql_fetch_assoc(....)) {
$i++;
switch $i {
case 1 : echo "<tr><td>" . $rec[$i] . "</td>";
break;
case 2 : echo "<td>" . $rec[$i] . "</td>";
break;
case 3 : echo "<td>" . $rec[$i] . "</td></tr>";
$i=0;
break;
}
}
if ($i < 3)
echo "</tr>";
echo "</table>";
[/PHP]

Ronald :cool:
Oct 7 '06 #3
Being a novice maybe I'm screwing this up but neither one work.


echo "<table>";

$i = 0;

while ($rec = mysql_fetch_assoc($result)) {

$i++;
// THE NEXT LINE IS LINE 27
switch $i {
case 1 : echo "<tr><td>" . $rec[$i] . "</td>";

break;

case 2 : echo "<td>" . $rec[$i] . "</td>";

break;

case 3 : echo "<td>" . $rec[$i] . "</td></tr>";

$i=0;

break;

}

}

if ($i < 3)

echo "</tr>";

echo "</table>";

}
?>


Gives me this error

Parse error: parse error, unexpected T_VARIABLE, expecting '(' in /home/dir/public_html/phundays/testphp/column_test.php on line 27


As for the other code



$i = 0; // THE NEXT LINE IS LINE 19
$columncount = 0:

//I started my table
echo "<table><tr>";

//I created a while loop for the recordsets

while ($i < $num) {
$field=mysql_result($result,$i,"carid");

echo "<td>".$field."</td>";

//inside the loop, I check the column counter to see if I reached the 3rd column

if ($columncount = 4) {
//close that row and open a new one
echo "</tr><tr>";
}
//increment my counts by 1
$i++;
$columncount++;
}
//then close my table
echo "</tr></table>";



Parse error: parse error, unexpected ':' in /home/dir/public_html/phundays/testphp/columns_nov.php on line 19

:(
Oct 7 '06 #4
ronverdonk
4,258 Expert 4TB
You indeed are screwing it up. The code I sent you worked, but why did you add the right brace at the end?? [php]
if ($i < 3)
echo "</tr>";
echo "</table>";

}
[/php]

Why don't you show us all the code you have used to test this solution (the one I showed here).

Ronald :cool:
Oct 7 '06 #5
Here's the code. Less my db info

<html>
<head>
<title>Column Test</title>
</head>
<body>


<?php
$db_host = "localhost";
$db_user = "XXXXXX";
$db_pwd = "YYYYYY";
$db_name = "ZZZZZZZ";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);

echo "<table>";

$i = 0;

while ($rec = mysql_fetch_assoc(....)) {

$i++;

switch $i {

case 1 : echo "<tr><td>" . $rec[$i] . "</td>";

break;

case 2 : echo "<td>" . $rec[$i] . "</td>";

break;

case 3 : echo "<td>" . $rec[$i] . "</td></tr>";

$i=0;

break;

}

}

if ($i < 3)

echo "</tr>";

echo "</table>";
?>

</body>
</html>
Here's the error

Parse error: parse error, unexpected '.', expecting ')' in /home/phelanfi/public_html/phundays/testphp/text3.php on line 20

This is line 20
while ($rec = mysql_fetch_assoc(....)) {

I did fix the html tag from
echo "<table">;

to
echo "<table>";

Were you testing me?? LOL
Anyway, I appreciate the help!!!!

If I replace the (....) with my db's name (cars)

I then get
Parse error: parse error, unexpected T_VARIABLE, expecting '(' in /home/phelanfi/public_html/phundays/testphp/text3.php on line 24

Line 24 is
switch $i {

Thanks
Oct 7 '06 #6
ronverdonk
4,258 Expert 4TB
But where is your database query statement (mysql_query) executed? It is not in your code! It will never work without it!

Ronald :cool:
Oct 7 '06 #7
I tried it with this

mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
$query="SELECT * FROM cars";
$result=mysql_query($query);

Same results
Parse error: parse error, unexpected T_VARIABLE, expecting '(' in /home/phelanfi/public_html/phundays/testphp/text3.php on line 26

Line 26
switch $i {

As I said, I've only been playing with PHP for about a week.

THANK YOU!!
Oct 7 '06 #8
ronverdonk
4,258 Expert 4TB
My mistake, switch operand should be within parentheses. Anyway, the following code is tested by me and it works. Notice that I have included as comments at the beginning the way I set up the table 'cars'. Here is the working sample (don't forget to fill in your db data):
[php]<?php
/*
create table cars (id int primary key, field varchar(20));

insert into cars values(1, "type 1");
insert into cars values(2, "line 2");
insert into cars values(3, "row 3");
insert into cars values(4, "row 4");
insert into cars values(5, "and 5");
insert into cars values(6, "line 6");
insert into cars values(7, "last 7");
*/
$db_host = "localhost";
$db_user = "xxxxxx";
$db_pwd = "yyyyy";
$db_name = "zzzzz";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
$query="SELECT * FROM cars";
$result=mysql_query($query);
echo "<table>";
$i = 0;
while ($rec = mysql_fetch_assoc($result)) {
$value = $rec['field'];
$i++;
switch ($i) {
case 1 : echo "<tr><td>$value</td>";
break;
case 2 : echo "<td>$value</td>";
break;
case 3 : echo "<td>$value</td></tr>";
$i=0;
break;
} // End switch
} // End while
if ($i < 3)
echo "</tr>";
echo "</table>";
?>[/php]

Good luck! Ronald :cool:
Oct 7 '06 #9
BINGO!!

THANK YOU!!!

One more question. Can I have one field in one row and another in the next?

IMAGE 1 | IMAGE 2 | IMAGE 3
DESCRIPTION 1 | DESCRIPTION 2 | DESCRIPTION 3


IMAGE 4 | IMAGE 5 | IMAGE 6
DESCRIPTION 4 | DESCRIPTION 5 | DESCRIPTION 6

I can play with it but I thought I'd ask just to save some frustration in case it's not possible.

Thanks
Oct 7 '06 #10
ronverdonk
4,258 Expert 4TB
Yes, if your description field is in db column 'description'then you could setup the while loop as follows:
[php]while ($rec = mysql_fetch_assoc($result)) {
$value = $rec['field'];
$descr = $rec['description'];
$i++;
switch ($i) {
case 1 : echo "<tr><td>$value<br>$descr</td>";
break;
case 2 : echo "<td>$value<br>$descr</td>";
break;
case 3 : echo "<td>$value<br>$descr</td></tr>";
$i=0;
break;
} // End switch
} // End while
[/php]

Ronald :cool:
Oct 7 '06 #11
WORKS GREAT!!!!

You are AWESOME!!!

Thanks again!
The B.A.T.Man
Oct 7 '06 #12
Thought you might like to see the results. (TEMPORARY results anyway.) I still need to populate the db.

http://phelanfinedesign.com/phundays/carshow.php

Thanks again,
The B.A.T.Man
Oct 7 '06 #13
ronverdonk
4,258 Expert 4TB
Looks good to me!
Oct 8 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Bryan J Gudorf | last post by:
PDO, an open source python module for interfacing with RDBMS (SQL databases), has now reached 1.2.0! PDO provides an object oriented API, similar to that of ADO or JDBC, to python developers. PDO...
4
by: Haydnw | last post by:
Hi, I'd like to put a load of database results (several rows for 5 fields) into a two-dimensional array. Now, this may be a really stupid question, but can someone give me a pointer for how to...
1
by: JRB | last post by:
Hi, I have a table stored in a database called "Compare" with three columns. The first column is the CompareID (it is an auto increment integer), the second is "Person1", and the third is...
15
by: Rob Nicholson | last post by:
I'm starting to worry a bit now. We're getting the above error when two users hit the same database/page on an ASP.NET application using ADO.NET, talking to a SQL 7 server. The error is perfectly...
1
by: isaac2004 | last post by:
hi i am trying to use the record count object to count the number of books in a database of mine. i get this error saying that i have closed the object and i dont think i closed it. here is my code...
3
by: josh.kuo | last post by:
Sorry about the subject, I can't think of a better one. I recently wrote some PHP classes that I think might be of interest to this group. Since I have been reaping the benefits of reading news...
1
by: dallasfreeman | last post by:
I'm looking at a quick way to get results that are displayed as rows to display as columns. I have three tables:- - The Questions for the survey - The Results of the survey (Columns are listed...
10
by: jimmy | last post by:
Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.