Jump to content

looping problems


blaghdesign

Recommended Posts

Hi,

 

I am having a problem, which if I don't get some help, promises to melt my brain.

 

It may have a simple solution, but I cannot conjure it with my limited, and relatively new coding skills.

 

I need to grab everything from a table, group them up by a tag and then, in their tag groups, print them out into div boxes.

 

details below.

 


<?php




// grab everything from the database and group by tag, sort by adddate

$query = " select * from portfolio group by tag sort by adddate ";
$result = mysql_query ($query);
$row = mysql_fetch_array($result);


/* 	I now need to loop through the array putting each tag group into separate div boxes.. HOW?? 
at the momenet I am using a while loop, but I think I should be using a for loop with foreach as well?
*/


// below is my current loop, which just takes each entry and sticks them all into seperate div boxes. which is crap.

while ($row)
   
    {
   
    echo "<div class='boxgrid'><a href=".$row['fullPath']." rel='prettyPhoto[".$row['tag']."]'><img src=".$row['thumbPath']." title=".$row['desc']."/></a></div>";  
	  
    $row = mysql_fetch_array($result);

    }    	

include('./scripts/footer.php');

mysql_close($con);


// my actual desired output would be like shown below


?>




        <div class="boxgrid">
                <a href="./images/imagename01.jpg" rel="prettyPhoto[TAG]"><img src="./images/thumbs/thumbname.jpg"/></a>
	<a href="./images/imagename02.jpg" rel="prettyPhoto[TAG]"><img src="./images/thumbs/thumbname.jpg"/></a>
	<a href="./images/imagename03.jpg" rel="prettyPhoto[TAG]"><img src="./images/thumbs/thumbname.jpg"/></a>
	<a href="./images/imagename04.jpg" rel="prettyPhoto[TAG]"><img src="./images/thumbs/thumbname.jpg"/></a>
        </div>

        <div class="boxgrid">
        	<a href="./images/imagename01.jpg" rel="prettyPhoto[TAG]"><img src="./images/thumbs/thumbname.jpg"/></a>
	<a href="./images/imagename02.jpg" rel="prettyPhoto[TAG]"><img src="./images/thumbs/thumbname.jpg"/></a>
        </div>

        <div class="boxgrid">
        	<a href="./images/imagename01.jpg" rel="prettyPhoto[TAG]"><img src="./images/thumbs/thumbname.jpg"/></a>
        </div>

        <div class="boxgrid">
        		<a href="./images/imagename01.jpg" rel="prettyPhoto[TAG]"><img src="./images/thumbs/thumbname.jpg"/></a>
        </div>

        <div class="boxgrid">
        		<a href="./images/imagename01.jpg" rel="prettyPhoto[TAG]"><img src="./images/thumbs/thumbname.jpg"/></a>
        </div>
        




 

 

if anyone here can help, It is quite likely that I may cry.

 

Ben.

Link to comment
Share on other sites

OK, I have some code for you, going by what I think you are looking for.  Have a look over it and replace your current while loop with the following if you think it suits:

$query = "SELECT id, thumbpath, fullpath, tag, desc, adddate FROM portfolio ORDER BY adddate ASC";
$tagCheck = 'nonsenese value';     //anything that is never going to be an actual tag value
$fistCheck = true; // variable to check if the output is the first because if it's not we will need to close the div before opening another one
$result = mysql_query ($query); //you could realy use some error capture on your query stuff
While($row = mysql_fetch_array($result)) // loop through the results from the query
{
   if($tagCheck != $row['tag']) //check if the tag value from the database is different to what the last value was
   { //if it is different then
     if($firstCheck != true) //check if this is NOT the first record returned
     { //if this is not the first record returned then
      echo "</div>"; // close the last div
     }else //if this is the first record
     {
      $firstCheck = false; //update variable to show that first record has now been returned
     }
    $tagCheck = $row['tag']; // still if the tag value is different - update tag value to be the same
    echo "<div class='boxgrid'>"; // as this is a new tag we need a new div
   } // the following information will be processed every time  
   $fPath = $row['fullPath'];
   $tPath = $row['thumbPath'];
   $desc = $row['desc'];
   
    echo "<a href=\"$fpath\" rel=\"prettyPhoto['$tagCheck']\"><img src=\"$tPath\" title=\"$desc\"/></a>";  
} //end of recordset return loop
echo "</div>" //now we are done close off the last div

It's not tested, so will odds on have a few errors in it. Let me knopw what you think.

Link to comment
Share on other sites

OK, I have some code for you, going by what I think you are looking for.  Have a look over it and replace your current while loop with the following if you think it suits:

$query = "SELECT id, thumbpath, fullpath, tag, desc, adddate FROM portfolio ORDER BY adddate ASC";
$tagCheck = 'nonsenese value';     //anything that is never going to be an actual tag value
$fistCheck = true; // variable to check if the output is the first because if it's not we will need to close the div before opening another one
$result = mysql_query ($query); //you could realy use some error capture on your query stuff
While($row = mysql_fetch_array($result)) // loop through the results from the query
{
   if($tagCheck != $row['tag']) //check if the tag value from the database is different to what the last value was
   { //if it is different then
     if($firstCheck != true) //check if this is NOT the first record returned
     { //if this is not the first record returned then
      echo "</div>"; // close the last div
     }else //if this is the first record
     {
      $firstCheck = false; //update variable to show that first record has now been returned
     }
    $tagCheck = $row['tag']; // still if the tag value is different - update tag value to be the same
    echo "<div class='boxgrid'>"; // as this is a new tag we need a new div
   } // the following information will be processed every time  
   $fPath = $row['fullPath'];
   $tPath = $row['thumbPath'];
   $desc = $row['desc'];
   
    echo "<a href=\"$fpath\" rel=\"prettyPhoto['$tagCheck']\"><img src=\"$tPath\" title=\"$desc\"/></a>";  
} //end of recordset return loop
echo "</div>" //now we are done close off the last div

It's not tested, so will odds on have a few errors in it. Let me knopw what you think.

 

Thank you for taking the time to help. I will give this a try and let you know the results.

 

Thanks gain.

Ben.

Link to comment
Share on other sites

I'll add my two cents. In the case where you need to have content before and after a subset of records (i.e. the opening and closing DIV tags) I prefer to use the loop to add the records to a temp array in the loop and call a display function when a change is detected. It makes it much easier to "visualize" the output being created as well as to make edits. Just a much cleaner solution IMHO

 

function createTagGroupDiv($portfolioArray)
{
    if(count($portfolioArray)<1) { return false; } //Ensures nothing is created on first record
    echo "<div class='boxgrid'>\n";
    foreach($portfolioArray as $portfolio)
    {
        echo "<a href='{$portfolio['fullPath']}' rel='prettyPhoto[{$portfolio['tag']}]'><img src='{$portfolio['thumbPath']}' title='{$portfolio['desc']}' /></a>\n";  
    }
    echo "</div>\n";
}

//Create and run query
$query  = "SELECT id, thumbpath, fullpath, tag, desc, adddate FROM portfolio ORDER BY adddate ASC";
$result = mysql_query ($query); //you could realy use some error capture on your query stuff

$current_tag = false;         //Var to track changes in tag
$portfolios_by_tag = array(); //Array to hold records by tag
//Loop through results
While($row = mysql_fetch_array($result)) // loop through the results from the query
{
    //Check if this tag is different from the last
    if($current_tag != $row['tag'])
    {
        //Set current tag
        $current_tag = $row['tag'];
        //Display results from previous group
        createTagGroupDiv($portfolios_by_tag);
        //Clear last group records
        $portfolios_by_tag = array();
    }
    //Add current record to group array
    $portfolios_by_tag[] = $row;
}

//Display the last group
createTagGroupDiv($portfolios_by_tag);

Link to comment
Share on other sites

I'll add my two cents. In the case where you need to have content before and after a subset of records (i.e. the opening and closing DIV tags) I prefer to use the loop to add the records to a temp array in the loop and call a display function when a change is detected. It makes it much easier to "visualize" the output being created as well as to make edits. Just a much cleaner solution IMHO

 

This also worked, Thank you all for your time, it really is appreciated.

 

Ben.

 

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.