0

I have created a search form that allows users to search for results in a packaging database. I recently had problems with printing the results out in rows of three, as all the results were printing one underneath, each of them. I managed to get that fixed and now the results are in rows of three, except now, there are random gaps where there will only be one result in a row or two, but then the results will carry on the next row with 3. I can't seem to pinpoint why this is randomly happening.

There doesn't seem to be any pattern to it, it varies depending on what the user searches for.

Here is the PHP code:

       <?php
            $con = mysql_connect ("localhost", "horizon1", "");
                   mysql_select_db ("horizon1_delyn", $con);

            if (!$con)
                { 
                    die ("Could not connect: " . mysql_error());
                }

            $descrip = mysql_real_escape_string($_POST['descrip']); 
            $depth   = mysql_real_escape_string($_POST['depth']);
            $varWidth = mysql_real_escape_string($_POST['traywidth']);
            $varHeight= mysql_real_escape_string($_POST['trayheight']);
            $varRange = mysql_real_escape_string($_POST['trayrange']);
            $varType  = mysql_real_escape_string($_POST['traytype']);
            $varShape = mysql_real_escape_string($_POST['trayshape']);
            $varImage = mysql_real_escape_string($_POST['imagename']);

            if (isset($varHeight) && !empty($varHeight)) {
                    $low  = ($varHeight."00");
                    $high = ($varHeight."99");
                } else {
                    $low  = ("000");
                    $high = ("999");
                }

            if (isset($varWidth) && !empty($varWidth)) {
                    $min  = ($varWidth."00");
                    $max = ($varWidth."99");
                } else {
                    $min  = ("000");
                    $max = ("999");
                }   


            $sql = "SELECT * FROM range WHERE 
                        description LIKE '%".$descrip."%'  
                    AND trayrange   LIKE '%".$varRange."%' 
                    AND traytype    LIKE '%".$varType."%' 
                    AND trayshape   LIKE '%".$varShape."%'
                    AND traywidth   BETWEEN '".$min."'  AND '".$max."' 
                    AND trayheight  BETWEEN '".$low."' AND '".$high."' ";


                $r_query = mysql_query($sql);  

    $count = 0;
    while ($row = mysql_fetch_array($r_query)) 
    { 

    $t = $count%1;
    echo ($t==4) ?  '<div id="results">' : '<div id="results" style="float:left">';

    echo '<p class="image">
            <img src="   '. $row['imagename'] . '" width="150" length="80"> 
          </p>';
    echo '<div id="table">
          <br /><strong> Tool Code:  </strong> '. $row['toolcode'];
    echo '<br /><strong> Description:</strong> '. $row['description']; 
    echo '<br /><strong> Tray range: </strong> '. $row['trayrange']; 
    echo '<br /><strong> Tray type:  </strong> '. $row['traytype'];
    echo '<br /><strong> Tray size:  </strong> '. $row['traysize']; 
    echo '<br /><strong> Tray shape: </strong> '. $row['trayshape'] . '</div>' . '<br />';
    echo '<a href="http://www.delynpackaging.co.uk/contact.htm">Enquiry Page</a> <br />' . 
         '<a href="http://176.67.171.11/range-test/rangetest.php">Back to Search</a>';
    echo '</div>' . '</div>';

    $count++;
}

            if (mysql_num_rows($r_query) <= 0){
                echo 'No results match your search, please try again';
           }
    ?>

Can anybody see why this might be happening?

LiamHorizon
  • 171
  • 2
  • 3
  • 10
  • possible duplicate of [PHP file cannot enter some part of code](http://stackoverflow.com/questions/11575531/php-file-cannot-enter-some-part-of-code) – hakre Oct 25 '12 at 07:21
  • FYI, It's non-semantical to have multiple elements with the same ID. Also, `$t` Can never be `= 4` since `$count%1 = 0` at any time – Touki Oct 25 '12 at 07:22
  • Just a word of warning, an `id` should only have 1 DOM instance. It looks like you are outputting a div with `id="table"` >1 times. Either add an index to it to make it a unique identifier e.g. `id="table1"` or change it to a class e.g. `class="table"` [Read more here](http://stackoverflow.com/questions/602168/in-css-what-is-the-difference-between-and-when-declaring-a-set-of-styles/602177#602177) – Adam Tomat Oct 25 '12 at 07:24

1 Answers1

0

It might depend on the width of the boxes, that they wrap when they don't fit the row. Also $t = $count%1; looks unusual, it should be $t = $count%3; and then the condition should read: ($count>0 && $t==0)

Adder
  • 5,708
  • 1
  • 28
  • 56
  • They do fit because they are all the same size. So if a row of three above can fit, it just doesn't make sense as to why there are rows of just maybe 1 or 2. I changed the code but it didn't make a difference unfortunately. But thanks for your help. – LiamHorizon Oct 25 '12 at 09:10