[SOLVED] adding column to displayed MySQL table via PHP

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
zacthespack
Forum Newbie
Posts: 16
Joined: Mon Oct 11, 2010 9:08 am

[SOLVED] adding column to displayed MySQL table via PHP

Post by zacthespack »

I am in the process of redesigning a website so that products are taken from a MySQL database and displayed on a page using PHP, however I need to add a column to all for pictures and I need to align the table with the headings that are static on the page. Seehttp://www2.acehireltd.co.uk/template.php, I will also need to change the outputted products font and colour, any help would be very grateful!
The PHP used is as below:

Code: Select all

<?php
$db_host = 'localhost';
$db_user = '*******';
$db_pwd = '**********';

$database = '***********';
$table = '**********';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);
echo "<table border='1'><tr>";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";


    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}
mysql_free_result($result);
?>
Last edited by zacthespack on Wed Jul 20, 2011 8:13 am, edited 1 time in total.
beetree
Forum Commoner
Posts: 26
Joined: Mon Jul 18, 2011 6:30 pm
Location: Peninsula

Re: adding column to displayed MySQL table via PHP

Post by beetree »

You need to add an attribute to your table:

ALTER TABLE [table name] ADD new_attribute varchar(255);

In my understanding the PHP code will automatically display the contents of the new attribute by adding a new column on the far right in your <table>.
sspatel82
Forum Newbie
Posts: 9
Joined: Tue Jul 19, 2011 10:52 am

Re: adding column to displayed MySQL table via PHP

Post by sspatel82 »

One more thing is you don't need "\n" here

Code: Select all

echo "</tr>\n";
zacthespack
Forum Newbie
Posts: 16
Joined: Mon Oct 11, 2010 9:08 am

Re: adding column to displayed MySQL table via PHP

Post by zacthespack »

Ok thanks guys, I have sorted the table lay out for the picture section and have found out how to include links to the photos in the database to display them easily. However now I need help changing the font and background colour or the table etc. Can anyone help with this?
sspatel82
Forum Newbie
Posts: 9
Joined: Tue Jul 19, 2011 10:52 am

Re: adding column to displayed MySQL table via PHP

Post by sspatel82 »

Here is how you change background color with css.
http://www.w3schools.com/css/css_background.asp
and here is how you change font with css..
http://www.w3schools.com/css/css_font.asp

--------------
Sanket Patel
beetree
Forum Commoner
Posts: 26
Joined: Mon Jul 18, 2011 6:30 pm
Location: Peninsula

Re: adding column to displayed MySQL table via PHP

Post by beetree »

sspatel82 wrote:One more thing is you don't need "\n" here

Code: Select all

echo "</tr>\n";
Why? Because IE3 otherwise doesn't parse the table correctly? :)
sspatel82
Forum Newbie
Posts: 9
Joined: Tue Jul 19, 2011 10:52 am

Re: adding column to displayed MySQL table via PHP

Post by sspatel82 »

beetree wrote:
sspatel82 wrote:One more thing is you don't need "\n" here

Code: Select all

echo "</tr>\n";
Why? Because IE3 otherwise doesn't parse the table correctly? :)

Hi beetree,
because <tr> is used for table row...so when you finish with </tr> and start with <tr> it will automatically jump to next row ....so no need for "\n"
zacthespack
Forum Newbie
Posts: 16
Joined: Mon Oct 11, 2010 9:08 am

Re: adding column to displayed MySQL table via PHP

Post by zacthespack »

sspatel82 wrote:Here is how you change background color with css.
http://www.w3schools.com/css/css_background.asp
and here is how you change font with css..
http://www.w3schools.com/css/css_font.asp

--------------
Sanket Patel
I know how to use css It just working out how to set it up with the php table, I have changed the script used to make it easier. It is now:

Code: Select all

<?php
mysql_connect('localhost', '*************', '***********') or die (mysql_error());
mysql_select_db('************') or die (mysql_error());
$result = mysql_query("SELECT * from *************");
//Table starting tag and header cells
echo "<table border='1'><tr><th>Hire Code</th><th>Picture (click to enlarge)</th><th>Product Description</th><th>Hire Cost £ (Excl. VAT)</th>";
while($row = mysql_fetch_array($result)){
   //Display the results in different cells
   echo "<tr><td>" . $row['productcode'] . "</td><td>" . $row['picture'] . "</td><td>" . $row['description'] . "</td><td>" . $row['salesprice'] . "</td></tr>";
}
//Table closing tag
echo "</table>";
?>
How could I change the font and colour for the header cells?
zacthespack
Forum Newbie
Posts: 16
Joined: Mon Oct 11, 2010 9:08 am

Re: adding column to displayed MySQL table via PHP

Post by zacthespack »

Dont worry I got it sorted now, thank you all very much for your help
beetree
Forum Commoner
Posts: 26
Joined: Mon Jul 18, 2011 6:30 pm
Location: Peninsula

Re: adding column to displayed MySQL table via PHP

Post by beetree »

sspatel82 wrote:
beetree wrote:
sspatel82 wrote:One more thing is you don't need "\n" here

Code: Select all

echo "</tr>\n";
Why? Because IE3 otherwise doesn't parse the table correctly? :)

Hi beetree,
because <tr> is used for table row...so when you finish with </tr> and start with <tr> it will automatically jump to next row ....so no need for "\n"
Oh sorry, I misread you. I thought you were saying that he _needs_ to add \n :)

Cool :)
Post Reply