Results 1 to 15 of 15

Thread: The HTML bit of PHP - simple!

  1. #1
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts

    The HTML bit of PHP - simple!

    Code:
    <html>
    <head><title> How to Grab your MySQL Data </title></head>
    <body bgcolor="#f0f0f0">
    
    <?
    $DBhost = "localhost";
    $DBuser = "root";
    $DBpass = "psswrod";
    $DBName = "contacts";
    $table = "contacts";
    mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
    
    @mysql_select_db("$DBName") or die("Unable to select
    database $DBName");
    
    
    $sqlquery = "SELECT * FROM $table";
    $result = mysql_query($sqlquery);
    $number = mysql_numrows($result);
    
    
    $i = 0;
    
    if ($number < 1) {
    print "<CENTER><P>There Were No Results for Your Search</CENTER>";
    }
    else {
    while ($number > $i) {
    $name = mysql_result($result,$i,"name");
    $office = mysql_result($result,$i,"office");
    $mobile = mysql_result($result,$i,"mobile");
    
    
    print "Name: $name&nbsp;&nbsp;&nbsp;&nbsp;Office: $office&nbsp;&nbsp;&nbsp;&nbsp;Mobile: $mobile<br>";
    
    
    $i++;
    }
    }
    ?>
    </BODY></HTML>

    how can i get that to display as a table? if i put my html code after the print " it all goes pete tong.


    my html:

    Code:
    <table border="1" width="100%" height="30" style="border-style: solid; border-width: 1px">
    	<tr>
    		<td style="border-style: dotted; border-width: 1px" bgcolor="#3F789A">
    		<font face="Verdana" size="2" color="#FFFFFF"><b>Name</b></font></td>
    		<td style="border-style: dotted; border-width: 1px" bgcolor="#3F789A">
    		<font face="Verdana" size="2" color="#FFFFFF"><b>Office </b></font><b>
    		<font face="Verdana" size="2" color="#FFFFFF">Phone</font></b></td>
    		<td style="border-style: dotted; border-width: 1px" bgcolor="#3F789A">
    		<font face="Verdana" size="2" color="#FFFFFF"><b>Mobile</b></font></td>
    			</tr>
    	<tr>
    		<td style="border-style: dotted; border-width: 1px"><font face="Verdana" size="2"><?php echo($name); ?></td>
    		<td style="border-style: dotted; border-width: 1px"><font face="Verdana" size="2"><?php echo($office); ?></td>
    		<td style="border-style: dotted; border-width: 1px"><font face="Verdana" size="2"><?php echo($mobile); ?></td>
    	</tr>
    </table>
    Last edited by Vini; 22-09-2005 at 05:04 PM.

  2. #2
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    Is that honestly your password you've just pasted in that code?

    Well, just wrap the table tags around the PHP script, then in the while loop output whatever is in the tr and td tags I guess? Should be pretty straightforward

  3. #3
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,164
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    oh dear god, your letting people run any request they want as root. See SQL injection attacks.
    throw new ArgumentException (String, String, Exception)

  4. #4
    Bigger than Jesus Norky's Avatar
    Join Date
    Feb 2005
    Posts
    1,579
    Thanks
    1
    Thanked
    8 times in 8 posts
    Quote Originally Posted by TheAnimus
    oh dear god, your letting people run any request they want as root. See SQL injection attacks.
    No he isn't.

    To put that data into a table is pretty simple:

    <table><tr><th>blah</th><th>blah</th></tr>
    **start results loop**
    <tr><td>blah</td><td>blah</td></tr>
    **end results loops**
    </table>

  5. #5
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,164
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    ah didn't see $table been defined above, its still a very bad idea to use root thou.
    throw new ArgumentException (String, String, Exception)

  6. #6
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts
    Quote Originally Posted by TheAnimus
    ah didn't see $table been defined above, its still a very bad idea to use root thou.
    its on the intranet (not www) and in an office full of women. i dont see the problem

  7. #7
    HEXUS.net Webmaster
    Join Date
    Jul 2003
    Location
    UK
    Posts
    3,108
    Thanks
    1
    Thanked
    0 times in 0 posts
    - use echo not print that way you can spread it out over several lines and make it easier to read

    - mysql_numrows is not a function, mysql_num_rows is

    - use single quotes for text, you can then include double quotes in HTML without escaping them and single quotes have been proven to parse faster in PHP

    - W3C standards indicate HTML tags should be in lower case

    Your code tidied up (and untested)
    PHP Code:
    <html>
    <head><title> How to Grab your MySQL Data </title></head>
    <body bgcolor="#f0f0f0">
    <?
        $DBhost 
    'localhost';
        
    $DBuser 'root';
        
    $DBpass 'psswrod';
        
    $DBName 'contacts';
        
    $table 'contacts';
        
    mysql_connect($DBhost,$DBuser,$DBpass) or die('Unable to connect to database');
        @
    mysql_select_db($DBName) or die('Unable to select database '.$DBName);
        
        
        
    $sqlquery "SELECT * FROM ".$table;
        
    $result mysql_query($sqlquery);
        
        if (
    mysql_num_rows($result) < 1)
        {
            echo 
    '<center><p>There Were No Results for Your Search</center>';
        } else {
            echo 
    '<table>';
            while (
    $row mysql_fetch_array($result)
            {
                echo 
    '<tr>
                        <th>Name</th>
                        <td>'
    .$row['name'].'</td>
                        <th>Office</th>
                        <td>'
    .$row['office'].'</td>
                        <th>Mobile</th>
                        <td>'
    .$row['mobile'].'</td>
                    </tr>'
    ;
            }
            echo 
    '</table>';
        }
    ?>
    </body></html>

  8. #8
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts
    hiya mate, ill have a play... seems a little over me at the minute

    your code, gives: Parse error: syntax error, unexpected '{' in C:\Program Files\Apache\Apache2\htdocs\contacts\show4.php on line 23

    how do i get to add fancy things to the tables too, i want a diff colour cell for the headers, when i do i get all sorts of php errors. like the above one.
    Last edited by Vini; 23-09-2005 at 12:14 PM.

  9. #9
    HEXUS.net Webmaster
    Join Date
    Jul 2003
    Location
    UK
    Posts
    3,108
    Thanks
    1
    Thanked
    0 times in 0 posts
    missing a bracket at the end of line 23, add one in

  10. #10
    HEXUS.net Webmaster
    Join Date
    Jul 2003
    Location
    UK
    Posts
    3,108
    Thanks
    1
    Thanked
    0 times in 0 posts
    use style sheets to apply styles rather than inline HTML

    PHP Code:
    <html>
    <head><title> How to Grab your MySQL Data </title>
    <style type="text/css">
        body {
            background: #f0f0f0;
            font: 12px normal Verdana, Arial, sans-serif;}
        th, td {
            border: 1px dotted #000000;
            font: 12px normal Verdana, Arial, sans-serif;}
        th {
            background: #3F789A;
            font-weight: bold;}    
    </style>
    </head>
    <body>
    <?
        $DBhost 
    'localhost';
        
    $DBuser 'root';
        
    $DBpass 'psswrod';
        
    $DBName 'contacts';
        
    $table 'contacts';
        
    mysql_connect($DBhost,$DBuser,$DBpass) or die('Unable to connect to database');
        @
    mysql_select_db($DBName) or die('Unable to select database '.$DBName);
        
        
        
    $sqlquery "SELECT * FROM ".$table;
        
    $result mysql_query($sqlquery);
        
        if (
    mysql_num_rows($result) < 1)
        {
            echo 
    '<center><p>There Were No Results for Your Search</center>';
        } else {
            echo 
    '<table>';
            while (
    $row mysql_fetch_array($result))
            {
                echo 
    '<tr>
                        <th>Name</th>
                        <td>'
    .$row['name'].'</td>
                        <th>Office</th>
                        <td>'
    .$row['office'].'</td>
                        <th>Mobile</th>
                        <td>'
    .$row['mobile'].'</td>
                    </tr>'
    ;
            }
            echo 
    '</table>';
        }
    ?>
    </body></html>

  11. #11
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts
    Iain,

    now that ive seen working code, i have so much to play with.

    do you accept cyber kisses? pints? money?

    excellent!

    one this it seems to be doing, is repeating the table headers for each result, how would i stop that?
    Last edited by Vini; 23-09-2005 at 01:39 PM.

  12. #12
    HEXUS.net Webmaster
    Join Date
    Jul 2003
    Location
    UK
    Posts
    3,108
    Thanks
    1
    Thanked
    0 times in 0 posts
    take them out the while loop

  13. #13
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts
    easy when you know, much appreciated. one more problem im having, which is probably the last of the lot...



    my menu has been compressed after doing an <? include ('schedule/schedule.php') ?> why would that be? im guessing to do with php somewhere, durr

    and, my tables on the schedule are following the css from the included php, why? surely the css should just be for the said php file <? include ('schedule/schedule.php') ?>

  14. #14
    HEXUS.net Webmaster
    Join Date
    Jul 2003
    Location
    UK
    Posts
    3,108
    Thanks
    1
    Thanked
    0 times in 0 posts
    Looks like the embedded CSS is being used by the menu. Either move the CSS into your external style sheet so it's consistent across the site or give the table an ID and apply the styles only to that ID. I'm making an assumption that the menu on the left might be in a table.

  15. #15
    Squeeler Vini's Avatar
    Join Date
    Jul 2003
    Location
    Sheffield
    Posts
    1,769
    Thanks
    44
    Thanked
    8 times in 8 posts
    Quote Originally Posted by Iain
    Looks like the embedded CSS is being used by the menu. Either move the CSS into your external style sheet so it's consistent across the site or give the table an ID and apply the styles only to that ID. I'm making an assumption that the menu on the left might be in a table.
    got it! thanks for all your work Iain!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Can you lot help me a bit?
    By Zak33 in forum Automotive
    Replies: 13
    Last Post: 14-04-2004, 11:14 AM
  2. 64 bit over p4s, What do you think?
    By SMOOTH2_HEXUS in forum PC Hardware and Components
    Replies: 15
    Last Post: 24-03-2004, 04:44 AM
  3. Bit of basic PHP help, please chaps?
    By Theo in forum Software
    Replies: 4
    Last Post: 04-12-2003, 02:54 AM
  4. memtester for a 64 bit CPU
    By wilsonian in forum Software
    Replies: 5
    Last Post: 20-11-2003, 07:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •