Help placing mysql results in html table.

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

Skunk1311
Forum Newbie
Posts: 11
Joined: Tue Jan 20, 2009 9:22 am

Help placing mysql results in html table.

Post by Skunk1311 »

Hey all, sorry about my first post asking for help!

Well i need help displaying mysql results in a html table.
The table consits of 6 columns and 20 rows.

I have a html form that has 6 input boxes. Each box inputs that info to a different database in a mysql database. I have 6 tables in my mysql database.

My form works and inputs the data into each table in my mysql database just fine.

So say i input the textboxes on my form where i submit to my database with a word in each. Thats 6 different words. I want word 1 in the first column of the first row word 2 in second column of first row and so on till it has done 6 columns on the first row.

Then say i go back to my form and input 6 more different words. I want to do the same as the first but on a new row. And so on..

Here is what i have atm. Unfortunately it keeps displaying the next submitted results in the next column instead of starting a new row.

Code: Select all

<?PHP
include 'mysql_connect.php';
 
echo "<table border='1' width='100%' cellspacing='0' cellpadding='0'>";
 
$query=mysql_query("select * from news_posts, realname, skill, favmap, 
 
ctwep, twep") or die("Error Occured,plz try again");
$left = true;
while($row=mysql_fetch_array($query))
{
if ($left)
{
echo "<tr>";
}
echo "<td>";
echo $row['title'];
echo"</td>";
echo "<td>";
echo $row['realname'];
echo"</td>";
echo "<td>";
echo $row['skill'];
echo"</td>";
echo "<td>";
echo $row['favmap'];
echo"</td>";
echo "<td>";
echo $row['ctwep'];
echo"</td>";
echo "<td>";
echo $row['twep'];
echo"</td>";
if (!$left)
{
echo"</tr>";
}
$left = !$left;
}
 
echo"</table>";
 
?>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Help placing mysql results in html table.

Post by Burrito »

get rid of your $left boolean.
Skunk1311
Forum Newbie
Posts: 11
Joined: Tue Jan 20, 2009 9:22 am

Re: Help placing mysql results in html table.

Post by Skunk1311 »

Ok, not it is showing a row with 6 colums but the row is like squished to like 1 pixel or something. Cant see any entrys in it. Although there is 18 entries all together in all my database tables. So there should be 3 rows, 18 cells altogether =/

Here is my code :

Code: Select all

<?PHP
include 'mysql_connect.php';
 
echo "<table border='1' width='100%' cellspacing='0' cellpadding='0'>";
 
$query=mysql_query("select * from news_posts, realname, skill, favmap, ctwep, twep") or die("Error Occured,plz try again");
while($row=mysql_fetch_array($query))
echo "<tr>";
 
echo "<td>";
echo $row['title'];
echo"</td>";
echo "<td>";
echo $row['realname'];
echo"</td>";
echo "<td>";
echo $row['skill'];
echo"</td>";
echo "<td>";
echo $row['favmap'];
echo"</td>";
echo "<td>";
echo $row['ctwep'];
echo"</td>";
echo "<td>";
echo $row['twep'];
echo"</td>";
 
echo"</tr>";
 
 
echo"</table>";
 
?>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Help placing mysql results in html table.

Post by Burrito »

look at the source to find out why it's 'shrinking' the cell widths.

you have 18 entries...does that mean 3 rows with 6 entries each? That's how I interepreted your original post.

if you're trying to create new rows based on a count of your loop, look at using a modulous operator.
Skunk1311
Forum Newbie
Posts: 11
Joined: Tue Jan 20, 2009 9:22 am

Re: Help placing mysql results in html table.

Post by Skunk1311 »

Hi, i will have a look shortly.

And yes your right 3 rows 6 entries each.
But i want it to create a new row for every 6 more entries.

Thanks for help so far man.

edit: I dont think its the source i think its just not displaying the entries or something?
I cant find anything related.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Help placing mysql results in html table.

Post by Burrito »

when you view the source do you see the values from your table being output?
Skunk1311
Forum Newbie
Posts: 11
Joined: Tue Jan 20, 2009 9:22 am

Re: Help placing mysql results in html table.

Post by Skunk1311 »

Sorry mate i think you are on a different track than me.

Im very new to mysql/php so please bear with me.

Ok so i have a blank php page.

I have a mysql database. In my mysql database i have 6 tables.

Image

I have a form where i input data to my mysql tables. 6 Textboxes, each one submits to a different table for example:

Image

Now I have them in my mysql database. I want like the first textbox input to the first cell, second textbox to second cell and so on for six cells because there is six inputs. My script so far does that fine :)

But when i go back to the form where i input data to the mysql tables. I input another 6 inputs. And i want it to start a new row and do the same as last time but on a new row with the new inputs. The last row stays there aswell this one will go underneath. And so on.

Sorry i have tried to make it more clear. And sorry if your last post was on track. I just didn't understand.

Peace.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Help placing mysql results in html table.

Post by Burrito »

fyi, that's really not a good way to structure your tables.

what I"m trying to determine is if your while() loop is actually outputting any data.

try just echoing from your $row[] array w/o putting anything inside html tables to make sure you are indeed retrieving information from your database.
Skunk1311
Forum Newbie
Posts: 11
Joined: Tue Jan 20, 2009 9:22 am

Re: Help placing mysql results in html table.

Post by Skunk1311 »

Yes i can display information from my database as stated earlier i can also get them in the first six cells like a want all i need it to do is start a new row after and insert the next set of results from the tables.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Help placing mysql results in html table.

Post by Burrito »

Code: Select all

 
$query=mysql_query("select * from news_posts, realname, skill, favmap, ctwep, twep") or die("Error Occured,plz try again");
while($row=mysql_fetch_array($query))
{
echo "<tr>";
  
echo "<td>";
echo $row['title'];
echo"</td>";
echo "<td>";
echo $row['realname'];
echo"</td>";
echo "<td>";
echo $row['skill'];
echo"</td>";
echo "<td>";
echo $row['favmap'];
echo"</td>";
echo "<td>";
echo $row['ctwep'];
echo"</td>";
echo "<td>";
echo $row['twep'];
echo"</td>";
  
echo"</tr>";
}
 
 
 
Skunk1311
Forum Newbie
Posts: 11
Joined: Tue Jan 20, 2009 9:22 am

Re: Help placing mysql results in html table.

Post by Skunk1311 »

Wow strange result :S

If you scroll down to the table on http://spectrumclan.co.uk/members.php
You will see what i mean.

The first row should be as follows :

paul jessica high de_dust2 m4 ak

The second row should be :

sdgdsfds dsfdsfdsf dsfdsfsdf dsfdsfds fsdfsdfd sfdsfsdf

But it has mangled them all up and added like 500 rows which has really messed up the template.
Please take a look. Thanks.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Help placing mysql results in html table.

Post by Burrito »

that's because of your mysql table structure. It's selecting one row for each table's value against every other table. That's why I said it's not a good table layout.
Skunk1311
Forum Newbie
Posts: 11
Joined: Tue Jan 20, 2009 9:22 am

Re: Help placing mysql results in html table.

Post by Skunk1311 »

How do you suggest i lay it out? its my first time actually using mysql btw
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Help placing mysql results in html table.

Post by Burrito »

just create different fields for each of your values on one table.

then change your insert statement to:

Code: Select all

 
INSERT INTO `myTable` (`field1`,`field2`,`field3`,`field4`,`field5`,`field6`) VALUES ('value1','value2','value3','value4','value5','value6')
 
then select them by:

Code: Select all

 
SELECT * FROM `myTable`
 
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Help placing mysql results in html table.

Post by Burrito »

the only time you want to use multiple tables is for database normalization (google it).

if you have a one to many relationship.

ex:
one user has multiple guns in CSS. You would create a user table and a guns table and tie them together using a 'key'.
in that case, you'd have multiple rows on your guns table that are tied to one user by the id from the user table.
Post Reply