(php)Need help with this code

Discussion in 'Software' started by laggy, May 27, 2011.

  1. laggy

    laggy Private E-2

    I am trying to get this working but for some reason the table will come up.
    Mysql thing to show the text shows up but I'm having problem with the table
    Here is the code
    Code:
    <html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
    <html>
    <link rel="Stylesheet" type="text/css" href="style.css" />
    <head>
    </head>
    <body>
    
    <?php
    
    include "var.php"; // 1.
    
    
    $query = "SELECT *," .
    "DATE_FORMAT(postdate, '%Y-%m-%d') as date " .
    "FROM news ORDER BY id DESC LIMIT $news_limit"; // 1.
    $result=mysql_query($query);
    
    $num=mysql_numrows($result);
    
    mysql_close();
    
    echo "<b><center>Database Output</center></b><br><br>";
    
    $i=0;
    while ($i < $num) {
    
    $title=mysql_result($result,$i,"title");
    $postdate=mysql_result($result,$i,"postdate");
    $content=mysql_result($result,$i,"content");
    ?>
    <table id="news">
    <tr>
    <th>1<?php$title ?></th>
    <th>Date:<?php $postdate?></th>
    </tr>
    <tr class="white">
    <td>1 <?php $content?></td>
    </tr>
    </table>
    <?php
    $i++;
    }
    
    ?>
    
    
    </body>
    </html>
    and the style.css
    Code:
    #news
    {
    font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
    width:100%;
    border-collapse:collapse;
    opacity: 0.85;
    }
    #news td, #news th 
    {
    font-size:1em;
    border:1px solid #98bf21;
    padding:3px 7px 2px 7px;
    }
    #news th 
    {
    font-size:1.1em;
    text-align:left;
    padding-top:5px;
    padding-bottom:4px;
    background-color:#A7C942;
    color:#ffffff;
    }
    #news tr.alt td 
    {
    color:#000000;
    background-color:#EAF2D3;
    opacity: 0.65;
    filter: alpha(opacity=50);
    
    }
    #news tr.white td
    {
    background-color:#FFFFFF;
    color:#000000;
    
    
            /* Modern!
            /* Firefox 0.9+, Safari 2?, Chrome any?
            /* Opera 9+, IE 9+ */
            opacity: 0.5;
    		filter: alpha(opacity=50);
    
    
    
    }
     
  2. Mada_Milty

    Mada_Milty MajorGeek

    I'm not sure exactly what you are trying to achieve, but I notice that in your tables, in the first row, you have two header cells, and in the second row, you have only one cell, which may cause alignment issues. You could try adding a second cell, so all rows match, or you can add a "colspan=2" attribute to the single content cell to make it stretch across two columns.

    If that doesn't help, maybe a little more explanation of what you are trying to do, and what your code IS doing would help.
     
  3. laggy

    laggy Private E-2

    The extra th is not the problem ,if you want you can check this
    http://86.43.189.109/news2.html which is what Im trying to achieve but I want it in php.
     
  4. laggy

    laggy Private E-2

    Actually try this as I don't keep my pc turned on the whole time
    http://h2f1.com/
     
  5. Mada_Milty

    Mada_Milty MajorGeek

    Here's what I would do, assuming you've already done your database connection rigamarole:

    Code:
    <?
        $qry = mysql_query ("SELECT *, DATE_FORMAT(postdate, '%Y-%m-%d')
        as date FROM news ORDER BY id DESC LIMIT $news_limit;");
        while ($row = mysql_fetch_assoc($qry)) { ?>
            <table>
                <tr>
                    <th><? echo $row['title']; ?></th>
                    <th>Date: <? echo $row['date']; ?></th>
                </tr>
                <tr>
                    <td>1 <? echo $row['content']; ?></td>
                </tr>
            </table><?
        } ?>
    
     
  6. PC-XT

    PC-XT Master Sergeant

    AFAIK, this code section won't work:
    Code:
    <th>1<?php$title ?></th>
    <th>Date:<?php $postdate?></th>
    </tr>
    <tr class="white">
    <td>1 <?php $content?></td>
    Specifically, code like <?php$var?> doesn't work. It needs an echo to send the variable to the browser: <?php echo$var?>
    Add an echo command to each of those tags, and see if that makes it better.
    When I try to run the code without echo in a couple PHP versions, I just get "parse error, unexpected T_VARIABLE"

    Or try Mada_Milty's idea. I didn't see that post.:-o
     
    Last edited: May 28, 2011
  7. laggy

    laggy Private E-2

    Thanks Guys, both of ye are right in away but it doesn't work with $row so I tried
    Code:
    while ($i < $num) {
    
    $title=mysql_result($result,$i,"title");
    $postdate=mysql_result($result,$i,"postdate");
    $content=mysql_result($result,$i,"content");
    
    ?>
    <table id="news">
    <tr>
    <th><? echo $title ?></th>
    <th>Date:<? echo $postdate ?></th>
    </tr>
    <tr class="white">
    <td><?php echo $content ?></td>
    </tr>
    </table> 
    <?
    $i++;
    }
    
    ?>
    And it worked :)
    Now I just have to fix up the css
    Thanks ;)
     
  8. Mada_Milty

    Mada_Milty MajorGeek

    True, unless the variable is inside a PHP string, in that case, it will automatically expand without an echo statement :) Probably the cause of the OP's confusion there.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds