Jump to content

Maybe another set of eyes on this could help? While loop problems


kb9yjg

Recommended Posts

Hello all, I have some code that is giving me problems. I am trying to get a search results page working properly and the while loop for some odd reason is not functioning as I had hoped. What is happening is when I search for anything it shows the two entries in the db 4x in each row. Now the 4 spaces is fine, but instead of a row of four of the same I would like a row of x number of different items to show up. So for instance I will show you an example below.

 

c233p c233p c233p c233p

er356p er356p er356p er356p

 

I would like it as follows

c233p er356p ...

 

Below is my code. If anyone can offer any insight into this it would be greatly apreciated.

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

<?php

$columns = 4;
$current_column = 1;

while($info=mysql_fetch_array($query)){

    //Start new row
    if ($current_column == 1) {
        echo "<tr>";
    }

    //Display the current data
    echo "<td align=\"center\">";
    echo "<img src=\"".$previewp.$info['itemno']."\" height=\"32\" width=\"32\">";
    echo "<br><a href=\"iteminfo.php?itemno=".$info['itemno']."\" target=\"_blank\">";
    echo "<span class=\"style2\">".$info['itemno']."</span></a><br>";
    echo "<span class=\"style2\">".$info['pthick']."x".$info['pwidth']."</span></td>";

    $current_column++

    //Close the current row if last column
    if ($current_column > $columns) {
        echo "</tr>";
        $current_column = 1;
    }
}

//Close out remaining columns (if needed)
if ($current_column>1) {
    for ($col=$current_column; $col<=$columns; $col++) {
        echo "<td> </td>";
    }
    echo "</tr>"; 
}

?>

Link to comment
Share on other sites

Heres my updated code befoe your suggestions are added. I think I may have figured out a portion of my issue but am still having issues with searching. I am attaching my search form. Maybe you can see something I totally messed up on. By the way thanks for the help. Before I post next I will try the solution you suggested. I am trying to allow a user to either use one part of the form or all of it for their search. Am I going about things the right way using one query or should i be using multiple queries?

 

[attachment deleted by admin]

Link to comment
Share on other sites

Hey mjdamato, Thanks for the code! Works like a charm! Over the past 12 hours I have also figured out the other issue I had. Instead of using one long query I broke it up and tested to see which fields were filled in. Depending on the fields that were filled a different query was run. Any way for future situations that are similar I am posting the code below. It will show the search results as well as criteria specified.

 

<table border="0">
        <tr> 
          <td Valign="top" Align="center" width="200"><span class="style2">
              <b>Item 
            Search</b> </p> 
            <p>
<?
include("inc/connect.inc");
$table="profiles";
if ( $_GET[‘limit’] == NULL ){
$limit = 10;
} else {
$limit = $_GET[‘limit’];
}

if ($_GET[‘page’]==NULL){
$start = 0;
} else {
$start = $_GET[‘page’];
}

// the if above, states that if the variable in the URL named page has a value, $start will be set to it, otherwise by default it is set to 0, meaning that it will start at the beginning of the table by default.

if($_GET[‘page’] <= 0){
$_GET['page']=0;
$_GET['limit']=$limit;
}

// this if above checks that the start variable isn’t negative, because if it is, our script will screw up. If it finds that it is, it redirects to the default page where the start is 0.

//$q="select * from profiles where pthick between '$thick1' and '$thick2' and pwidth between '$height1' and '$height2' and pdesc like '%$descr%' and itemno like '%$item1%' and pdate between '$date1' and '$date2' and ptype like '%$cmbCategory%' ORDER BY pid LIMIT $start,$limit";
if (isset($thick1) && isset($thick2)){
$q="select * from profiles where pthick between '$thick1' and '$thick2' order by pid LIMIT $start,$limit";
}
if (isset($height1) && isset($height2)){
$q="select * from profiles where pwidth between '$height1' and '$height2' order by pid LIMIT $start,$limit";
}
	if(isset($descr)){
	$q="select * from profiles where pdesc like '%$descr%' order by pid LIMIT $start,$limit";
	}
		if(isset($item1)){
		$q="select * from profiles where itemno like '%$item1%' order by pid LIMIT $start,$limit";
		}
			if(isset($date1) && isset($date2)){
			$q="select * from profiles where pdate between '$date1' and '$date2' order by pid LIMIT $start,$limit";
			}
				if(isset($cmbCategory)){
				$q="select * from profiles where ptype like '%$cmbCategory%' order by pid LIMIT $start,$limit";
				}elseif(isset($cmbNewCategory) || isset($_GET['GO'])){
					$q="select * from profiles where ptype like '%$cmbNewCategory%' order by pid LIMIT $start,$limit";
					} 
$query = mysql_query($q) or die("Error: ".mysql_error());
// the query above now has the [a] and [b] operators incorporated, making our script paginated. All we need to do now is add the next and previous links.

$previous = $start + $limit;
$next = $start - $limit;
echo '<a href="presults.php?start='.$previous.'">Previous</a> - ';
echo '<a href="presults.php?start='.$next.'">Next</a>';

// the set of statements above displays the previous and next page links
?></p></span>
<? if(isset($_GET['GO'])){
$q="select * from profiles where ptype like '%$cmbNewCategory%' order by pid LIMIT $start,$limit";
$query = mysql_query($q) or die("Error: ".mysql_error());
}else{?>
            <form METHOD="GET" ACTION="#"> 
          </td>
        </tr>
    <tr valign="bottom"> 
          <td width="200"> 
            <p>
              <br>
              <span class="style2"Select another catagory</font><br>
              <select id="select1" name="cmbNewCategory" style="HEIGHT: 22px; WIDTH: 153px">
                <option value="" selected><font face="Verdana" size="2">All 
                Moldings</font></option>
                    <option value="Back">Back Band</option><option value="Base">Base</option><option value="Base_Cap">Base Cap</option><option value="Base_Shoe">Base Shoe</option><option value="Brick">Brick Mold</option><option value="Casing">Casing</option><option value="Chair_Rail">Chair Rail</option><option value="Corner">Corner Mold</option><option value="Cornice">Cornice</option><option value="Cove">Cove</option><option value="Crown">Crown</option><option value="Decorative">Decorative</option><option value="Dentil">Dentil</option><option value="Flat_Stock">Flat Stock</option><option value="Half_Round">Half Round</option><option value="Jamb_Stock">Jamb Stock</option><option value="Panel">Panel Mold</option><option value="Plant">Plant On</option><option value="Quarter_Round">Quarter Round</option><option value="Stop">Stop</option><option value="Stucco">Stucco Mold</option>
              </select>
              <input type="submit" Value="GO" id="cmdJumpto" name="cmdJumpto">
            </p><? } ?>
            <p> <font size="2" face="Verdana, Arial, Helvetica, sans-serif"><br>
              <a HREF="advsearch.html">Start Over</a> </font></p>
            </form>
            <p align="left"> <br>
              <font size="1" face="Verdana, Arial, Helvetica, sans-serif"><span class="style2"> <b>Searching 
              Criteria:</b><br>
              Category: <? echo $cmbCategory; ?><br>
              Thickness: <? echo $thick1; ?> 
              to <? echo $thick2; ?>
              <br>
              Width: <? echo $height1; ?> 
              to <? echo $height2; ?>
              <br>
              Items: <? echo $item1; ?>
              <br>
              Add Dates:<? echo $date1; ?> 
              to <? echo $date2; ?><br />
		  Description: <? echo $descr; ?></span>
              </font></p>
            <p align="center">  </p>
          </td>
        </tr></table></td>
	<td align=center>
	<table border="0" cellpadding="2" cellspacing="2">
	<?
	$previewp="preview/";
	$columns = 7;
	$current_column = 1;	
while($info=mysql_fetch_array($query)){
if ($current_column == 1) {
        echo "<tr>";
    }
	echo "<td align=center><img src=".$previewp.$info['itemno']." height=32 width=32><br><a href=iteminfo.php?itemno=".$info['itemno']." target=_blank><span class=style2>".$info['itemno']."</span></a><br><span class=style2>".$info['pthick']."x".$info['pwidth']."</span></td>";
	$current_column++;
if ($current_column > $columns) {
        echo "</tr>";
        $current_column = 1;
    }
}
//Close out remaining columns (if needed)
if ($current_column>1) {
    for ($col=$current_column; $col<=$columns; $col++) {
        echo "<td> </td>";
    }
    echo "</tr>"; 
}

?>
	</table>

 

mjdamato, I owe you one. If theres anything I can do for you just let me know.

Link to comment
Share on other sites

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.