SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Gloucestershire, UK
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trouble with script

    Hello,

    I've nearly finished putting together a page to display products depending on information passed to the page through the URL. There's also a product filter where users can select which products they's like to see in more detail. I've set up pagination and dynamic headings/titles/descriptions and I've got to add a filter where users can select the order they receive results.

    As far as I can tell there's only one problem, if you only select a brand in the filter it throws out an error.

    Code:
    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/fhlinux195/p/pencollection.co.uk/user/htdocs/products.php on line 57
    Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
    This is a bit confusing, because if I only select a product type (which works exactly the same way) it works perfectly. I'm echoing out the main queries to see if they are any help.

    Here's a link to the page with some products selected: http://www.pencollection.co.uk/products.php?type_id=4

    Here's the script, sorry about the spaghetti code!

    PHP Code:

    <?php

    require_once('../mysql_connect.php');



    if ((isset(
    $_GET['type_id'])) || (isset($_GET['brand_id'])) || (isset($_GET['range_id'])) || (isset($_POST['submitted']))) {
        
        
    $pagquery "SELECT COUNT(*) FROM product_hub, products WHERE product_hub.prod_id = products.prod_id AND ";
        
        
    $query "SELECT products.prod_id, product_hub.prod_id, products.prod_name, products.prod_description, products.image, products.price FROM product_hub, products WHERE product_hub.prod_id = products.prod_id AND ";
        
        if (
    is_numeric($_GET['type_id']) || is_numeric($_POST['type_id'])) {
            
            if(
    $_POST['type_id'] > 0){
                
    $type_id $_POST['type_id'];
            }else{
                
    $type_id $_GET['type_id'];
            }
            
            
    //fetch type name
            
    $query2 "SELECT type_name, type_description FROM type WHERE type_id=$type_id";
            
    $result2 mysqli_query($dbc$query2);
            
    $row2 mysqli_fetch_array($result2MYSQL_ASSOC);
            
            
    $type_name $row2['type_name'].'s';
            
    $type_desc $row2['type_description'];
            
            
    mysqli_free_result($result2);
            
            
    //build product query
            
    $query.= "product_hub.type_id=$type_id AND ";
            
    $pagquery.= "product_hub.type_id=$type_id AND ";
        }
        
        if (
    is_numeric($_GET['brand_id']) || is_numeric($_POST['brand_id'])) {
            
            if(
    $_POST['type_id'] > 0){
                
    $brand_id $_POST['brand_id'];
            }else{
                
    $brand_id $_GET['brand_id'];
            }
            
            
    //fetch brand name
            
    $query2 "SELECT brand_name, brand_description FROM brand WHERE brand_id=$brand_id";
            
    $result2 mysqli_query($dbc$query2);
    //LINE 57 BELOW
            
    $row2 mysqli_fetch_array($result2MYSQL_ASSOC) or die("Error: ".mysqli_error($dbc));
            
            
    $brand_name $row2['brand_name'];
            
    $brand_desc $row2['brand_description'];
            
            
    mysqli_free_result($result2);
            
            
    //build product query
            
    $query.= "product_hub.brand_id=$brand_id AND ";
            
    $pagquery.= "product_hub.brand_id=$brand_id AND ";
        }
        
        if (
    is_numeric($_GET['range_id']) || is_numeric($_POST['range_id'])) {
            
            if(
    $_POST['range_id'] > 0){
                
    $range_id $_POST['range_id'];
            }else{
                
    $range_id $_GET['range_id'];
            }
            
            
    //fetch brand name
            
    $query2 "SELECT range_name, range_description, brand_id FROM range WHERE range_id=$range_id";
            
    $result2 mysqli_query($dbc$query2);
            
    $row2 mysqli_fetch_array($result2MYSQL_ASSOC);
            
            
    $range_name $row2['range_name'];
            
    $range_desc $row2['range_description'];
            
    $brand_id $row2['brand_id'];
            
            
    $query3 "SELECT brand_name FROM brand WHERE brand_id=$brand_id";
            
    $result3 mysqli_query($dbc$query3);
            
    $row3 mysqli_fetch_array($result3MYSQL_ASSOC);
            
            
    $brand_name $row3['brand_name'];
            
            
    mysqli_free_result($result3);
            
            
    mysqli_free_result($result2);
            
            
    //build product query
            
    $query.= "product_hub.range_id=$range_id AND ";
            
    $pagquery.= "product_hub.range_id=$range_id AND ";
        }
        
        
    //prepare dynamic heading
        
    $heading '';
        
        if (isset(
    $brand_name)){
            
    $heading.= $brand_name.' ';
        }
        
        if (isset(
    $range_name)){
            
    $heading.= $range_name.' ';
        }
        
        if (isset(
    $type_name)){
            
    $heading.= $type_name.' ';
        }
        
        
    $page_title 'Pen Collection - '$heading;
        
        
    //include top template
        
    include('includes/top.htm');
        
        
        echo 
    '<h1>'$heading .'</h1>';
        
        
    //select description whilst avoiding content duplication
        
    if ($range_desc) {
            echo 
    '<p>'$range_desc .'</p>';
        }elseif (
    $brand_desc){
            echo 
    '<p>'$brand_desc .'</p>';
        }elseif (
    $type_desc) {
            echo 
    '<p>'$type_desc .'</p>';
        }else{
            echo 
    '<p>No information available.</p>';
        }
        
        
    //product filter form
        
    echo '
        <form id="filter" method="post" action="'
    $_SERVER['PHP_SELF'] .'">
        <select name="type_id">
        <option value="">Select Type</option>'
    ;
        
        
    //select type
            
        
    $query2 "SELECT * FROM type ORDER BY type_name ASC";
        
    $result2 mysqli_query($dbc$query2);
            
        while (
    $row2 mysqli_fetch_array($result2MYSQL_ASSOC)) {
            echo 
    '<option value="'$row2['type_id'] .'"';
            if (
    $type_id == $row2['type_id']) {
                echo 
    ' selected="selected"';
            }
                
            echo 
    '>'$row2['type_name'] .'</option>';
        }
        echo 
    '</select>';
            
        
    mysqli_free_result($result2);
        
        
    //select brand
        
        
    echo '<select name="brand_id"><option value="">Select Brand</option>';
        
        
    $query2 "SELECT * FROM brand ORDER BY brand_name ASC";
        
    $result2 mysqli_query($dbc$query2);
            
        while (
    $row2 mysqli_fetch_array($result2MYSQL_ASSOC)) {
            echo 
    '<option value="'$row2['brand_id'] .'"';
            if (
    $brand_id == $row2['brand_id']) {
                echo 
    ' selected="selected"';
            }
                
            echo 
    '>'$row2['brand_name'] .'</option>';
        }
        
        echo 
    '</select>';
            
        
    mysqli_free_result($result2);
        
        
        
    //select range
        
        
    echo '<select name="range_id"><option value="">Select Range</option>';
        
        
    $query2 "SELECT * FROM brand ORDER BY brand_name ASC";
        
    $result2 mysqli_query($dbc$query2);

        while (
    $row2 mysqli_fetch_array($result2MYSQL_ASSOC)) {
            echo 
    '<optgroup label="'$row2['brand_name'] .'">';
            
    $query3 "SELECT range_id, range_name FROM range WHERE brand_id={$row2['brand_id']} ORDER BY range_name ASC";
            
    $result3 mysqli_query($dbc$query3);
                
            while (
    $row3 mysqli_fetch_array($result3MYSQL_ASSOC)) {
                echo 
    '<option value="'$row3['range_id'] .'"';
                if (
    $range_id == $row3['range_id']) {
                    echo 
    ' selected="selected"';
                }
                echo 
    '> '$row3['range_name'] .'</option>';
            }
                
            
    mysqli_free_result($result3);
                
            echo 
    '</optgroup>';
        }
            
        echo 
    '</select>';    
            
        
    mysqli_free_result($result2);
        
        
    //order by
        
        /*echo '<select name="order"><option value="">Order By</option>';
        
        $order_by = array();*/
            
            
        
    echo '<input type="hidden" name="submitted" value="TRUE" />
        <input type="submit" name="submit" value="Filter" class="subbtn" /></form>'
    ;
        
        
    //paginate
        
    $pagquery.= "products.available='y' ORDER BY prod_name";
        
    $display 2;
        
        echo 
    $pagquery;
        
        if (isset(
    $_GET['np'])) {
            
    $num_pages $_GET['np'];
        }else{
            
    $pagresult mysqli_query($dbc$pagquery);
            
    $pagrow mysqli_fetch_array($pagresultMYSQL_NUM) or die("Error: ".mysqli_error($dbc));
            
    $num_records $pagrow[0];
            
            if (
    $num_records $display){
                
    $num_pages ceil($num_records/$display);
            }else{
                
    $num_pages 1;
            }
            
        }
        
        if (isset(
    $_GET['s'])){
            
    $start $_GET['s'];
        }else{
            
    $start 0;
        }
        
        
        
    //complete product query and fetch array
        
    $query.= "products.available='y' ORDER BY prod_name ASC LIMIT $start$display";
        
        echo 
    $query;
        
        
    $result mysqli_query($dbc$query) or die("Error: ".mysqli_error($dbc));
        
        
        
    //build product results
        
    while ($row mysqli_fetch_array($resultMYSQL_ASSOC)) {
            echo 
    '<p>'$row['prod_name'] .'</p>';
        }
        
        
    mysqli_free_result($result);
        
        if (
    $num_pages 1){
            echo 
    '<br /><br /><p>';
            
            
    //determine current page
            
    $current_page = ($start/$display)+1;
            
            
    //make the previous button
            
    if ($current_page != 1){
                
    $prev '<a href="products.php?';
                
                if (
    $type_id) {
                    
    $prev.= 'type_id='$type_id .'&';
                }
                
                if (
    $brand_id) {
                    
    $prev.= 'brand_id'$brand_id .'&';
                }
                
                if (
    $range_id) {
                    
    $prev.= 'range_id'$range_id .'&';
                }
                
                
    $prev.= 's='. ($start $display) .'&np='.$num_pages.'">Previous</a>';
                
                echo 
    $prev;
            }
            
            
            
    //make numbered pages
            
    for ($i 1$i <= $num_pages$i++) {
                if (
    $i != $current_page) {
                    
    $pagnum '<a href="products.php?';
                    if (
    $type_id) {
                        
    $pagnum.= 'type_id='$type_id .'&';
                    }
                
                    if (
    $brand_id) {
                        
    $pagnum.= 'brand_id'$brand_id .'&';
                    }
                
                    if (
    $range_id) {
                        
    $pagnum.= 'range_id'$range_id .'&';
                    }
                    
                    
    $pagnum.= 's='. (($display * ($i 1))) .'&np='$num_pages .'"> '$i .' </a>';
                    
                    echo 
    $pagnum;
                    
                }else{
                    echo 
    $i.' ';
                }
            }
            
            
    //make next page
            
    if ($current_page != $num_pages) {
                
    $next '<a href="products.php?';
                
                if (
    $type_id) {
                    
    $next.= 'type_id='$type_id .'&';
                }
                
                if (
    $brand_id) {
                    
    $next.= 'brand_id'$brand_id .'&';
                }
                
                if (
    $range_id) {
                    
    $next.= 'range_id'$range_id .'&';
                }
                
                
    $next.= 's='. ($start $display) .'&np='$num_pages .'">Next</a>';
                
                echo 
    $next;
                
            }
            
            echo 
    '</p>';
        }
        
        
    }else{
        
    //no get array
    }

    ?>
    I've marked out the line that gets flagged up with the error.


    Thanks for your help.

    Cheers,

    Jon

  2. #2
    SitePoint Member kellogg9's Avatar
    Join Date
    Jun 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will check into this for you and reply later today

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Gloucestershire, UK
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers Kellogg9, but I've finally solved it.

    After a few hours of testing and isolating the problem...I found a typo.

    painful...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •