MYSQL database read and print to screen

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pagegen
Forum Commoner
Posts: 32
Joined: Sat May 31, 2008 6:38 am

MYSQL database read and print to screen

Post by pagegen »

mite be a simple qustion for few..

i want to read from MYSQL databasa and write the results to the screen using a table..

sounds easy right?

well its not just a table where every row of the data goes downwards..

i want columns.. example

i have 4 rows in my database saying hello.. i want to the print the hellos like this
Hello hello
hello hello

so that will be a table with 2 columns and 2 rows

If u want to see my code then i have no prob posting it here but what my code does is
hello
hello
hello
hello

so its no good :(

Thanks
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: MYSQL database read and print to screen

Post by andyhoneycutt »

That's a pretty ambiguous example there. Please post code.

Thanks,
Andy.
pagegen
Forum Commoner
Posts: 32
Joined: Sat May 31, 2008 6:38 am

Re: MYSQL database read and print to screen

Post by pagegen »

andyhoneycutt wrote:That's a pretty ambiguous example there. Please post code.

Thanks,
Andy.
oh here is the code.. it reads from table and then writes to screen but not the way i want

Code: Select all

Echo '<table style="width: 100%">';
                Echo "<TR><TD> $row[id] </td></tr>";
                Echo '</table>';
 
 
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: MYSQL database read and print to screen

Post by andyhoneycutt »

pagegen wrote:
andyhoneycutt wrote:That's a pretty ambiguous example there. Please post code.

Thanks,
Andy.
oh here is the code.. it reads from table and then writes to screen but not the way i want

Code: Select all

Echo '<table style="width: 100%">';
                Echo "<TR><TD> $row[id] </td></tr>";
                Echo '</table>';
 
 
if you are looking to write into a two column format the same column over and over again, you'll need to have two columns in your html table's row, e.g.

Code: Select all

<tr><td>Column One</td><td>Column Two</td></tr>
So you could do something like this:

Code: Select all

 
$i=1;
echo "<tr>";
while($row = mysql_fetch_assoc($result))
{
  if($i%2==0)
  {
    echo "</tr><tr>";
  }
  echo "<td>{$row['id']}</td>";
  $i++;
}
echo "</tr>";
 
This will allow you to write:

Code: Select all

 
ID1    ID2
ID3    ID4
 
Hope this helps,
Andy.
pagegen
Forum Commoner
Posts: 32
Joined: Sat May 31, 2008 6:38 am

Re: MYSQL database read and print to screen

Post by pagegen »

andyhoneycutt wrote:
pagegen wrote:
andyhoneycutt wrote:That's a pretty ambiguous example there. Please post code.

Thanks,
Andy.
oh here is the code.. it reads from table and then writes to screen but not the way i want

Code: Select all

Echo '<table style="width: 100%">';
                Echo "<TR><TD> $row[id] </td></tr>";
                Echo '</table>';
 
 
if you are looking to write into a two column format the same column over and over again, you'll need to have two columns in your html table's row, e.g.

Code: Select all

<tr><td>Column One</td><td>Column Two</td></tr>
So you could do something like this:

Code: Select all

 
$i=1;
echo "<tr>";
while($row = mysql_fetch_assoc($result))
{
  if($i%2==0)
  {
    echo "</tr><tr>";
  }
  echo "<td>{$row['id']}</td>";
  $i++;
}
echo "</tr>";
 
This will allow you to write:

Code: Select all

 
ID1    ID2
ID3    ID4
 
Hope this helps,
Andy.

thank u, ill give it a shot 2moz and see how i do :)
pagegen
Forum Commoner
Posts: 32
Joined: Sat May 31, 2008 6:38 am

Re: MYSQL database read and print to screen

Post by pagegen »

works great. Thanks..
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: MYSQL database read and print to screen

Post by andyhoneycutt »

awesome, glad to hear it!

-Andy
Post Reply