简体   繁体   中英

PHP mySQL - How do i print only different attributes of two things that share a common name?

I'm trying to print out a list of products on a page. This is cake so far.. However Some of my items share names but have different attributes. Example would be like...:

product:

keyboard
keyboard

size:

25 inches
23 inches

color:

red
blue

My table looks something like this: id, product, size, color, so on...

So my .php I'm doing my query and print from looks something like this

<div id="accordion">
<?php
$letter = $_GET['letter'];
//echo "$id";
include("database.php");
$result = mysql_query("SELECT UPPER(product) AS upperName, PRODUCTS.* FROM PRODUCTS WHERE product LIKE '$letter%' ORDER BY UPPER(product) ");

$prodName = "";
while($row = mysql_fetch_array($result))
{
    if ($row['upperName'] != $prodName)
    {
        print('<div style="background-color:#666; padding-bottom:25px; margin-bottom:25px;">');
        print ("<h1>" . "$row[product]" . "</h1>");
    }

        print ("$row[ndc]" . "<br />");
        print ("$row[size]" . "<br />");
        print ("$row[strength]" . "<br />");
        print ("$row[imprint]" . "<br />");
        print ("$row[form]" . "<br />");
        print ("$row[color]" . "<br />");

        print('</div>');
    $prodName = $row['upperName'];  


}
mysql_close($linkID);

?>
</div>

My problem comes from trying to style the attributes..

Do you see that /div tag? I want to style that stuff within an accordion however that stuff repeats for each product that shares the same name in that accordion. So if i include the div there, it prints out for 3 times with the same name 3 end div tags (hence breaking all my html nooo!!)

Is there a way to maybe loop? or use some kind of conditional logic to print that top stuff, once for the product name that all the products share, then the loop and print all the different attributes, then when each attribute is finished, to then include my closing html?

So if i have 6 products and half are named "keyboard", and the other half are named "shoes" could i get it to print out TABLE product name: KEYBOARD table for all my attributes end table for all my attributes end TABLE

TABLE product name: SHOES table for all my attributes end table for all my attributes end TABLE

That way i can style all my attributes. I'm really sorry if this isn't correctly explained I'm still learning. Any help is appreciated!


Extra stuff that you may not need to figure out my problem just an example of a table i'm printing to and why the way the attributes print is a problem. the 1-2-3-4-5-6-7-8-9 are data for the attributes.

<table cellspacing="0" cellpadding="0" border="0" width="649">
    <tr>
        <td width="180" height="10" valign="top"></td><td width="36" height="10" valign="top"></td><td width="433" height="10" valign="top"></td>
    </tr>
    <tr>
        <td width="180" valign="top"><img src="images/slide1.gif" width="180" height="200" border="0" /></td>
        <td width="36" valign="top"></td>
        <td width="433" valign="top"><table cellspacing="0" cellpadding="0" border="0" width="433">
            <tr>
                <td valign="top"><span class="in-table-head">Name:</span></td>
                <td valign="top"><span class="in-table-name">$row[name]</span></td>
            </tr>
            <tr>
                <td valign="top"><span class="in-table-head">Therapeutic Category:</span></td>
                <td valign="top"><span class="in-table-name">$row[therapeutic]</span></td>
            </tr>
            <tr>
                <td colspan="2" valign="top"><span class="in-table-providers">Information for Providers</span></td>
            </tr>
        </table></td>
    </tr></table>
<table cellspacing="0" cellpadding="0" border="0" width="649">
    <tr>
        <td><span class="in-table-att">NDC#:</span></td>
        <td><span class="in-table-att">Strength:</span></td>
        <td><span class="in-table-att">Size:</span></td>
        <td><span class="in-table-att">Imprint:</span></td>
        <td><span class="in-table-att">Form:</span></td>
        <td><span class="in-table-att">Color:</span></td>
        <td><span class="in-table-att">Shape:</span></td>
        <td><span class="in-table-att">Pack Size:</span></td>
        <td><span class="in-table-att">Rating:</span></td>
    </tr>
</table>
<table cellspacing="0" cellpadding="0" border="0" width="649">
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
        <td>5</td>
        <td>6</td>
        <td>7</td>
        <td>8</td>
        <td>9</td>
    </tr>
</table>

Thank you again to anyone who looks at this problem!

stop matching by name but start using sku's as a selector. that way you're always spot on, no matter how many keyboards you have. set up your pages to pass the sku instead of generic name.

otherwise you need to add more criteria to your where statement like dimensions, weight, color, price etc... to get ta reliable result.

-- Update --

An example.

page.php?product_type=keyboard

<?php
// set up database
$db = mysqli_connect("localhost", "user", "pass", "database_name");
// Just using mysql escape string, you should consider adding more securty checking to prevent injection
$producttype = mysql_real_escape_string($_GET['product_type']);
$result = $db->query("SELECT * FROM `products` WHERE `products`.`type`='$producttype'");
$features["width"][]
$features["height"][]
$features["color"][]
$features["whatever"][]
while ($row = $result->fetch_assoc())
    {
    $features["width"][] = $row["width"];
    $features["height"][] = $row["height"];
    $features["color"][] = $row["color"];
    $features["whatever"][] = $row["whatever"];
    }
// printing the features
echo "You have selected $producttype<BR>";
echo "The following widths can be selected<BR><UL>";
foreach($features["width"] as $width)
    echo "<LI>$width</lI>";
echo "</UL><P>The following heights can be selected<BR><UL>";
foreach($features["heights"] as $height)
echo "<LI>$height</lI>";
    echo "</UL><P>The following colors can be selected<BR><UL>";
foreach($features["colors"] as $color)
    echo "<LI>$color</lI>";
echo "</UL><P>The following whatevers can be selected<BR><UL>";
foreach($features["whatevers"] as $whatevers)
    echo "<LI>$whatevers</lI>";
echo "</UL>";
echo "have a nice day";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM