473,624 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[CLOSED] Database results in columns

8 New Member
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 1737
ldndude
5 New Member
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 $columncountSTO RE 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 Recognized Expert Specialist
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_ass oc(....)) {
$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
TheBATManPhln
8 New Member
Being a novice maybe I'm screwing this up but neither one work.


echo "<table>";

$i = 0;

while ($rec = mysql_fetch_ass oc($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_re sult($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 Recognized Expert Specialist
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
TheBATManPhln
8 New Member
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_ass oc(....)) {

$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_ass oc(....)) {

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 Recognized Expert Specialist
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
TheBATManPhln
8 New Member
I tried it with this

mysql_connect($ db_host, $db_user, $db_pwd);
mysql_select_db ($db_name);
$query="SELECT * FROM cars";
$result=mysql_q uery($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 Recognized Expert Specialist
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_q uery($query);
echo "<table>";
$i = 0;
while ($rec = mysql_fetch_ass oc($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
TheBATManPhln
8 New Member
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

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

Similar topics

10
3334
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 features column access by name. This new release adds support for the cx_Oracle, DB2 and adodbapi modules, allowing users to use PDO with a variety of database systems. 9 different DBAPI modules are now supported, allowing for PDO to be used...
4
5019
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 do it? I can bind data to datagrids and lists and stuff all day long, but can't seem to grasp this one. Any pointers to a useful article / demo (or just any useful pointers!) would be much appreciated. Thanks,
1
1039
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 "Person2". When one of my pages loads I pull the whole table into a DataTable. What I want to do is display each row on my page in a table allowing the user to choose either Person1 or Person2 for each row using RadioButtons to make the choice mutually...
15
8270
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 repeatable :-( But this should help! The error is occurring inside ExecuteReader which uses a DataReader internally. Here are some things that I'm pretty sure it's *NOT*:
1
3397
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 <% dim strBrowse, strSearch, Count Count = objRS.RecordCount strBrowse = request.querystring("strBrowse")
3
2932
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 groups for years, I figure it's time for me to contribute a little bit back, maybe some people out there will find this useful. * Introduction This is a "how-to" style article, showing by example how to dynamically
1
1461
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 as question numbers) - The Survey Extra Results (As additional questions can be listed into the Questions table, has 3 columns (a link/id to the survey id, a link/id to the question id, and the answer the user gave).
10
6095
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 code below which when executed generates the following error message: 'There is already an open datareader with this command which must be closed first' Private Sub MainMenu_Load(ByVal sender As System.Object, ByVal e As
0
8242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8681
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8629
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.