Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP & mySQL loop and formatting *help*

    Hi guys,

    Basically what i'm doing is making a website for a band. For the news section i'm trying to just make it so the actual band members can go in and add new news content with out having to contact me every time they want new news posted. So i'm just making a simple form which saves to a mysql database and then is pulled from the database and viewed in tables.

    I have it working so it views the latest entry but it wont show all of them... currently i have 3 entries in the database. I don't know how to make it so it will create a new table for the number of entries and display them all.

    Here is the code i came up with:

    PHP Code:
    <body>
    <?
    $host 
    'localhost'
    $user 'username'
    $pass 'password'
    $db 'ctechnow_news'

    $m_connect = @mysql_connect($host,$user,$pass) or die ("Error while trying to connect to mysql: " mysql_error()); 

    $select_my_db = @mysql_select_db($db,$m_connect) or die ("Error selecting database: " mysql_error()); 

    $query="SELECT * FROM tblNews";
    $result=mysql_query($query);

    $num=mysql_numrows($result);

    mysql_close();

    $i=0;
    while (
    $i $num) {

    $title=mysql_result($result,$i,"fldTitle");
    $message=mysql_result($result,$i,"fldMessage");

    $i++;
    }

    ?>

    <table width="708" height="97" border="0">
      <tr>
        <td width="46"><div align="right"><span class="style1">Topic:</span></div></td>
        <td width="652"><? echo $title?></td>
      </tr>
      <tr>
        <td><div align="right"><span class="style1">Date:</span></div></td>
        <td>PHP Date</td>
      </tr>
      <tr>
        <td><div align="right"><span class="style1">Message:</span></div></td>
        <td><? echo $message?></td>
      </tr>
      
    <?
    $i
    ++;
    ?>
      
    </table>
    <p>&nbsp;</p>
    </body>
    (ignore the date portion i havn't worked on that yet)

    you can post comment from here:
    http://ctechnow.com/jdulude/test/mys...post_news.html

    and view the output here:
    http://ctechnow.com/jdulude/test/mys...news_table.php


    also my other question is once that problem is resolved and it shows multiple tables, is there a way to make every other post a different color. I was thinking maybe an if statement could do it. The database has a 'fldID' which autonumbers. So maybe something like when the odd fldID numbers are displayed it will change the background color of the table.

    Any help would be great,
    Thanks,
    ~Jamie

  • #2
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    mysql_num_rows() is the correct function. Yes, I've seen this tutorial that has the typo with no underscore, so its just a mistake.

    Here is some more fixes.

    PHP Code:
    <body> 
    <? 
    $host 
    'localhost';  
    $user 'username';  
    $pass 'password';  
    $db 'ctechnow_news';  

    $m_connect = @mysql_connect($host,$user,$pass) or die ("Error while trying to connect to mysql: " mysql_error());  

    $select_my_db = @mysql_select_db($db,$m_connect) or die ("Error selecting database: " mysql_error());  

    $query="SELECT * FROM tblNews"
    $result=mysql_query($query); 

    $num=mysql_num_rows($result); 

    mysql_close(); 

    $i=0
    while (
    $i $num) { 

    $title=mysql_result($result,$i,"fldTitle"); 
    $message=mysql_result($result,$i,"fldMessage"); 

    ?> 

    <table width="708" height="97" border="0"> 
      <tr> 
        <td width="46"><div align="right"><span class="style1">Topic:</span></div></td> 
        <td width="652"><? echo $title?></td> 
      </tr> 
      <tr> 
        <td><div align="right"><span class="style1">Date:</span></div></td> 
        <td>PHP Date</td> 
      </tr> 
      <tr> 
        <td><div align="right"><span class="style1">Message:</span></div></td> 
        <td><? echo $message?></td> 
      </tr> 
       
    <? 

    $i
    ++; 

    }

    ?> 
       
    </table> 
    <p>&nbsp;</p> 
    </body>

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that worked great... displays all the posts now. does any one know how to change the background colors which i explained above?

    Also how would i make it so displays the posts in reverse order? So that the newest post is on top?

    Thanks,
    ~Jamie

  • #4
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $query="SELECT * FROM tblNews ORDER BY date DESC";

    Date would be the time field you want to sort by. It might be 'posttime' or whatever.
    "$question = ( to() ) ? be() : ~be();"

  • #5
    Regular Coder
    Join Date
    Jan 2006
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can someone look at my if else statement to see if i'm coming even close? This is for trying to change the bg color of every other table. Right now i set the variable "odd" to 3 just to test so it should only change the color of the table displaying fldID 3. You can see in the table where i'm trying to slip my the variable bg_color to try and change the table

    Thanks.

    PHP Code:
    <body>
    <table width="200" border="0" align="center">
      <tr>
        <td><div align="center">H2S News </div></td>
      </tr>
    </table>
    <p>&nbsp;</p>

    <?
    $host 
    'localhost'
    $user 'username'
    $pass 'password'
    $db 'ctechnow_news'

    $m_connect = @mysql_connect($host,$user,$pass) or die ("Error while trying to connect to mysql: " mysql_error()); 

    $select_my_db = @mysql_select_db($db,$m_connect) or die ("Error selecting database: " mysql_error()); 

    $query="SELECT * FROM tblNews ORDER BY fldDate DESC";
    $result=mysql_query($query);

    $num=mysql_num_rows($result);

    mysql_close();

    $i=0;
    while (
    $i $num) {

    $title=mysql_result($result,$i,"fldTitle");
    $date=mysql_result($result,$i,"fldDate");
    $show_date=date("h:ia m/d/Y"$date);
    $message=mysql_result($result,$i,"fldMessage");


    ?>
    <?
    //IF-Else Statement for changing table color

    $id=mysql_result($result,$i,"fldID");
    $odd=3;


    if (
    $id==$odd){
     
    $bg_color "83090C";
    }
    ?>


    <table width="708" height="97" border="0" bgcolor=<? $bg_color ?>>
      <tr>
        <td width="46"><div align="right"><span class="style1">Topic:</span></div></td>
        <td width="652"><? echo $title?></td>
      </tr>
      <tr>
        <td><div align="right"><span class="style1">Date:</span></div></td>
        <td><? echo $show_date?></td>
      </tr>
      <tr>
        <td><div align="right"><span class="style1">Message:</span></div></td>
        <td><? echo $message?></td>
      </tr>
    <br />
    <hr />
      
    <?
    $i
    ++;
    }
    ?>
      
    </table>

    <p>&nbsp;</p>
    </body>
    Last edited by JayStang; 01-09-2006 at 04:09 AM.

  • #6
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    You can use something like this:

    PHP Code:
    // somewhere outside the while loop
    $bgcolors = array('evencolor''oddcolor');

    // then, inside, wherever you specify the color use
    echo $bgcolors[$i%2]; 
    % is the modulus operator, used to determine the remainder after division. Modulus by 2 returns 0 for even numbers and 1 for odd numbers, so it will alternate the index of the $bgcolors array.

    edit for bonus limited edition bitwise method:
    PHP Code:
    echo $bgcolors[1&$i]; 
    Last edited by ralph l mayo; 01-09-2006 at 04:46 AM.

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    mysql_num_rows() is the correct function. Yes, I've seen this tutorial that has the typo with no underscore, so its just a mistake.
    mysql_numrows() is a deprecated function not a typo, I mean it IS wrong cos its deprecated, but I still sadly see lots of code that uses it.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #8
    Regular Coder
    Join Date
    Jan 2006
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ralf... thank you for the help... that worked perfectly!............in Internet Explorer.

    When i tried it in firefox it doesn't display any background color at all. Its just plan solid background all the way through.

    Check it out
    http://ctechnow.com/jdulude/test/mys...able_color.php

    i have no idea why its doing this. This is the code that i'm using after your help.

    PHP Code:
    <body>
    <table width="200" border="0" align="center">
      <tr>
        <td><div align="center">H2S News </div></td>
      </tr>
    </table>
    <p>&nbsp;</p>

    <?
    $host 
    'localhost'
    $user 'ctechnow'
    $pass 'ctechn2'
    $db 'ctechnow_news'

    $m_connect = @mysql_connect($host,$user,$pass) or die ("Error while trying to connect to mysql: " mysql_error()); 

    $select_my_db = @mysql_select_db($db,$m_connect) or die ("Error selecting database: " mysql_error()); 

    $query="SELECT * FROM tblNews ORDER BY fldDate DESC";
    $result=mysql_query($query);

    $num=mysql_num_rows($result);

    mysql_close();



    //Statement for changing table color

    $id=mysql_result($result,$i,"fldID");


    $bgcolors = array('b9b989''83090C');



    $i=0;
    while (
    $i $num) {

    $title=mysql_result($result,$i,"fldTitle");
    $date=mysql_result($result,$i,"fldDate");
    $show_date=date("h:ia m/d/Y"$date);
    $message=mysql_result($result,$i,"fldMessage");


    ?>


    <table width="708" height="97" border="0" align="center" bgcolor=<? echo $bgcolors[$i%2]; ?>>
      <tr>
        <td width="46" valign="top"><div align="right"><span class="style1">Topic:</span></div></td>
        <td width="652"><? echo $title?></td>
      </tr>
      <tr>
        <td valign="top"><div align="right"><span class="style1">Date:</span></div></td>
        <td><? echo $show_date?></td>
      </tr>
      <tr>
        <td valign="top"><div align="right"><span class="style1">Message:</span></div></td>
        <td><? echo $message?></td>
      </tr>
      
    <?
    $i
    ++;
    }
    ?>
      
    </table>

    <p>&nbsp;</p>
    </body>
    anyone know why it wouldn't display in the table bg colors in firefox and how to fix it?

  • #9
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Code:
    $bgcolors = array('b9b989', '83090C');
    ...
    bgcolor=<? echo $bgcolors[$i%2]; ?>>
    bgcolor is deprecated, replace the relevant code with something like:
    style="background:<? echo $bgcolors[$i%2]; ?>;"

    Note the attributes are surrounded by quotation marks with a trailing semicolon.
    Also, preface your color codes with a hash mark, like '#b9b989'

  • #10
    Regular Coder
    Join Date
    Jan 2006
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    perfect!... all it needed was the '#' in front of it... thank you for all the help!!

    ~Jamie

  • #11
    New Coder
    Join Date
    May 2006
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I just try this:

    <?php $bgcolors = array('#b9b989', '#83090C'); ?>
    <?php do { ?>
    <tr bgcolor=<php echo $bgcolors[$i%2]; ?>>
    <td width="15" class="bodytext11_blue"><div align="left"><?php echo $row_Recordset1['T&#237;tulo Honor&#237;fico']; ?></div></td>
    <td width="300" class="bodytext11_blue"><div align="left"><?php echo $row_Recordset1['Nome']; ?></div></td>
    <td width="180" class="bodytext11_blue">

    but it shows the same color, can anyone discover whats wrong?


  •  

    Posting Permissions

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