Jump to content

Need multiple headers from one query


shawhey

Recommended Posts

I'm not sure if this would be considered a math question, but I wasn't sure where to post it. Before I start, here's a link to the page:

http://ezliquidators.1free.ws/products.php?mat=granite

 

I'm trying to create a header for each of the different sizes of tiles if the user doesn't specify a size. This is difficult, because if I just put it in the array, it will create a header for each individual tile as opposed to creating one for each unique size. Any ideas? I'm at a loss for anything that could work right now. Here's the PHP:

 

<?php
$mat = $_GET['mat'];
$size = $_GET['size'];
$count = -1;
if ($size >= 1) {
$sql = mysql_query("SELECT * FROM Products WHERE Type = '".$mat."' AND Size = '".$size."' ");
}
else {
$sql = mysql_query("SELECT * FROM Products WHERE Type = '".$mat."' ");
}
while ($r = mysql_fetch_array($sql)) {

$count++;
if ($newrow==$count%4) {
?>
</tr><tr>
<?php
}
?>

<td>
<a href="<?php echo $r[image]; ?>" rel="lightbox" title="<?php echo $r[Name]; ?>"><img 

id="tile" src="<?php echo$r[image]; ?>"></a>
<br>
<p id="tileinfo">
<?php
echo "$r[Name] ($r[Dimensions])";
}
?>

 

Cheers,

 

Chahe

Link to comment
Share on other sites

You can have your script remember the size of the last tile, and only print a header when the size changes, or if it's the first tile.  I assume you would want to order the results by size as well in your SQL.

Link to comment
Share on other sites

Yes, that's right. I know how to sort by size, but how would I write the script to remember the size of the last tile? Also, if it's remembering the last tile, wouldn't that mean it would put the header in one tile too late? Since it wouldn't realize the size had changed until after a new one comes out.

Link to comment
Share on other sites

It would look like this:

 

$last_tile_size = null;
while ($r = mysql_fetch_array($sql)) {
    # New code
    if ($last_tile_size === null || $last_tile_size != $r['Size']) {
         echo "<h4>Welcome to tile size {$r['Size']}!</h4>";
    }
    $last_tile_size = $r['Size'];

    # Existing code goes here
}

 

This will print a header before the data for the first tile (which is why it checks for null), and for any tile where the previous tile was a different size.

Link to comment
Share on other sites

Cheers for the help. I figured it out last night with a slightly different approach.  :D

 

if ($r[size]>$prevsize) {
?> 
</table>
<h3 id="dimension"><?php echo $r[Dimensions]; ?> </h3>
<table>
<?php
}
$prevsize = $r[size];

 

I have a new problem now that the active link in the sidebar doesn't change since it's all on one page that just refreshes with different values to the mat and size variables. I'm assuming all I'll need to do on the html end is add a body id that equals those variables, but I haven't quite figured out the css end of it.

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.