SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    May 2011
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP in imbedded HTML

    I'm trying to embed some HTML into a PHP file to display the fields returned
    from my query in a table structure. I'm new to PHP and the error messages
    are not very user friendly, and I'm not sure what I'm doing wrong. Any
    suggestions would be greatly appriciated

    PHP Code:
    <?php
    $today
    =date("d-m-Y");

    // Make a MySQL Connection
    mysql_connect("localhost""chuck""0Tnrivl33@^N") or die(mysql_error());
    mysql_select_db("chuck_main") or die(mysql_error());




    $query=mysql_query("SELECT frndescshort, envyear, frncolor, frnimageclick, FROM inventory WHERE frnsubtype = 'drlf' OR frnsubtype = 'stds'") or die(mysql_error());  
    $result=mysql_query($query);
     
    $cols=3;        // Here we define the number of columns
    $echo "<table>";    // The container table with $cols columns
    do{
    echo 
    "<tr>";
    for(
    $i=1;$i<=$cols;$i++){    // All the rows will have $cols columns even
    // the records are less than $cols
    $row=mysql_fetch_array($result);
    if(
    $row){
    $img $row['frnimageclick'];  
    echo 
    "<td>";
    echo 
    "<table>";
       echo 
    '<tr valign="top">';
          echo 
    '<td><img src="http://www.sitepoint.com/forums/images/AntiquePics/Tables/$img"/></td>'// columns can have both text and images
          
    echo "<td>";
            echo 
    "<b><$row['frndescshort']></b><br/>";
            echo 
    "<$row['envyearl']><br/>";
            echo 
    "<$row['frncolor']><br/>";
          echo 
    "</td>";
          echo 
    '<td width="50">&nbsp;</td>';    <!-- Create gap between columns -->
       echo 
    "</tr>";
    echo 
    "</table>";
    echo 
    "</td>";

    }
    else{
    echo 
    "<td>&nbsp;</td>";    //If there are no more records at the end, add a blank column
     
    }
     }
    } while(
    $row);
    echo 
    "</table>";





    ?>
    Last edited by AnthonySterling; May 17, 2011 at 06:45. Reason: added bbcode

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Welcome to Sitepoint, joepikepark.

    Have a play around with this, you should be able to modify it to suit. If you have any questions, just holla.

    PHP Code:
    <?php
    error_reporting
    (-1);
    ini_set('display_errors'true);

    function 
    exitWithMessage($message){
      echo 
    $message;
      exit;
    }

    $con mysql_connect('localhost''chuck''0Tnrivl33@^N');

    if(
    false === is_resource($con)){
      
    exitWithMessage('Cannot connect to database: ' mysql_error());
    }

    if(
    false === mysql_select_db('chuck_main')){
      
    exitWithMessage('Cannot select database: ' mysql_error());
    }

    $sql "SELECT frndescshort, envyear, frncolor, frnimageclick, FROM inventory WHERE frnsubtype = 'drlf' OR frnsubtype = 'stds';";

    $res mysql_query($sql);

    if(
    false === is_resource($res)){
      
    exitWithMessage('Cannot execute query: ' mysql_error());
    }
    ?>
    <html>
      <head>
        <title>Demo</title>
      </head>
      <body>
        <h4>We found <?php echo mysql_num_rows($res); ?> records.
        <table>
          <?php while($record mysql_fetch_assoc($res)): ?>
            <tr>
              <?php foreach($record as $value): ?>
                <td>
                  <?php echo $value?>
                </td>
              <?php endforeach; ?>
            </tr>
          <?php endwhile; ?>
        </table>
      </body>
    </html>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Member
    Join Date
    May 2011
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks alot. I'll try it out

  4. #4
    SitePoint Member
    Join Date
    May 2011
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried to run the php file but go an error pointing to line 30. Am I missing
    something? Also, I assume once I get this working that value will display
    the entire record. How to I adjust it to display specific fields ?
    thanks again for all of your help !

    <?php
    error_reporting(-1);
    ini_set('display_errors', true);

    function exitWithMessage($message){
    echo $message; exit;
    }

    $con = mysql_connect('localhost', 'chuck', '0Tnrivl33@^N');

    if(false === is_resource($con)){
    exitWithMessage('Cannot connect to database: ' . mysql_error());
    }

    if(false === mysql_select_db('chuck_main')){
    exitWithMessage('Cannot select database: ' . mysql_error());
    }

    $sql = "SELECT frndescshort, envyear, frncolor, frnimageclick, FROM inventory WHERE frnsubtype = 'drlf' OR frnsubtype = 'stds';";

    $res = mysql_query($sql);


    if(false === is_resource($res)){
    exitWithMessage('Cannot execute query: ' . mysql_error());
    }

    ?>



    <html>
    <head>
    <title>Demo</title>
    </head>
    <body>
    <h4>We found <?php echo mysql_num_rows($res); ?> records.
    <table>
    <?php while($record = mysql_fetch_assoc($res)): ?>
    <tr>
    <?php foreach($record as $value): ?>
    <td>
    <?php echo $value; ?>
    </td>
    <?php endforeach; ?>
    </tr>
    <?php endwhile; ?>
    </table>
    </body>
    </html>


    Error :

    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/chuck/public_html/displaytables.php on line 30

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Try:
    PHP Code:
    <?php
    error_reporting
    (-1);
    ini_set('display_errors'true);

    function 
    exitWithMessage($message){
      echo 
    $message;
      exit;
    }

    $con mysql_connect('localhost''chuck''0Tnrivl33@^N');

    if(
    false === is_resource($con)){
      
    exitWithMessage('Cannot connect to database: ' mysql_error());
    }

    if(
    false === mysql_select_db('chuck_main')){
      
    exitWithMessage('Cannot select database: ' mysql_error());
    }

    $sql "SELECT frndescshort, envyear, frncolor, frnimageclick FROM inventory WHERE frnsubtype IN ('drlf', 'stds');";

    $res mysql_query($sql);

    if(
    false === is_resource($res)){
      
    exitWithMessage('Cannot execute query: ' mysql_error());
    }
    ?>
    <html>
      <head>
        <title>Demo</title>
      </head>
      <body>
        <h4>We found <?php echo mysql_num_rows($res); ?> records.
        <table>
          <?php while($record mysql_fetch_assoc($res)): ?>
            <tr>
              <?php foreach($record as $value): ?>
                <td>
                  <?php echo $value?>
                </td>
              <?php endforeach; ?>
            </tr>
          <?php endwhile; ?>
        </table>
      </body>
    </html>
    Weird error though.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

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