Page 1 of 1

MYSQL database read and print to screen

Posted: Tue Sep 02, 2008 11:29 am
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

Re: MYSQL database read and print to screen

Posted: Tue Sep 02, 2008 11:31 am
by andyhoneycutt
That's a pretty ambiguous example there. Please post code.

Thanks,
Andy.

Re: MYSQL database read and print to screen

Posted: Tue Sep 02, 2008 11:35 am
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>';
 
 

Re: MYSQL database read and print to screen

Posted: Tue Sep 02, 2008 11:46 am
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.

Re: MYSQL database read and print to screen

Posted: Tue Sep 02, 2008 4:08 pm
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 :)

Re: MYSQL database read and print to screen

Posted: Fri Sep 12, 2008 2:09 pm
by pagegen
works great. Thanks..

Re: MYSQL database read and print to screen

Posted: Wed Sep 17, 2008 9:25 am
by andyhoneycutt
awesome, glad to hear it!

-Andy