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 10 of 10
  1. #1
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,778
    Thanks
    19
    Thanked 155 Times in 146 Posts

    using php to create product catalog....

    Using php, I'm trying to make the rows of my product catalog alternate colors, like the catalog looks on this page:

    http://www.mediamogulsweb.com/access...#product_block

    So I devised a php script to perform database queries, populate my catalog, and automatically format my tables with alternating colors.....everything works except for the colors...see this page:

    http://www.mediamogulsweb.com/create...nt_catalog.php

    Green is a good color for grass, but not for my catalog....haha

    I specify two different hexadecimal shades of gray in my code, but it no worky....Here is the code:

    PHP Code:
    <HTML>
    <HEAD>
    <TITLE></TITLE>
    </HEAD>

    <BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" VLINK="#800000" ALINK="#FF00FF">


    <p><font color="#3333CC" size="+2" face="Tunga"><strong><font color="#666666" face="Palatino Linotype"><em><font face="Times New Roman, Times, serif">Accessories</font></em></font></strong></font></p>
    <table border="0" cellspacing="1" cellpadding="8" width="650">
      <tr bgcolor="#666666">
        <td><b><font color="#FFFFFF" face="Palatino Linotype">Item #</font></b></td>
        <td><b><font color="#FFFFFF" face="Palatino Linotype">Description</font></b></td>
        <td><b><font color="#FFFFFF" face="Palatino Linotype">Price</font></b></td>
      </tr>


    <?php

    // File create_component_catalog.php


    $dbcon mysql_connect("localhost","username","password");

    mysql_select_db("MyDatabase",$dbcon);


    $queryA "SELECT product_code FROM parts WHERE product_code LIKE '%AC%'";
    $resultA mysql_query($queryA);
    $limit mysql_num_rows($resultA);


    $color1 "#CCCCCC";
    $color2 "#E5E5E5";


    function 
    odd_even($num)
    {
        if(!
    is_long($num/2))
        {
            
    $rtn 1;
        }
        else
        {
            
    $rtn 0;
        }
        return 
    $rtn;
    }



    for (
    $i 1$i <= $limit$i++)
    {
        if (
    odd_even($i) == 0)
            
    $row_color $color1;
        else
            
    $row_color $color2;

        
    $query "SELECT product_code FROM parts WHERE product_code LIKE '%AC%' AND RECNO = $i";
        
    $result mysql_query($query);
        if (
    mysql_num_rows($result) >= '1')
        {
            while (
    $row mysql_fetch_assoc($result))
            {
                echo 
    '<tr bgcolor="$row_color">
                <td>'
    $row['product_code'], '</td>';
            }
        }
        else
        {
            echo 
    '<span style="font-style:italic">No records found.</span><br><br>';
        }

        
    $query2 "SELECT description FROM parts WHERE product_code LIKE '%AC%' AND RECNO = $i";
        
    $result2 mysql_query($query2);
        if (
    mysql_num_rows($result2) >= '1')
        {
            while (
    $row2 mysql_fetch_assoc($result2))
            {
                echo 
    '<td>'$row2['description'], '</td>';
            }
        }
        else
        {
            echo 
    '<span style="font-style:italic">No records found.</span><br><br>';
        }

        
    $query3 "SELECT price FROM parts WHERE product_code LIKE '%AC%' AND RECNO = $i";
        
    $result3 mysql_query($query3);
        if (
    mysql_num_rows($result3) >= '1')
        {
            while (
    $row3 mysql_fetch_assoc($result3))
            {
                echo 
    '<td>$'$row3['price'], '</td></tr>';
            }
        }
        else
        {
            echo 
    '<span style="font-style:italic">No records found.</span><br><br>';
        }
    }



    ?>



    </table>


    </BODY>
    </HTML>
    Can anyone see where my problem with the color lies?...thanks in advance
    Last edited by chump2877; 02-02-2005 at 12:51 AM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Two things:
    First: Use $i % 2 == 0 instead of your function to check for even or odd
    Second: Your background color is being set to $row_color, no its value. Use ' . $row_color . ' to use variables inside of single quotes.

    However, with echo, it is faster to use commas with it, as you seem to be using.

    echo '<tr bgcolor="', $row_color, '">';

  • #3
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    String literals.

    echo '$var'; //outputs same as echo "\$var"
    echo "$var";//outputs same as echo $var

    First: Use $i % 2 == 0 instead of your function to check for even or odd
    I believe that requires even more processing than:
    PHP Code:
    if($alt){

    }else{

    }
    $alt = !$alt
    It's limited to simple binary arithmetic there, no real calculation.
    Last edited by Mhtml; 02-02-2005 at 01:57 AM.
    Omnis mico antequam dominus Spookster!

  • #4
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Actually, I retract my previous statement, $i % 2 would be the fastest.

    Edit: And he is checking for even and odd, not if a value is set or not, but that is is how it works. Modulus divides $a ($i for this model) by $b (2 in this model) and checks the remainder. If it $a is even, it returns zero, and thus false. But if it is odd, it will return some number and thus true.

    Faster than your wonky if else statement.
    Last edited by Kurashu; 02-02-2005 at 02:18 AM.

  • #5
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,778
    Thanks
    19
    Thanked 155 Times in 146 Posts
    Thanks for the help fixing the color of my catalog and the tip for finding even/odd numbers....Everything works now....
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #6
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Kurashu
    Faster than your wonky if else statement.
    Sigh, before you get all excited about your own supposed superiority think about the maths behind this. You are doing divisions with modulus, I am just inverting a single bit.

    Well these are the times I get for both methods:


    Modulus = 0.000109
    Invert = 9.7E-5

    Now is it me, or do I only count 3 zero's after that decimal point in the modulus time? The only thing wonky here is your maths.

    [edit:] Managed to get slightly faster times for both of them on a faster machine, however modulus is still lagging. But for ranges from 0 - 10, 0 - 100 and 0 - 1000 inverting still kicks modulus butt.
    Last edited by Mhtml; 02-02-2005 at 01:49 PM.
    Omnis mico antequam dominus Spookster!

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    its even simpler than that , if/else in this case is a simple comparison , 50% of the time the }else{ is never even seen by the interpreter.

    regardless , both methods are valid , the modulus is the more popular method employed though personally if/else makes more sense to me where there are only 2 possible states.

    anyway , I posted to say keep it nice , any more bickering & I close the thread
    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
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Perhaps I read wrong I did only just wake up so more than likely, both methods will need to use an if/else control structure. I was just proposing a faster condition.

    PHP Code:
    if($alt){
        
    $row_color $color1;
    }else{
        
    $row_color $color1;
    }
    $alt = !$alt
    vs
    PHP Code:
    if($i%2==0){
        
    $row_color $color1;
    }else{
        
    $row_color $color1;

    I didn't mean anything by my somewhat colorful post, was pretty tired at the time it must of been like 1am.
    Last edited by Mhtml; 02-03-2005 at 02:12 AM.
    Omnis mico antequam dominus Spookster!

  • #9
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Either could be reduced a ternary expression as well.

    PHP Code:
    //color1 for true, color2 for false
    $color = ($i%2==0) ? $color1 $color2//remove == 0 for reverse
    $color = ($alt) ? $color1 $color2
    I now know what your if statement was doing now too. It took some thought, something I wasn't able to clearly do at the time. Stupid flu-cold-sickness thingy. Oh well

    Also, on a more personal note, I thought I was spelling color wrong.

  • #10
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Fair enough.
    I would write it that way in my own code, but for clarity I write it as a standard control structure.

    And as for color, we actually spell it colour here in Aus, but most programming and markup languages spell it color so I stick to that way on the web.
    Omnis mico antequam dominus Spookster!


  •  

    Posting Permissions

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