PHP with CSS

Discussion in 'PHP' started by raztastic, Jul 3, 2007.

  1. #1
    I have created a file that will display a table using php and wanted to style the table using css, i tried several ways but could not get it working. can some help please by attaching a css table to th php code as an example. thank you

    $link = mysql_connect('localhost','root','');
    $db = mysql_select_db('jewellery',$link);
    $SQL = "SELECT * FROM products WHERE ProductID=" . $_POST['products'];
    echo $SQL;
    $data = mysql_query($SQL,$link);
    $record = mysql_fetch_assoc($data);
    while ($record) {
    echo "<tr>";
    echo "<form action='login.php' method='post'>";
    echo "<td>";
    echo $record['ProductName'];
    echo "</td>";
    echo "<td>";
    echo $record['Category'];
    echo "</td>";
    echo "<td>";
    echo $record['ProductDescription'];
    echo "</td>";
    echo "<td>";
    echo $record['SellingPrice'];
    echo "</td>";
    echo "<td><img src='phonesimages/";
    echo $record['PhotoFileName'];
    echo "_l.jpg'></td>";
    echo "<td>";
    echo "<h4> Please select the quantity and the colour you want</h4>";
    echo "<select name='Quantity'> <option>1</option><option>2</option><option>3</option><option>4</option><option>5</option></select><br />";
    echo "<select name='Colour'> <option>Red</option><option>Black</option><option>Silver</option><option>Blue</option></select><br />";
    echo "<input type='submit' value='Buy Me'>";
    echo "</td>";
    echo "<input type='hidden' name='products' value='";
    echo $_POST['products'];
    echo "'>";
    echo "</form>";
    echo "</tr>";
    $record = mysql_fetch_assoc($data);
    };

    ?>

    </table>

    </body>

    </html>
     
    raztastic, Jul 3, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    What is a CSS table?

    Just do:
    
    echo '<table class="your-class">';
    
    PHP:
    And use normal CSS to style it?
     
    nico_swd, Jul 3, 2007 IP
  3. SEV3N

    SEV3N Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here's another example:

    
    <table style="color:red">
    <?php
    $x = 'this is a variable';
    echo '<tr><td style="color:green">this is a line</td></tr>';
    echo "<tr><td class='xxClass'>$x</td></tr>";
    ?>
    </table>
    
    PHP:
    This will be a table with the font color red. First row will have the font color green and second row the style of xxClass class. PHP prints anything found after echo or print and between tags, it doesn't care what's there.
    So basicly you write a table using the <table> tag. Start a php tag and there read the database. Then you use a loop to read the items from the database and in that loop you echo a <td> or <tr> tag, depending on what you want the table to look like. To style the table use the same technique used when creating a simple html file.
    I don't know if the script is the one that you will use but here's a line that you should take care of:
    you should always read what the user sends to the script and check if it is a valid value (ie check $_POST['products'] if it is a number, if it is too long etc.). At least use
    $SQL = "SELECT * FROM products WHERE ProductID=" . mysql_real_escape_string($_POST['products']);
    PHP:
     
    SEV3N, Jul 3, 2007 IP