Welcome to WebDesignForums.net!
You're currently viewing WDF as a guest. By registering for a free account, you'll be able to participate with other members in our friendly community. Being a member allows you to ask questions and get answers for those troublesome web development tasks!

In addition, as a member you'll be able to post your websites up for review. Using our unique website review system you can gain some amazing feedback from some of the best web developers around. This is a completely free service to all registered members.

Ready to register yet? Registration is 100% free. Click Here To Join Now!

Cycling through mysql_fetch_array

Discussion in 'Database Systems Help' started by kalchas, Feb 11, 2008.

  1. Offline

    kalchas New Member

    Message Count:
    49
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Scotts Valley, CA
    Hi all,

    So I'm trying to cycle through some mysql_fetch_array results and provide different code in a form depending on the results. I'm running into difficulty with the function below. $iteration refers to where I'm at in the database, $row is the result of a mysql_fetch_array. The other global variables are just pieces of html that make the whole thing look nice. I'm able to get pretty much everything to work with the exception of the switch functions below.

    PHP:

    function createSizeInputCells()
        {
        global 
    $row;
        global 
    $iteration;
        global 
    $sizeInputId;
        global 
    $nameInput;
        global 
    $classInput;
        global 
    $typeAndJavascriptInput;
        global 
    $inputEnderAndStarter;
        
        
    $sizeClassifier NULL;
        
    $sizeInputId NULL;
        
    $sizeInputName NULL;
        
    $sizeInputClass NULL;
        
    $sizeInputValue NULL;
        
    $i 1;
        while (
    $i<=9)
            {
            switch (
    $i)
                {
                case 
    1:
                
    $sizeClassifier $row["SI1"];
                break;
                case 
    2
                
    $sizeClassifier $row["SI2"];
                break;
                case 
    3:
                
    $sizeClassifier $row["SI3"];
                break;
                case 
    4:
                
    $sizeClassifier $row["SI4"];
                break;
                case 
    5:
                
    $sizeClassifier $row["SI5"];
                break;
                case 
    6:
                
    $sizeClassifier $row["SI6"];
                break;
                case 
    7:
                
    $sizeClassifier $row["SI7"];
                break;
                case 
    8:
                
    $sizeClassifier $row["SI8"];
                break;
                case 
    9:
                
    $sizeClassifier $row["SI9"];
                break;
                }
                
            
    $sizeInputId "siz".$i."[".$iteration."]";
            
    $sizeInputName "size".$i."[".$iteration."]";

            switch (
    $sizeClassifier)
                {
                case 
    "xs":
                
    $sizeInputClass "empty";
                break;
                default:
                
    $sizeInputClass "units";
                }

            if (
    $sizeInputClass "units")
                {
                
    $sizeInputValue 0;
                }

            else if(
    $sizeInputClass "empty")
                {
                
    $sizeInputValue " ";
                } 
    //Set $sizeInputValue based on $sizeInputClass.

            
    $sizeCellArray = array($sizeInputId$nameInput$sizeInputName$classInput$sizeInputClass$typeAndJavascriptInput$sizeInputValue$inputEnderAndStarter);

            
    $sizeCellPrint implode(" "$sizeCellArray);

            echo 
    $sizeCellPrint;

            
    $i++;
            }
        }

    I'd like to be able to change the appearance of the form elements depending on the value of $row at each size, which is the reason for the two classes.

    Any help will be much appreciated.

    Cheers,

    K.


  2. Offline

    kalchas New Member

    Message Count:
    49
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Scotts Valley, CA
    Just an update... I've managed to resolve this issue. The new code for the order form is:

    PHP:

    ob_start
    ("ob_gzhandler");

    include 
    "databaseConnectors.php";
    connectToOrderManagementDB();
    $mySQLQuery "SELECT * FROM stylelist";
    $result mysql_query($mySQLQuery);

    $sizeRowString "<tr><td>Style Number</td><td>Style Name</td><td>Color Code</td><td>Color Name</td><td>sizeRowThe1</td><td>sizeRowThe2</td><td>sizeRowThe3</td><td>sizeRowThe4</td><td>sizeRowThe5</td><td>sizeRowThe6</td><td>sizeRowThe7</td><td>sizeRowThe8</td><td>sizeRowThe9</td><td>Total Units</td><td>Price</td><td>Total Dollars</td></tr>";
    $startOfCellsString "<tr><td><input id='styNumberiterateIt' name='styleNumberiterateIt' class='stno' type='text' readonly='readonly' value='styleNumberValueThe1' /></td><td><input id='styNameiterateIt' name='styleNameiterateIt' class='stna' type='text' readonly='readonly' value='styleNameValueThe1' /></td><td><input id='";
    $colorCodeString "colCodeiterateIt' name='colorCodeiterateIt' class='clno' type='text' readonly='readonly' value='colorValueThe1' /></td><td>colorNameThe1</td><td><input id='";
    $sizeCellString "sizeInputIdThe1' name='sizeInputNameThe1' class='sizeInputClassThe1' type='text' onchange='sumUnits(); recheckTotals();' value='0' /></td><td><input id='";
    $endOfCellsString "totUnitsiterateIt' name='totalUnitsiterateIt' class='unitTotal' type='text' readonly='readonly'/></td><td><input id='priNameiterateIt' name='priceNameiterateIt' class='price' type='text' readonly='readonly' value='priceValueThe1' /></td><td><input id='totDollarsiterateIt' name='totalDollarsiterateIt' class='dollarTotal' type='text' readonly='readonly' /></td></tr>";

    $sizeRowReplacements = array( sizeRowThe1sizeRowThe2sizeRowThe3sizeRowThe4sizeRowThe5sizeRowThe6sizeRowThe7sizeRowThe8sizeRowThe9);
    $startOfCellsReplacements = array(iterateItstyleNumberValueThe1styleNameValueThe1);
    $colorCodeReplacements = array(iterateItcolorValueThe1colorNameThe1);
    $sizeCellReplacements = array(sizeInputIdThe1sizeInputNameThe1sizeInputClassThe1);
    $endOfCellsReplacements = array(iterateItpriceValueThe1); 

     
    //These variables flesh out the HTML.

    $iteration 0;
        
    while (
    $row mysql_fetch_array($resultMYSQL_ASSOC))
        {
        
    $colorCodesArray = array($row["CO1"], $row["CO2"], $row["CO3"], $row["CO4"], $row["CO5"], $row["CO6"]); 
        
    $colorNamesArray= array($row["CN1"], $row["CN2"], $row["CN3"], $row["CN4"], $row["CN5"], $row["CN6"]);
        
    $sizeArray = array($row["SI1"], $row["SI2"], $row["SI3"], $row["SI4"], $row["SI5"], $row["SI6"], $row["SI7"], $row["SI8"], $row["SI9"]);
        
    ob_start();
        
    sizeRow($sizeArray$sizeRowString$sizeRowReplacements);
        
    ob_end_flush();
        
    orderFormRow($row$iteration$sizeArray$colorCodesArray$colorNamesArray$startOfCellsString$startOfCellsReplacements$colorCodeString$colorCodeReplacements,  $sizeCellString$sizeCellReplacements$endOfCellsString$endOfCellsReplacements) ;
        
    $iteration++;
        }

    function 
    sizeRow($sizeArray$sizeRowString$sizeRowReplacements)
        {
        
    $theSizeRow str_replace($sizeRowReplacements$sizeArray$sizeRowString);
        echo 
    $theSizeRow;
        }

    function 
    createStartOfCells($row$iteration$startOfCellsString$startOfCellsReplacements)
        {
        
    $startOfCellsArray = array("[$iteration]"$row["NUM"], $row["NAM"]); 
        
    $theStartOfCells str_replace($startOfCellsReplacements$startOfCellsArray$startOfCellsString);
        echo 
    $theStartOfCells;     
        }
        
    function 
    createColorCodeCells($colorValue$colorName$row$iteration$colorCodeString$colorCodeReplacements)
        {
        
    $colorCodeArray = array($iteration$colorValue$colorName);
        
    $colorCodeCells str_replace($colorCodeReplacements$colorCodeArray$colorCodeString);
        
        echo 
    $colorCodeCells;
        }

    function 
    createSizeInputCell($sizeArray$j$k$row$iteration$sizeCellString$sizeCellReplacements)
        {
        
    $index $k 1;

        if(
    $sizeArray[$j]==NULL)
            {
            
    $sizeInputClass "empty";
            }
        else
            {
            
    $sizeInputClass "units";
            }
            
        
    $sizeCellArray = array("siz".$index."[".$iteration."]""size".$index."[".$iteration."]"$sizeInputClass);
        
    $sizeCellInstance str_replace($sizeCellReplacements$sizeCellArray$sizeCellString);
        echo 
    $sizeCellInstance
        }

    function 
    createEndOfCells($iteration$endOfCellsString$endOfCellsReplacements)
        {
        
    $priceValue NULL//Get price from mySQL.
        
        
    $endOfCellsArray = array("[$iteration]"$priceValue);
        
        
    $endOfCells str_replace($endOfCellsReplacements$endOfCellsArray$endOfCellsString);
        echo 
    $endOfCells;
        }

    function 
    orderFormRow($row$iteration$sizeArray$colorCodesArray$colorNamesArray$startOfCellsString$startOfCellsReplacements$colorCodeString$colorCodeReplacements,  $sizeCellString$sizeCellReplacements$endOfCellsString$endOfCellsReplacements)
        {
        for(
    $i=0$i<6$i++)
            {
            
    $colorValue $colorCodesArray[$i];
            
    $colorName $colorNamesArray[$i];
            if(
    $colorValue != NULL)
                {
                
    ob_start();
                
    createStartOfCells($row$iteration$startOfCellsString$startOfCellsReplacements);
                
    createColorCodeCells($colorValue$colorName$row$iteration$colorCodeString$colorCodeReplacements);
                
    ob_end_flush();
                for(
    $j=0$j<=8$j++)
                    {
                    
    $k=$j;
                    
    ob_start();
                    
    createSizeInputCell($sizeArray$j$k$row$iteration$sizeCellString$sizeCellReplacements);
                    
    ob_end_flush();
                    }
                    
    ob_start();
                    
    createEndOfCells($iteration$endOfCellsString$endOfCellsReplacements);
                    
    ob_end_flush();
                }
            }
        }
    If anyone can spot ways to improve how quickly this loads, it would be much appreciated. This is included inside HTML that provides the start of the form and the start of the table.

    Cheers,

    K.


  3. Offline

    smoseley Administrator

    Message Count:
    9,727
    Likes Received:
    192
    Trophy Points:
    63
    Location:
    Boston, MA
    Just a note.... it looks like you're using one huge denormalized table for your data:

    $colorCodesArray = array($row["CO1"], $row["CO2"], $row["CO3"], $row["CO4"], $row["CO5"], $row["CO6"]);
    $colorNamesArray= array($row["CN1"], $row["CN2"], $row["CN3"], $row["CN4"], $row["CN5"], $row["CN6"]);
    $sizeArray = array($row["SI1"], $row["SI2"], $row["SI3"], $row["SI4"], $row["SI5"], $row["SI6"], $row["SI7"], $row["SI8"], $row["SI9"]);



    You should redesign it with proper "3rd normal form" (do some research on it) using foreign key relationships so you're not putting metadata in your column headers.

    Specifically, you should have the following tables in your database:

    CREATE TABLE colors (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL
    );

    INSERT INTO colors (name) VALUES ('red'), ('blue'), ('yellow'), ('etc.');

    CREATE TABLE sizes (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL
    );

    INSERT INTO sizes (name) VALUES ('SM'), ('MD'), ('LG'), ('etc.');

    CREATE TABLE products (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL
    );

    INSERT INTO styles (name) VALUES ('Summer Dress'), ('Fancy shorts'), ('Ugly shoes'), ('etc.');

    CREATE TABLE product_colors (
    product_id INT NOT NULL REFERENCES products(id),
    color_id INT NOT NULL REFERENCES colors(id)
    );

    INSERT INTO product_colors (product_id, color_id) VALUES (1, 1), (1, 2), (2, 1), (2, 2);

    CREATE TABLE product_sizes (
    product_id INT NOT NULL REFERENCES products(id),
    size_id INT NOT NULL REFERENCES sizes(id)
    );

    INSERT INTO product_sizes (product_id, size_id) VALUES (1, 1), (1, 2), (2, 1), (2, 2);


    Then you could query by joining the tables and get your data in whatever format you want.

    Also, you could add colors or sizes without modifying your table structure.


Share This Page