Page 1 of 1

[SOLVED] adding column to displayed MySQL table via PHP

Posted: Tue Jul 19, 2011 7:32 am
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);
?>

Re: adding column to displayed MySQL table via PHP

Posted: Tue Jul 19, 2011 11:24 am
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>.

Re: adding column to displayed MySQL table via PHP

Posted: Tue Jul 19, 2011 1:56 pm
by sspatel82
One more thing is you don't need "\n" here

Code: Select all

echo "</tr>\n";

Re: adding column to displayed MySQL table via PHP

Posted: Tue Jul 19, 2011 3:11 pm
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?

Re: adding column to displayed MySQL table via PHP

Posted: Tue Jul 19, 2011 4:19 pm
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

Re: adding column to displayed MySQL table via PHP

Posted: Tue Jul 19, 2011 4:28 pm
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? :)

Re: adding column to displayed MySQL table via PHP

Posted: Tue Jul 19, 2011 4:36 pm
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"

Re: adding column to displayed MySQL table via PHP

Posted: Wed Jul 20, 2011 5:36 am
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?

Re: adding column to displayed MySQL table via PHP

Posted: Wed Jul 20, 2011 6:54 am
by zacthespack
Dont worry I got it sorted now, thank you all very much for your help

Re: adding column to displayed MySQL table via PHP

Posted: Wed Jul 20, 2011 7:00 pm
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 :)