PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Nov 22, 2014 6:38 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sat Jul 14, 2012 10:09 am 
Offline
Forum Newbie

Joined: Thu Apr 08, 2010 2:00 am
Posts: 4
Hello,

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

This is my Class:

Syntax: [ Download ] [ Hide ]
<?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:
Syntax: [ Download ] [ Hide ]
<?php
    include_once "class/merken.class.php";
    $merkclass = new merken($dbo);
?>
                    <br />
                    <div class="bandwielkolom">
                        <form action="index.php?lang=nl&amp;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>&nbsp;</td>
                                    <td><input type="submit" name="wiel_submit" value="Zoek" /></td>
                                </tr>
                                <tr>
                                    <td colspan="2">&nbsp;</td>
                                </tr>
                                <tr>
                                    <td colspan="2">&nbsp;</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


Top
 Profile  
 
PostPosted: Mon Jul 16, 2012 1:22 am 
Offline
Forum Newbie

Joined: Tue May 29, 2012 8:36 am
Posts: 11
Nobody that can help me out? I know I'm very close but cant figure out how to fix this... I allready tried DISTINCT but that did not work very well either... somebody that can help me out?


Top
 Profile  
 
PostPosted: Mon Jul 16, 2012 2:49 pm 
Offline
Forum Contributor
User avatar

Joined: Thu May 11, 2006 8:58 pm
Posts: 305
Location: Utah, USA
Not understanding your question, but you may need a `GROUP BY`. Untested example:
Syntax: [ Download ] [ Hide ]
SELECT merk_code, merk_naam
FROM somthing_merken
GROUP BY merk_code, merk_naam
ORDER BY merk_naam


Top
 Profile  
 
PostPosted: Tue Jul 17, 2012 3:01 am 
Offline
Forum Newbie

Joined: Tue May 29, 2012 8:36 am
Posts: 11
Hello,

Thanks for your reply...

When I use this Class:

Syntax: [ Download ] [ Hide ]
public function getMerken($database, $id = NULL)
        {
                $sql = "SELECT merk_code, merk_naam, merk_logo, wiel_foto, wiel_nummer, wiel_info FROM ".$database."_merken GROUP BY merk_code, merk_naam, merk_logo";
                if(!empty($id))
                {
                        $sql .= " WHERE merk_code=:id";
                }
                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() );
                }
        }


I get the following error:

Fatal error: Call to a member function bindParam() on a non-object in /class/merken.class.php on line 34

This is the code I'm reading the Class with:

Syntax: [ Download ] [ Hide ]
            <div id="wielen-zoeken">
<?php
        include_once "class/merken.class.php";
        $merkclass = new merken($dbo);
?>
                                        <br />
                                        <div class="bandwielkolom">
                                                <form action="index.php?lang=nl&amp;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, $merk_code);
        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>&nbsp;</td>
                                                                        <td><input type="submit" name="wiel_submit" value="Zoek" /></td>
                                                                </tr>
                                                                <tr>
                                                                        <td colspan="2">&nbsp;</td>
                                                                </tr>
                                                                <tr>
                                                                        <td colspan="2">&nbsp;</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);
                foreach($merken as $merk)
        {
?>
                                        <img src="http://www.etyre.net/preview/bnet/logos/<?php echo str_replace(".png", "_150.png", $merk->merk_logo); ?>" width="150" class="logo" alt="<?php echo $merk->merk_naam; ?>"/>
                                        <div id="merken">
                    <li><span class="title"><?php echo $merk->wiel_info; ?></span>
                                                        <a href="http://www.inter-tyre.nl/inter-tyre/images/w3/<?php echo $merk->wiel_foto; ?>" class="preview" title="Fotonummer: <?php echo $merk->wiel_foto;  ?>">
                                                                <img src="http://www.inter-tyre.nl/inter-tyre/images/w2/<?php echo $merk->wiel_foto; ?>" alt="Fotonummer: <?php echo $merk->wiel_foto; ?>" class="wheelImg"/>
                                                        </a>
                                                <div class="clearboth"></div>
                                        </div>
<?php
        }
?>
<?php
}
?>


Do you know what I'm doing wrong?


Top
 Profile  
 
PostPosted: Tue Jul 17, 2012 1:05 pm 
Offline
Forum Contributor
User avatar

Joined: Thu May 11, 2006 8:58 pm
Posts: 305
Location: Utah, USA
In this line `$stmt = $this->db->prepare($sql);` the variable `$stmt` is not an object. mysqli::prepare will return false if there is an error. See the docs

Turn on full error reporting to see the error that mysqli::prepare is generating:
Syntax: [ Download ] [ Hide ]
ini_set('display_errors',1);
error_reporting(E_ALL);


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: 6vwytt05084m, Bing [Bot], Exabot [Bot], Google [Bot], Majestic-12 [Bot], PHPHelpNeeded, Yahoo [Bot] and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group