Hi all, i have m y search setup which brings back a part number, a description and then underneath any associated manufacturers name and part number which could range from 1 to 20, my problem is i just want to populate my table if there is something there and if there isnt then dont show it.

lets say if i search 1212 it would give me
part number 1212
description = various models
volvo = 3434
vw = 5656
opel = ?

i want to hide the empty row becuase there isnt anything there
hope this is clear i tried my best to explain

regards
dan

Dani AI

Generated

Good news: you do not need to print a row and then hide it. Let the database return only the manufacturer rows that actually exist. Your hunch about the schema is right, . Put manufacturers in rows, not columns: parts(id, part_number, description), manufacturers(id, name), and a link table part_xref(part_id, manufacturer_id, mfg_part_number). That way you never loop 120 columns or emit empty markup. ’s idea works as a quick fix, but filtering at the SQL layer keeps the DOM clean. And is spot on about joins; here is a practical pattern.

// Given a search term $q (e.g., "1212"), fetch the part and only real cross-refs
$pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);

$partStmt = $pdo->prepare(
    'SELECT id, part_number, description FROM parts WHERE part_number = :q LIMIT 1'
);
$partStmt->execute([':q' => $q]);
if ($part = $partStmt->fetch(PDO::FETCH_ASSOC)) {
    echo '<h3>' . htmlspecialchars($part['part_number']) . '</h3>';
    echo '<p>' . htmlspecialchars($part['description']) . '</p>';

    $xrefStmt = $pdo->prepare(
        'SELECT m.name AS manufacturer, x.mfg_part_number
         FROM part_xref x
         JOIN manufacturers m ON m.id = x.manufacturer_id
         WHERE x.part_id = :pid
           AND TRIM(COALESCE(x.mfg_part_number, "")) <> ""
         ORDER BY m.name'
    );
    $xrefStmt->execute([':pid' => $part['id']]);

    foreach ($xrefStmt as $row) {
        echo '<tr><td>' . htmlspecialchars($row['manufacturer']) .
             '</td><td>' . htmlspecialchars($row['mfg_part_number']) . '</td></tr>';
    }
}

Notes: store unknowns as NULL (not "?"), and index part_xref(part_id) and part_xref(manufacturer_id) for speed. This design prints nothing for Opel when there is no number, exactly as you wanted.

Recommended Answers

All 3 Replies

<?php if($mydata>0)  { ?>
<tr style='display:inline'>
<td> this is visible</td>
</tr>

<?php
}
else
{
?>
<tr style='display:none'>
<td> invisible</td>
</tr>

<?php
}
?>

thanks for that, it works perfectly, although i cant help think that maybe i need to restructure my database becuase i have all the different manufacturs in there own colum and so the code you gave me has to run for each column and there are around 120, and although it will work great i want to do it properly so thanks for you help im going to go away now and try to redo the database

The structure is fine - you just need to run a better query. Lookup how to do Joins or querying multiple tables.

example

$query = "SELECT `p`.`partnumber`, `p`.`description`, `m`.`manufacturer` FROM `parts` `p`, `manufacturers` `m` WHERE `p`.`manufacturer_id` = `m`.`manufacturer_id`"

This will set a condition to get both the parts information and get the manufacturer name where the id's are linked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.