Jump to content

Recommended Posts

Hello,

 

I need help with some code because I cant figure it out on my own:(

 

This is my Class:

 

<?php

include_once "connect.class.php";

class merken extends connect
{    
    
    private $merkenlijst;
    
    public function getMerken($database, $id = NULL)
    {
        $sql = "SELECT * FROM ".$database."_merken";
        if(!empty($id))
        {
            $sql .= " WHERE merk_code=:id LIMIT 1";
        }
        else
        {
            $sql .= " ORDER BY merk_naam ASC";
        }
        try
        {
            $stmt = $this->db->prepare($sql);
            if(!empty($id)){ $stmt->bindParam(":id", $id, PDO::PARAM_STR); }
            $stmt->execute();
            $this->merkenlijst = $stmt->fetchAll(PDO::FETCH_OBJ);
            $stmt->closeCursor();
            
            return $this->merkenlijst;
        }
        catch (Exception $e)
        {
            die ( $e->getMessage() );
        }
    }
    
    public function __construct($dbo = NULL)
    {
        parent::__construct($dbo);
    }
}

?>

 

 

And this is my code for the output:

 

<?php
    include_once "class/merken.class.php";
    $merkclass = new merken($dbo);
?>
                    <br />
                    <div class="bandwielkolom">
                        <form action="index.php?lang=nl&p=<?php echo $_GET['p']; ?>#keuze" method="post">
                            <table class="bandentabel">
                                <tr>
                                    <th colspan="2">Zoek op merk<a name="wiel"></a></th>
                                </tr>
                                <tr>
                                    <td>Merk:</td>
                                    <td>
                                        <select name="wiel_merk">
                                            <option value="0">- Merk -</option>
<?php
    $merken = $merkclass->getMerken($website);
    foreach($merken as $merk)
    {
        echo "\t\t\t\t\t\t\t\t\t\t\t<option value=\"".$merk->merk_code."\"";
        if(isset($_GET['search']) && $_GET['search'] == "wiel" && isset($_GET['merk']) && $_GET['merk'] == $merk->merk_code || isset($_POST['wiel_submit']) && $_POST['wiel_merk'] == $merk->merk_code) { echo " selected=\"selected\""; }
        echo ">".$merk->merk_naam."</option>\n";
    }
?>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td> </td>
                                    <td><input type="submit" name="wiel_submit" value="Zoek" /></td>
                                </tr>
                                <tr>
                                    <td colspan="2"> </td>
                                </tr>
                                <tr>
                                    <td colspan="2"> </td>
                                </tr>
                            </table>
                        </form>
                    </div>
                    <div class="clearboth"></div>
                    <br />
<?php
if(isset($_POST['wiel_submit']) && $_POST['wiel_submit'] == "Zoek" || isset($_GET['merk']))
{
    $merk = NULL;
    if(isset($_POST['wiel_submit']) && $_POST['wiel_submit'] == "Zoek")
    {
        $merk = $_POST['wiel_merk'];
    }
    $merken = $merkclass->getMerken($website, $merk);
?>
                    <img src="http://www.etyre.net/preview/bnet/logos/<?php echo str_replace(".png", "_150.png", $merken[0]->merk_logo); ?>" width="150" class="logo" alt="<?php echo $merken[0]->merk_naam; ?>"/>
                    <div id="merken">
                    <li><span class="title">Foto <?php echo $merken[0]->wiel_foto; ?></span>
                            <a href="http://www.inter-tyre.nl/inter-tyre/images/w3/<?php echo $merken[0]->wiel_foto; ?>" class="preview" title="Fotonummer: <?php echo $merken[0]->wiel_foto;  ?>">
                                <img src="http://www.inter-tyre.nl/inter-tyre/images/w2/<?php echo $merken[0]->wiel_foto; ?>" alt="Fotonummer: <?php echo $merken[0]->wiel_foto; ?>" class="wheelImg"/>
                            </a>
                        <div class="clearboth"></div>
                    </div>
<?php
}
?> 

 

What I try to accomplish is the following:

 

I got a option selectfield and you can select different brands. After you submit you get the name and logo of the brand and the wheel that has that brand.

 

Now I only have 11 different brands with a wheel but I got more wheels with the same brand and I want to show all the wheels that belong to the brand you selected.

 

Here is how my database looks like:

 

http://imageshack.us/photo/my-images/685/databaseh.png

 

So lets say I put in another Rosso with a different picture. When I select Rosso and click submit I get 2 wheels of Rosso. The only thing that is happening now is that in my selectbox I can choose Rosse 2 times! And when I add another Rosso wheel I can choose 3 times. Same is with merk_logo that is there 3 times...

 

Can somebody help me with the code I got this far and help me getting this thing to work like a charm? :)

 

I know I am very close but just need to fix this...

 

Thanks

 

Only select the columns you need.  If you're only displaying merk_name, then only select merk_name from the table.  Then your DISTINCT will work.

 

Also, this is not the right way to set up your database, look into normalization and third normal form.  Your "merk"s should be their own table, with each row linking back to them.  That way, you could have done a select * from merk to get your dropdown.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.