Turning MySQL data into a PHP table
Moderator: General Moderators
Turning MySQL data into a PHP table
I know i need to make a query like
SELECT id,LEFT(title,20) FROM "tablename"
then do that for each column in the database i want on the table.
now all i need to know is how to actually echo back a table to put my queries on.
SELECT id,LEFT(title,20) FROM "tablename"
then do that for each column in the database i want on the table.
now all i need to know is how to actually echo back a table to put my queries on.
Re: Turning MySQL data into a PHP table
Uhh... what?synical21 wrote:I know i need to make a query like
SELECT id,LEFT(title,20) FROM "tablename"
then do that for each column in the database i want on the table.
Code: Select all
SELECT * FROM `tablename`Anyways, an example:
Code: Select all
$result = mysql_query("SELECT * FROM `tablename`");
echo "<table>";
echo "<tr><th>ID</th><th>Title</th></tr>";
if (mysql_num_rows($result) == 0) {
echo "<tr><td colspan='2'>No results</td></tr>";
}
while ($line = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>", htmlentities($line["id"]), "</td>";
echo "<td>", htmlentities($line["title"]), "</td>";
echo "</tr>";
}
echo "</table>";Re: Turning MySQL data into a PHP table
Thanks for the reply i will try your method now 
Re: Turning MySQL data into a PHP table
Ok this is the code im using now and it does the job except i cant add more queries, need just a little help on how to add another column.
This is the query:
This is the table:
Now this works exactly how i want it to it allows me to link the title to a new php page. Only problem now is when i try to add the new column "min" it just overlaps the title and goes wrong. i just want it to go like:
ID---Title----------------Min
1.....Title info here.....3
Hope that makes it a little clearer
Thanks for viewing.
This is the query:
Code: Select all
$sql = 'SELECT id,LEFT(title,20) as short_title FROM fulldata';
$res = mysql_query($sql);
if(!$res) die(mysql_error());Code: Select all
echo '<table>';
echo '<tr><th>id#</th><th>Title</th></tr>';
while($row = mysql_fetch_assoc($res)) {
echo '<tr>'.
'<td>'.$row['id'].'</td>'.
'<td><a href="fulldata.php?ID='.$row['id'].'">'.$row['short_title'].'</a></td>'.
'</tr>';
}ID---Title----------------Min
1.....Title info here.....3
Hope that makes it a little clearer
Thanks for viewing.
Re: Turning MySQL data into a PHP table
What do you mean "add more queries", and overlaps the title? And what's your new code?
Might be just me, but your post seems a little unclear...
Might be just me, but your post seems a little unclear...
Re: Turning MySQL data into a PHP table
Ill just show the script in one piece
So far the script makes a table with a title column
To make it less confusing i will just sum up, i basically need a table with title,minutes and costs as columns, but i am finding it hard to add more columns as when ever i try to add the minutes next to the title column the title column just disappears even though i have not edited the title part.
So i just need a way to add multiple columns to my table as i can only manage one column.
That might clear it up i dunno
Code: Select all
# connect to the database
mysql_connect('localhost','*****','****');
mysql_select_db('Mini');
# query
$sql = 'SELECT id,LEFT(title,20) as short_title FROM fulldata';
$res = mysql_query($sql);
if(!$res) die(mysql_error());
#HTML table
echo '<table>';
echo '<tr><th>id#</th><th>Title</th></tr>';
while($row = mysql_fetch_assoc($res)) {
echo '<tr>'.
'<td>'.$row['id'].'</td>'.
'<td><a href="fulldata.php?ID='.$row['id'].'">'.$row['short_title'].'</a></td>'.
'</tr>';
}
echo '</table>';To make it less confusing i will just sum up, i basically need a table with title,minutes and costs as columns, but i am finding it hard to add more columns as when ever i try to add the minutes next to the title column the title column just disappears even though i have not edited the title part.
So i just need a way to add multiple columns to my table as i can only manage one column.
That might clear it up i dunno
Re: Turning MySQL data into a PHP table
Code: Select all
echo '<table>';
echo '<tr><th>id#</th><th>Title</th><th>Third column</tr>';
while($row = mysql_fetch_assoc($res)) {
echo '<tr>'.
'<td>'.$row['id'].'</td>'.
'<td><a href="fulldata.php?ID='.$row['id'].'">'.$row['short_title'].'</a></td><td>Third column</td>'.
'</tr>';
}Re: Turning MySQL data into a PHP table
Ahhhh now its starting to look more like a table. I think i can work the rest from here dam that was to easy, i need a break from php i think.... trying to learn to much in my first 3 days of learning it for the first time 
One last question so i dont have to bug you again. The other two columns are not included in the query yet, i presume i have to edit the query to include 'min' and 'cost'.
One last question so i dont have to bug you again. The other two columns are not included in the query yet, i presume i have to edit the query to include 'min' and 'cost'.
Re: Turning MySQL data into a PHP table
Tbh, I think you need to learn some HTML first 
In regard to your question - read tasairis's post again.
In regard to your question - read tasairis's post again.
Re: Turning MySQL data into a PHP table
Table done thanks, Yeah i need to learn html urgently i was hopeing i would pick it up as i tried to learn php but thats not the case. Never knew scripting would be so hard.
Ah well thanks again.
Ah well thanks again.
Re: Turning MySQL data into a PHP table
Yeah, that generally is the case...I think html is a language you just kind of pick up, not necessarily learn.
However...it's always good to run through a tutorial if you're just starting out. There's an awesome tutorial here that I used when I started out learning HTML. It'll probably take you like an hour...and you'll pretty much "know" html.
However...it's always good to run through a tutorial if you're just starting out. There's an awesome tutorial here that I used when I started out learning HTML. It'll probably take you like an hour...and you'll pretty much "know" html.